/****************************************************/ /** This program converts EpiInfo .rec **/ /** files into Stata dictionary files. **/ /** **/ /** Author: R. Mark Esman **/ /** Date: 12/5/96 **/ /** Version 1.1 **/ /****************************************************/ #include #include #include #include #define MAXSTRING 100 /* maximum filename length */ /* function prototypes */ int read_rec( FILE *, FILE *, int ); int chk_width( int ); void get_rawdata( FILE *, FILE *, int ); /* beginning of main program function */ void main( int argc, char *argv[] ) { /* define global variables and file pointers */ char c, file_name[MAXSTRING], outfile_name[MAXSTRING]; int i, hd, column; FILE *ifp, *ofp; /* check for correct number of arguments and display syntax */ if (argc != 3) { fprintf(stderr, "\nThis program converts EpiInfo data files "); fprintf(stderr, "(.rec) into Stata dictionary\nformat files"); fprintf(stderr, " (.dct)."); fprintf(stderr, "\n\nSyntax:\n\tepi2dct [input filename] "); fprintf(stderr, "[output filename]\n\nwhere [input filename] "); fprintf(stderr, "is the EpiInfo .rec file and [output "); fprintf(stderr, "filename] \nis the Stata dictionary file "); fprintf(stderr, "to be created. Filename extensions "); fprintf(stderr, "should\nbe included.\n"); exit(1); } /* check input file for reading */ if ((ifp = fopen(argv[1], "r")) == NULL) { fprintf(stderr,"\nInput file could not be opened - EXITING..."); exit(2); } /* check output file for writing */ if ((ofp = fopen(argv[2], "w")) == NULL) { fprintf(stderr,"\nOutput dictionary file could not be opened"); fprintf(stderr," - EXITING..."); exit(3); } /* read number of lines of header info */ fscanf(ifp, "%d", &hd); while(!feof(ifp) && (fgetc(ifp) != '\n')) ; /* begin writing dictionary file */ fprintf(ofp,"dictionary {\n"); /* initialize output file column counter */ column = 1; /* loop through header info for each variable */ for(i=1;i<=hd;i++) { /* call the read_rec function */ column = read_rec(ifp,ofp, column); } /* add closing brace to dictionary */ fprintf(ofp,"}\n"); /* call get_rawdata function to extract raw data from .rec file */ get_rawdata( ifp, ofp, column ); /* close input/output files and finish dictionary file */ fclose(ifp); fclose(ofp); fprintf(stderr,"\nConversion complete...\n%s has been written to disk\n", argv[2]); } /* read_rec function reads variable information one variable at a time. */ /* this function is looped for every line of header information. */ read_rec( FILE *ifp, FILE *ofp, int column ) { /* declare variables */ char c, *t, var_name[15], temp_name[9]="\0", *strncpy(); char data_label[100]="\0", temp_label[32] = "\0"; int i, j, var_type, var_width, len, color, f1, f2, f3, f4, f5; fscanf( ifp, "%14s %d %d %d %d %d %d %d %d", var_name, &f1, &f2, &f3, &f4, &f5, &var_type, &var_width, &color); t = data_label; c = fgetc(ifp); while (c!='\n') { *t = c; c = fgetc(ifp); t++; } *t = '\0'; i=0; while ( data_label[i] != '\0') { data_label[i] = data_label[i+1]; i++; } /* if var_width > 0, remove leading space or characters from variable name and determine data type (float or string). */ if (chk_width( var_width )) { if(!(isalpha( var_name[0] ))) { j=0; while(var_name[j] != '\0') { var_name[j]=var_name[j+1]; j++; } } strncpy( temp_name, var_name, 8 ); strncpy( temp_label, data_label, 31 ); /* Change variable names to lower case */ i=0; while(temp_name[i] != '\0') { temp_name[i]=tolower(temp_name[i]); i++; } /* Write dictionary statements to file */ if(data_label[0] != '\0') { if((var_type!=0) && (var_type<100)) fprintf(ofp,"\tstr%d %s %%%ds \"%s\"\n", var_width, temp_name, var_width, data_label); else fprintf(ofp,"\t%s %%%df \"%s\"\n", temp_name, var_width, data_label); } else { if(var_type!=0) fprintf(ofp,"\tstr%d %s %%%ds\n", var_width, temp_name, var_width); else fprintf(ofp,"\t%s %%%df\n", temp_name, var_width); } } /* Insert newline in dictionary if column >= 78 */ /* if(column>=78) { fprintf(ofp,"\t_newline\n"); column = 1; } else */ if(var_type !=0) { column += var_width; fprintf(ofp,"\t_column(%d)\n", column); } else column += var_width; return column; } /* Check width of variable */ chk_width( int width ) { if (width == 0) return 0; else return 1; } /* Function to read the raw data from input file */ /* and write it out to dictionary file */ void get_rawdata( FILE *ifp, FILE *ofp, int column ) { int c, i=1; while((c=fgetc(ifp))!=EOF) { if(c!='\n' && c!='!' && i=column) { while(c!='!') { fputc(c,ofp); } fputc('\n',ofp); i=1; } } }