Added prefix filtering to GfDirGetListFiltered
git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@2589 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 948d6d8e8fa4d49f76975d910074c3f03cac6370 Former-commit-id: bd79fcfa7831c9b58eb7924c7de4a8b1dd236ed3
This commit is contained in:
parent
fede6877a2
commit
b45bffb20f
5 changed files with 47 additions and 56 deletions
|
@ -54,10 +54,10 @@ tFList * GfDirGetList(const char *dir)
|
|||
@param dir directory name
|
||||
@return The list of files
|
||||
*/
|
||||
tFList * GfDirGetListFiltered(const char *dir, const char *suffix)
|
||||
tFList * GfDirGetListFiltered(const char *dir, const char *prefix, const char *suffix)
|
||||
{
|
||||
if (GfOs.dirGetListFiltered) {
|
||||
return GfOs.dirGetListFiltered(dir, suffix);
|
||||
return GfOs.dirGetListFiltered(dir, prefix, suffix);
|
||||
} else {
|
||||
return (tFList*)NULL;
|
||||
}
|
||||
|
@ -69,23 +69,21 @@ tFList * GfDirGetListFiltered(const char *dir, const char *suffix)
|
|||
@param freeUserData User function used to free the user data
|
||||
@return none
|
||||
*/
|
||||
void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freename, bool freedispname)
|
||||
void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freeName, bool freeDispName)
|
||||
{
|
||||
//tFList *cur;
|
||||
|
||||
if (list) {
|
||||
// The list contains at least one element, checked above.
|
||||
tFList *rl = list;
|
||||
do {
|
||||
tFList *tmp = rl;
|
||||
rl = rl->next;
|
||||
if ((freeUserData) && (tmp->userData)) {
|
||||
if (freeUserData && tmp->userData) {
|
||||
freeUserData(tmp->userData);
|
||||
}
|
||||
if (freename) {
|
||||
if (freeName) {
|
||||
freez(tmp->name);
|
||||
}
|
||||
if (freedispname) {
|
||||
if (freeDispName) {
|
||||
freez(tmp->dispName);
|
||||
}
|
||||
free(tmp);
|
||||
|
@ -93,26 +91,5 @@ void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freename,
|
|||
}
|
||||
|
||||
list = NULL;
|
||||
|
||||
|
||||
/*
|
||||
while (list) {
|
||||
if (list->next == list) {
|
||||
if ((freeUserData) && (list->userData)) {
|
||||
freeUserData(list->userData);
|
||||
}
|
||||
free(list);
|
||||
list = NULL;
|
||||
} else {
|
||||
cur = list->next;
|
||||
list->next = cur->next;
|
||||
cur->next->prev = list;
|
||||
if ((freeUserData) && (cur->userData)) {
|
||||
freeUserData(cur->userData);
|
||||
}
|
||||
free(cur);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ typedef int (*tfModFreeInfoList)(tModList **);
|
|||
|
||||
/* directory interface */
|
||||
typedef tFList *(*tfDirGetList)(const char *);
|
||||
typedef tFList *(*tfDirGetListFiltered)(const char *, const char *);
|
||||
typedef tFList *(*tfDirGetListFiltered)(const char *, const char *, const char *);
|
||||
|
||||
/* time interface */
|
||||
typedef double (*tfTimeClock)(void);
|
||||
|
|
|
@ -240,7 +240,7 @@ TGF_API int GfModFreeInfoList(tModList **modlist);
|
|||
* Directory management *
|
||||
************************/
|
||||
|
||||
/** List of (DLL) files for a Directory
|
||||
/** List of files for a Directory
|
||||
@see GfDirGetList
|
||||
*/
|
||||
typedef struct FList
|
||||
|
@ -253,9 +253,9 @@ typedef struct FList
|
|||
} tFList;
|
||||
|
||||
TGF_API tFList *GfDirGetList(const char *dir);
|
||||
TGF_API tFList *GfDirGetListFiltered(const char *dir, const char *suffix);
|
||||
TGF_API tFList *GfDirGetListFiltered(const char *dir, const char *prefix, const char *suffix);
|
||||
typedef void (*tfDirfreeUserData)(void*); /**< Function to call for releasing the user data associated with file entry */
|
||||
TGF_API void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserDatabool, bool freename = false, bool freedispname = false);
|
||||
TGF_API void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freeName = false, bool freeDispName = false);
|
||||
|
||||
|
||||
/**********************************
|
||||
|
|
|
@ -510,20 +510,20 @@ linuxDirGetList(const char *dir)
|
|||
* list of directory entries
|
||||
*/
|
||||
static tFList *
|
||||
linuxDirGetListFiltered(const char *dir, const char *suffix)
|
||||
linuxDirGetListFiltered(const char *dir, const char *prefix, const char *suffix)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *ep;
|
||||
tFList *flist = (tFList*)NULL;
|
||||
tFList *curf;
|
||||
int suffixLg;
|
||||
int prefixLg, suffixLg;
|
||||
int fnameLg;
|
||||
|
||||
if ((suffix == NULL) || (strlen(suffix) == 0)) {
|
||||
if ((!prefix || strlen(prefix) == 0) && (!suffix || strlen(suffix) == 0))
|
||||
return linuxDirGetList(dir);
|
||||
}
|
||||
|
||||
suffixLg = strlen(suffix);
|
||||
suffixLg = suffix ? strlen(suffix) : 0;
|
||||
prefixLg = prefix ? strlen(prefix) : 0;
|
||||
|
||||
/* open the current directory */
|
||||
dp = opendir(dir);
|
||||
|
@ -531,9 +531,14 @@ linuxDirGetListFiltered(const char *dir, const char *suffix)
|
|||
/* some files in it */
|
||||
while ((ep = readdir(dp)) != 0) {
|
||||
fnameLg = strlen(ep->d_name);
|
||||
if ((fnameLg > suffixLg) && (strcmp(ep->d_name + fnameLg - suffixLg, suffix) == 0)) {
|
||||
if ((!prefix || (fnameLg > prefixLg
|
||||
&& strncmp(ep->d_name, prefix, prefixLg) == 0))
|
||||
&& (!suffix || (fnameLg > suffixLg
|
||||
&& strncmp(ep->d_name + fnameLg - suffixLg, suffix, suffixLg) == 0))) {
|
||||
curf = (tFList*)calloc(1, sizeof(tFList));
|
||||
curf->name = strdup(ep->d_name);
|
||||
curf->dispName = 0;
|
||||
curf->userData = 0;
|
||||
if (flist == (tFList*)NULL) {
|
||||
curf->next = curf;
|
||||
curf->prev = curf;
|
||||
|
|
|
@ -477,35 +477,42 @@ windowsDirGetList(const char *dir)
|
|||
* Get a list of entries in a directory
|
||||
*
|
||||
* Parameters
|
||||
* directory name
|
||||
* dir : directory name
|
||||
* prefix : file name prefix to match (may be NULL)
|
||||
* suffix : file name suffix to match (may be NULL)
|
||||
*
|
||||
* Return
|
||||
* list of directory entries
|
||||
*/
|
||||
static tFList *
|
||||
windowsDirGetListFiltered(const char *dir, const char *suffix)
|
||||
windowsDirGetListFiltered(const char *dir, const char *prefix, const char *suffix)
|
||||
{
|
||||
tFList *flist = NULL;
|
||||
tFList *curf;
|
||||
int suffixLg;
|
||||
int fnameLg;
|
||||
tFList *flist = NULL;
|
||||
tFList *curf;
|
||||
int prefixLg, suffixLg;
|
||||
int fnameLg;
|
||||
|
||||
if ((suffix == NULL) || (strlen(suffix) == 0))
|
||||
if ((!prefix || strlen(prefix) == 0) && (!suffix || strlen(suffix) == 0))
|
||||
return windowsDirGetList(dir);
|
||||
|
||||
suffixLg = strlen(suffix);
|
||||
suffixLg = suffix ? strlen(suffix) : 0;
|
||||
prefixLg = prefix ? strlen(prefix) : 0;
|
||||
|
||||
_finddata_t FData;
|
||||
char Dir_name[ 1024 ];
|
||||
sprintf( Dir_name, "%s\\*.*", dir );
|
||||
GfOut("trying dir %s\n",dir);
|
||||
long Dirent = _findfirst( Dir_name, &FData );
|
||||
if ( Dirent != -1 ) {
|
||||
char Dir_name[1024];
|
||||
sprintf(Dir_name, "%s\\*.*", dir);
|
||||
long Dirent = _findfirst(Dir_name, &FData);
|
||||
if (Dirent != -1) {
|
||||
do {
|
||||
fnameLg = strlen(FData.name);
|
||||
if ((fnameLg > suffixLg) && (strcmp(FData.name + fnameLg - suffixLg, suffix) == 0)) {
|
||||
if ((!prefix || (fnameLg > prefixLg
|
||||
&& strncmp(FData.name, prefix, prefixLg) == 0))
|
||||
&& (!suffix || (fnameLg > suffixLg
|
||||
&& strncmp(FData.name + fnameLg - suffixLg, suffix, suffixLg) == 0))) {
|
||||
curf = (tFList*)calloc(1, sizeof(tFList));
|
||||
curf->name = strdup(FData.name);
|
||||
curf->dispName = 0;
|
||||
curf->userData = 0;
|
||||
if (flist == (tFList*)NULL) {
|
||||
curf->next = curf;
|
||||
curf->prev = curf;
|
||||
|
@ -515,12 +522,14 @@ windowsDirGetListFiltered(const char *dir, const char *suffix)
|
|||
if (_stricmp(curf->name, flist->name) > 0) {
|
||||
do {
|
||||
flist = flist->next;
|
||||
} while ((stricmp(curf->name, flist->name) > 0) && (stricmp(flist->name, flist->prev->name) > 0));
|
||||
} while (stricmp(curf->name, flist->name) > 0
|
||||
&& stricmp(flist->name, flist->prev->name) > 0);
|
||||
flist = flist->prev;
|
||||
} else {
|
||||
do {
|
||||
flist = flist->prev;
|
||||
} while ((stricmp(curf->name, flist->name) < 0) && (stricmp(flist->name, flist->next->name) < 0));
|
||||
} while (stricmp(curf->name, flist->name) < 0
|
||||
&& stricmp(flist->name, flist->next->name) < 0);
|
||||
}
|
||||
curf->next = flist->next;
|
||||
flist->next = curf;
|
||||
|
@ -529,7 +538,7 @@ windowsDirGetListFiltered(const char *dir, const char *suffix)
|
|||
flist = curf;
|
||||
}
|
||||
}
|
||||
} while ( _findnext( Dirent, &FData ) != -1 );
|
||||
} while (_findnext( Dirent, &FData ) != -1);
|
||||
}
|
||||
|
||||
return flist;
|
||||
|
|
Loading…
Reference in a new issue