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