فهرست منبع

Make __PHYSFS_platformDirSeparator into a single char.

This multichar thing was always stupid. Pull it out of revision control if
 you ever need it.
Ryan C. Gordon 14 سال پیش
والد
کامیت
03dbc3f758
5فایلهای تغییر یافته به همراه38 افزوده شده و 46 حذف شده
  1. 9 6
      src/archiver_dir.c
  2. 21 31
      src/physfs.c
  3. 8 4
      src/physfs_internal.h
  4. 0 4
      src/platform_posix.c
  5. 0 1
      src/platform_windows.c

+ 9 - 6
src/archiver_dir.c

@@ -14,10 +14,10 @@
 static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
 {
     PHYSFS_Stat statbuf;
-    const char *dirsep = PHYSFS_getDirSeparator();
+    const char dirsep = __PHYSFS_platformDirSeparator;
     char *retval = NULL;
     const size_t namelen = strlen(name);
-    const size_t seplen = strlen(dirsep);
+    const size_t seplen = 1;
     int exists = 0;
 
     assert(io == NULL);  /* shouldn't create an Io for these. */
@@ -28,11 +28,14 @@ static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
     retval = allocator.Malloc(namelen + seplen + 1);
     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
 
-    /* make sure there's a dir separator at the end of the string */
-    /* !!! FIXME: is there really any place where (seplen != 1)? */
     strcpy(retval, name);
-    if (strcmp((name + namelen) - seplen, dirsep) != 0)
-        strcat(retval, dirsep);
+
+    /* make sure there's a dir separator at the end of the string */
+    if (retval[namelen - 1] != dirsep)
+    {
+        retval[namelen] = dirsep;
+        retval[namelen + 1] = '\0';
+    } /* if */
 
     return retval;
 } /* DIR_openArchive */

+ 21 - 31
src/physfs.c

@@ -1052,17 +1052,16 @@ static char *calculateUserDir(void)
     char *retval = __PHYSFS_platformGetUserDir();
     if (retval == NULL)
     {
-        const char *dirsep = PHYSFS_getDirSeparator();
+        const char dirsep = __PHYSFS_platformDirSeparator;
         const char *uname = __PHYSFS_platformGetUserName();
         const char *str = (uname != NULL) ? uname : "default";
+        const size_t len = strlen(baseDir) + strlen(str) + 7;
 
-        retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
-                                           strlen(dirsep) + 6);
-
+        retval = (char *) allocator.Malloc(len);
         if (retval == NULL)
             __PHYSFS_setError(ERR_OUT_OF_MEMORY);
         else
-            sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
+            sprintf(retval, "%susers%c%s", baseDir, dirsep, str);
 
         allocator.Free((void *) uname);
     } /* else */
@@ -1070,23 +1069,29 @@ static char *calculateUserDir(void)
     return retval;
 } /* calculateUserDir */
 
-
+/*
+ * !!! FIXME: remove this and require userdir and basedir to have dirsep
+ * !!! FIXME:  appended in the platform layer
+ */
 static int appendDirSep(char **dir)
 {
-    const char *dirsep = PHYSFS_getDirSeparator();
-    char *ptr;
+    const char dirsep = __PHYSFS_platformDirSeparator;
+    char *ptr = *dir;
+    const size_t len = strlen(ptr);
 
-    if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
+    if (ptr[len - 1] == dirsep)
         return 1;
 
-    ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
+    ptr = (char *) allocator.Realloc(ptr, len + 2);
     if (!ptr)
     {
         allocator.Free(*dir);
         return 0;
     } /* if */
 
-    strcat(ptr, dirsep);
+    ptr[len] = dirsep;
+    ptr[len+1] = '\0';
+
     *dir = ptr;
     return 1;
 } /* appendDirSep */
@@ -1094,8 +1099,8 @@ static int appendDirSep(char **dir)
 
 static char *calculateBaseDir(const char *argv0)
 {
+    const char dirsep = __PHYSFS_platformDirSeparator;
     char *retval = NULL;
-    const char *dirsep = NULL;
     char *ptr = NULL;
 
     /* Give the platform layer first shot at this. */
@@ -1106,26 +1111,10 @@ static char *calculateBaseDir(const char *argv0)
     /* We need argv0 to go on. */
     BAIL_IF_MACRO(argv0 == NULL, ERR_ARGV0_IS_NULL, NULL);
 
-    dirsep = PHYSFS_getDirSeparator();
-    if (strlen(dirsep) == 1)  /* fast path. */
-        ptr = strrchr(argv0, *dirsep);
-    else
-    {
-        ptr = strstr(argv0, dirsep);
-        if (ptr != NULL)
-        {
-            char *p = ptr;
-            while (p != NULL)
-            {
-                ptr = p;
-                p = strstr(p + 1, dirsep);
-            } /* while */
-        } /* if */
-    } /* else */
-
+    ptr = strrchr(argv0, dirsep);
     if (ptr != NULL)
     {
-        size_t size = (size_t) (ptr - argv0);
+        const size_t size = (size_t) (ptr - argv0);
         retval = (char *) allocator.Malloc(size + 1);
         BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
         memcpy(retval, argv0, size);
@@ -1310,7 +1299,8 @@ void PHYSFS_freeList(void *list)
 
 const char *PHYSFS_getDirSeparator(void)
 {
-    return __PHYSFS_platformDirSeparator;
+    static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
+    return retval;
 } /* PHYSFS_getDirSeparator */
 
 

+ 8 - 4
src/physfs_internal.h

@@ -1031,11 +1031,15 @@ int UNPK_stat(dvoid *opaque, const char *fn, int *exists, PHYSFS_Stat *stat);
 
 
 /*
- * The dir separator; "/" on unix, "\\" on win32, ":" on MacOS, etc...
- *  Obviously, this isn't a function, but it IS a null-terminated string.
+ * The dir separator; '/' on unix, '\\' on win32, ":" on MacOS, etc...
+ *  Obviously, this isn't a function. If you need more than one char for this,
+ *  you'll need to pull some old pieces of PhysicsFS out of revision control.
  */
-extern const char *__PHYSFS_platformDirSeparator;
-
+#if (((defined _WIN32) || (defined _WIN64)) && (!defined __CYGWIN__))
+#define __PHYSFS_platformDirSeparator '\\'
+#else
+#define __PHYSFS_platformDirSeparator '/'
+#endif
 
 /*
  * Initialize the platform. This is called when PHYSFS_init() is called from

+ 0 - 4
src/platform_posix.c

@@ -30,10 +30,6 @@
 
 #include "physfs_internal.h"
 
-
-const char *__PHYSFS_platformDirSeparator = "/";
-
-
 char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
 {
     const char *envr = getenv(varname);

+ 0 - 1
src/platform_windows.c

@@ -93,7 +93,6 @@ typedef struct
 } WinApiFile;
 
 
-const char *__PHYSFS_platformDirSeparator = "\\";
 static char *userDir = NULL;
 static HANDLE libUserEnv = NULL;
 static HANDLE detectCDThreadHandle = NULL;