Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moved all the file i/o from stdio-style to POSIX-style.
Instead of trying to deal with a count of objects, just give 'em a stream of
bytes. This is WAY simpler to work with at the implementation level, and
removes confusion about what to do with a partial read.

This will be very useful when we expose the i/o interface to applications.
  • Loading branch information
icculus committed Aug 21, 2010
1 parent 68632d3 commit 4000b23
Show file tree
Hide file tree
Showing 16 changed files with 401 additions and 422 deletions.
14 changes: 4 additions & 10 deletions src/archiver_dir.c
Expand Up @@ -14,21 +14,15 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
PHYSFS_sint64 retval;
retval = __PHYSFS_platformRead(opaque, buffer, objSize, objCount);
return retval;
return __PHYSFS_platformRead(opaque, buffer, len);
} /* DIR_read */


static PHYSFS_sint64 DIR_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 DIR_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
PHYSFS_sint64 retval;
retval = __PHYSFS_platformWrite(opaque, buffer, objSize, objCount);
return retval;
return __PHYSFS_platformWrite(f, buf, len);
} /* DIR_write */


Expand Down
38 changes: 18 additions & 20 deletions src/archiver_grp.c
Expand Up @@ -57,6 +57,12 @@ typedef struct
} GRPfileinfo;


static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */


static void GRP_dirClose(dvoid *opaque)
{
GRPinfo *info = ((GRPinfo *) opaque);
Expand All @@ -66,28 +72,25 @@ static void GRP_dirClose(dvoid *opaque)
} /* GRP_dirClose */


static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
GRPentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
const GRPentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
PHYSFS_sint64 rc;

if (objsLeft < objCount)
objCount = objsLeft;
if (bytesLeft < len)
len = bytesLeft;

rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
finfo->curPos += (PHYSFS_uint32) rc;

return rc;
} /* GRP_read */


static PHYSFS_sint64 GRP_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 GRP_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* GRP_write */
Expand Down Expand Up @@ -150,7 +153,7 @@ static int grp_open(const char *filename, int forWriting,
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);

if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
if (!readAll(*fh, buf, 12))
goto openGrp_failed;

if (memcmp(buf, "KenSilverman", 12) != 0)
Expand All @@ -159,7 +162,7 @@ static int grp_open(const char *filename, int forWriting,
goto openGrp_failed;
} /* if */

if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
if (!readAll(*fh, count, sizeof (PHYSFS_uint32)))
goto openGrp_failed;

*count = PHYSFS_swapULE32(*count);
Expand Down Expand Up @@ -236,7 +239,8 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)

for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
if ( (!readAll(fh, &entry->name, 12)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
{
__PHYSFS_platformClose(fh);
return 0;
Expand All @@ -246,12 +250,6 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
if ((ptr = strchr(entry->name, ' ')) != NULL)
*ptr = '\0'; /* trim extra spaces. */

if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
{
__PHYSFS_platformClose(fh);
return 0;
} /* if */

entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = location;
location += entry->size;
Expand Down
50 changes: 22 additions & 28 deletions src/archiver_hog.c
Expand Up @@ -71,6 +71,12 @@ typedef struct
} HOGfileinfo;


static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */


static void HOG_dirClose(dvoid *opaque)
{
HOGinfo *info = ((HOGinfo *) opaque);
Expand All @@ -80,28 +86,25 @@ static void HOG_dirClose(dvoid *opaque)
} /* HOG_dirClose */


static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
HOGentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
const HOGentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
PHYSFS_sint64 rc;

if (objsLeft < objCount)
objCount = objsLeft;
if (bytesLeft < len)
len = bytesLeft;

rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
finfo->curPos += (PHYSFS_uint32) rc;

return rc;
} /* HOG_read */


static PHYSFS_sint64 HOG_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 HOG_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* HOG_write */
Expand Down Expand Up @@ -168,7 +171,7 @@ static int hog_open(const char *filename, int forWriting,
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);

if (__PHYSFS_platformRead(*fh, buf, 3, 1) != 1)
if (!readAll(*fh, buf, 3))
goto openHog_failed;

if (memcmp(buf, "DHF", 3) != 0)
Expand All @@ -179,10 +182,10 @@ static int hog_open(const char *filename, int forWriting,

while (1)
{
if (__PHYSFS_platformRead(*fh, buf, 13, 1) != 1)
if (!readAll(*fh, buf, 13))
break; /* eof here is ok */

if (__PHYSFS_platformRead(*fh, &size, 4, 1) != 1)
if (!readAll(*fh, &size, sizeof (PHYSFS_uint32)))
goto openHog_failed;

size = PHYSFS_swapULE32(size);
Expand Down Expand Up @@ -269,32 +272,23 @@ static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)

for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
{
__PHYSFS_platformClose(fh);
return 0;
} /* if */

if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
if ( (!readAll(fh, &entry->name, 13)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
{
__PHYSFS_platformClose(fh);
return 0;
} /* if */

entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
if (entry->startPos == -1)
{
__PHYSFS_platformClose(fh);
return 0;
}

/* Skip over entry */
if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size))
if ( (entry->startPos == -1) ||
(!__PHYSFS_platformSeek(fh, entry->startPos + entry->size)) )
{
__PHYSFS_platformClose(fh);
return 0;
}
} /* if */
} /* for */

__PHYSFS_platformClose(fh);
Expand Down

0 comments on commit 4000b23

Please sign in to comment.