| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#define MAX_FILES 1000 |
| 7 |
|
| 8 |
void usage( void ); |
| 9 |
|
| 10 |
char* program_name; |
| 11 |
|
| 12 |
|
| 13 |
int main( int argc, char* argv[] ){ |
| 14 |
|
| 15 |
double realTime, currentTime, lastTime; |
| 16 |
double boxX, boxY, boxZ; |
| 17 |
char* foo; |
| 18 |
char* in_name[MAX_FILES]; |
| 19 |
char readBuffer[2000]; |
| 20 |
int i,j,k; |
| 21 |
int lineNum; |
| 22 |
|
| 23 |
int current_flag; |
| 24 |
int done; |
| 25 |
|
| 26 |
char* out_name; |
| 27 |
int have_out = 0; |
| 28 |
|
| 29 |
int timeMod = 1; |
| 30 |
|
| 31 |
int nFiles; |
| 32 |
int index; |
| 33 |
|
| 34 |
int printMe = 0; |
| 35 |
int skipFirst = 0; |
| 36 |
|
| 37 |
FILE *in_file; |
| 38 |
FILE *out_file; |
| 39 |
|
| 40 |
program_name = argv[0]; |
| 41 |
|
| 42 |
nFiles = 0; |
| 43 |
for( i = 1; i < argc; i++){ |
| 44 |
|
| 45 |
if(argv[i][0] =='-'){ |
| 46 |
|
| 47 |
// parse the option |
| 48 |
|
| 49 |
if(argv[i][1] == '-' ){ |
| 50 |
|
| 51 |
// parse long word options |
| 52 |
|
| 53 |
fprintf( stderr, |
| 54 |
"Invalid option \"%s\"\n", argv[i] ); |
| 55 |
usage(); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
else{ |
| 60 |
|
| 61 |
// parse single character options |
| 62 |
|
| 63 |
done = 0; |
| 64 |
j = 1; |
| 65 |
current_flag = argv[i][j]; |
| 66 |
while( (current_flag != '\0') && (!done) ){ |
| 67 |
|
| 68 |
switch(current_flag){ |
| 69 |
|
| 70 |
case 'o': |
| 71 |
// -o <outFile> => the output file. |
| 72 |
|
| 73 |
i++; |
| 74 |
out_name = argv[i]; |
| 75 |
have_out = 1; |
| 76 |
done = 1; |
| 77 |
break; |
| 78 |
|
| 79 |
case 't': |
| 80 |
// -t <#> => the time increment |
| 81 |
|
| 82 |
i++; |
| 83 |
timeMod = atoi( argv[i] ); |
| 84 |
done = 1; |
| 85 |
break; |
| 86 |
|
| 87 |
case 'h': |
| 88 |
// -h => display help |
| 89 |
|
| 90 |
usage(); |
| 91 |
break; |
| 92 |
|
| 93 |
case 's': |
| 94 |
// -b => skip first frame |
| 95 |
|
| 96 |
skipFirst = 1; |
| 97 |
break; |
| 98 |
|
| 99 |
default: |
| 100 |
|
| 101 |
(void)fprintf(stderr, "Bad option \"-%c\"\n", current_flag); |
| 102 |
usage(); |
| 103 |
} |
| 104 |
j++; |
| 105 |
current_flag = argv[i][j]; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
else{ |
| 111 |
|
| 112 |
nFiles++; |
| 113 |
if( nFiles >= MAX_FILES ){ |
| 114 |
printf( "Error, MAX_FILES, %d, exceeded at %s\n", MAX_FILES, argv[i] ); |
| 115 |
exit(0); |
| 116 |
} |
| 117 |
|
| 118 |
in_name[(nFiles-1)] = argv[i]; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if( !have_out ){ |
| 123 |
|
| 124 |
in_file = fopen( in_name[0], "r" ); |
| 125 |
lineNum =0; |
| 126 |
if( in_file == NULL ){ |
| 127 |
printf( "Error opening \"%s\"\n", out_name ); |
| 128 |
exit(8); |
| 129 |
} |
| 130 |
|
| 131 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 132 |
lineNum++; |
| 133 |
if( feof( in_file ) ){ |
| 134 |
printf( "File %s ended unexpectedly at line %d\n", out_name, lineNum ); |
| 135 |
exit(8); |
| 136 |
} |
| 137 |
|
| 138 |
while( !feof( in_file ) ){ |
| 139 |
|
| 140 |
i = atoi(readBuffer); |
| 141 |
|
| 142 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 143 |
lineNum++; |
| 144 |
if( feof( in_file ) ){ |
| 145 |
printf( "File %s ended unexpectedly at line %d\n", |
| 146 |
out_name, lineNum); |
| 147 |
exit(8); |
| 148 |
} |
| 149 |
|
| 150 |
foo = strtok( readBuffer, " \t;,\n" ); |
| 151 |
lastTime = atof( foo ); |
| 152 |
|
| 153 |
for(j=0; j<i; j++){ |
| 154 |
|
| 155 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 156 |
lineNum++; |
| 157 |
if( feof( in_file ) ){ |
| 158 |
printf( "File %s ended unexpectedly at line %d," |
| 159 |
" with atom %d\n", out_name, lineNum, j); |
| 160 |
exit(8); |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 166 |
lineNum++; |
| 167 |
} |
| 168 |
|
| 169 |
fclose( in_file ); |
| 170 |
|
| 171 |
out_file = fopen( in_name[0], "a" ); |
| 172 |
} |
| 173 |
|
| 174 |
else{ |
| 175 |
|
| 176 |
out_file = fopen( out_name, "w" ); |
| 177 |
lastTime = 0.0; |
| 178 |
|
| 179 |
in_file = fopen( in_name[0], "r" ); |
| 180 |
lineNum = 0; |
| 181 |
if( in_file == NULL ){ |
| 182 |
printf( "Error opening \"%s\"\n", in_name[0] ); |
| 183 |
exit(8); |
| 184 |
} |
| 185 |
|
| 186 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 187 |
lineNum++; |
| 188 |
if( feof( in_file ) ){ |
| 189 |
printf( "File %s ended unexpectedly at line %d\n", in_name[0], lineNum ); |
| 190 |
exit(8); |
| 191 |
} |
| 192 |
|
| 193 |
while( !feof( in_file ) ){ |
| 194 |
|
| 195 |
|
| 196 |
i = atoi(readBuffer); |
| 197 |
|
| 198 |
fgets(readBuffer, sizeof(readBuffer), in_file); |
| 199 |
lineNum++; |
| 200 |
if( feof( in_file ) ){ |
| 201 |
printf( "File %s ended unexpectedly at line %d\n", in_name[0], |
| 202 |
lineNum ); |
| 203 |
exit(8); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
foo = strtok(readBuffer, " ,;\t"); |
| 208 |
if(foo == NULL){ |
| 209 |
printf("error in reading time at line %d in %s\n", lineNum, |
| 210 |
in_name[0]); |
| 211 |
exit(8); |
| 212 |
} |
| 213 |
|
| 214 |
currentTime = atof( foo ); |
| 215 |
realTime = currentTime + lastTime; |
| 216 |
|
| 217 |
|
| 218 |
foo = strtok(NULL, " ,;\t"); |
| 219 |
if(foo == NULL){ |
| 220 |
printf("error in reading boxX at line %d in %s\n", lineNum, |
| 221 |
in_name[0]); |
| 222 |
exit(8); |
| 223 |
} |
| 224 |
|
| 225 |
boxX = atof( foo ); |
| 226 |
|
| 227 |
foo = strtok(NULL, " ,;\t"); |
| 228 |
if(foo == NULL){ |
| 229 |
printf("error in reading boxY at line %d in %s\n", lineNum, |
| 230 |
in_name[0]); |
| 231 |
exit(8); |
| 232 |
} |
| 233 |
|
| 234 |
boxY = atof( foo ); |
| 235 |
|
| 236 |
foo = strtok(NULL, " ,;\t"); |
| 237 |
if(foo == NULL){ |
| 238 |
printf("error in reading boxZ at line %d in %s\n", lineNum, |
| 239 |
in_name[0]); |
| 240 |
exit(8); |
| 241 |
} |
| 242 |
|
| 243 |
boxZ = atof( foo ); |
| 244 |
|
| 245 |
printMe = !( ((int)realTime) % timeMod ); |
| 246 |
|
| 247 |
if( printMe ){ |
| 248 |
fprintf( out_file, "%d\n", i ); |
| 249 |
fprintf( out_file, "%lf\t%lf\t%lf\t%lf\n", |
| 250 |
realTime, boxX, boxY, boxZ ); |
| 251 |
} |
| 252 |
|
| 253 |
for(j=0; j<i; j++){ |
| 254 |
|
| 255 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 256 |
lineNum++; |
| 257 |
if( feof( in_file ) ){ |
| 258 |
printf( "File %s ended unexpectedly at line %d," |
| 259 |
" with atom %d\n", in_name[0], lineNum, j); |
| 260 |
exit(8); |
| 261 |
} |
| 262 |
|
| 263 |
if(printMe) fprintf( out_file, "%s", readBuffer ); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 268 |
lineNum++; |
| 269 |
} |
| 270 |
|
| 271 |
lastTime = realTime; |
| 272 |
fclose( in_file ); |
| 273 |
} |
| 274 |
|
| 275 |
for( k=1; k<nFiles; k++ ){ |
| 276 |
|
| 277 |
in_file = fopen( in_name[k], "r" ); |
| 278 |
lineNum = 0; |
| 279 |
if( in_file == NULL ){ |
| 280 |
printf( "Error opening \"%s\"\n", in_name[k] ); |
| 281 |
exit(8); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
// if this is a time = 0.0 frame then don't write it. |
| 286 |
|
| 287 |
if( skipFirst ){ |
| 288 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 289 |
lineNum++; |
| 290 |
if( feof( in_file ) ){ |
| 291 |
printf( "File %s ended unexpectedly at line %d\n", in_name[k], |
| 292 |
lineNum ); |
| 293 |
exit(8); |
| 294 |
} |
| 295 |
|
| 296 |
i = atoi(readBuffer); |
| 297 |
|
| 298 |
fgets(readBuffer, sizeof(readBuffer), in_file); |
| 299 |
lineNum++; |
| 300 |
if( feof( in_file ) ){ |
| 301 |
printf( "File %s ended unexpectedly at line %d\n", in_name[k], |
| 302 |
lineNum ); |
| 303 |
exit(8); |
| 304 |
} |
| 305 |
|
| 306 |
for(j=0; j<i; j++){ |
| 307 |
|
| 308 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 309 |
lineNum++; |
| 310 |
if( feof( in_file ) ){ |
| 311 |
printf( "File %s ended unexpectedly at line %d," |
| 312 |
" with atom %d\n", in_name[k], lineNum, j); |
| 313 |
exit(8); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// now start reading and writing |
| 319 |
|
| 320 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 321 |
lineNum++; |
| 322 |
if( feof( in_file ) ){ |
| 323 |
printf( "File %s ended unexpectedly at line %d\n", in_name, lineNum ); |
| 324 |
exit(8); |
| 325 |
} |
| 326 |
|
| 327 |
while( !feof( in_file ) ){ |
| 328 |
|
| 329 |
i = atoi(readBuffer); |
| 330 |
|
| 331 |
fgets(readBuffer, sizeof(readBuffer), in_file); |
| 332 |
lineNum++; |
| 333 |
if( feof( in_file ) ){ |
| 334 |
printf( "File %s ended unexpectedly at line %d\n", in_name[k], |
| 335 |
lineNum ); |
| 336 |
exit(8); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
foo = strtok(readBuffer, " ,;\t"); |
| 341 |
if(foo == NULL){ |
| 342 |
printf("error in reading time at line %d in %s\n", lineNum, |
| 343 |
in_name[k]); |
| 344 |
exit(8); |
| 345 |
} |
| 346 |
|
| 347 |
currentTime = atof( foo ); |
| 348 |
realTime = currentTime + lastTime; |
| 349 |
|
| 350 |
|
| 351 |
foo = strtok(NULL, " ,;\t"); |
| 352 |
if(foo == NULL){ |
| 353 |
printf("error in reading boxX at line %d in %s\n", lineNum, |
| 354 |
in_name[k]); |
| 355 |
exit(8); |
| 356 |
} |
| 357 |
|
| 358 |
boxX = atof( foo ); |
| 359 |
|
| 360 |
foo = strtok(NULL, " ,;\t"); |
| 361 |
if(foo == NULL){ |
| 362 |
printf("error in reading boxY at line %d in %s\n", lineNum, |
| 363 |
in_name[k]); |
| 364 |
exit(8); |
| 365 |
} |
| 366 |
|
| 367 |
boxY = atof( foo ); |
| 368 |
|
| 369 |
foo = strtok(NULL, " ,;\t"); |
| 370 |
if(foo == NULL){ |
| 371 |
printf("error in reading boxZ at line %d in %s\n", lineNum, |
| 372 |
in_name[k]); |
| 373 |
exit(8); |
| 374 |
} |
| 375 |
|
| 376 |
boxZ = atof( foo ); |
| 377 |
|
| 378 |
printMe = !( ((int)realTime) % timeMod ); |
| 379 |
|
| 380 |
if( printMe ){ |
| 381 |
fprintf( out_file, "%d\n", i ); |
| 382 |
fprintf( out_file, "%lf\t%lf\t%lf\t%lf\n", |
| 383 |
realTime, boxX, boxY, boxZ ); |
| 384 |
} |
| 385 |
|
| 386 |
for(j=0; j<i; j++){ |
| 387 |
|
| 388 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 389 |
lineNum++; |
| 390 |
if( feof( in_file ) ){ |
| 391 |
printf( "File %s ended unexpectedly at line %d," |
| 392 |
" with atom %d\n", in_name[k], lineNum, j); |
| 393 |
exit(8); |
| 394 |
} |
| 395 |
|
| 396 |
if(printMe) fprintf( out_file, "%s", readBuffer ); |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
| 401 |
lineNum++; |
| 402 |
} |
| 403 |
|
| 404 |
lastTime = realTime; |
| 405 |
fclose( in_file ); |
| 406 |
} |
| 407 |
|
| 408 |
fclose( out_file ); |
| 409 |
|
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
/*************************************************************************** |
| 415 |
* prints out the usage for the command line arguments, then exits. |
| 416 |
***************************************************************************/ |
| 417 |
|
| 418 |
void usage(){ |
| 419 |
(void)fprintf(stderr, |
| 420 |
"The proper usage is: %s [options] dumpFile1 dumpFile2...\n" |
| 421 |
"\n" |
| 422 |
"Options:\n" |
| 423 |
"\n" |
| 424 |
" -o <outFile> the output file\n" |
| 425 |
" *default is to append to first file.\n" |
| 426 |
" -t <#> only print out time steps that are\n" |
| 427 |
" increments of <#>\n" |
| 428 |
" -s skip the first frame (t = 0.0 )\n" |
| 429 |
"\n" |
| 430 |
" -h display this message\n" |
| 431 |
"\n", |
| 432 |
program_name); |
| 433 |
exit(8); |
| 434 |
} |