//#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_BUFFER_SIZE 0xFFFF
#define NLS_CHAR "set names win1251;"
//#define NLS_CHAR "set names dos866;"
#define CONNECT_STRING "connect c:\\tst\\tst.fdb user sysdba password masterkey;"
FILE *in_file, *out_file;
enum METHOD_OF_WRITE {simply, database};
typedef struct {
unsigned char chk;
unsigned char refresh_yy;
unsigned char refresh_mm;
unsigned char refresh_dd;
unsigned int record_count;
unsigned short header_length;
unsigned short r1;
unsigned char f1;
unsigned char code;
unsigned char r2[12];
unsigned char f2;
unsigned char id_lng;
unsigned char r3[2];
} type_header;
typedef struct {
unsigned char name[11];
unsigned char field_type;
unsigned char r1[4];
unsigned char size;
unsigned char nn;
unsigned char r2[2];
unsigned char id;
unsigned char r3[10];
unsigned char mdx;
unsigned char term;
unsigned char b34;
} type_field_descriptor;
typedef struct {
unsigned char name[11];
unsigned char field_type;
unsigned char size;
unsigned char b14;
} t_w;
int main(int argc, char** argv)
{
type_header *s0;
type_field_descriptor *s1;
t_w *s2;
unsigned char *s, *point_s, *ws, *point_ws;
unsigned char in_file_name[512], out_file_name[512], table_name[512], t_key, s_key[34];
int i, j, k, l, m, n, n_field, rec_length;
t_key = 0;
strcpy(s_key, ";");
for(i = 1; i < argc; i++)
if((argv[i][0] == '-') && (strlen(argv[i]) > 1))
{
switch(argv[i][1])
{
case 't' :
if(strlen(argv[i]) > 2)
{
printf("Wrong option: %s\nTerminate.\n\n", argv[i]);
return 1;
}
else
t_key = 1;
break;
case 's' :
if((strlen(argv[i]) == 2) && ((i + 1) < argc))
{
i++;
strcpy(s_key, argv[i]);
}
else
{
for(j = 2; j < (int) strlen(argv[i]); j++)
s_key[j - 2] = argv[i][j];
s_key[j - 2] = 0;
}
break;
default:
printf("Wrong option: %s\nTerminate.\n\n", argv[i]);
return 1;
}
}
else
{
break;
}
k = i;
for(i = 0; i < (int) strlen(s_key); i++)
if((s_key[i] == '\\') && (s_key[i + 1] == 't'))
{
s_key[i] = '\t';
for(j = (i + 1); j < (int) strlen(s_key); j++)
s_key[j] = s_key[j + 1];
}
n = 0;
#ifdef WIN32
for(i = 0; i < (int) strlen(argv[0]); i++) if(argv[0][i] == '\\') n = i;
#else
for(i = 0; i < (int) strlen(argv[0]); i++) if(argv[0][i] == '/') n = i;
#endif
n++;
for(i = n, j = 0; i < (int) strlen(argv[0]); i++, j++) table_name[j] = argv[0][i]; table_name[j] = 0;
for(i = 0; i < (int) strlen(table_name); i++) if(table_name[i] == '.') table_name[i] = 0;
n = 0;
if(k < argc)
{
strcpy(in_file_name, argv[k]);
k++;
n = 1;
}
if(k < argc)
{
strcpy(out_file_name, argv[k]);
n = 2;
}
else
out_file_name[0] = 0;
if(!strcmp(in_file_name, out_file_name))
{
printf("Input and Output file is coincide.\n");
return 1;
}
if(!n)
{
printf("\nDBF to TEXT utility.\n\n");
printf("Usage: %s -[options] in_file [out_file]\n\n", table_name);
printf("Options are:\n");
printf("t - insert into table syntax\n");
printf("s - separator; Default - \";\"\n\n");
return 1;
}
s = (unsigned char*) malloc(MAX_BUFFER_SIZE);
ws = (unsigned char*) malloc(MAX_BUFFER_SIZE);
point_s = s;
point_ws = ws;
s0 = (type_header*) malloc(sizeof(type_header));
s1 = (type_field_descriptor*) malloc(sizeof(type_field_descriptor));
if( (in_file = fopen(in_file_name, "rb" )) == NULL )
{
printf( "The file %s was not opened\n", in_file_name);
return 1;
}
if(n == 1)
out_file = stdout;
else
if( (out_file = fopen(out_file_name, "wt" )) == NULL )
{
printf( "The file 'data-write' was not opened\n" );
fclose(in_file);
return 1;
}
fgets((char*) s0, sizeof(type_header), in_file);
//printf("Last modification: %02d/", s0->refresh_dd);
//printf("%02d/", s0->refresh_mm);
//printf("%02d\n", s0->refresh_yy);
//printf("Count of records : %d\n", s0->record_count);
s1->term = 0;
n_field = 1;
while(s1->term != 13)
{
fseek(in_file, (32 * n_field), SEEK_SET);
fgets((char*) s1, sizeof(type_field_descriptor), in_file);
n_field++;
}
n_field--;
s2 = (t_w*) malloc(sizeof(t_w)*n_field);
for(i=0; i<n_field; i++)
{
fseek(in_file, 32*(i+1), SEEK_SET);
fgets((char*) s1, sizeof(type_field_descriptor), in_file);
strcpy(s2[i].name, s1->name);
s2[i].field_type = s1->field_type;
s2[i].size = s1->size;
}
// === Write result ===
m = 0;
if(t_key)
{
#ifdef NLS_CHAR
fprintf(out_file, "%s\n", NLS_CHAR);
#endif
#ifdef CONNECT_STRING
fprintf(out_file, "%s\n", CONNECT_STRING);
#endif
for(i = 0; (i < (int) strlen(in_file_name)) && (in_file_name[i] != '.'); i++)
table_name[i] = in_file_name[i];
table_name[i] = '_';
table_name[i + 1] = 0;
// fprintf(out_file, "delete from %s;\n", table_name);
// fprintf(out_file, "commit;\n", table_name);
fprintf(out_file, "drop table %s;\n", table_name);
fprintf(out_file, "create table %s(\n", table_name);
for(i=0; i < (n_field - 1); i++)
fprintf(out_file, "%s_ varchar(%d),\n", s2[i].name, s2[i].size);
fprintf(out_file, "%s_ varchar(%d));\n\n", s2[i].name, s2[i].size);
}
rec_length = 0;
for(i=0; i<n_field; i++)
rec_length += s2[i].size;
rec_length += 2;
fseek(in_file, s0->header_length, SEEK_SET);
while(fgets((char*) s, rec_length, in_file))
if(s[0] == ' ')
{
k = 1;
if(t_key) fprintf(out_file,"insert into %s values(", table_name);
for(i = 0; i < n_field; i++)
{
l = -1;
for(j = 0; j < s2[i].size; j++)
{
ws[j] = s[j + k];
if(ws[j] != ' ') l = j;
}
l++;
ws[l] = 0;
while(ws[0] == ' ') for(j = 0; j < s2[i].size; j++) ws[j] = ws[j + 1];
if(t_key)
{
if(strlen(ws))
fprintf(out_file,"'%s'", ws);
else
fprintf(out_file,"NULL");
if(i != (n_field - 1)) fprintf(out_file,",");
k += s2[i].size;
}
else
{
fprintf(out_file,"%s", ws);
if(i != (n_field - 1)) fprintf(out_file,"%s", s_key);
k += s2[i].size;
}
}
if(t_key)
{
fprintf(out_file,");\n");
m++;
if(!(m % 2000))
{
fprintf(out_file,"commit;\n");
m = 0;
}
}
else
fprintf(out_file,"\n");
}
if(t_key)
{
fprintf(out_file,"commit;\n");
fprintf(out_file,"exit;\n");
}
free(s);
free(s1);
free(s2);
fclose(in_file);
fclose(out_file);
return 0;
}
|