]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/file.cpp
Added logfile logging, ZIP file fishing
[virtualjaguar] / src / file.cpp
index 4fc852c8c430f309db0c2d64e716a63e830d1973..8c1a7038842a56e2cfa34b707fe075b76018ec34 100644 (file)
@@ -3,6 +3,14 @@
 //
 // File support
 // by James L. Hammons
+// (C) 2010 Underground Software
+//
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -------------------------------------------------------------
+// JLH  01/16/2010  Created this log ;-)
+// JLH  02/28/2010  Added functions to look inside .ZIP files and handle contents
 //
 
 #include "file.h"
 #include "eeprom.h"
 #include "jaguar.h"
 #include "log.h"
+#include "memory.h"
 #include "unzip.h"
 #include "zlib.h"
 
 // Private function prototypes
 
 static int gzfilelength(gzFile gd);
+static bool CheckExtension(const char * filename, const char * ext);
 
 //
 // Generic ROM loading
@@ -27,7 +37,7 @@ uint32 JaguarLoadROM(uint8 * rom, char * path)
 {
 // We really should have some kind of sanity checking for the ROM size here to prevent
 // a buffer overflow... !!! FIX !!!
-#warning !!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!
+#warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
        uint32 romSize = 0;
 
 WriteLog("JaguarLoadROM: Attempting to load file '%s'...", path);
@@ -279,7 +289,7 @@ Start of Data Segment = 0x00803dd0
                }
                else
                {
-                       WriteLog("GUI: Couldn't find correct ABS format: %02X %02X\n", jaguarMainROM[0], jaguarMainROM[1]);
+                       WriteLog("GUI: Couldn't find correct ABS/COF format: %02X %02X\n", jaguarMainROM[0], jaguarMainROM[1]);
                        return false;
                }
        }
@@ -337,3 +347,80 @@ static int gzfilelength(gzFile gd)
    gzrewind(gd);
    return length;
 }
+
+//
+// Compare extension to passed in filename. If equal, return true; otherwise false.
+//
+static bool CheckExtension(const char * filename, const char * ext)
+{
+       const char * filenameExt = strrchr(filename, '.');      // Get the file's extension (if any)
+       return (strcasecmp(filenameExt, ext) == 0 ? true : false);
+}
+
+//
+// Get file from .ZIP
+// Returns the size of the file inside the .ZIP file that we're looking at
+//
+uint32 GetFileFromZIP(const char * zipFile, FileType type, uint8 * &buffer)
+{
+#warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
+       const char ftStrings[5][32] = { "Software", "EEPROM", "Label", "Box Art", "Controller Overlay" };
+       ZIP * zip = openzip(0, 0, zipFile);
+
+       if (zip == NULL)
+       {
+               WriteLog("FILE: Could not open file '%s'!\n", zipFile);
+               return 0;
+       }
+
+       while (readzip(zip))
+       {
+               zipent * ze = &zip->ent;
+               bool found = false;
+
+               if ((type == FT_LABEL) && (CheckExtension(ze->name, ".png") || CheckExtension(ze->name, ".jpg") || CheckExtension(ze->name, ".gif")))
+               {
+                       found = true;
+                       WriteLog("FILE: Found image file '%s'.\n", ze->name);
+               }
+
+               if ((type == FT_SOFTWARE) && (CheckExtension(ze->name, ".j64") || CheckExtension(ze->name, ".rom") || CheckExtension(ze->name, ".abs") || CheckExtension(ze->name, ".cof")))
+               {
+                       found = true;
+                       WriteLog("FILE: Found software file '%s'.\n", ze->name);
+               }
+
+               if ((type == FT_EEPROM) && (CheckExtension(ze->name, ".eep") || CheckExtension(ze->name, ".eeprom")))
+               {
+                       found = true;
+                       WriteLog("FILE: Found EEPROM file '%s'.\n", ze->name);
+               }
+
+               if (found)
+               {
+                       WriteLog("FILE: Uncompressing...");
+                       buffer = new uint8[ze->uncompressed_size];
+
+                       if (readuncompresszip(zip, ze, (char *)buffer) == 0)
+                       {
+                               WriteLog("success! (%u bytes)\n", ze->uncompressed_size);
+                               return ze->uncompressed_size;
+                       }
+                       else
+                       {
+                               WriteLog("FAILED!\n");
+                               delete[] buffer;
+                               return 0;
+                       }
+               }
+       }
+
+       closezip(zip);
+
+       WriteLog("FILE: Failed to find file of type %s...\n", ftStrings[type]);
+       // Didn't find what we're looking for...
+       return 0;
+}
+
+//ParseFileType, etc.
+