Skip to content

Commit

Permalink
Removed __PHYSFS_platformCvtToDependent().
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
icculus committed Mar 15, 2012
1 parent 03dbc3f commit be4ae4d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 82 deletions.
57 changes: 43 additions & 14 deletions src/archiver_dir.c
Expand Up @@ -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;
Expand Down Expand Up @@ -41,34 +66,35 @@ static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
} /* 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 */


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)
{
Expand All @@ -80,7 +106,7 @@ static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
*fileExists = 1;
} /* else */

allocator.Free(f);
__PHYSFS_smallFree(f);

return io;
} /* doOpen */
Expand All @@ -106,24 +132,26 @@ static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)

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

Expand All @@ -137,12 +165,13 @@ static void DIR_dirClose(dvoid *opaque)
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 */

Expand Down
1 change: 1 addition & 0 deletions src/physfs.c
Expand Up @@ -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"
Expand Down
14 changes: 0 additions & 14 deletions src/physfs_internal.h
Expand Up @@ -1243,20 +1243,6 @@ char *__PHYSFS_platformGetUserDir(void);
*/
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
Expand Down
28 changes: 0 additions & 28 deletions src/platform_posix.c
Expand Up @@ -113,34 +113,6 @@ char *__PHYSFS_platformGetUserDir(void)
} /* __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,
Expand Down
26 changes: 0 additions & 26 deletions src/platform_windows.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -467,28 +463,6 @@ static int isSymlinkAttrs(const DWORD attr, const DWORD tag)
} /* 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,
Expand Down

0 comments on commit be4ae4d

Please sign in to comment.