Cleaned up all the readAll() cut and paste.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 09 Mar 2012 04:50:27 -0500
changeset 1203 55f147714ce2
parent 1202 2d6647b47a05
child 1204 0906a37b4347
Cleaned up all the readAll() cut and paste.
docs/TODO.txt
src/archiver_grp.c
src/archiver_hog.c
src/archiver_mvl.c
src/archiver_qpak.c
src/archiver_wad.c
src/archiver_zip.c
src/physfs.c
src/physfs_internal.h
--- a/docs/TODO.txt	Fri Mar 09 03:55:23 2012 -0500
+++ b/docs/TODO.txt	Fri Mar 09 04:50:27 2012 -0500
@@ -82,7 +82,6 @@
 - Doxygen replacement? (manpages suck.)
 - Fix coding standards to match.
 - See if we can ditch some #include lines...
-- push readAll() to somewhere common.
 - We lost Vista symlink support when removing isSymLink(). Pull it back from
   revision control.
 - PHYSFS_exists() fails if you mountIo with a NULL filename. We need to decide
--- a/src/archiver_grp.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_grp.c	Fri Mar 09 04:50:27 2012 -0500
@@ -29,11 +29,6 @@
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
 
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
 static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
 {
     PHYSFS_uint32 location = 16;  /* sizeof sig. */
@@ -48,8 +43,8 @@
 
     for (entry = entries; fileCount > 0; fileCount--, entry++)
     {
-        GOTO_IF_MACRO(!readAll(io, &entry->name, 12), NULL, grpLoad_failed);
-        GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, grpLoad_failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), NULL, failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
         entry->name[12] = '\0';  /* name isn't null-terminated in file. */
         if ((ptr = strchr(entry->name, ' ')) != NULL)
             *ptr = '\0';  /* trim extra spaces. */
@@ -61,7 +56,7 @@
 
     return entries;
 
-grpLoad_failed:
+failed:
     allocator.Free(entries);
     return NULL;
 } /* grpLoadEntries */
@@ -70,23 +65,23 @@
 static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
 {
     PHYSFS_uint8 buf[12];
-    PHYSFS_uint32 entryCount = 0;
+    PHYSFS_uint32 count = 0;
     UNPKentry *entries = NULL;
 
     assert(io != NULL);  /* shouldn't ever happen. */
 
     BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
 
-    BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL, NULL);
     if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
         BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
 
-    BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
-    entryCount = PHYSFS_swapULE32(entryCount);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
+    count = PHYSFS_swapULE32(count);
 
-    entries = grpLoadEntries(io, entryCount);
+    entries = grpLoadEntries(io, count);
     BAIL_IF_MACRO(entries == NULL, NULL, NULL);
-    return UNPK_openArchive(io, entries, entryCount);
+    return UNPK_openArchive(io, entries, count);
 } /* GRP_openArchive */
 
 
--- a/src/archiver_hog.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_hog.c	Fri Mar 09 04:50:27 2012 -0500
@@ -34,11 +34,6 @@
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
 
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
 static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
 {
     const PHYSFS_uint64 iolen = io->length(io);
@@ -53,13 +48,13 @@
     {
         entCount++;
         ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
-        GOTO_IF_MACRO(ptr == NULL, ERR_OUT_OF_MEMORY, hogLoad_failed);
+        GOTO_IF_MACRO(ptr == NULL, ERR_OUT_OF_MEMORY, failed);
         entries = (UNPKentry *) ptr;
         entry = &entries[entCount-1];
 
-        GOTO_IF_MACRO(!readAll(io, &entry->name, 13), NULL, hogLoad_failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), NULL, failed);
         pos += 13;
-        GOTO_IF_MACRO(!readAll(io, &size, 4), NULL, hogLoad_failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &size, 4), NULL, failed);
         pos += 4;
 
         entry->size = PHYSFS_swapULE32(size);
@@ -67,13 +62,13 @@
         pos += size;
 
         /* skip over entry */
-        GOTO_IF_MACRO(!io->seek(io, pos), NULL, hogLoad_failed);
+        GOTO_IF_MACRO(!io->seek(io, pos), NULL, failed);
     } /* while */
 
     *_entCount = entCount;
     return entries;
 
-hogLoad_failed:
+failed:
     allocator.Free(entries);
     return NULL;
 } /* hogLoadEntries */
@@ -82,17 +77,17 @@
 static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
 {
     PHYSFS_uint8 buf[3];
-    PHYSFS_uint32 entryCount = 0;
+    PHYSFS_uint32 count = 0;
     UNPKentry *entries = NULL;
 
     assert(io != NULL);  /* shouldn't ever happen. */
     BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
-    BAIL_IF_MACRO(!readAll(io, buf, 3), NULL, 0);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 3), NULL, 0);
     BAIL_IF_MACRO(memcmp(buf, "DHF", 3) != 0, ERR_NOT_AN_ARCHIVE, NULL);
 
-    entries = hogLoadEntries(io, &entryCount);
+    entries = hogLoadEntries(io, &count);
     BAIL_IF_MACRO(entries == NULL, NULL, NULL);
-    return UNPK_openArchive(io, entries, entryCount);
+    return UNPK_openArchive(io, entries, count);
 } /* HOG_openArchive */
 
 
--- a/src/archiver_mvl.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_mvl.c	Fri Mar 09 04:50:27 2012 -0500
@@ -32,13 +32,6 @@
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
 
-
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
-
 static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
 {
     PHYSFS_uint32 location = 8;  /* sizeof sig. */
@@ -52,8 +45,8 @@
 
     for (entry = entries; fileCount > 0; fileCount--, entry++)
     {
-        GOTO_IF_MACRO(!readAll(io, &entry->name, 13), NULL, mvlLoad_failed);
-        GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, mvlLoad_failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), NULL, failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
         entry->size = PHYSFS_swapULE32(entry->size);
         entry->startPos = location;
         location += entry->size;
@@ -61,7 +54,7 @@
 
     return entries;
 
-mvlLoad_failed:
+failed:
     allocator.Free(entries);
     return NULL;
 } /* mvlLoadEntries */
@@ -70,19 +63,19 @@
 static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
 {
     PHYSFS_uint8 buf[4];
-    PHYSFS_uint32 entryCount = 0;
+    PHYSFS_uint32 count = 0;
     UNPKentry *entries = NULL;
 
     assert(io != NULL);  /* shouldn't ever happen. */
     BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
-    BAIL_IF_MACRO(!readAll(io, buf, 4), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 4), NULL, NULL);
     BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, ERR_NOT_AN_ARCHIVE, NULL);
-    BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
 
-    entryCount = PHYSFS_swapULE32(entryCount);
-    entries = mvlLoadEntries(io, entryCount);
+    count = PHYSFS_swapULE32(count);
+    entries = mvlLoadEntries(io, count);
     BAIL_IF_MACRO(entries == NULL, NULL, NULL);
-    return UNPK_openArchive(io, entries, entryCount);
+    return UNPK_openArchive(io, entries, count);
 } /* MVL_openArchive */
 
 
--- a/src/archiver_qpak.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_qpak.c	Fri Mar 09 04:50:27 2012 -0500
@@ -69,12 +69,6 @@
 #define QPAK_SIG 0x4b434150   /* "PACK" in ASCII. */
 
 
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
-
 static void QPAK_dirClose(dvoid *opaque)
 {
     QPAKinfo *info = ((QPAKinfo *) opaque);
@@ -225,9 +219,9 @@
 
     for (entry = info->entries; fileCount > 0; fileCount--, entry++)
     {
-        BAIL_IF_MACRO(!readAll(io, &entry->name, 56), NULL, 0);
-        BAIL_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, 0);
-        BAIL_IF_MACRO(!readAll(io, &entry->size, 4), NULL, 0);
+        BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 56), NULL, 0);
+        BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), NULL, 0);
+        BAIL_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, 0);
         entry->size = PHYSFS_swapULE32(entry->size);
         entry->startPos = PHYSFS_swapULE32(entry->startPos);
     } /* for */
@@ -248,13 +242,13 @@
 
     BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
 
-    BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
     BAIL_IF_MACRO(PHYSFS_swapULE32(val) != QPAK_SIG, ERR_NOT_AN_ARCHIVE, NULL);
 
-    BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
     pos = PHYSFS_swapULE32(val);  /* directory table offset. */
 
-    BAIL_IF_MACRO(!readAll(io, &val, 4), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), NULL, NULL);
     count = PHYSFS_swapULE32(val);
 
     /* corrupted archive? */
--- a/src/archiver_wad.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_wad.c	Fri Mar 09 04:50:27 2012 -0500
@@ -47,19 +47,13 @@
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
 
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
-
 static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
 {
     PHYSFS_uint32 directoryOffset;
     UNPKentry *entries = NULL;
     UNPKentry *entry = NULL;
 
-    BAIL_IF_MACRO(!readAll(io, &directoryOffset, 4), NULL, 0);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &directoryOffset, 4), NULL, 0);
     directoryOffset = PHYSFS_swapULE32(directoryOffset);
 
     BAIL_IF_MACRO(!io->seek(io, directoryOffset), NULL, 0);
@@ -69,9 +63,9 @@
 
     for (entry = entries; fileCount > 0; fileCount--, entry++)
     {
-        GOTO_IF_MACRO(!readAll(io, &entry->startPos, 4), NULL, wadLoad_failed);
-        GOTO_IF_MACRO(!readAll(io, &entry->size, 4), NULL, wadLoad_failed);
-        GOTO_IF_MACRO(!readAll(io, &entry->name, 8), NULL, wadLoad_failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->startPos, 4), NULL, failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), NULL, failed);
+        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 8), NULL, failed);
 
         entry->name[8] = '\0'; /* name might not be null-terminated in file. */
         entry->size = PHYSFS_swapULE32(entry->size);
@@ -80,7 +74,7 @@
 
     return entries;
 
-wadLoad_failed:
+failed:
     allocator.Free(entries);
     return NULL;
 } /* wadLoadEntries */
@@ -90,21 +84,21 @@
 {
     PHYSFS_uint8 buf[4];
     UNPKentry *entries = NULL;
-    PHYSFS_uint32 entryCount = 0;
+    PHYSFS_uint32 count = 0;
 
     assert(io != NULL);  /* shouldn't ever happen. */
 
     BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
-    BAIL_IF_MACRO(!readAll(io, buf, sizeof (buf)), NULL, NULL);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL, NULL);
     if ((memcmp(buf, "IWAD", 4) != 0) && (memcmp(buf, "PWAD", 4) != 0))
         BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
 
-    BAIL_IF_MACRO(!readAll(io, &entryCount, sizeof (entryCount)), NULL, NULL);
-    entryCount = PHYSFS_swapULE32(entryCount);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof (count)), NULL, NULL);
+    count = PHYSFS_swapULE32(count);
 
-    entries = wadLoadEntries(io, entryCount);
+    entries = wadLoadEntries(io, count);
     BAIL_IF_MACRO(entries == NULL, NULL, NULL);
-    return UNPK_openArchive(io, entries, entryCount);
+    return UNPK_openArchive(io, entries, count);
 } /* WAD_openArchive */
 
 
--- a/src/archiver_zip.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/archiver_zip.c	Fri Mar 09 04:50:27 2012 -0500
@@ -169,18 +169,13 @@
 } /* zlib_err */
 
 
-static inline int readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
-{
-    return (io->read(io, buf, len) == len);
-} /* readAll */
-
 /*
  * Read an unsigned 32-bit int and swap to native byte order.
  */
 static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val)
 {
     PHYSFS_uint32 v;
-    BAIL_IF_MACRO(!readAll(io, &v, sizeof (v)), NULL, 0);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), NULL, 0);
     *val = PHYSFS_swapULE32(v);
     return 1;
 } /* readui32 */
@@ -192,7 +187,7 @@
 static int readui16(PHYSFS_Io *io, PHYSFS_uint16 *val)
 {
     PHYSFS_uint16 v;
-    BAIL_IF_MACRO(!readAll(io, &v, sizeof (v)), NULL, 0);
+    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &v, sizeof (v)), NULL, 0);
     *val = PHYSFS_swapULE16(v);
     return 1;
 } /* readui16 */
@@ -462,14 +457,14 @@
         /* make sure we catch a signature between buffers. */
         if (totalread != 0)
         {
-            if (!readAll(io, buf, maxread - 4))
+            if (!__PHYSFS_readAll(io, buf, maxread - 4))
                 return -1;
             memcpy(&buf[maxread - 4], &extra, sizeof (extra));
             totalread += maxread - 4;
         } /* if */
         else
         {
-            if (!readAll(io, buf, maxread))
+            if (!__PHYSFS_readAll(io, buf, maxread))
                 return -1;
             totalread += maxread;
         } /* else */
@@ -724,7 +719,7 @@
     BAIL_IF_MACRO(path == NULL, ERR_OUT_OF_MEMORY, 0);
     
     if (entry->compression_method == COMPMETH_NONE)
-        rc = readAll(io, path, size);
+        rc = __PHYSFS_readAll(io, path, size);
 
     else  /* symlink target path is compressed... */
     {
@@ -733,7 +728,7 @@
         PHYSFS_uint8 *compressed = (PHYSFS_uint8*) __PHYSFS_smallAlloc(complen);
         if (compressed != NULL)
         {
-            if (readAll(io, compressed, complen))
+            if (__PHYSFS_readAll(io, compressed, complen))
             {
                 initializeZStream(&stream);
                 stream.next_in = compressed;
@@ -970,7 +965,7 @@
 
     entry->name = (char *) allocator.Malloc(fnamelen + 1);
     BAIL_IF_MACRO(entry->name == NULL, ERR_OUT_OF_MEMORY, 0);
-    if (!readAll(io, entry->name, fnamelen))
+    if (!__PHYSFS_readAll(io, entry->name, fnamelen))
         goto zip_load_entry_puked;
 
     entry->name[fnamelen] = '\0';  /* null-terminate the filename. */
--- a/src/physfs.c	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/physfs.c	Fri Mar 09 04:50:27 2012 -0500
@@ -2777,5 +2777,11 @@
     } /* if */
 } /* __PHYSFS_smallFree */
 
+
+int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
+{
+    return (io->read(io, buf, len) == len);
+} /* __PHYSFS_readAll */
+
 /* end of physfs.c ... */
 
--- a/src/physfs_internal.h	Fri Mar 09 03:55:23 2012 -0500
+++ b/src/physfs_internal.h	Fri Mar 09 04:50:27 2012 -0500
@@ -1007,6 +1007,11 @@
                                    void (*destruct)(void *));
 
 
+/*
+ * Read (len) bytes from (io) into (buf). Returns non-zero on success,
+ *  zero on i/o error. Literally: "return (io->read(io, buf, len) == len);"
+ */
+int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len);
 
 
 /* These are shared between some archivers. */