Cleaned up PHYSFS_openRead() a little. PHYSFS_addToSearchPath() now
authorRyan C. Gordon <icculus@icculus.org>
Mon, 23 Jul 2001 09:23:17 +0000
changeset 51 42f4befc1d0b
parent 50 989b413188e5
child 52 bbb26eacc532
Cleaned up PHYSFS_openRead() a little. PHYSFS_addToSearchPath() now returns successful for duplicates, but doesn't add them a second time.
physfs.c
--- a/physfs.c	Mon Jul 23 07:16:22 2001 +0000
+++ b/physfs.c	Mon Jul 23 09:23:17 2001 +0000
@@ -522,22 +522,26 @@
 
 int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
 {
-    DirInfo *di = buildDirInfo(newDir, 0);
+    DirInfo *di;
+    DirInfo *i = searchPath;
+    DirInfo *prev = NULL;
+
+    while (i != NULL)
+    {
+        if (strcmp(newDir, i->dirName) == 0)  /* already in search path. */
+            return(1);
+
+        prev = i;
+        i = i->next;
+    } /* while */
+
+    di = buildDirInfo(newDir, 0);
 
     BAIL_IF_MACRO(di == NULL, NULL, 0);
 
     if (appendToPath)
     {
-        DirInfo *i = searchPath;
-        DirInfo *prev = NULL;
-
         di->next = NULL;
-        while (i != NULL)
-        {
-            prev = i;
-            i = i->next;
-        } /* while */
-
         if (prev == NULL)
             searchPath = di;
         else
@@ -1147,7 +1151,6 @@
 
 PHYSFS_file *PHYSFS_openRead(const char *fname)
 {
-    PHYSFS_file *retval = NULL;
     FileHandle *rc = NULL;
     FileHandleList *list;
     DirInfo *i;
@@ -1155,9 +1158,6 @@
     while (*fname == '/')
         fname++;
 
-    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;
@@ -1170,16 +1170,15 @@
     } /* for */
 
     if (rc == NULL)
-        free(list);
-    else
-    {
-        list->handle.opaque = (void *) rc;
-        list->next = openReadList;
-        openReadList = list;
-        retval = &(list->handle);
-    } /* else */
+        return(NULL);
 
-    return(retval);
+    list = (FileHandleList *) malloc(sizeof (FileHandleList));
+    BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
+    list->handle.opaque = (void *) rc;
+    list->next = openReadList;
+    openReadList = list;
+
+    return(&(list->handle));
 } /* PHYSFS_openRead */