Make __PHYSFS_platformDirSeparator into a single char.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 15 Mar 2012 01:54:57 -0400
changeset 1225 671d4ea47c13
parent 1224 51a6cd2ad280
child 1226 3ea51c799aba
Make __PHYSFS_platformDirSeparator into a single char. This multichar thing was always stupid. Pull it out of revision control if you ever need it.
src/archiver_dir.c
src/physfs.c
src/physfs_internal.h
src/platform_posix.c
src/platform_windows.c
--- a/src/archiver_dir.c	Thu Mar 15 00:40:00 2012 -0400
+++ b/src/archiver_dir.c	Thu Mar 15 01:54:57 2012 -0400
@@ -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 @@
     retval = allocator.Malloc(namelen + seplen + 1);
     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
 
+    strcpy(retval, name);
+
     /* 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);
+    if (retval[namelen - 1] != dirsep)
+    {
+        retval[namelen] = dirsep;
+        retval[namelen + 1] = '\0';
+    } /* if */
 
     return retval;
 } /* DIR_openArchive */
--- a/src/physfs.c	Thu Mar 15 00:40:00 2012 -0400
+++ b/src/physfs.c	Thu Mar 15 01:54:57 2012 -0400
@@ -1052,17 +1052,16 @@
     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";
-
-        retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
-                                           strlen(dirsep) + 6);
-
+        const size_t len = strlen(baseDir) + strlen(str) + 7;
+
+        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 @@
     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;
-
-    if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
+    const char dirsep = __PHYSFS_platformDirSeparator;
+    char *ptr = *dir;
+    const size_t len = strlen(ptr);
+
+    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 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 @@
     /* 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 @@
 
 const char *PHYSFS_getDirSeparator(void)
 {
-    return __PHYSFS_platformDirSeparator;
+    static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
+    return retval;
 } /* PHYSFS_getDirSeparator */
 
 
--- a/src/physfs_internal.h	Thu Mar 15 00:40:00 2012 -0400
+++ b/src/physfs_internal.h	Thu Mar 15 01:54:57 2012 -0400
@@ -1031,11 +1031,15 @@
 
 
 /*
- * 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
--- a/src/platform_posix.c	Thu Mar 15 00:40:00 2012 -0400
+++ b/src/platform_posix.c	Thu Mar 15 01:54:57 2012 -0400
@@ -30,10 +30,6 @@
 
 #include "physfs_internal.h"
 
-
-const char *__PHYSFS_platformDirSeparator = "/";
-
-
 char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
 {
     const char *envr = getenv(varname);
--- a/src/platform_windows.c	Thu Mar 15 00:40:00 2012 -0400
+++ b/src/platform_windows.c	Thu Mar 15 01:54:57 2012 -0400
@@ -93,7 +93,6 @@
 } WinApiFile;
 
 
-const char *__PHYSFS_platformDirSeparator = "\\";
 static char *userDir = NULL;
 static HANDLE libUserEnv = NULL;
 static HANDLE detectCDThreadHandle = NULL;