1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
|
5 |
#define STR_BUFFER_SIZE 500 |
6 |
|
7 |
// ******************************************************* |
8 |
// Uncomment the following two tyedefs to support multiple |
9 |
// input files |
10 |
// ******************************************************* |
11 |
|
12 |
typedef struct{ |
13 |
char fileName[STR_BUFFER_SIZE]; |
14 |
FILE* filePtr; |
15 |
} fileStruct; |
16 |
|
17 |
typedef struct linkedNameTag { |
18 |
char name[STR_BUFFER_SIZE]; |
19 |
struct linkedNameTag* next; |
20 |
} linkedName; |
21 |
|
22 |
// ******************************************************* |
23 |
// end multiple file support |
24 |
// ******************************************************* |
25 |
|
26 |
|
27 |
char *programName; /*the name of the program */ |
28 |
void usage(void); |
29 |
|
30 |
int main(argC, argV) |
31 |
int argC; |
32 |
char *argV[]; |
33 |
{ |
34 |
int i,j; // loop counters |
35 |
|
36 |
char* outPrefix; // the output prefix |
37 |
|
38 |
char* conversionCheck; |
39 |
int conversionError; |
40 |
int optionError; |
41 |
|
42 |
char currentFlag; // used in parsing the flags |
43 |
int done = 0; // multipurpose boolean |
44 |
int havePrefix; // boolean for the output prefix |
45 |
|
46 |
int flag1; |
47 |
char flag1Arg1[STR_BUFFER_SIZE]; |
48 |
char flag1Arg2[STR_BUFFER_SIZE]; |
49 |
int flag2; |
50 |
double flag2Val; |
51 |
int flag3; |
52 |
int rFlag; |
53 |
int iFlag, iVal; |
54 |
|
55 |
// ********************************************************** |
56 |
// to support single or multiple input files, uncomment the |
57 |
// appropriate declarations |
58 |
// ********************************************************** |
59 |
// single: |
60 |
|
61 |
// char* inName; |
62 |
// FILE* inFile; |
63 |
|
64 |
// *********************************************************** |
65 |
// multiple: |
66 |
|
67 |
fileStruct* inputArray; |
68 |
linkedName* headName; |
69 |
linkedName* currentName; |
70 |
int nInputs; |
71 |
|
72 |
// end input file declarations |
73 |
// *********************************************************** |
74 |
|
75 |
|
76 |
|
77 |
// intialize default values |
78 |
|
79 |
|
80 |
// ******************************************** |
81 |
// single input: |
82 |
|
83 |
// inName = NULL; |
84 |
// inFile = NULL; |
85 |
|
86 |
// ********************************************* |
87 |
// multiple input: |
88 |
|
89 |
nInputs = 0; |
90 |
inputArray = NULL; |
91 |
headName = NULL; |
92 |
currentName = NULL; |
93 |
|
94 |
// end file initialization |
95 |
// ********************************************** |
96 |
|
97 |
|
98 |
|
99 |
outPrefix = NULL; |
100 |
|
101 |
conversionError = 0; |
102 |
optionError = 0; |
103 |
|
104 |
havePrefix = 0; |
105 |
flag1 = 0; |
106 |
flag2 = 0; |
107 |
flag3 = 0; |
108 |
rFlag = 0; |
109 |
iFlag = 0; |
110 |
|
111 |
flag2Val = 0.0; |
112 |
iVal = 0; |
113 |
|
114 |
|
115 |
|
116 |
// here we set the name of the program need by the usage message |
117 |
programName = argV[0]; |
118 |
|
119 |
|
120 |
for( i = 1; i < argC; i++){ |
121 |
|
122 |
if(argV[i][0] =='-'){ |
123 |
|
124 |
// parse the option |
125 |
|
126 |
if(argV[i][1] == '-' ){ |
127 |
|
128 |
// parse long word options |
129 |
|
130 |
if( !strcmp( argV[i], "--flag1" ) ){ |
131 |
flag1 = 1; // set flag1 to true |
132 |
|
133 |
i++; |
134 |
if( i>=argC ){ |
135 |
fprintf( stderr, |
136 |
"\n" |
137 |
"not enough arguments for flag1\n"); |
138 |
usage(); |
139 |
exit(0); |
140 |
} |
141 |
strcpy( flag1Arg1, argV[i] ); |
142 |
|
143 |
i++; |
144 |
if( i>=argC ){ |
145 |
fprintf( stderr, |
146 |
"\n" |
147 |
"not enough arguments for flag1\n"); |
148 |
usage(); |
149 |
exit(0); |
150 |
} |
151 |
strcpy( flag1Arg2, argV[i] ); |
152 |
} |
153 |
|
154 |
else if( !strcmp( argV[i], "--flag2" ) ){ |
155 |
flag2 = 1; // set the flag2 to true; |
156 |
|
157 |
i++; |
158 |
if( i>=argC ){ |
159 |
fprintf( stderr, |
160 |
"\n" |
161 |
"not enough arguments for flag2\n"); |
162 |
usage(); |
163 |
exit(0); |
164 |
} |
165 |
|
166 |
flag2Val = strtod( argV[i], &conversionCheck ); |
167 |
if( conversionCheck == argV[i] ) conversionError = 1; |
168 |
if( *conversionCheck != '\0' ) conversionError = 1; |
169 |
|
170 |
if( conversionError ){ |
171 |
|
172 |
fprintf( stderr, |
173 |
"Error converting \"%s\" to a double\n", argV[i] ); |
174 |
usage(); |
175 |
exit(0); |
176 |
} |
177 |
|
178 |
} |
179 |
|
180 |
else if( !strcmp( argV[i], "--flag3") ){ |
181 |
flag3 = 1; // set flag3 to be true |
182 |
|
183 |
} |
184 |
|
185 |
// anything else is an error |
186 |
|
187 |
else{ |
188 |
fprintf( stderr, |
189 |
"Invalid option \"%s\"\n", argV[i] ); |
190 |
usage(); |
191 |
exit(0); |
192 |
} |
193 |
} |
194 |
|
195 |
else{ |
196 |
|
197 |
// parse single character options |
198 |
|
199 |
done =0; |
200 |
j = 1; |
201 |
currentFlag = argV[i][j]; |
202 |
while( (currentFlag != '\0') && (!done) ){ |
203 |
|
204 |
switch(currentFlag){ |
205 |
|
206 |
case 'h': |
207 |
// -h => give the usage help message |
208 |
|
209 |
usage(); |
210 |
exit(0); |
211 |
break; |
212 |
|
213 |
case 'o': |
214 |
// -o <prefix> => the output prefix. |
215 |
|
216 |
j++; |
217 |
currentFlag = argV[i][j]; |
218 |
|
219 |
if( currentFlag != '\0' ) optionError = 1; |
220 |
|
221 |
if( optionError ){ |
222 |
fprintf( stderr, |
223 |
"\n" |
224 |
"The -o flag should end an option sequence.\n" |
225 |
" example: -ro <outname> *NOT* -or <outname>\n" ); |
226 |
usage(); |
227 |
exit(0); |
228 |
} |
229 |
|
230 |
i++; |
231 |
if( i>=argC ){ |
232 |
fprintf( stderr, |
233 |
"\n" |
234 |
"not enough arguments for -o\n"); |
235 |
usage(); |
236 |
exit(0); |
237 |
} |
238 |
|
239 |
outPrefix = argV[i]; |
240 |
if( outPrefix[0] == '-' ) optionError = 1; |
241 |
|
242 |
if( optionError ){ |
243 |
fprintf( stderr, |
244 |
"\n" |
245 |
"\"%s\" is not a valid out prefix/name.\n" |
246 |
"Out prefix/name should not begin with a dash.\n", |
247 |
outPrefix ); |
248 |
usage(); |
249 |
exit(0); |
250 |
} |
251 |
|
252 |
havePrefix = 1; |
253 |
done = 1; |
254 |
break; |
255 |
|
256 |
|
257 |
|
258 |
case 'r': |
259 |
// the r flag |
260 |
|
261 |
rFlag = 1; // set rflag to true |
262 |
break; |
263 |
|
264 |
case 'i': |
265 |
// -i <int> set <int> to the iVal |
266 |
|
267 |
iFlag = 1; // set iFlag to true |
268 |
j++; |
269 |
currentFlag = argV[i][j]; |
270 |
|
271 |
if( currentFlag != '\0' ) optionError = 1; |
272 |
|
273 |
if( optionError ){ |
274 |
fprintf( stderr, |
275 |
"\n" |
276 |
"The -i flag should end an option sequence.\n" |
277 |
" example: -ri <int> *NOT* -ir <int>\n" ); |
278 |
usage(); |
279 |
exit(0); |
280 |
} |
281 |
|
282 |
i++; |
283 |
if( i>=argC ){ |
284 |
fprintf( stderr, |
285 |
"\n" |
286 |
"not enough arguments for -i\n"); |
287 |
usage(); |
288 |
exit(0); |
289 |
} |
290 |
|
291 |
iVal = (int)strtol( argV[i], &conversionCheck, 10 ); |
292 |
if( conversionCheck == argV[i] ) conversionError = 1; |
293 |
if( *conversionCheck != '\0' ) conversionError = 1; |
294 |
|
295 |
if( conversionError ){ |
296 |
|
297 |
fprintf( stderr, |
298 |
"Error converting \"%s\" to a int\n", argV[i] ); |
299 |
usage(); |
300 |
exit(0); |
301 |
} |
302 |
|
303 |
done = 1; |
304 |
|
305 |
break; |
306 |
|
307 |
default: |
308 |
|
309 |
(void)fprintf(stderr, |
310 |
"\n" |
311 |
"Bad option \"-%c\"\n", currentFlag); |
312 |
usage(); |
313 |
} |
314 |
j++; |
315 |
currentFlag = argV[i][j]; |
316 |
} |
317 |
} |
318 |
} |
319 |
|
320 |
else{ |
321 |
|
322 |
|
323 |
// ******************************************************** |
324 |
// for only a single input file, leave this as it is. |
325 |
// ******************************************************** |
326 |
|
327 |
// if( inName != NULL ){ |
328 |
// fprintf( stderr, |
329 |
// "\n" |
330 |
// "Error at \"%s\", program does not currently support\n" |
331 |
// "more than one input file.\n" |
332 |
// "\n", |
333 |
// argV[i]); |
334 |
// usage(); |
335 |
// exit(0); |
336 |
// } |
337 |
|
338 |
// inName = argV[i]; |
339 |
|
340 |
// ************************************************************* |
341 |
|
342 |
|
343 |
// ************************************************************ |
344 |
// To support more than one input file, uncomment the following |
345 |
// section. |
346 |
// ************************************************************ |
347 |
|
348 |
|
349 |
nInputs++; |
350 |
currentName = (linkedName *) malloc( sizeof( linkedName ) ); |
351 |
if( currentName == NULL ){ |
352 |
fprintf( stderr, |
353 |
"\n" |
354 |
"Ran out of memory\n" ); |
355 |
exit(0); |
356 |
} |
357 |
|
358 |
strcpy( currentName->name, argV[i] ); |
359 |
|
360 |
currentName->next = headName; |
361 |
headName = currentName; |
362 |
|
363 |
// ********************************************************** |
364 |
// end multiple input files |
365 |
// ********************************************************** |
366 |
} |
367 |
} |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
// initialize the input file(s) |
373 |
|
374 |
// *********************************************************** |
375 |
// single file: |
376 |
|
377 |
// if(inName == NULL){ |
378 |
|
379 |
// fprintf( stderr, |
380 |
// "\n" |
381 |
// "Error, no input file was given\n"); |
382 |
// usage(); |
383 |
// exit(0); |
384 |
// } |
385 |
|
386 |
// inFile = fopen( inName, "r" ); |
387 |
// if( inFile == NULL ){ |
388 |
|
389 |
// fprintf( stderr, |
390 |
// "\n" |
391 |
// "Error trying to open \"%s\" for reading\n", |
392 |
// inName ); |
393 |
// exit(0); |
394 |
// } |
395 |
|
396 |
// ************************************************************** |
397 |
// multiple files: |
398 |
|
399 |
if( !nInputs ){ |
400 |
|
401 |
fprintf( stderr, |
402 |
"\n" |
403 |
"Error, no input files were given\n"); |
404 |
usage(); |
405 |
exit(0); |
406 |
} |
407 |
|
408 |
// create the input array |
409 |
|
410 |
inputArray = (fileStruct *) calloc( nInputs, sizeof( fileStruct ) ); |
411 |
if( inputArray == NULL ){ |
412 |
fprintf(stderr, |
413 |
"\n" |
414 |
"Ran out of memory\n" ); |
415 |
exit(0); |
416 |
} |
417 |
|
418 |
j = nInputs - 1; |
419 |
currentName = headName; |
420 |
while( currentName != NULL ){ |
421 |
|
422 |
strcpy( inputArray[j].fileName, currentName->name ); |
423 |
inputArray[j].filePtr = NULL; |
424 |
j--; |
425 |
currentName = currentName->next; |
426 |
} |
427 |
|
428 |
// delete linked list |
429 |
|
430 |
while( headName != NULL ){ |
431 |
|
432 |
currentName = headName; |
433 |
headName = currentName->next; |
434 |
|
435 |
free( currentName ); |
436 |
} |
437 |
|
438 |
|
439 |
// open the files for reading |
440 |
|
441 |
for(i=0; i<nInputs; i++){ |
442 |
|
443 |
inputArray[i].filePtr = fopen( inputArray[i].fileName, "r" ); |
444 |
if( inputArray[i].filePtr == NULL ){ |
445 |
|
446 |
fprintf( stderr, |
447 |
"\n" |
448 |
"Error trying to open \"%s\" for reading\n", |
449 |
inputArray[i].fileName ); |
450 |
exit(0); |
451 |
} |
452 |
|
453 |
//read and do stuff here |
454 |
|
455 |
} |
456 |
|
457 |
// ************************************************************* |
458 |
// end file opening |
459 |
// ************************************************************* |
460 |
|
461 |
|
462 |
|
463 |
// or read and do stuff here. |
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
// close files when we are done. |
470 |
|
471 |
// *********************************************************** |
472 |
// single: |
473 |
|
474 |
// fclose( inFile ); |
475 |
|
476 |
// ************************************************************* |
477 |
// multiple: |
478 |
|
479 |
for( i=0; i<nInputs; i++){ |
480 |
|
481 |
fclose( inputArray[i].filePtr ); |
482 |
} |
483 |
|
484 |
free( inputArray ); // free the input Array if we are done; |
485 |
inputArray = NULL; |
486 |
|
487 |
// ************************************************************ |
488 |
// end file closing |
489 |
// ************************************************************ |
490 |
|
491 |
} |
492 |
|
493 |
|
494 |
/*************************************************************************** |
495 |
* prints out the usage for the command line arguments, then exits. |
496 |
***************************************************************************/ |
497 |
|
498 |
void usage(){ |
499 |
(void)fprintf(stdout, |
500 |
"\n" |
501 |
"The proper usage is: %s [options] <input_file>\n" |
502 |
"\n" |
503 |
"Options:\n" |
504 |
"\n" |
505 |
" short:\n" |
506 |
" ------\n" |
507 |
" -h Display this message\n" |
508 |
" -o <name> The output name/prefix\n" |
509 |
" -r the r flag\n" |
510 |
" -i <int> set number to <#>\n" |
511 |
|
512 |
"\n" |
513 |
" long:\n" |
514 |
" -----\n" |
515 |
" --flag1 <arg1> <arg2> does flag 1 for arg1 and 2\n" |
516 |
" --flag2 <double> does flag 2 for double\n" |
517 |
" --flag3 does flag 3\n" |
518 |
|
519 |
"\n" |
520 |
"\n", |
521 |
programName); |
522 |
exit(0); |
523 |
} |