gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > trying to get all the files

#110725 - Lick - Fri Dec 01, 2006 12:15 am

Code:
void getFileListRecursive(char *filter) {
   freeFileList();
    u32 depth = 0;
   
   int type = FAT_FindFirstFileLFN(fileName);
   
   while(1)
    {       
        if(type == FT_NONE)
        {
            if(depth == 0)
                return;
            else
            {
                FAT_chdir("..");
                depth--;
                type = FAT_FindNextFileLFN(fileName);
                continue;
            }
        }
        else if(type == FT_DIR)
        {
            strcat(fileName, "/");
            if(FAT_chdir(fileName))
            {
                depth++;
                type = FAT_FindFirstFileLFN(fileName);
                continue;
            }
        }
        else if(type == FT_FILE)
        {
          addFile(type, fileName, filter);
        }
       
        type = FAT_FindNextFileLFN(fileName);
   }
}



This code somehow hangs. Anyone see anything weird about it?
_________________
http://licklick.wordpress.com

#110734 - gmiller - Fri Dec 01, 2006 1:15 am

Is "filter" somehow used to get the "filename"? Is there some init function that needs to be called with "filter" to set the selection mask?

#110737 - josath - Fri Dec 01, 2006 2:25 am

perhaps FindFile stuff gets reset after a chdir?

So say you read files in this order:

/blah.txt -> addFile
/someDir/ -> chdir(someDir)
/someDir/another.txt -> addfile
NONE -> chdir(..)

at this point, it will start listing the files in / again, from the begining?

#110764 - Mighty Max - Fri Dec 01, 2006 10:44 am

Code:

                FAT_chdir("..");
                depth--;
                type = FAT_FindNextFileLFN(fileName);
                continue;


This code will not resume searching from where you left the directory.
You have to use FindFirstFileLFN again and iterate through until you come to the point where you left the dir and continue from there.
_________________
GBAMP Multiboot

#110776 - chishm - Fri Dec 01, 2006 1:47 pm

Yet another reason I rewrote gba_nds_fat to libfat. FindFirstFile and FindNextFile are not reentrant. Worst still, calling any other function which works with file or directory names (chdir, fopen) will overwrite te FindNextFile data.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#110806 - tepples - Fri Dec 01, 2006 8:52 pm

So how do I take advantage of it? If it requires compiling devkitARM pre-R20 from CVS, do you have a step-by-step guide to doing this on Microsoft Windows?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#110829 - Lick - Sat Dec 02, 2006 12:32 am

Code:
void getFileListRecursive(char *filter) {
//---------------------------------------------------------------------------------
   freeFileList();
    u32 depth = 0;
    u32 position[32] = {0};
   
   int type = FAT_FindFirstFile(fileName);
   
   while(1)
    {       
        if(type == FT_NONE)
        {
            if(depth == 0)
                return;
            else
            {
                FAT_chdir("..");
                depth--;
                type = FAT_FindFirstFile(fileName);
                for(u32 i=0; i<position[depth]; i++)
                    type = FAT_FindNextFile(fileName);
                continue;
            }
        }
        else if(type == FT_DIR)
        {
            position[depth]++;
            FAT_chdir(fileName);
            depth++;
            type = FAT_FindFirstFile(fileName);
            position[depth] = 0;
            continue;
        }
        else if(type == FT_FILE)
        {
            position[depth]++;
          addFile(type, fileName, filter);
        }
       
        type = FAT_FindNextFile(fileName);
   }
}



I added (unoptimized) position memorization, but it's still hanging. Why?!
_________________
http://licklick.wordpress.com

#110832 - Lick - Sat Dec 02, 2006 12:45 am

Code:
//---------------------------------------------------------------------------------
// Returns all FILES that include filter
void getFileListRecursive(char *filter) {
//---------------------------------------------------------------------------------
   freeFileList();
    u32 depth = 0;
    u32 position[32] = {0};
   
   int type = FAT_FindFirstFile(fileName);
   
   while(1)
    {       
        if(type == FT_NONE)
        {
            if(depth == 0)
                return;
            else
            {
                FAT_chdir("..");
                depth--;
                type = FAT_FindFirstFile(fileName);
                for(u32 i=0; i<position[depth]; i++)
                    type = FAT_FindNextFile(fileName);
                continue;
            }
        }
        else if(type == FT_DIR)
        {
            position[depth]++;
            if(strcmp(fileName, ".")!=0 && strcmp(fileName, "..")!=0)
            {               
                FAT_chdir(fileName);
                depth++;
                type = FAT_FindFirstFile(fileName);
                position[depth] = 0;
                continue;
            }
        }
        else if(type == FT_FILE)
        {           
            position[depth]++;
          addFile(type, fileName, filter);
        }
       
        type = FAT_FindNextFile(fileName);
   }
}


It works! Thanks to davr giving me advice on printing the results instead of just taking wild guesses, I found that the code hang at "." and ".."! I didn't take those stupid relative directories into account when debugging. BLEEEGH!!
_________________
http://licklick.wordpress.com