PHYSFS_setSaneConfig uses enum callbacks now.

This commit is contained in:
Ryan C. Gordon 2017-08-13 19:45:31 -04:00
parent 0d61295781
commit 1364f6a915
1 changed files with 51 additions and 30 deletions

View File

@ -1858,27 +1858,53 @@ void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
} /* PHYSFS_getSearchPathCallback */ } /* PHYSFS_getSearchPathCallback */
/* Split out to avoid stack allocation in a loop. */ typedef struct setSaneCfgEnumData
static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
int archivesFirst)
{ {
const char *d = PHYSFS_getRealDir(i); const char *archiveExt;
const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1; size_t archiveExtLen;
char *str = (char *) __PHYSFS_smallAlloc(allocsize); int archivesFirst;
if (str != NULL) PHYSFS_ErrorCode errcode;
} setSaneCfgEnumData;
static int setSaneCfgEnumCallback(void *_data, const char *dir, const char *f)
{
setSaneCfgEnumData *data = (setSaneCfgEnumData *) _data;
const size_t extlen = data->archiveExtLen;
const size_t l = strlen(f);
const char *ext;
if ((l > extlen) && (f[l - extlen - 1] == '.'))
{ {
snprintf(str, allocsize, "%s%s%s", d, dirsep, i); ext = f + (l - extlen);
PHYSFS_mount(str, NULL, archivesFirst == 0); if (PHYSFS_utf8stricmp(ext, data->archiveExt) == 0)
__PHYSFS_smallFree(str); {
const char dirsep = __PHYSFS_platformDirSeparator;
const char *d = PHYSFS_getRealDir(f);
const size_t allocsize = strlen(d) + l + 2;
char *str = (char *) __PHYSFS_smallAlloc(allocsize);
if (str == NULL)
data->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
else
{
snprintf(str, allocsize, "%s%c%s", d, dirsep, f);
if (!PHYSFS_mount(str, NULL, data->archivesFirst == 0))
data->errcode = currentErrorCode();
__PHYSFS_smallFree(str);
} /* else */
} /* if */
} /* if */ } /* if */
} /* setSaneCfgAddPath */
/* !!! FIXME: if we want to abort on errors... */
/* return (data->errcode != PHYSFS_ERR_OK) ? -1 : 1; */
return 1; /* keep going */
} /* setSaneCfgEnumCallback */
int PHYSFS_setSaneConfig(const char *organization, const char *appName, int PHYSFS_setSaneConfig(const char *organization, const char *appName,
const char *archiveExt, int includeCdRoms, const char *archiveExt, int includeCdRoms,
int archivesFirst) int archivesFirst)
{ {
const char *dirsep = PHYSFS_getDirSeparator();
const char *basedir; const char *basedir;
const char *prefdir; const char *prefdir;
@ -1892,7 +1918,7 @@ int PHYSFS_setSaneConfig(const char *organization, const char *appName,
BAIL_IF(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0); BAIL_IF(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
/* !!! FIXME-3.0: these can fail... */ /* !!! FIXME: these can fail and we should report that... */
/* Put write dir first in search path... */ /* Put write dir first in search path... */
PHYSFS_mount(prefdir, NULL, 0); PHYSFS_mount(prefdir, NULL, 0);
@ -1913,24 +1939,19 @@ int PHYSFS_setSaneConfig(const char *organization, const char *appName,
/* Root out archives, and add them to search path... */ /* Root out archives, and add them to search path... */
if (archiveExt != NULL) if (archiveExt != NULL)
{ {
/* !!! FIXME-3.0: turn this into a callback */ setSaneCfgEnumData data;
char **rc = PHYSFS_enumerateFiles("/"); memset(&data, '\0', sizeof (data));
char **i; data.archiveExt = archiveExt;
size_t extlen = strlen(archiveExt); data.archiveExtLen = strlen(archiveExt);
char *ext; data.archivesFirst = archivesFirst;
data.errcode = PHYSFS_ERR_OK;
for (i = rc; *i != NULL; i++) if (!PHYSFS_enumerate("/", setSaneCfgEnumCallback, &data))
{ {
size_t l = strlen(*i); /* !!! FIXME: use this if we're reporting errors.
if ((l > extlen) && ((*i)[l - extlen - 1] == '.')) PHYSFS_ErrorCode errcode = currentErrorCode();
{ if (errcode == PHYSFS_ERR_APP_CALLBACK)
ext = (*i) + (l - extlen); errcode = data->errcode; */
if (PHYSFS_utf8stricmp(ext, archiveExt) == 0) } /* if */
setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
} /* if */
} /* for */
PHYSFS_freeList(rc);
} /* if */ } /* if */
return 1; return 1;