comparison sidinfo.c @ 19:16cfbdf20eaf

Handle multiple files.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Sep 2014 02:49:24 +0300
parents c30fe2b4251f
children 6058339ffe0e
comparison
equal deleted inserted replaced
18:8052423cf151 19:16cfbdf20eaf
68 const int noptPSFlags = sizeof(optPSFlags) / sizeof(optPSFlags[0]); 68 const int noptPSFlags = sizeof(optPSFlags) / sizeof(optPSFlags[0]);
69 69
70 70
71 /* Options 71 /* Options
72 */ 72 */
73 char * optInFilename = NULL;
74 BOOL optParsable = FALSE, 73 BOOL optParsable = FALSE,
75 optNoNamePrefix = FALSE, 74 optNoNamePrefix = FALSE,
76 optHexadecimal = FALSE; 75 optHexadecimal = FALSE;
77 uint32_t optFields = PSF_ALL; 76 uint32_t optFields = PSF_ALL;
77 int optNFiles = 0;
78 78
79 79
80 /* Arguments 80 /* Arguments
81 */ 81 */
82 static optarg_t optList[] = 82 static optarg_t optList[] =
213 optNoNamePrefix = TRUE; 213 optNoNamePrefix = TRUE;
214 break; 214 break;
215 215
216 default: 216 default:
217 THERR("Unknown option '%s'.\n", currArg); 217 THERR("Unknown option '%s'.\n", currArg);
218 return FALSE;
219 }
220
221 return TRUE;
222 }
223
224
225 BOOL argHandleFile(char *currArg)
226 {
227 if (!optInFilename)
228 optInFilename = currArg;
229 else
230 {
231 THERR("Filename already specified on commandline!\n");
232 return FALSE; 218 return FALSE;
233 } 219 }
234 220
235 return TRUE; 221 return TRUE;
236 } 222 }
475 fprintf(outFile, "\n"); 461 fprintf(outFile, "\n");
476 } 462 }
477 } 463 }
478 464
479 465
466 BOOL argHandleFile(char *filename)
467 {
468 static PSIDHeader psid;
469 static FILE *inFile = NULL;
470 optNFiles++;
471
472 if ((inFile = fopen(filename, "rb")) == NULL)
473 {
474 THERR("Could not open file '%s'.\n", filename);
475 return FALSE;
476 }
477
478 // Read PSID data
479 if (siReadPSIDFile(inFile, &psid) != 0)
480 goto error;
481
482 // Output
483 siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, filename, &psid);
484
485 // Shutdown
486 error:
487 if (inFile != NULL)
488 fclose(inFile);
489
490 return TRUE;
491 }
492
493
480 int main(int argc, char *argv[]) 494 int main(int argc, char *argv[])
481 { 495 {
482 FILE *inFile = NULL;
483 int ret = -1;
484 PSIDHeader psid;
485
486 // Initialize 496 // Initialize
487 th_init("SIDInfo", "PSID/RSID information displayer", "0.2", NULL, NULL); 497 th_init("SIDInfo", "PSID/RSID information displayer", "0.2", NULL, NULL);
488 th_verbosityLevel = 0; 498 th_verbosityLevel = 0;
489 499
490 // Parse command line arguments 500 // Parse command line arguments
491 if (!th_args_process(argc, argv, optList, optListN, 501 if (!th_args_process(argc, argv, optList, optListN,
492 argHandleOpt, argHandleFile, FALSE)) 502 argHandleOpt, argHandleFile, FALSE))
493 return -1; 503 return -1;
494 504
495 if (optInFilename == NULL) 505 if (optNFiles == 0)
496 { 506 {
497 argShowHelp(); 507 argShowHelp();
498 THERR("No filename specified.\n"); 508 THERR("No filename(s) specified.\n");
499 goto error; 509 }
500 } 510
501 511 return 0;
502 // Try to open the file 512 }
503 if ((inFile = fopen(optInFilename, "rb")) == NULL)
504 {
505 THERR("Could not open file '%s'.\n", optInFilename);
506 goto error;
507 }
508
509 // Read PSID data
510 if ((ret = siReadPSIDFile(inFile, &psid)) != 0)
511 goto error;
512
513 // Output
514 siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, optInFilename, &psid);
515
516 ret = 0;
517
518 // Shutdown
519 error:
520 if (inFile != NULL)
521 fclose(inFile);
522
523 return ret;
524 }