Skip to content
Permalink
Browse files
Cleanup BAIL_* and GOTO_* macros to be less cluttered.
This also fixes really aggressive compiler warnings about the "if (!ERRPASS)"
construct producing unreachable code.
  • Loading branch information
icculus committed Jul 6, 2017
1 parent 9aebcff commit 32da097a557a252f278b7642b087c4d09818b168
@@ -15,7 +15,7 @@

static char *cvtToDependent(const char *prepend, const char *path, char *buf)
{
BAIL_IF_MACRO(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
BAIL_IF(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
sprintf(buf, "%s%s", prepend ? prepend : "", path);

if (__PHYSFS_platformDirSeparator != '/')
@@ -45,12 +45,12 @@ static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
const size_t seplen = 1;

assert(io == NULL); /* shouldn't create an Io for these. */
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &st), ERRPASS, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_platformStat(name, &st), NULL);
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);

retval = allocator.Malloc(namelen + seplen + 1);
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);

strcpy(retval, name);

@@ -86,7 +86,7 @@ static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
char *f = NULL;

CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(!f, ERRPASS, NULL);
BAIL_IF_ERRPASS(!f, NULL);

io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
@@ -127,7 +127,7 @@ static int DIR_remove(void *opaque, const char *name)
char *f;

CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(!f, ERRPASS, 0);
BAIL_IF_ERRPASS(!f, 0);
retval = __PHYSFS_platformDelete(f);
__PHYSFS_smallFree(f);
return retval;
@@ -140,7 +140,7 @@ static int DIR_mkdir(void *opaque, const char *name)
char *f;

CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(!f, ERRPASS, 0);
BAIL_IF_ERRPASS(!f, 0);
retval = __PHYSFS_platformMkDir(f);
__PHYSFS_smallFree(f);
return retval;
@@ -159,7 +159,7 @@ static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
char *d;

CVT_TO_DEPENDENT(d, opaque, name);
BAIL_IF_MACRO(!d, ERRPASS, 0);
BAIL_IF_ERRPASS(!d, 0);
retval = __PHYSFS_platformStat(d, stat);
__PHYSFS_smallFree(d);
return retval;
@@ -37,14 +37,14 @@ static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
char *ptr = NULL;

entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);

location += (16 * fileCount);

for (entry = entries; fileCount > 0; fileCount--, entry++)
{
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), ERRPASS, failed);
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, failed);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 12), failed);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->size, 4), failed);
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */
@@ -70,17 +70,17 @@ static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)

assert(io != NULL); /* shouldn't ever happen. */

BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);

BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);

BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
count = PHYSFS_swapULE32(count);

entries = grpLoadEntries(io, count);
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
BAIL_IF_ERRPASS(!entries, NULL);
return UNPK_openArchive(io, entries, count);
} /* GRP_openArchive */

@@ -48,21 +48,21 @@ static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
{
entCount++;
ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
GOTO_IF_MACRO(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
GOTO_IF(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
entries = (UNPKentry *) ptr;
entry = &entries[entCount-1];

GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), ERRPASS, failed);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 13), failed);
pos += 13;
GOTO_IF_MACRO(!__PHYSFS_readAll(io, &size, 4), ERRPASS, failed);
GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), failed);
pos += 4;

entry->size = PHYSFS_swapULE32(size);
entry->startPos = pos;
pos += size;

/* skip over entry */
GOTO_IF_MACRO(!io->seek(io, pos), ERRPASS, failed);
GOTO_IF_ERRPASS(!io->seek(io, pos), failed);
} /* while */

*_entCount = entCount;
@@ -81,12 +81,12 @@ static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
UNPKentry *entries = NULL;

assert(io != NULL); /* shouldn't ever happen. */
BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 3), ERRPASS, NULL);
BAIL_IF_MACRO(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 3), NULL);
BAIL_IF(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);

entries = hogLoadEntries(io, &count);
BAIL_IF_MACRO(!entries, ERRPASS, NULL);
BAIL_IF_ERRPASS(!entries, NULL);
return UNPK_openArchive(io, entries, count);
} /* HOG_openArchive */

0 comments on commit 32da097

Please sign in to comment.