00001 #include <stdio.h>
00002 #define BUFLEN 4096
00003 #define formatstrlen 64
00004
00005 char searchstr1[] = "Yes";
00006 char searchstr2[] = "S[1]";
00007 char searchstr3[] = "S[2]";
00008 char searchstr4[] = "# Probe Data Number :: N=";
00009 char searchstr5[] = "#C Index";
00010 char seps1[] = "# ,\t\n";
00011 char seps23[] = " \n";
00012 char seps4[] = "x \"\n";
00013
00014
00015 int hmf(char *buff , char *srchstr, char *seps)
00016 {
00017 char *token;
00018 int ret;
00019 ret = 0;
00020 token = strtok( buff, seps );
00021 while( token != NULL )
00022 {
00023
00024
00025 if (!strcmp( token, srchstr )) ret++;
00026
00027 token = strtok( NULL, seps );
00028 }
00029 return ret;
00030 }
00031
00032
00033 int wiff(char *buff , char *srchstr)
00034 {
00035 char *pdest;
00036 int ret;
00037 if ( (pdest = strstr( buff, srchstr )) == NULL ) {
00038
00039 return 0;
00040 }
00041 else {
00042 ret = (int)( pdest - buff );
00043
00044 return ret;
00045 }
00046 }
00047
00048 int main( int argc, char *argv[] )
00049 {
00050 FILE *fptr, *fptr2;
00051 char *buffer, *buf, *formatstr;
00052 float *val, *bias, readout[21];
00053 int result = 0, filesize = 0, i = 0, j = 0, numofcol = 0, numcol = 0, numofpasup = 0, numofpasdn = 0, numofpnt = 0, numofpas = 0, headerbeginat = 0, headerlength = 0, index =0, pointspropass = 0;
00054 char filetoread[255], filetowrite[255];
00055 int xcol=0, ycol=0;
00056
00057 switch ( argc )
00058 {
00059 case (5):
00060 printf("We have found %d commandline parameters: ", argc-1);
00061 for (i=1; i<argc; i++)
00062 {
00063 printf("%s ", argv[i]);
00064
00065 }
00066 printf("\n");
00067 if ((sscanf(argv[1], "%s", &filetoread)!=1) | (sscanf(argv[2], "%s", &filetowrite)!=1) | (sscanf(argv[3], "%d", &xcol)!=1) | (sscanf(argv[4], "%d", &ycol)!=1))
00068 {
00069 printf("Cannot parse commandline!\n");
00070 printf("Syntax: converter filetoread filetowrite xcol ycol\n");
00071
00072 exit(-1);
00073 }
00074 break;
00075 default:
00076 printf("\nSyntax: converter filetoread filetowrite xcol ycol\n");
00077
00078 exit(-1);
00079 }
00080
00081 if ( ( fptr = fopen( filetoread, "r" ))== NULL ) {
00082 printf( "fopen(): File not found!\n" );
00083
00084 exit(-1);
00085 }
00086
00087 if ( ( fptr2 = fopen( filetowrite, "w+" )) == NULL ) {
00088 printf( "fopen(): File cannot be created!\n" );
00089
00090 exit(-1);
00091 }
00092
00093 buffer = malloc(BUFLEN);
00094 if( buffer == NULL ) {
00095 printf( "malloc(): Memory allocation failed!\n" );
00096
00097 exit(-1);
00098 }
00099 filesize=0;
00100 while ( !feof(fptr) ) filesize += fread( buffer, sizeof( char ), BUFLEN, fptr );
00101 printf( "Filesize: %d\n", filesize );
00102
00103
00104 buf = malloc( filesize );
00105 if( buf == NULL ) {
00106 printf( "malloc(): Memory allocation failed!\n" );
00107
00108 exit(-1);
00109 }
00110
00111
00112 if ( fseek( fptr, 0L, SEEK_SET ) ) {
00113 printf( "fseek(): File seek failed!\n" );
00114
00115 exit(-1);
00116 }
00117 result = fread( buf, sizeof( char ), filesize, fptr );
00118 numofcol = hmf(buf, searchstr1, seps1);
00119
00120 if (numofcol == 0)
00121 {
00122 numofcol=1;
00123 printf("\"%s\": %d -> %d column, nothing to do...\n", searchstr1, 0, numofcol);
00124
00125 exit(-1);
00126 }
00127 else
00128 {
00129 printf("\"%s\": %d -> %d column(s)\n", searchstr1, numofcol, numofcol+2);
00130 numofcol+= 2;
00131 }
00132
00133
00134 if ( fseek( fptr, 0L, SEEK_SET ) ) {
00135 printf( "fseek(): File seek failed!\n" );
00136
00137 exit(-1);
00138 }
00139 result = fread( buf, sizeof( char ), filesize, fptr );
00140 numofpasup=hmf(buf, searchstr2, seps23);
00141 printf("\"%s\": %d -> %d pass(es) up\n", searchstr2, numofpasup, numofpasup);
00142
00143
00144 if ( fseek( fptr, 0L, SEEK_SET ) ) {
00145 printf( "fseek(): File seek failed!\n" );
00146
00147 exit(-1);
00148 }
00149 result = fread( buf, sizeof( char ), filesize, fptr );
00150 numofpasdn=hmf(buf, searchstr3, seps23);
00151 printf("\"%s\": %d -> %d pass(es) down\n", searchstr3, numofpasdn, numofpasdn);
00152
00153 numofpas=numofpasup + numofpasdn - 1;
00154 printf("Total number: %d pass(es)\n", numofpas);
00155
00156
00157 if ( fseek( fptr, 0L, SEEK_SET ) ) {
00158 printf( "fseek(): File seek failed!\n" );
00159
00160 exit(-1);
00161 }
00162 result = fread( buf, sizeof( char ), filesize, fptr );
00163
00164 printf( "Searched string \"%s\" found @ %d\n",searchstr4, result = wiff(buf, searchstr4) );
00165 sscanf(buf + result + sizeof (searchstr4) - 1, "%d", &numofpnt);
00166 printf("Number of Points: %d\n", numofpnt);
00167
00168
00169 pointspropass= numofpnt / numofpas;
00170 printf("Number of points pro pass: %d\n", pointspropass);
00171
00172
00173 printf( "Searched string \"%s\" found @ %d\n", searchstr5, result=wiff(buf, searchstr5) );
00174 headerbeginat=result;
00175 headerlength=wiff(buf + headerbeginat, "\n");
00176 printf("Header at %d, length %d\n", headerbeginat, headerlength );
00177 strncpy(buffer, buf + headerbeginat, headerlength + 1);
00178 printf( "Number of column(s), tabs method: %d\n", numcol = hmf(buffer, "\t", seps4) );
00179 free(buffer);
00180 free( buf );
00181
00182 if (numcol!=numofcol) printf("Warning! Number of column(s) %d is not equal to number of \"Yes\"(s) %d\n", numcol, numofcol);
00183
00184 if ((xcol>numcol) | (ycol>numcol) | (xcol==ycol))
00185 {
00186 printf("Commandline parameters for columns are not correct!\n");
00187
00188 exit(-1);
00189 }
00190
00191 formatstr = malloc (formatstrlen);
00192 if( formatstr == NULL ) {
00193 printf( "malloc(): Memory allocation failed!\n" );
00194
00195 exit(-1);
00196 }
00197 strcpy(formatstr, "%d\t");
00198 for (i=1; i<=numcol -1; i++)
00199 {
00200 strcat(formatstr, "%f\t");
00201 }
00202 strcat(formatstr, "\n");
00203 printf("Format:%s", formatstr);
00204
00205 if ( fseek( fptr, headerbeginat + headerlength + 1, SEEK_SET ) ) {
00206 printf( "fseek(): File seek failed!\n" );
00207
00208 exit(-1);
00209 }
00210
00211 val = malloc( numofpnt * sizeof( float ) );
00212 if( val == NULL ) {
00213 printf( "malloc(): Memory allocation failed!\n" );
00214
00215 exit(-1);
00216 }
00217
00218
00219 bias = malloc( (pointspropass + 1) * sizeof (float) );
00220 if( bias == NULL ) {
00221 printf( "malloc(): Memory allocation failed!\n" );
00222
00223 exit(-1);
00224 }
00225
00226 for (i = 0; i <= numofpnt - 1; i++) {
00227
00228 fscanf( fptr, formatstr, &index, &readout[0], &readout[1], &readout[2], &readout[3], &readout[4], &readout[5], &readout[6], &readout[7], &readout[8], &readout[9], &readout[10], &readout[11], &readout[12], &readout[13], &readout[14], &readout[15], &readout[16], &readout[17], &readout[18], &readout[19], &readout[20], &readout[21] );
00229
00230 if ((i >= pointspropass - 1) && (i<= 2*pointspropass - 1)) {
00231 bias[i-pointspropass + 1] = readout[xcol-2];
00232
00233 }
00234 val[i] = readout[ycol-2];
00235
00236 }
00237 fclose( fptr );
00238
00239 for (i = 0; i <= pointspropass; i++){
00240 fprintf(fptr2, "%d\t%f\t", i, bias[i]);
00241 for (j=0; j < numofpas; j++){
00242
00243 if ( j % 2 == 0) {
00244 fprintf(fptr2, "%f\t", val[(j + 1) * pointspropass - i - 1]);
00245 }
00246 else {
00247 fprintf(fptr2, "%f\t", val[j * pointspropass + i - 1]);
00248 }
00249
00250 }
00251 fprintf(fptr2, "\n");
00252 }
00253
00254 fclose( fptr2 );
00255 free( bias );
00256 free( val );
00257 free(formatstr);
00258
00259 return;
00260 }