physfs.c
changeset 27 e1d3292769b2
parent 25 96b32c0fd7f3
child 28 529214f57d1b
--- a/physfs.c	Mon Jul 09 01:43:28 2001 +0000
+++ b/physfs.c	Mon Jul 09 01:45:13 2001 +0000
@@ -35,7 +35,7 @@
 
 typedef struct __PHYSFS_FILEHANDLELIST__
 {
-    PHYSFS_file *handle;
+    PHYSFS_file handle;
     struct __PHYSFS_FILEHANDLELIST__ *next;
 } FileHandleList;
 
@@ -236,7 +236,7 @@
 
     for (i = openList; i != NULL; i = i->next)
     {
-        const DirHandle *h = ((FileHandle *) i->handle->opaque)->dirHandle;
+        const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
         BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
     } /* for */
 
@@ -387,14 +387,13 @@
     for (i = *list; i != NULL; i = next)
     {
         next = i->next;
-        h = (FileHandle *) (i->handle->opaque);
-        if (!h->funcs->fileClose(i->handle->opaque))
+        h = (FileHandle *) (i->handle.opaque);
+        if (!h->funcs->fileClose(h))
         {
             *list = i;
             return(0);
         } /* if */
 
-        free(i->handle);
         free(i);
     } /* for */
 
@@ -1052,13 +1051,12 @@
 
 static PHYSFS_file *doOpenWrite(const char *fname, int appending)
 {
-    PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
+    PHYSFS_file *retval = NULL;
     FileHandle *rc = NULL;
     DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
     const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
     FileHandleList *list;
 
-    BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
     BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
     BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
 
@@ -1067,17 +1065,13 @@
 
     rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
     if (rc == NULL)
-    {
         free(list);
-        free(retval);
-        retval = NULL;
-    } /* if */
     else
     {
-        retval->opaque = (void *) rc;
-        list->handle = retval;
+        list->handle.opaque = (void *) rc;
         list->next = openWriteList;
         openWriteList = list;
+        retval = &(list->handle);
     } /* else */
 
     return(retval);
@@ -1098,19 +1092,13 @@
 
 PHYSFS_file *PHYSFS_openRead(const char *fname)
 {
-    PHYSFS_file *retval;
+    PHYSFS_file *retval = NULL;
     FileHandle *rc = NULL;
     FileHandleList *list;
     DirInfo *i;
 
-    retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
-    BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
     list = (FileHandleList *) malloc(sizeof (FileHandleList));
-    if (!list)
-    {
-        free(retval);
-        BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
-    } /* if */
+    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
 
     for (i = searchPath; i != NULL; i = i->next)
     {
@@ -1124,57 +1112,66 @@
     } /* for */
 
     if (rc == NULL)
-    {
         free(list);
-        free(retval);
-        retval = NULL;
-    } /* if */
     else
     {
-        retval->opaque = (void *) rc;
-        list->handle = retval;
+        list->handle.opaque = (void *) rc;
         list->next = openReadList;
         openReadList = list;
+        retval = &(list->handle);
     } /* else */
 
     return(retval);
 } /* PHYSFS_openRead */
 
 
-int PHYSFS_close(PHYSFS_file *handle)
+static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
 {
     FileHandle *h = (FileHandle *) handle->opaque;
+    FileHandleList *prev = NULL;
     FileHandleList *i;
-    FileHandleList *prev;
-    FileHandleList **_lists[] = { &openWriteList, &openReadList, NULL };
-    FileHandleList ***lists = _lists;  /* gay. */
     int rc;
 
-    while (lists != NULL)
+    for (i = *list; i != NULL; i = i->next)
     {
-        for (i = *(*lists), prev = NULL; i != NULL; prev = i, i = i->next)
+        if (&i->handle == handle)  /* handle is in this list? */
         {
-            if (i->handle == handle)
-            {
-                rc = h->funcs->fileClose(h);
-                if (!rc)
-                    return(0);
+            rc = h->funcs->fileClose(h);
+            if (!rc)
+                return(-1);
+
+            if (prev == NULL)
+                *list = i->next;
+            else
+                prev->next = i->next;
+
+            free(i);
+            return(1);
+        } /* if */
+        prev = i;
+    } /* for */
 
-                if (prev == NULL)
-                    *(*lists) = i->next;
-                else
-                    prev->next = i->next;
+    return(0);
+} /* closeHandleInOpenList */
+
+
+int PHYSFS_close(PHYSFS_file *handle)
+{
+    int rc;
 
-                free(handle);
-                free(i);
-                return(1);
-            } /* if */
-        } /* for */
-        lists++;
-    } /* while */
+    /* -1 == close failure. 0 == not found. 1 == success. */
+    rc = closeHandleInOpenList(&openReadList, handle);
+    BAIL_IF_MACRO(rc == -1, NULL, 0);
+    if (!rc)
+    {
+        rc = closeHandleInOpenList(&openWriteList, handle);
+        BAIL_IF_MACRO(rc == -1, NULL, 0);
+    } /* if */
 
-    __PHYSFS_setError(ERR_NOT_A_HANDLE);
-    return(0);
+    if (!rc)
+        __PHYSFS_setError(ERR_NOT_A_HANDLE);
+
+    return(rc);
 } /* PHYSFS_close */