Now uses platform file i/o abstraction.

This commit is contained in:
Ryan C. Gordon 2002-03-25 04:06:06 +00:00
parent 723702fedb
commit 51b65e1d5a
1 changed files with 30 additions and 59 deletions

View File

@ -97,82 +97,54 @@ const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer, static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount) PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{ {
FILE *h = (FILE *) (handle->opaque); PHYSFS_sint64 retval;
size_t retval; retval = __PHYSFS_platformRead(handle->opaque, buffer, objSize, objCount);
return(retval);
errno = 0;
retval = fread(buffer, objSize, objCount, h);
BAIL_IF_MACRO((retval < (size_t) objCount) && (ferror(h)),
strerror(errno), (int) retval);
return((int) retval);
} /* DIR_read */ } /* DIR_read */
static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer, static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount) PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
{ {
FILE *h = (FILE *) (handle->opaque); PHYSFS_sint64 retval;
size_t retval; retval = __PHYSFS_platformWrite(handle->opaque, buffer, objSize, objCount);
return(retval);
errno = 0;
retval = fwrite(buffer, (size_t) objSize, objCount, h);
if ( (retval < (signed int) objCount) && (ferror(h)) )
__PHYSFS_setError(strerror(errno));
return((int) retval);
} /* DIR_write */ } /* DIR_write */
static int DIR_eof(FileHandle *handle) static int DIR_eof(FileHandle *handle)
{ {
return(feof((FILE *) (handle->opaque))); return(__PHYSFS_platformEOF(handle->opaque));
} /* DIR_eof */ } /* DIR_eof */
static PHYSFS_sint64 DIR_tell(FileHandle *handle) static PHYSFS_sint64 DIR_tell(FileHandle *handle)
{ {
return(ftell((FILE *) (handle->opaque))); return(__PHYSFS_platformTell(handle->opaque));
} /* DIR_tell */ } /* DIR_tell */
static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset) static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset)
{ {
return(fseek((FILE *) (handle->opaque), offset, SEEK_SET) == 0); return(__PHYSFS_platformSeek(handle->opaque, offset));
} /* DIR_seek */ } /* DIR_seek */
static PHYSFS_sint64 DIR_fileLength(FileHandle *handle) static PHYSFS_sint64 DIR_fileLength(FileHandle *handle)
{ {
return(__PHYSFS_platformFileLength((FILE *) (handle->opaque))); return(__PHYSFS_platformFileLength(handle->opaque));
} /* DIR_fileLength */ } /* DIR_fileLength */
static int DIR_fileClose(FileHandle *handle) static int DIR_fileClose(FileHandle *handle)
{ {
FILE *h = (FILE *) (handle->opaque);
#if 0
/* /*
* we manually fflush() the buffer, since that's the place fclose() will * we manually flush the buffer, since that's the place a close will
* most likely fail, but that will leave the file handle in an undefined * most likely fail, but that will leave the file handle in an undefined
* state if it fails. fflush() failures we can recover from. * state if it fails. Flush failures we can recover from.
*/ */
BAIL_IF_MACRO(!__PHYSFS_platformFlush(handle->opaque), NULL, 0);
/* keep trying until there's success or an unrecoverable error... */ BAIL_IF_MACRO(!__PHYSFS_platformClose(handle->opaque), NULL, 0);
do {
__PHYSFS_platformTimeslice();
errno = 0;
} while ( (fflush(h) == EOF) && ((errno == EAGAIN) || (errno == EINTR)) );
/* EBADF == "Not open for writing". That's fine. */
BAIL_IF_MACRO((errno != 0) && (errno != EBADF), strerror(errno), 0);
#endif
/* if fclose fails anyhow, we just have to pray that it's still usable. */
errno = 0;
BAIL_IF_MACRO(fclose(h) == EOF, strerror(errno), 0); /* (*shrug*) */
free(handle); free(handle);
return(1); return(1);
} /* DIR_fileClose */ } /* DIR_fileClose */
@ -265,10 +237,12 @@ static int DIR_isSymLink(DirHandle *h, const char *name)
} /* DIR_isSymLink */ } /* DIR_isSymLink */
static FileHandle *doOpen(DirHandle *h, const char *name, const char *mode) static FileHandle *doOpen(DirHandle *h, const char *name,
void *(*openFunc)(const char *filename),
const FileFunctions *fileFuncs)
{ {
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL); char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
FILE *rc; void *rc;
FileHandle *retval; FileHandle *retval;
char *str; char *str;
@ -278,43 +252,44 @@ static FileHandle *doOpen(DirHandle *h, const char *name, const char *mode)
if (!retval) if (!retval)
{ {
free(f); free(f);
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL); BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */ } /* if */
errno = 0; rc = openFunc(f);
rc = fopen(f, mode);
str = strerror(errno);
free(f); free(f);
if (!rc) if (!rc)
{ {
free(retval); free(retval);
BAIL_IF_MACRO(1, str, NULL); BAIL_MACRO(str, NULL);
} /* if */ } /* if */
retval->opaque = (void *) rc; retval->opaque = (void *) rc;
retval->dirHandle = h; retval->dirHandle = h;
retval->funcs = (mode[0] == 'r') ? retval->funcs = fileFuncs;
&__PHYSFS_FileFunctions_DIR : &__PHYSFS_FileFunctions_DIRW;
return(retval); return(retval);
} /* doOpen */ } /* doOpen */
static FileHandle *DIR_openRead(DirHandle *h, const char *filename) static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
{ {
return(doOpen(h, filename, "rb")); return(doOpen(h, filename, __PHYSFS_platformOpenRead,
&__PHYSFS_FileFunctions_DIR));
} /* DIR_openRead */ } /* DIR_openRead */
static FileHandle *DIR_openWrite(DirHandle *h, const char *filename) static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
{ {
return(doOpen(h, filename, "wb")); return(doOpen(h, filename, __PHYSFS_platformOpenWrite,
&__PHYSFS_FileFunctions_DIRW));
} /* DIR_openWrite */ } /* DIR_openWrite */
static FileHandle *DIR_openAppend(DirHandle *h, const char *filename) static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
{ {
return(doOpen(h, filename, "ab")); return(doOpen(h, filename, __PHYSFS_platformOpenAppend,
&__PHYSFS_FileFunctions_DIRW));
} /* DIR_openAppend */ } /* DIR_openAppend */
@ -325,6 +300,7 @@ static int DIR_remove(DirHandle *h, const char *name)
BAIL_IF_MACRO(f == NULL, NULL, 0); BAIL_IF_MACRO(f == NULL, NULL, 0);
/* !!! FIXME: Abstract in platform drivers. */
errno = 0; errno = 0;
retval = (remove(f) == 0); retval = (remove(f) == 0);
if (!retval) if (!retval)
@ -341,12 +317,7 @@ static int DIR_mkdir(DirHandle *h, const char *name)
int retval; int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0); BAIL_IF_MACRO(f == NULL, NULL, 0);
errno = 0;
retval = __PHYSFS_platformMkDir(f); retval = __PHYSFS_platformMkDir(f);
if (!retval)
__PHYSFS_setError(strerror(errno));
free(f); free(f);
return(retval); return(retval);
} /* DIR_mkdir */ } /* DIR_mkdir */