PageRenderTime 263ms CodeModel.GetById 140ms app.highlight 7ms RepoModel.GetById 114ms app.codeStats 0ms

/tools/imconvert/fitx_convert.c

http://ftk.googlecode.com/
C | 119 lines | 74 code | 16 blank | 29 comment | 11 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/*
 26 * History:
 27 * ================================================================
 28 * 2010-02-12 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include <stdio.h>
 32#include <unistd.h>
 33#include <stdlib.h>
 34#include <sys/stat.h>
 35#include <sys/types.h>
 36
 37#define MAX_LINES 400 * 1024
 38typedef struct _TableHeader
 39{
 40	int nr;
 41	unsigned int offset[1];
 42}TableHeader;
 43
 44char* read_file(const char* file_name, int* length)
 45{
 46	struct stat st = {0};
 47	if(stat(file_name, &st))
 48	{
 49		return NULL;
 50	}
 51	else
 52	{
 53		char* buffer = malloc(st.st_size + 1);
 54		FILE* fp = fopen(file_name, "rb");
 55		size_t fread_ret = 0;
 56		fread_ret = fread(buffer, 1, st.st_size, fp);
 57		fclose(fp);
 58		buffer[st.st_size] = '\0';
 59		*length = st.st_size;
 60
 61		return buffer;
 62	}
 63}
 64
 65int main(int argc, char* argv[])
 66{
 67	FILE* out = NULL;
 68	int length = 0;
 69	char* data = NULL;
 70	char* line = NULL;
 71	char* end  = NULL;
 72	TableHeader* h = NULL;
 73
 74	if(argc != 3)
 75	{
 76		printf("#####################################################\n");
 77		printf("usage: %s in out\n", argv[0]);
 78		printf("       Converting fitx data to ftk lookup table data.\n");
 79		printf("#####################################################\n");
 80
 81		return 0;
 82	}
 83
 84	h = calloc(1, sizeof(TableHeader) + MAX_LINES * sizeof(int));
 85	data = read_file(argv[1], &length);
 86
 87	line = data;
 88	end = data + length;
 89
 90	while(line < end)
 91	{
 92		h->offset[h->nr] = line - data;
 93		h->nr++; 
 94		while(line != end && *line != '\n') line++;
 95		
 96		if(*line == '\n')
 97		{
 98			line++;
 99		}
100
101		if(line >= end)
102		{
103			break;
104		}
105	}
106
107	if((out = fopen(argv[2], "wb+")) != NULL)
108	{
109		fwrite(h, 1, sizeof(h->nr) + h->nr * sizeof(int), out);
110		fwrite(data, 1, length, out);
111		fflush(out);
112		fclose(out);
113	}
114
115	free(h);
116
117	return 0;
118}
119