Factor out different {clear,get}filenames definitions into fnlist_t + functions.

Don't actually replace the instances in the code now.
Additions in common.h:
 - fnlist_t, which combines CACHE1D_FIND_REC *finddirs, *findfiles and
   int32_t numdirs, numfiles
 - the FNLIST_INITIALIZER macro, which MUST be used for automatic variables
 - fnlist_clearnames, fnlist_getnames functions
 - G_LoadGroupsInDir, G_DoAutoload, two often-occurring uses of these

git-svn-id: https://svn.eduke32.com/eduke32@2555 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-03-28 19:41:57 +00:00
parent d77e388522
commit a7f6f3bacc
2 changed files with 87 additions and 0 deletions

View file

@ -8,6 +8,7 @@
#define EDUKE32_COMMON_H_
#include "scriptfile.h"
#include "cache1d.h"
//// TYPES
@ -24,6 +25,15 @@ typedef struct
}
tokenlist;
typedef struct
{
CACHE1D_FIND_REC *finddirs, *findfiles;
int32_t numdirs, numfiles;
}
fnlist_t;
#define FNLIST_INITIALIZER { NULL, NULL, 0, 0 }
enum
{
T_EOF = -2,
@ -43,4 +53,11 @@ int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens);
int32_t check_file_exist(const char *fn);
void fnlist_clearnames(fnlist_t *fnl);
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
int32_t dirflags, int32_t fileflags);
void G_LoadGroupsInDir(const char *dirname);
void G_DoAutoload(const char *dirname);
#endif

View file

@ -97,3 +97,73 @@ int32_t check_file_exist(const char *fn)
return 0;
}
//// FILE NAME / DIRECTORY LISTS ////
void fnlist_clearnames(fnlist_t *fnl)
{
klistfree(fnl->finddirs);
klistfree(fnl->findfiles);
fnl->finddirs = fnl->findfiles = NULL;
fnl->numfiles = fnl->numdirs = 0;
}
// dirflags, fileflags:
// -1 means "don't get dirs/files",
// otherwise ORed to flags for respective klistpath
int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
int32_t dirflags, int32_t fileflags)
{
CACHE1D_FIND_REC *r;
fnlist_clearnames(fnl);
if (dirflags != -1)
fnl->finddirs = klistpath(dirname, "*", CACHE1D_FIND_DIR|dirflags);
if (fileflags != -1)
fnl->findfiles = klistpath(dirname, pattern, CACHE1D_FIND_FILE|fileflags);
for (r=fnl->finddirs; r; r=r->next)
fnl->numdirs++;
for (r=fnl->findfiles; r; r=r->next)
fnl->numfiles++;
return(0);
}
// loads all group (grp, zip, pk3) files in the given directory
void G_LoadGroupsInDir(const char *dirname)
{
static const char *extensions[3] = { "*.grp", "*.zip", "*.pk3" };
char buf[BMAX_PATH];
int32_t i;
fnlist_t fnlist = FNLIST_INITIALIZER;
for (i=0; i<3; i++)
{
CACHE1D_FIND_REC *rec;
fnlist_getnames(&fnlist, dirname, extensions[i], -1, 0);
for (rec=fnlist.findfiles; rec; rec=rec->next)
{
Bsnprintf(buf, sizeof(buf), "%s/%s", dirname, rec->name);
initprintf("Using group file \"%s\".\n", buf);
initgroupfile(buf);
}
fnlist_clearnames(&fnlist);
}
}
void G_DoAutoload(const char *dirname)
{
char buf[BMAX_PATH];
Bsnprintf(buf, sizeof(buf), "autoload/%s", dirname);
G_LoadGroupsInDir(buf);
}