Skip to content

Commit

Permalink
Added PHYSFS_fileLength(). Bleh.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 9, 2001
1 parent 096d57e commit eea7435
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
22 changes: 15 additions & 7 deletions archivers/dir.c
Expand Up @@ -70,6 +70,12 @@ static int DIR_seek(FileHandle *handle, int offset)
} /* DIR_seek */


static int DIR_fileLength(FileHandle *handle)
{
return(__PHYSFS_platformFileLength((FILE *) (handle->opaque)));
} /* DIR_fileLength */


static int DIR_fileClose(FileHandle *handle)
{
FILE *h = (FILE *) (handle->opaque);
Expand Down Expand Up @@ -283,18 +289,20 @@ static const FileFunctions __PHYSFS_FileFunctions_DIR =
DIR_eof, /* eof() method */
DIR_tell, /* tell() method */
DIR_seek, /* seek() method */
DIR_fileClose, /* fileClose() method */
DIR_fileLength, /* fileLength() method */
DIR_fileClose /* fileClose() method */
};


static const FileFunctions __PHYSFS_FileFunctions_DIRW =
{
NULL, /* read() method */
DIR_write, /* write() method */
DIR_eof, /* eof() method */
DIR_tell, /* tell() method */
DIR_seek, /* seek() method */
DIR_fileClose /* fileClose() method */
NULL, /* read() method */
DIR_write, /* write() method */
DIR_eof, /* eof() method */
DIR_tell, /* tell() method */
DIR_seek, /* seek() method */
DIR_fileLength, /* fileLength() method */
DIR_fileClose /* fileClose() method */
};


Expand Down
20 changes: 14 additions & 6 deletions archivers/grp.c
Expand Up @@ -115,6 +115,13 @@ static int GRP_seek(FileHandle *handle, int offset)
} /* GRP_seek */


static int GRP_fileLength(FileHandle *handle)
{
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
return(finfo->size);
} /* GRP_fileLength */


static int GRP_fileClose(FileHandle *handle)
{
free(handle->opaque);
Expand Down Expand Up @@ -340,12 +347,13 @@ static void GRP_dirClose(DirHandle *h)

static const FileFunctions __PHYSFS_FileFunctions_GRP =
{
GRP_read, /* read() method */
NULL, /* write() method */
GRP_eof, /* eof() method */
GRP_tell, /* tell() method */
GRP_seek, /* seek() method */
GRP_fileClose /* fileClose() method */
GRP_read, /* read() method */
NULL, /* write() method */
GRP_eof, /* eof() method */
GRP_tell, /* tell() method */
GRP_seek, /* seek() method */
GRP_fileLength, /* fileLength() method */
GRP_fileClose /* fileClose() method */
};


Expand Down
18 changes: 12 additions & 6 deletions archivers/zip.c
Expand Up @@ -42,6 +42,11 @@ static int ZIP_seek(FileHandle *handle, int offset)
} /* ZIP_seek */


static int ZIP_fileLength(FileHandle *handle)
{
} /* ZIP_fileLength */


static int ZIP_fileClose(FileHandle *handle)
{
} /* ZIP_fileClose */
Expand Down Expand Up @@ -89,12 +94,13 @@ static void ZIP_dirClose(DirHandle *h)

static const FileFunctions __PHYSFS_FileFunctions_ZIP =
{
ZIP_read, /* read() method */
NULL, /* write() method */
ZIP_eof, /* eof() method */
ZIP_tell, /* tell() method */
ZIP_seek, /* seek() method */
ZIP_fileClose /* fileClose() method */
ZIP_read, /* read() method */
NULL, /* write() method */
ZIP_eof, /* eof() method */
ZIP_tell, /* tell() method */
ZIP_seek, /* seek() method */
ZIP_fileLength, /* fileLength() method */
ZIP_fileClose /* fileClose() method */
};


Expand Down
10 changes: 10 additions & 0 deletions physfs.c
Expand Up @@ -1227,5 +1227,15 @@ int PHYSFS_seek(PHYSFS_file *handle, int pos)
} /* PHYSFS_seek */


int PHYSFS_fileLength(PHYSFS_file *handle)
{
FileHandle *h = (FileHandle *) handle->opaque;
assert(h != NULL);
assert(h->funcs != NULL);
BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
return(h->funcs->fileLength(h));
} /* PHYSFS_filelength */


/* end of physfs.c ... */

14 changes: 14 additions & 0 deletions physfs.h
Expand Up @@ -791,6 +791,20 @@ int PHYSFS_tell(PHYSFS_file *handle);
*/
int PHYSFS_seek(PHYSFS_file *handle, int pos);


/**
* Get total length of a file in bytes. Note that if the file size can't
* be determined (since the archive is "streamed" or whatnot) than this
* with report (-1). Also note that if another process/thread is writing
* to this file at the same time, then the information this function
* supplies could be incorrect before you get it. Use with caution, or
* better yet, don't use at all.
*
* @param handle handle returned from PHYSFS_open*().
* @return size in bytes of the file. -1 if can't be determined.
*/
int PHYSFS_fileLength(PHYSFS_file *handle);

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 14 additions & 0 deletions physfs_internal.h
Expand Up @@ -82,6 +82,13 @@ typedef struct __PHYSFS_FILEFUNCTIONS__
*/
int (*seek)(FileHandle *handle, int offset);

/*
* Return number of bytes available in the file, or -1 if you
* aren't able to determine.
* On failure, call __PHYSFS_setError().
*/
int (*fileLength)(FileHandle *handle);

/*
* Close the file, and free the FileHandle structure (including "opaque").
* returns non-zero on success, zero if can't close file.
Expand Down Expand Up @@ -402,6 +409,13 @@ void __PHYSFS_platformTimeslice(void);
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname);


/*
* Determine the current size of a file, in bytes, from a stdio FILE *.
* Return -1 if you can't do it, and call __PHYSFS_setError().
*/
int __PHYSFS_platformFileLength(FILE *handle);


#ifdef __cplusplus
extern "C" {
#endif
Expand Down
9 changes: 9 additions & 0 deletions platform/unix.c
Expand Up @@ -328,5 +328,14 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname)
} /* __PHYSFS_platformEnumerateFiles */


int __PHYSFS_platformFileLength(FILE *handle)
{
struct stat statbuf;
errno = 0;
BAIL_IF_MACRO(fstat(fileno(handle), &statbuf) == -1, strerror(errno), -1);
return(statbuf.st_size);
} /* __PHYSFS_platformFileLength */


/* end of unix.c ... */

0 comments on commit eea7435

Please sign in to comment.