changeset 19:16cfbdf20eaf

Handle multiple files.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Sep 2014 02:49:24 +0300
parents 8052423cf151
children 6058339ffe0e
files sidinfo.c
diffstat 1 files changed, 32 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Thu Sep 25 02:35:31 2014 +0300
+++ b/sidinfo.c	Thu Sep 25 02:49:24 2014 +0300
@@ -70,11 +70,11 @@
 
 /* Options
  */
-char *	optInFilename = NULL;
 BOOL	optParsable = FALSE,
         optNoNamePrefix = FALSE,
         optHexadecimal = FALSE;
 uint32_t optFields = PSF_ALL;
+int     optNFiles = 0;
 
 
 /* Arguments
@@ -222,20 +222,6 @@
 }
 
 
-BOOL argHandleFile(char *currArg)
-{
-    if (!optInFilename)
-        optInFilename = currArg;
-    else
-    {
-        THERR("Filename already specified on commandline!\n");
-        return FALSE;
-    }
-
-    return TRUE;
-}
-
-
 typedef struct
 {
     char magic[PSID_MAGIC_LEN + 1]; // "PSID" / "RSID" magic identifier
@@ -477,12 +463,36 @@
 }
 
 
+BOOL argHandleFile(char *filename)
+{
+    static PSIDHeader psid;
+    static FILE *inFile = NULL;
+    optNFiles++;
+
+    if ((inFile = fopen(filename, "rb")) == NULL)
+    {
+        THERR("Could not open file '%s'.\n", filename);
+        return FALSE;
+    }
+
+    // Read PSID data
+    if (siReadPSIDFile(inFile, &psid) != 0)
+        goto error;
+
+    // Output
+    siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, filename, &psid);
+
+    // Shutdown
+error:
+    if (inFile != NULL)
+        fclose(inFile);
+
+    return TRUE;
+}
+
+
 int main(int argc, char *argv[])
 {
-    FILE *inFile = NULL;
-    int ret = -1;
-    PSIDHeader psid;
-
     // Initialize
     th_init("SIDInfo", "PSID/RSID information displayer", "0.2", NULL, NULL);
     th_verbosityLevel = 0;
@@ -492,33 +502,11 @@
                          argHandleOpt, argHandleFile, FALSE))
         return -1;
 
-    if (optInFilename == NULL)
+    if (optNFiles == 0)
     {
         argShowHelp();
-        THERR("No filename specified.\n");
-        goto error;
-    }
-
-    // Try to open the file
-    if ((inFile = fopen(optInFilename, "rb")) == NULL)
-    {
-        THERR("Could not open file '%s'.\n", optInFilename);
-        goto error;
+        THERR("No filename(s) specified.\n");
     }
 
-    // Read PSID data
-    if ((ret = siReadPSIDFile(inFile, &psid)) != 0)
-        goto error;
-
-    // Output
-    siPrintPSIDInformation(stdout, optParsable, optHexadecimal, optFields, optInFilename, &psid);
-
-    ret = 0;
-
-    // Shutdown
-error:
-    if (inFile != NULL)
-        fclose(inFile);
-
-    return ret;
+    return 0;
 }