3 |
|
#include <string.h> |
4 |
|
#include <unistd.h> |
5 |
|
|
6 |
+ |
#define MAX_FILES 1000 |
7 |
|
|
8 |
|
void usage( void ); |
9 |
|
|
15 |
|
double realTime, currentTime, lastTime; |
16 |
|
double boxX, boxY, boxZ; |
17 |
|
char* foo; |
18 |
< |
char* in_name; |
18 |
< |
char* out_name; |
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 |
< |
out_name = argv[1]; |
29 |
< |
|
30 |
< |
if( !access( out_name, F_OK ) ){ |
122 |
> |
if( !have_out ){ |
123 |
|
|
124 |
< |
in_file = fopen( out_name, "r" ); |
124 |
> |
in_file = fopen( in_name[0], "r" ); |
125 |
|
lineNum =0; |
126 |
|
if( in_file == NULL ){ |
127 |
|
printf( "Error opening \"%s\"\n", out_name ); |
168 |
|
|
169 |
|
fclose( in_file ); |
170 |
|
|
171 |
< |
out_file = fopen( out_name, "a" ); |
171 |
> |
out_file = fopen( in_name[0], "a" ); |
172 |
|
} |
173 |
|
|
174 |
|
else{ |
176 |
|
out_file = fopen( out_name, "w" ); |
177 |
|
lastTime = 0.0; |
178 |
|
|
179 |
< |
in_name = argv[2]; |
88 |
< |
|
89 |
< |
in_file = fopen( in_name, "r" ); |
179 |
> |
in_file = fopen( in_name[0], "r" ); |
180 |
|
lineNum = 0; |
181 |
|
if( in_file == NULL ){ |
182 |
< |
printf( "Error opening \"%s\"\n", in_name ); |
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, lineNum ); |
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 |
< |
fprintf( out_file, "%s", readBuffer ); |
214 |
> |
currentTime = atof( foo ); |
215 |
> |
realTime = currentTime + lastTime; |
216 |
> |
|
217 |
|
|
218 |
< |
i = atoi(readBuffer); |
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 |
< |
fgets(readBuffer, sizeof(readBuffer), in_file); |
108 |
< |
lineNum++; |
109 |
< |
if( feof( in_file ) ){ |
110 |
< |
printf( "File %s ended unexpectedly at line %d\n", in_name, lineNum ); |
111 |
< |
exit(8); |
112 |
< |
} |
113 |
< |
|
114 |
< |
fprintf( out_file, "%s", readBuffer ); |
225 |
> |
boxX = atof( foo ); |
226 |
|
|
227 |
< |
for(j=0; j<i; j++){ |
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 |
< |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
235 |
< |
lineNum++; |
236 |
< |
if( feof( in_file ) ){ |
237 |
< |
printf( "File %s ended unexpectedly at line %d," |
238 |
< |
" with atom %d\n", in_name, lineNum, j); |
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 |
< |
fprintf( out_file, "%s", readBuffer ); |
246 |
< |
} |
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=2; k<argc; k++ ){ |
275 |
> |
for( k=1; k<nFiles; k++ ){ |
276 |
|
|
277 |
< |
in_name = argv[k]; |
135 |
< |
|
136 |
< |
in_file = fopen( in_name, "r" ); |
277 |
> |
in_file = fopen( in_name[k], "r" ); |
278 |
|
lineNum = 0; |
279 |
|
if( in_file == NULL ){ |
280 |
< |
printf( "Error opening \"%s\"\n", in_name ); |
280 |
> |
printf( "Error opening \"%s\"\n", in_name[k] ); |
281 |
|
exit(8); |
282 |
|
} |
283 |
|
|
284 |
|
|
285 |
< |
// skip the zero frame |
285 |
> |
// if this is a time = 0.0 frame then don't write it. |
286 |
|
|
287 |
< |
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
147 |
< |
lineNum++; |
148 |
< |
if( feof( in_file ) ){ |
149 |
< |
printf( "File %s ended unexpectedly at line %d\n", in_name, lineNum ); |
150 |
< |
exit(8); |
151 |
< |
} |
152 |
< |
|
153 |
< |
i = atoi(readBuffer); |
154 |
< |
|
155 |
< |
fgets(readBuffer, sizeof(readBuffer), in_file); |
156 |
< |
lineNum++; |
157 |
< |
if( feof( in_file ) ){ |
158 |
< |
printf( "File %s ended unexpectedly at line %d\n", in_name, lineNum ); |
159 |
< |
exit(8); |
160 |
< |
} |
161 |
< |
|
162 |
< |
for(j=0; j<i; j++){ |
163 |
< |
|
287 |
> |
if( skipFirst ){ |
288 |
|
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
289 |
< |
lineNum++; |
289 |
> |
lineNum++; |
290 |
|
if( feof( in_file ) ){ |
291 |
< |
printf( "File %s ended unexpectedly at line %d," |
292 |
< |
" with atom %d\n", in_name, lineNum, j); |
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 |
< |
|
317 |
> |
|
318 |
|
// now start reading and writing |
319 |
|
|
320 |
|
fgets( readBuffer, sizeof( readBuffer ), in_file ); |
326 |
|
|
327 |
|
while( !feof( in_file ) ){ |
328 |
|
|
184 |
– |
fprintf( out_file, "%s", readBuffer ); |
185 |
– |
|
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, lineNum ); |
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, in_name); |
342 |
> |
printf("error in reading time at line %d in %s\n", lineNum, |
343 |
> |
in_name[k]); |
344 |
|
exit(8); |
345 |
|
} |
346 |
|
|
350 |
|
|
351 |
|
foo = strtok(NULL, " ,;\t"); |
352 |
|
if(foo == NULL){ |
353 |
< |
printf("error in reading boxX at line %d in %s\n", lineNum, in_name); |
353 |
> |
printf("error in reading boxX at line %d in %s\n", lineNum, |
354 |
> |
in_name[k]); |
355 |
|
exit(8); |
356 |
|
} |
357 |
|
|
359 |
|
|
360 |
|
foo = strtok(NULL, " ,;\t"); |
361 |
|
if(foo == NULL){ |
362 |
< |
printf("error in reading boxY at line %d in %s\n", lineNum, in_name); |
362 |
> |
printf("error in reading boxY at line %d in %s\n", lineNum, |
363 |
> |
in_name[k]); |
364 |
|
exit(8); |
365 |
|
} |
366 |
|
|
368 |
|
|
369 |
|
foo = strtok(NULL, " ,;\t"); |
370 |
|
if(foo == NULL){ |
371 |
< |
printf("error in reading boxZ at line %d in %s\n", lineNum, in_name); |
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 |
< |
fprintf( out_file, "%lf\t%lf\t%lf\t%lf\n", |
231 |
< |
realTime, boxX, boxY, boxZ ); |
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, lineNum, j); |
392 |
> |
" with atom %d\n", in_name[k], lineNum, j); |
393 |
|
exit(8); |
394 |
|
} |
395 |
|
|
396 |
< |
fprintf( out_file, "%s", readBuffer ); |
396 |
> |
if(printMe) fprintf( out_file, "%s", readBuffer ); |
397 |
|
|
398 |
|
} |
399 |
|
|
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 |
+ |
} |