I'm trying to recursively scan directories, and store the files found in a linked list. But, it's not working. When running the program,(Command-line program using a DLL, which is using a statically linked library, which has this directory scanning code.)
it works fine. But, it doesn't work correctly when debuggging, with wxDev-Cpp/gdb. When scanning the working directory, it skips the directory which contains the plugin, and that's what it's scanning for.(Possibly other directories also)
And when I scan the plugin's directory, it doesn't find any files/directories at all.
Does anybody knows of any directory scanning code I could use? My code recursively scans directories and stores the found files in a linked list. Is there any code I could use somewhere on Internet that I can use?
My code uses dirent.(I'm using Windows and some dirent for Windows code)
Any other code suggested which doesn't use dirent should be easily ported to dirent. The DLLIMPORT define doesn't do anything right now, as this code is in a staticly linked library, but later it will export the functions when the option of compiling this code as module/dll is added.
And for those of you interested in Wmb Asm, click
here.
dirscan.h
/*
Wmb Asm, the SDK, and all software in the Wmb Asm package are licensed under the MIT license:
Copyright (c) 2008 yellowstar
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the “Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef _H_DIRSCAN
#define _H_DIRSCAN
#ifdef __cplusplus
extern "C" {
#endif
typedef struct TFILE_LIST
{
char *filename;
TFILE_LIST *next;
} __attribute__ ((__packed__)) FILE_LIST;
FILE_LIST *ScanDirectory(FILE_LIST *filelist, char *dirname, char *ext);
void FreeDirectory(FILE_LIST *filelist);
#ifdef __cplusplus
}
#endif
#endif
dirscan.c
/*
Wmb Asm, the SDK, and all software in the Wmb Asm package are licensed under the MIT license:
Copyright (c) 2008 yellowstar
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the “Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#define BUILDING_SDK
#include "..\include\wmb_asm_sdk.h"
inline void AllocDir(FILE_LIST *files)
{
files->filename = (char*)malloc(256);
strcpy(files->filename,"");
files->next = (FILE_LIST*)malloc(sizeof(FILE_LIST));
memset(files->next,0,sizeof(FILE_LIST));
}
DLLIMPORT FILE_LIST *ScanDirectory(FILE_LIST *filelist, char *dirname, char *ext = NULL)
{
FILE_LIST *files=NULL;
FILE_LIST *cur_files=NULL;
DIR *dir = NULL;
dirent *ent = NULL;
char *DirName = (char*)malloc(256);
strcpy(DirName,dirname);
if(filelist!=NULL)
{
files = filelist;
while(files->next!=NULL)
{
files = files->next;
}
if(files->filename!=NULL && files->next!=NULL)files = files->next;
}
cur_files = files;
FILE *f = fopen(DirName,"rb");
if(f!=NULL)
{
printf("FOUND BA %s\n",DirName);
if(ext!=NULL)
{
if(strcmp((const char*)&ent->d_name[strlen(ent->d_name)-4],(const char*)ext)!=0)
{
return files;
}
}
if(cur_files==NULL)
{
cur_files = (FILE_LIST*)malloc(sizeof(FILE_LIST));
memset(cur_files,0,sizeof(FILE_LIST));
}
if(cur_files->filename==NULL)
AllocDir(cur_files);
fclose(f);
strcpy(cur_files->filename,DirName);
printf("FOUND %s\n",cur_files->filename);
return files;
}
dir=opendir(DirName);
if(dir==NULL)
{
return NULL;
}
else
{
printf("Scanning directory %s\n",DirName);
}
char c = DirName[strlen(DirName)];
if(c!='/' && c!='\\')
{
sprintf(DirName,"%s/",DirName);
}
char *str;
str = (char*)malloc(256);
strcpy(str,"");
int skip=0;
if(files==NULL)
{
if(files==NULL)
{
files = (FILE_LIST*)malloc(sizeof(FILE_LIST));
memset(files,0,sizeof(FILE_LIST));
}
AllocDir(files);
cur_files = files;
}
ent=(dirent*)1;
while(ent!=NULL)
{
ent = readdir(dir);
if(ent==NULL)break;
if(skip<2)
{
skip++;
continue;
}
else
{
sprintf(str,"%s%s",DirName,ent->d_name);
f = fopen(str,"rb");
if(f==NULL)
{
char *Str = (char*)malloc(256);
strcpy(Str,str);
if(ScanDirectory(cur_files,Str,ext)==NULL)
{
printf("Failed to open file or directory %s\n",Str);
}
else
{
while(cur_files->next!=NULL)
{
cur_files = cur_files->next;
}
if(cur_files->filename!=NULL && cur_files->next!=NULL)cur_files = cur_files->next;
}
free(Str);
}
else
{
fclose(f);
printf("FOUND A %s\n",ent->d_name);
if(ext!=NULL)
{
if(strcmp((const char*)&ent->d_name[strlen(ent->d_name)-4],(const char*)ext)!=0)
{
continue;
}
}
if(cur_files->filename==NULL)
AllocDir(cur_files);
strcpy(cur_files->filename,str);
printf("FOUND B %s dir %s\n",ent->d_name, cur_files->filename);
cur_files = cur_files->next;
}
}
}
closedir(dir);
free(str);
free(DirName);
return files;
}
DLLIMPORT void FreeDirectory(FILE_LIST *filelist)
{
FILE_LIST *files = filelist;
FILE_LIST *next_file = files;
files=next_file;
while(files!=NULL)
{
next_file = files->next;
free(files->filename);
free(files);
files = next_file;
}
}
[Edited by - yellowstar on July 23, 2008 4:02:46 PM]