Removed __PHYSFS_platformCvtToDependent().
authorRyan C. Gordon <icculus@icculus.org>
Thu, 15 Mar 2012 01:58:39 -0400
changeset 1226 3ea51c799aba
parent 1225 671d4ea47c13
child 1227 b5c017759a1f
Removed __PHYSFS_platformCvtToDependent(). It's only used by archive_dir.c, and it's trivial to make it into platform-independent code for that one module. Bonus: less malloc() pressure...now every access to the native filesystem doesn't require a temporary heap allocation.
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 01:54:57 2012 -0400
+++ b/src/archiver_dir.c	Thu Mar 15 01:58:39 2012 -0400
@@ -11,6 +11,31 @@
 
 /* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
 
+
+
+static char *cvtToDependent(const char *prepend, const char *path, char *buf)
+{
+    BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
+    sprintf(buf, "%s%s", prepend ? prepend : "", path);
+
+    if (__PHYSFS_platformDirSeparator != '/')
+    {
+        char *p;
+        for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/'))
+            *p = __PHYSFS_platformDirSeparator;
+    } /* if */
+
+    return buf;
+} /* cvtToDependent */
+
+
+#define CVT_TO_DEPENDENT(buf, pre, dir) { \
+    const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \
+    buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len)); \
+}
+
+
+
 static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
 {
     PHYSFS_Stat statbuf;
@@ -41,18 +66,18 @@
 } /* DIR_openArchive */
 
 
-/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
-
 static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
                                int omitSymLinks, PHYSFS_EnumFilesCallback cb,
                                const char *origdir, void *callbackdata)
 {
-    char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
+    char *d;
+
+    CVT_TO_DEPENDENT(d, opaque, dname);
     if (d != NULL)
     {
         __PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
                                         origdir, callbackdata);
-        allocator.Free(d);
+        __PHYSFS_smallFree(d);
     } /* if */
 } /* DIR_enumerateFiles */
 
@@ -60,15 +85,16 @@
 static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
                          const int mode, int *fileExists)
 {
-    char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
+    char *f;
     PHYSFS_Io *io = NULL;
     int existtmp = 0;
 
+    CVT_TO_DEPENDENT(f, opaque, name);
+    BAIL_IF_MACRO(f == NULL, NULL, NULL);
+
     if (fileExists == NULL)
         fileExists = &existtmp;
 
-    BAIL_IF_MACRO(f == NULL, NULL, NULL);
-
     io = __PHYSFS_createNativeIo(f, mode);
     if (io == NULL)
     {
@@ -80,7 +106,7 @@
         *fileExists = 1;
     } /* else */
 
-    allocator.Free(f);
+    __PHYSFS_smallFree(f);
 
     return io;
 } /* doOpen */
@@ -106,24 +132,26 @@
 
 static int DIR_remove(dvoid *opaque, const char *name)
 {
-    char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
     int retval;
+    char *f;
 
+    CVT_TO_DEPENDENT(f, opaque, name);
     BAIL_IF_MACRO(f == NULL, NULL, 0);
     retval = __PHYSFS_platformDelete(f);
-    allocator.Free(f);
+    __PHYSFS_smallFree(f);
     return retval;
 } /* DIR_remove */
 
 
 static int DIR_mkdir(dvoid *opaque, const char *name)
 {
-    char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
     int retval;
+    char *f;
 
+    CVT_TO_DEPENDENT(f, opaque, name);
     BAIL_IF_MACRO(f == NULL, NULL, 0);
     retval = __PHYSFS_platformMkDir(f);
-    allocator.Free(f);
+    __PHYSFS_smallFree(f);
     return retval;
 } /* DIR_mkdir */
 
@@ -137,12 +165,13 @@
 static int DIR_stat(dvoid *opaque, const char *name, int *exists,
                     PHYSFS_Stat *stat)
 {
-    char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
     int retval = 0;
+    char *d;
 
+    CVT_TO_DEPENDENT(d, opaque, name);
     BAIL_IF_MACRO(d == NULL, NULL, 0);
     retval = __PHYSFS_platformStat(d, exists, stat);
-    allocator.Free(d);
+    __PHYSFS_smallFree(d);
     return retval;
 } /* DIR_stat */
 
--- a/src/physfs.c	Thu Mar 15 01:54:57 2012 -0400
+++ b/src/physfs.c	Thu Mar 15 01:58:39 2012 -0400
@@ -9,6 +9,7 @@
  */
 
 /* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
+/* !!! FIXME: use snprintf(), not sprintf(). */
 
 #define __PHYSICSFS_INTERNAL__
 #include "physfs_internal.h"
--- a/src/physfs_internal.h	Thu Mar 15 01:54:57 2012 -0400
+++ b/src/physfs_internal.h	Thu Mar 15 01:58:39 2012 -0400
@@ -1243,20 +1243,6 @@
  */
 void *__PHYSFS_platformGetThreadID(void);
 
-/*
- * Convert (dirName) to platform-dependent notation, then prepend (prepend)
- *  and append (append) to the converted string.
- *
- *  So, on Win32, calling:
- *     __PHYSFS_platformCvtToDependent("C:\", "my/files", NULL);
- *  ...will return the string "C:\my\files".
- *
- * Be sure to allocator.Free() the return value when done with it.
- */
-char *__PHYSFS_platformCvtToDependent(const char *prepend,
-                                      const char *dirName,
-                                      const char *append);
-
 
 /*
  * Enumerate a directory of files. This follows the rules for the
--- a/src/platform_posix.c	Thu Mar 15 01:54:57 2012 -0400
+++ b/src/platform_posix.c	Thu Mar 15 01:58:39 2012 -0400
@@ -113,34 +113,6 @@
 } /* __PHYSFS_platformGetUserDir */
 
 
-char *__PHYSFS_platformCvtToDependent(const char *prepend,
-                                      const char *dirName,
-                                      const char *append)
-{
-    int len = ((prepend) ? strlen(prepend) : 0) +
-              ((append) ? strlen(append) : 0) +
-              strlen(dirName) + 1;
-    char *retval = (char *) allocator.Malloc(len);
-
-    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
-
-    /* platform-independent notation is Unix-style already.  :)  */
-
-    if (prepend)
-        strcpy(retval, prepend);
-    else
-        retval[0] = '\0';
-
-    strcat(retval, dirName);
-
-    if (append)
-        strcat(retval, append);
-
-    return retval;
-} /* __PHYSFS_platformCvtToDependent */
-
-
-
 void __PHYSFS_platformEnumerateFiles(const char *dirname,
                                      int omitSymLinks,
                                      PHYSFS_EnumFilesCallback callback,
--- a/src/platform_windows.c	Thu Mar 15 01:54:57 2012 -0400
+++ b/src/platform_windows.c	Thu Mar 15 01:58:39 2012 -0400
@@ -54,10 +54,6 @@
     } \
 } \
 
-#ifndef _MSC_VER
-#define _snprintf snprintf
-#endif
-
 /* !!! FIXME: this is wrong for UTF-16. */
 static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
 {
@@ -467,28 +463,6 @@
 } /* isSymlinkAttrs */
 
 
-char *__PHYSFS_platformCvtToDependent(const char *prepend,
-                                      const char *dirName,
-                                      const char *append)
-{
-    const size_t len = ((prepend) ? strlen(prepend) : 0) +
-              ((append) ? strlen(append) : 0) +
-              strlen(dirName) + 1;
-    char *retval = (char *) allocator.Malloc(len);
-    char *p;
-
-    BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
-
-    _snprintf(retval, len, "%s%s%s",
-              prepend ? prepend : "", dirName, append ? append : "");
-
-    for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
-        *p = '\\';
-
-    return retval;
-} /* __PHYSFS_platformCvtToDependent */
-
-
 void __PHYSFS_platformEnumerateFiles(const char *dirname,
                                      int omitSymLinks,
                                      PHYSFS_EnumFilesCallback callback,