Directory scanning...(SOLVED)

Started by
3 comments, last by yellowstar 15 years, 9 months ago
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]
My Website:link(Not hosted by Gamedev)
Advertisement
Quote:Original post by yellowstar
Does anybody knows of any directory scanning code I could use?


boost::filesystem

Does that work with DS homebrew? This software I'm working on is for mainly PC, but it needs to work on DS, too.(I had troubles just compiling that last time I tried, not sure what problems I was having with it... It might have been confusing instructions, I don't remember.)
My Website:link(Not hosted by Gamedev)
I found this example code via Google, but I'm not sure how helpful it will be:
Directory listing (C, Windows)

I presume you are using devkitPRO/ARM for your NDS development? Be aware that devkitPRO is closer to *nix like systems in functionality (as it uses newlib) than Windows, but is missing many useful functions available in *nix. The devs have removed almost everything from newlib related to file system access as it's presumed such a thing can't exist anyway... which is where libfat is useful for accessing the sd card in the various backup devices available.

Edit: Forgot to mention: I use ftw.h (file tree walk) under Linux to recurse through directories; check if Windows has a similar function.

[Edited by - DrTwox on July 20, 2008 4:20:00 AM]
I have solved this problem. There seems to be a bug in wxDev-Cpp/Dev-Cpp: When debugging, for some reason, instead of setting the working directory to the application's directory,(This is when running from a DLL project)
it was setting it to the DLL's directory. So only the files and directories in the DLL's directory was being scanned. When running normally the working directory would be set to the program's directory. I tried debugging as the program, and it worked.
My Website:link(Not hosted by Gamedev)

This topic is closed to new replies.

Advertisement