From a0b21c9ae9b844dc0746b2f1db4824a598b1c2de Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 9 Mar 2012 04:50:27 -0500 Subject: [PATCH] Cleaned up all the readAll() cut and paste. --- docs/TODO.txt | 1 - src/archiver_grp.c | 23 +++++++++-------------- src/archiver_hog.c | 23 +++++++++-------------- src/archiver_mvl.c | 25 +++++++++---------------- src/archiver_qpak.c | 18 ++++++------------ src/archiver_wad.c | 28 +++++++++++----------------- src/archiver_zip.c | 19 +++++++------------ src/physfs.c | 6 ++++++ src/physfs_internal.h | 5 +++++ 9 files changed, 62 insertions(+), 86 deletions(-) diff --git a/docs/TODO.txt b/docs/TODO.txt index 00c533b7..398fb4b9 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -82,7 +82,6 @@ Other stuff I thought of... - 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 diff --git a/src/archiver_grp.c b/src/archiver_grp.c index fc5fcf84..2e29cb1f 100644 --- a/src/archiver_grp.c +++ b/src/archiver_grp.c @@ -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 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) 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 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) return entries; -grpLoad_failed: +failed: allocator.Free(entries); return NULL; } /* grpLoadEntries */ @@ -70,23 +65,23 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) 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 */ diff --git a/src/archiver_hog.c b/src/archiver_hog.c index 8f218548..2eae5699 100644 --- a/src/archiver_hog.c +++ b/src/archiver_hog.c @@ -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 @@ static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount) { 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 @@ static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount) 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 UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount) 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 */ diff --git a/src/archiver_mvl.c b/src/archiver_mvl.c index 95bb49be..1f158645 100644 --- a/src/archiver_mvl.c +++ b/src/archiver_mvl.c @@ -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 @@ static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) 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 @@ static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) return entries; -mvlLoad_failed: +failed: allocator.Free(entries); return NULL; } /* mvlLoadEntries */ @@ -70,19 +63,19 @@ static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) 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 */ diff --git a/src/archiver_qpak.c b/src/archiver_qpak.c index ca3f1fda..fb694247 100644 --- a/src/archiver_qpak.c +++ b/src/archiver_qpak.c @@ -69,12 +69,6 @@ typedef struct #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 @@ static int qpak_load_entries(QPAKinfo *info) 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 @@ static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting) 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? */ diff --git a/src/archiver_wad.c b/src/archiver_wad.c index 2542434a..d0660a6f 100644 --- a/src/archiver_wad.c +++ b/src/archiver_wad.c @@ -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 @@ static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) 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 @@ static UNPKentry *wadLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount) return entries; -wadLoad_failed: +failed: allocator.Free(entries); return NULL; } /* wadLoadEntries */ @@ -90,21 +84,21 @@ static void *WAD_openArchive(PHYSFS_Io *io, const char *name, int forWriting) { 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 */ diff --git a/src/archiver_zip.c b/src/archiver_zip.c index 6606badd..76f8c3e4 100644 --- a/src/archiver_zip.c +++ b/src/archiver_zip.c @@ -169,18 +169,13 @@ static int zlib_err(int rc) } /* 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 readui32(PHYSFS_Io *io, PHYSFS_uint32 *val) 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 @@ static PHYSFS_sint64 zip_find_end_of_central_dir(PHYSFS_Io *io, PHYSFS_sint64 *l /* 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 @@ static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry) 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 @@ static int zip_resolve_symlink(PHYSFS_Io *io, ZIPinfo *info, ZIPentry *entry) 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 @@ static int zip_load_entry(PHYSFS_Io *io, ZIPentry *entry, PHYSFS_uint32 ofs_fixu 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. */ diff --git a/src/physfs.c b/src/physfs.c index 60114a80..aaf36b7f 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -2777,5 +2777,11 @@ void __PHYSFS_smallFree(void *ptr) } /* 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 ... */ diff --git a/src/physfs_internal.h b/src/physfs_internal.h index 0446832a..9dce6675 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -1007,6 +1007,11 @@ PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len, 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. */