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 |
|
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 |
strcpy( flag1Arg1, argV[i] ); |
135 |
i++; |
136 |
strcpy( flag1Arg2, argV[i] ); |
137 |
} |
138 |
|
139 |
else if( !strcmp( argV[i], "--flag2" ) ){ |
140 |
flag2 = 1; // set the flag2 to true; |
141 |
|
142 |
i++; |
143 |
flag2Val = strtod( argV[i], &conversionCheck ); |
144 |
if( conversionCheck == argV[i] ) conversionError = 1; |
145 |
if( *conversionCheck != '\0' ) conversionError = 1; |
146 |
|
147 |
if( conversionError ){ |
148 |
|
149 |
fprintf( stderr, |
150 |
"Error converting \"%s\" to a double\n", argV[i] ); |
151 |
usage(); |
152 |
exit(0); |
153 |
} |
154 |
|
155 |
} |
156 |
|
157 |
else if( !strcmp( argV[i], "--flag3") ){ |
158 |
flag3 = 1; // set flag3 to be true |
159 |
|
160 |
} |
161 |
|
162 |
// anything else is an error |
163 |
|
164 |
else{ |
165 |
fprintf( stderr, |
166 |
"Invalid option \"%s\"\n", argV[i] ); |
167 |
usage(); |
168 |
exit(0); |
169 |
} |
170 |
} |
171 |
|
172 |
else{ |
173 |
|
174 |
// parse single character options |
175 |
|
176 |
done =0; |
177 |
j = 1; |
178 |
currentFlag = argv[i][j]; |
179 |
while( (current_flag != '\0') && (!done) ){ |
180 |
|
181 |
switch(current_flag){ |
182 |
|
183 |
case 'h': |
184 |
// -h => give the usage help message |
185 |
|
186 |
usage(); |
187 |
exit(0); |
188 |
break; |
189 |
|
190 |
case 'o': |
191 |
// -o <prefix> => the output prefix. |
192 |
|
193 |
j++; |
194 |
currentFlag = argV[i][j]; |
195 |
|
196 |
if( currentFlag != '\0' ) optionError = 1; |
197 |
|
198 |
if( optionError ){ |
199 |
fprintf( stderr, |
200 |
"\n" |
201 |
"The -o flag should end an option sequence.\n" |
202 |
" example: -ro <outname> *NOT* -or <outname>\n" ); |
203 |
usage(); |
204 |
exit(0); |
205 |
} |
206 |
|
207 |
i++; |
208 |
outPrefix = argV[i]; |
209 |
if( outPrefix[0] == '-' ) optionError = 1; |
210 |
|
211 |
if( optionError ){ |
212 |
fprintf( stderr, |
213 |
"\n" |
214 |
"\"%s\" is not a valid out prefix/name.\n" |
215 |
"Out prefix/name should not begin with a dash.\n", |
216 |
outPrefix ); |
217 |
usage(); |
218 |
exit(0); |
219 |
} |
220 |
|
221 |
havePrefix = 1; |
222 |
done = 1; |
223 |
break; |
224 |
|
225 |
|
226 |
|
227 |
case 'r': |
228 |
// the r flag |
229 |
|
230 |
rFlag = 1; // set rflag to true |
231 |
break; |
232 |
|
233 |
case 'i': |
234 |
// -i <int> set <int> to the iVal |
235 |
|
236 |
iFlag = 1; // set iFlag to true |
237 |
j++; |
238 |
currentFlag = argV[i][j]; |
239 |
|
240 |
if( currentFlag != '\0' ) optionError = 1; |
241 |
|
242 |
if( optionError ){ |
243 |
fprintf( stderr, |
244 |
"\n" |
245 |
"The -i flag should end an option sequence.\n" |
246 |
" example: -ri <int> *NOT* -ir <int>\n" ); |
247 |
usage(); |
248 |
exit(0); |
249 |
} |
250 |
|
251 |
i++; |
252 |
iVal = (int)strtol( argV[i], &conversionCheck, 10 ); |
253 |
if( conversionCheck == argV[i] ) conversionError = 1; |
254 |
if( *conversionCheck != '\0' ) conversionError = 1; |
255 |
|
256 |
if( conversionError ){ |
257 |
|
258 |
fprintf( stderr, |
259 |
"Error converting \"%s\" to a int\n", argV[i] ); |
260 |
usage(); |
261 |
exit(0); |
262 |
} |
263 |
|
264 |
done = 1; |
265 |
|
266 |
break; |
267 |
|
268 |
default: |
269 |
|
270 |
(void)fprintf(stderr, |
271 |
"\n" |
272 |
"Bad option \"-%c\"\n", current_flag); |
273 |
usage(); |
274 |
} |
275 |
j++; |
276 |
current_flag = argv[i][j]; |
277 |
} |
278 |
} |
279 |
} |
280 |
|
281 |
else{ |
282 |
|
283 |
|
284 |
// ******************************************************** |
285 |
// for only a single input file, leave this as it is. |
286 |
// ******************************************************** |
287 |
|
288 |
if( inName != NULL ){ |
289 |
fprintf( stderr, |
290 |
"\n" |
291 |
"Error at \"%s\", program does not currently support\n" |
292 |
"more than one input file.\n" |
293 |
"\n", |
294 |
argV[i]); |
295 |
usage(); |
296 |
exit(0); |
297 |
} |
298 |
|
299 |
inName = argvV[i]; |
300 |
|
301 |
// ************************************************************* |
302 |
|
303 |
|
304 |
// ************************************************************ |
305 |
// To support more than one input file, uncomment the following |
306 |
// section. |
307 |
// ************************************************************ |
308 |
|
309 |
|
310 |
nInputs++; |
311 |
currentName = (linkedName *) malloc( sizeof( linkedName ) ); |
312 |
if( currentName == NULL ){ |
313 |
fprintf( stderr, |
314 |
"\n" |
315 |
"Ran out of memory\n" ); |
316 |
exit(0); |
317 |
} |
318 |
|
319 |
strcpy( currentName->name, argV[i] ); |
320 |
|
321 |
currentName->next = headName; |
322 |
headName = currentName; |
323 |
|
324 |
// ********************************************************** |
325 |
// end multiple input files |
326 |
// ********************************************************** |
327 |
} |
328 |
} |
329 |
|
330 |
|
331 |
|
332 |
|
333 |
// initialize the input file(s) |
334 |
|
335 |
// *********************************************************** |
336 |
// single file: |
337 |
|
338 |
if(inName == NULL){ |
339 |
|
340 |
fprintf( stderr, |
341 |
"\n" |
342 |
"Error, no input file was given\n"); |
343 |
usage(); |
344 |
exit(0); |
345 |
} |
346 |
|
347 |
inFile = fopen( inName, "r" ); |
348 |
if( inFile == NULL ){ |
349 |
|
350 |
fprintf( stderr, |
351 |
"\n" |
352 |
"Error trying to open \"%s\" for reading\n", |
353 |
inName ); |
354 |
exit(0); |
355 |
} |
356 |
|
357 |
// ************************************************************** |
358 |
// multiple files: |
359 |
|
360 |
if( !nInputs ){ |
361 |
|
362 |
fprintf( stderr, |
363 |
"\n" |
364 |
"Error, no input files were given\n"); |
365 |
usage(); |
366 |
exit(0); |
367 |
} |
368 |
|
369 |
// create the input array |
370 |
|
371 |
inputArray = (fileStruct *) calloc( nInputs, sizeof( fileStruct ) ); |
372 |
if( inputArray == NULL ){ |
373 |
fprintf(stderr, |
374 |
"\n" |
375 |
"Ran out of memory\n" ); |
376 |
exit(0); |
377 |
} |
378 |
|
379 |
j = nInputs - 1; |
380 |
currentName = headName; |
381 |
while( currentName != NULL ){ |
382 |
|
383 |
strcopy( inputArray[j].fileName, currentName->name ); |
384 |
inputArray[i].filePtr = NULL; |
385 |
j--; |
386 |
currentName = currentName->next; |
387 |
} |
388 |
|
389 |
// delete linked list |
390 |
|
391 |
while( headName != NULL ){ |
392 |
|
393 |
currentName = headName; |
394 |
headName = currentName->next; |
395 |
|
396 |
free( currentName ); |
397 |
} |
398 |
|
399 |
|
400 |
// open the files for reading |
401 |
|
402 |
for(i=0; i<nInputs; i++){ |
403 |
|
404 |
inputArray[i].filePtr = fopen( inName, "r" ); |
405 |
if( inputArray[i].filePtr == NULL ){ |
406 |
|
407 |
fprintf( stderr, |
408 |
"\n" |
409 |
"Error trying to open \"%s\" for reading\n", |
410 |
inputArray[i].fileName ); |
411 |
exit(0); |
412 |
} |
413 |
|
414 |
//read and do stuff here |
415 |
|
416 |
} |
417 |
|
418 |
// ************************************************************* |
419 |
// end file opening |
420 |
// ************************************************************* |
421 |
|
422 |
|
423 |
|
424 |
// or read files here |
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
// close files when we are done. |
431 |
|
432 |
// *********************************************************** |
433 |
// single: |
434 |
|
435 |
fclose( inFile ); |
436 |
|
437 |
// ************************************************************* |
438 |
// multiple: |
439 |
|
440 |
for( i=0; i<nInputs; i++){ |
441 |
|
442 |
fclose( inputArray[i].filePtr ); |
443 |
} |
444 |
|
445 |
free( inputArray ); // free the input Array if we are done; |
446 |
inputArray = NULL; |
447 |
|
448 |
// ************************************************************ |
449 |
// end file closing |
450 |
// ************************************************************ |
451 |
|
452 |
} |
453 |
|
454 |
|
455 |
/*************************************************************************** |
456 |
* prints out the usage for the command line arguments, then exits. |
457 |
***************************************************************************/ |
458 |
|
459 |
void usage(){ |
460 |
(void)fprintf(stdout, |
461 |
"The proper usage is: %s [options] <input_file>\n" |
462 |
"\n" |
463 |
"Options:\n" |
464 |
"\n" |
465 |
" short:\n" |
466 |
" ------\n" |
467 |
" -h Display this message\n" |
468 |
" -o <name> The output name/prefix\n" |
469 |
" -r the r flag\n" |
470 |
" -i <int> set number to <#>\n" |
471 |
|
472 |
"\n" |
473 |
" long:\n" |
474 |
" -----\n" |
475 |
" --flag1 <arg1> <arg2> does flag 1 for arg1 and 2\n" |
476 |
" --flag2 <double> does flag 2 for double\n" |
477 |
" --flag3 does flag 3\n" |
478 |
|
479 |
"\n" |
480 |
"\n", |
481 |
program_name) |
482 |
exit(0); |
483 |
} |