/tools/imconvert/fitx_convert.c

http://ftk.googlecode.com/ · C · 119 lines · 74 code · 16 blank · 29 comment · 13 complexity · 23cd492addf33d5d7b12165d7f78eab7 MD5 · raw file

  1. /*
  2. * File: fitx_convert.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: a tool converting fitx data to ftk lookup table data.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-02-12 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #define MAX_LINES 400 * 1024
  36. typedef struct _TableHeader
  37. {
  38. int nr;
  39. unsigned int offset[1];
  40. }TableHeader;
  41. char* read_file(const char* file_name, int* length)
  42. {
  43. struct stat st = {0};
  44. if(stat(file_name, &st))
  45. {
  46. return NULL;
  47. }
  48. else
  49. {
  50. char* buffer = malloc(st.st_size + 1);
  51. FILE* fp = fopen(file_name, "rb");
  52. size_t fread_ret = 0;
  53. fread_ret = fread(buffer, 1, st.st_size, fp);
  54. fclose(fp);
  55. buffer[st.st_size] = '\0';
  56. *length = st.st_size;
  57. return buffer;
  58. }
  59. }
  60. int main(int argc, char* argv[])
  61. {
  62. FILE* out = NULL;
  63. int length = 0;
  64. char* data = NULL;
  65. char* line = NULL;
  66. char* end = NULL;
  67. TableHeader* h = NULL;
  68. if(argc != 3)
  69. {
  70. printf("#####################################################\n");
  71. printf("usage: %s in out\n", argv[0]);
  72. printf(" Converting fitx data to ftk lookup table data.\n");
  73. printf("#####################################################\n");
  74. return 0;
  75. }
  76. h = calloc(1, sizeof(TableHeader) + MAX_LINES * sizeof(int));
  77. data = read_file(argv[1], &length);
  78. line = data;
  79. end = data + length;
  80. while(line < end)
  81. {
  82. h->offset[h->nr] = line - data;
  83. h->nr++;
  84. while(line != end && *line != '\n') line++;
  85. if(*line == '\n')
  86. {
  87. line++;
  88. }
  89. if(line >= end)
  90. {
  91. break;
  92. }
  93. }
  94. if((out = fopen(argv[2], "wb+")) != NULL)
  95. {
  96. fwrite(h, 1, sizeof(h->nr) + h->nr * sizeof(int), out);
  97. fwrite(data, 1, length, out);
  98. fflush(out);
  99. fclose(out);
  100. }
  101. free(h);
  102. return 0;
  103. }