added configure script
[drnoksnes] / loadzip.cpp
index 4794a78..da08d05 100644 (file)
 /* This file contains a function for loading a SNES ROM image from a zip file                */
 /**********************************************************************************************/
 
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <assert.h>
+
+#include "snes9x.h"
 
 #ifndef NO_INLINE_SET_GET
 #define NO_INLINE_SET_GET
 #endif
 
-#include "snes9x.h"
 #include "memmap.h"
-
 #include "unzip.h"
-//#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
 
 bool8 LoadZip(const char* zipname,
              int32 *TotalFileSize,
@@ -71,7 +71,7 @@ bool8 LoadZip(const char* zipname,
     
     unzFile file = unzOpen(zipname);
     if(file == NULL)
-       return (FALSE);
+               return FALSE;
 
     // find largest file in zip file (under MAX_ROM_SIZE)
     // or a file with extension .1
@@ -79,142 +79,143 @@ bool8 LoadZip(const char* zipname,
     int filesize = 0;
     int port = unzGoToFirstFile(file);
     unz_file_info info;
-    while(port == UNZ_OK)
+    while (port == UNZ_OK)
     {
-       char name[132];
-       unzGetCurrentFileInfo(file, &info, name,128, NULL,0, NULL,0);
+               char name[132];
+               unzGetCurrentFileInfo(file, &info, name,128, NULL,0, NULL,0);
 
 #if 0
-       int calc_size = info.uncompressed_size / 0x2000;
-       calc_size *= 0x2000;
-       if(!(info.uncompressed_size - calc_size == 512 || info.uncompressed_size == calc_size))
-       {
-           port = unzGoToNextFile(file);
-           continue;
-       }
+               int calc_size = info.uncompressed_size / 0x2000;
+               calc_size *= 0x2000;
+               if(!(info.uncompressed_size - calc_size == 512 || info.uncompressed_size == calc_size))
+               {
+                       port = unzGoToNextFile(file);
+                       continue;
+               }
 #endif
 
-       if(info.uncompressed_size > (CMemory::MAX_ROM_SIZE + 512))
-       {
-           port = unzGoToNextFile(file);
-           continue;
-       }
-       
-       if ((int) info.uncompressed_size > filesize)
-       {
-           strcpy(filename,name);
-           filesize = info.uncompressed_size;
-       }
-       int len = strlen(name);
-       if(name[len-2] == '.' && name[len-1] == '1')
+               if(info.uncompressed_size > (CMemory::MAX_ROM_SIZE + 512))
+               {
+                       port = unzGoToNextFile(file);
+                       continue;
+               }
+
+               if ((int) info.uncompressed_size > filesize)
+               {
+                       strcpy(filename,name);
+                       filesize = info.uncompressed_size;
+               }
+
+               int len = strlen(name);
+               if(name[len-2] == '.' && name[len-1] == '1')
+               {
+                       strcpy(filename,name);
+                       filesize = info.uncompressed_size;
+                       break;
+               }
+
+               port = unzGoToNextFile(file);
+    }
+       if( !(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0)
        {
-           strcpy(filename,name);
-           filesize = info.uncompressed_size;
-           break;
+               unzClose(file);
+               return FALSE;
        }
-       port = unzGoToNextFile(file);
-    }
-    if( !(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0)
-    {
-       unzClose(file);
-       return (FALSE);
-    }
 
-    // Find extension
-    char tmp[2];
-    tmp[0] = tmp[1] = 0;
-    char *ext = strrchr(filename,'.');
-    if(ext) ext++;
-    else ext = tmp;
-    
+       // Find extension
+       char tmp[2] = { '\0', '\0' };
+       char *ext = strrchr(filename, '.');
+       if (ext) ext++;
+       else ext = tmp;
+
     uint8 *ptr = Memory.ROM;
     bool8 more = FALSE;
 
     unzLocateFile(file,filename,1);
     unzGetCurrentFileInfo(file, &info, filename,128, NULL,0, NULL,0);
-    
+
     if( unzOpenCurrentFile(file) != UNZ_OK )
     {
-       unzClose(file);
-       return (FALSE);
+               unzClose(file);
+               return FALSE;
     }
 
     do
     {
-//     assert(info.uncompressed_size <= CMemory::MAX_ROM_SIZE + 512);
-       int FileSize = info.uncompressed_size;
-       
-       int calc_size = FileSize / 0x2000;
-       calc_size *= 0x2000;
-       
-       int l = unzReadCurrentFile(file,ptr,FileSize);
-       if(unzCloseCurrentFile(file) == UNZ_CRCERROR)
-       {
-               unzClose(file);
-           return (FALSE);
-       }
-       
-       if(l <= 0 || l != FileSize)
-       {
-           unzClose(file);
-           switch(l)
-           {
-               case UNZ_ERRNO:
-                   break;
-               case UNZ_EOF:
-                   break;
-               case UNZ_PARAMERROR:
-                   break;
-               case UNZ_BADZIPFILE:
-                   break;
-               case UNZ_INTERNALERROR:
-                   break;
-               case UNZ_CRCERROR:
-                   break;
-           }
-           return (FALSE);
-       }
+               int FileSize = info.uncompressed_size;
+               assert(FileSize <= CMemory::MAX_ROM_SIZE + 512);
 
-       if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
-           Settings.ForceHeader)
-       {
-           memmove (ptr, ptr + 512, calc_size);
-           (*headers)++;
-           FileSize -= 512;
-       }
-       ptr += FileSize;
-       (*TotalFileSize) += FileSize;
+               int calc_size = FileSize / 0x2000;
+               calc_size *= 0x2000;
 
-       int len;
-       if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
-           (isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
-       {
-           more = TRUE;
-           ext [0]++;
-       }
-       else if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
-                (((len = strlen (filename)) == 7 || len == 8) &&
-                 strncasecmp (filename, "sf", 2) == 0 &&
-                 isdigit (filename [2]) && isdigit (filename [3]) && isdigit (filename [4]) &&
-                 isdigit (filename [5]) && isalpha (filename [len - 1])))
-       {
-           more = TRUE;
-           filename [len - 1]++;
-       }
-       else
-           more = FALSE;
+               int l = unzReadCurrentFile(file,ptr,FileSize);
+               if(unzCloseCurrentFile(file) == UNZ_CRCERROR)
+               {
+                       unzClose(file);
+                       return FALSE;
+               }
+
+               if (l <= 0 || l != FileSize)
+               {
+                       unzClose(file);
+                       switch(l)
+                       {
+                       case UNZ_ERRNO:
+                               break;
+                       case UNZ_EOF:
+                               break;
+                       case UNZ_PARAMERROR:
+                               break;
+                       case UNZ_BADZIPFILE:
+                               break;
+                       case UNZ_INTERNALERROR:
+                               break;
+                       case UNZ_CRCERROR:
+                               break;
+                       }
+                       return FALSE;
+               }
+
+               if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
+                       Settings.ForceHeader)
+               {
+                       memmove (ptr, ptr + 512, calc_size);
+                       (*headers)++;
+                       FileSize -= 512;
+               }
+               ptr += FileSize;
+               (*TotalFileSize) += FileSize;
+
+               int len;
+               if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
+                       (isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
+               {
+                       more = TRUE;
+                       ext [0]++;
+               }
+               else if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
+                        (((len = strlen (filename)) == 7 || len == 8) &&
+                         strncasecmp (filename, "sf", 2) == 0 &&
+                         isdigit (filename [2]) && isdigit (filename [3]) && isdigit (filename [4]) &&
+                         isdigit (filename [5]) && isalpha (filename [len - 1])))
+               {
+                       more = TRUE;
+                       filename [len - 1]++;
+               }
+               else
+                       more = FALSE;
        
-       if(more)
-       {
-           if( unzLocateFile(file,filename,1) != UNZ_OK ||
-               unzGetCurrentFileInfo(file, &info, filename,128, NULL,0, NULL,0) != UNZ_OK ||
-               unzOpenCurrentFile(file) != UNZ_OK)
-               break;
-       }
+               if(more)
+               {
+                       if (unzLocateFile(file,filename,1) != UNZ_OK ||
+                               unzGetCurrentFileInfo(file, &info, filename,128, NULL,0, NULL,0) != UNZ_OK ||
+                               unzOpenCurrentFile(file) != UNZ_OK)
+                                       break;
+               }
        
     } while(more);
     
     unzClose(file);
-    return (TRUE);
+       return TRUE;
 }
 #endif