Skip to content

Commit

Permalink
Make __PHYSFS_platformDirSeparator into a single char.
Browse files Browse the repository at this point in the history
This multichar thing was always stupid. Pull it out of revision control if
 you ever need it.
  • Loading branch information
icculus committed Mar 15, 2012
1 parent aaf8868 commit 03dbc3f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
15 changes: 9 additions & 6 deletions src/archiver_dir.c
Expand Up @@ -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. */
Expand All @@ -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 */
Expand Down
52 changes: 21 additions & 31 deletions src/physfs.c
Expand Up @@ -1052,50 +1052,55 @@ 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 */

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 */


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. */
Expand All @@ -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);
Expand Down Expand Up @@ -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 */


Expand Down
12 changes: 8 additions & 4 deletions src/physfs_internal.h
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/platform_posix.c
Expand Up @@ -30,10 +30,6 @@

#include "physfs_internal.h"


const char *__PHYSFS_platformDirSeparator = "/";


char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname)
{
const char *envr = getenv(varname);
Expand Down
1 change: 0 additions & 1 deletion src/platform_windows.c
Expand Up @@ -93,7 +93,6 @@ typedef struct
} WinApiFile;


const char *__PHYSFS_platformDirSeparator = "\\";
static char *userDir = NULL;
static HANDLE libUserEnv = NULL;
static HANDLE detectCDThreadHandle = NULL;
Expand Down

0 comments on commit 03dbc3f

Please sign in to comment.