physfs.c
changeset 19 a0279b57398c
parent 15 418eacc97ac8
child 20 efdde0d21521
--- a/physfs.c	Sat Jul 07 08:28:51 2001 +0000
+++ b/physfs.c	Sat Jul 07 09:05:19 2001 +0000
@@ -956,52 +956,90 @@
     return(0);
 } /* PHYSFS_isSymbolicLink */
 
-/**
- * Open a file for writing, in platform-independent notation and in relation
- *  to the write path as the root of the writable filesystem. The specified
- *  file is created if it doesn't exist. If it does exist, it is truncated to
- *  zero bytes, and the writing offset is set to the start.
- *
- *   @param filename File to open.
- *  @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
- *           of the error can be gleaned from PHYSFS_getLastError().
- */
+
+static PHYSFS_file *doOpenWrite(const char *fname, int appending)
+{
+    PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
+    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);
+
+    list = (FileHandleList *) malloc(sizeof (FileHandleList));
+    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
+
+    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->next = openWriteList;
+        openWriteList = list;
+    } /* else */
+
+    return(retval);
+} /* doOpenWrite */
+
+
 PHYSFS_file *PHYSFS_openWrite(const char *filename)
 {
-return NULL;
+    return(doOpenWrite(filename, 0));
 } /* PHYSFS_openWrite */
 
 
-/**
- * Open a file for writing, in platform-independent notation and in relation
- *  to the write path as the root of the writable filesystem. The specified
- *  file is created if it doesn't exist. If it does exist, the writing offset
- *  is set to the end of the file, so the first write will be the byte after
- *  the end.
- *
- *   @param filename File to open.
- *  @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
- *           of the error can be gleaned from PHYSFS_getLastError().
- */
 PHYSFS_file *PHYSFS_openAppend(const char *filename)
 {
-return NULL;
+    return(doOpenWrite(filename, 1));
 } /* PHYSFS_openAppend */
 
 
-/**
- * Open a file for reading, in platform-independent notation. The search path
- *  is checked one at a time until a matching file is found, in which case an
- *  abstract filehandle is associated with it, and reading may be done.
- *  The reading offset is set to the first byte of the file.
- *
- *   @param filename File to open.
- *  @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
- *           of the error can be gleaned from PHYSFS_getLastError().
- */
-PHYSFS_file *PHYSFS_openRead(const char *filename)
+PHYSFS_file *PHYSFS_openRead(const char *fname)
 {
-return NULL;
+    PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
+    FileHandle *rc = NULL;
+    FileHandleList *list;
+    DirInfo *i;
+
+    BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
+    list = (FileHandleList *) malloc(sizeof (FileHandleList));
+    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
+
+    for (i = searchPath; i != NULL; i = i->next)
+    {
+        DirHandle *h = i->dirHandle;
+        if (__PHYSFS_verifySecurity(h, fname))
+        {
+            rc = h->funcs->openRead(h, fname);
+            if (rc != NULL)
+                break;
+        } /* if */
+    } /* for */
+
+    if (rc == NULL)
+    {
+        free(list);
+        free(retval);
+        retval = NULL;
+    } /* if */
+    else
+    {
+        retval->opaque = (void *) rc;
+        list->handle = retval;
+        list->next = openReadList;
+        openReadList = list;
+    } /* else */
+
+    return(retval);
 } /* PHYSFS_openRead */
 
 
@@ -1018,7 +1056,7 @@
     {
         for (i = *(*lists), prev = NULL; i != NULL; prev = i, i = i->next)
         {
-            if (((FileHandle *) i->handle->opaque) == h)
+            if (i->handle == handle)
             {
                 rc = h->funcs->close(h);
                 if (!rc)
@@ -1028,8 +1066,9 @@
                     *(*lists) = i->next;
                 else
                     prev->next = i->next;
+
+                free(handle);
                 free(i);
-                free(handle);
                 return(1);
             } /* if */
         } /* for */