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 > Licks Locator()

#134314 - Lick - Thu Jul 12, 2007 1:13 pm

Download
Demo: http://lickr.org/files/LicksLocator.zip (Demo searches for "DSLiveWeather" directory. Maximum depth is 3: /<1>/<2>/DSLiveWeather. Includes a timer.)
C file: http://lickr.org/files/LicksLocator.c
Scrolldown for melw's modified & better version

Description
It's simply a C function that searches for a directory or a file inside a certain start-directory and within a certain depth. Only 1 result is returned, but that's enough for many purposes.

Performance
It's fast enough, as long as you don't have thousands of directories in the depth range. The demo includes a timer, so see for yourself.

Usage
Code:
Locator(StartDirectory, SearchString, IsADirectory, Depth, OutputBuffer);


Example
This example will search for the DSLiveWeather directory within 3 depth levels starting from the root. It will find one of these:
- DSLiveWeather
- /<1>/DSLiveWeather
- /<1>/<2>/DSLiveWeather
Code:
Locator("/", "DSLiveWeather", true, 3, path);

_________________
http://licklick.wordpress.com


Last edited by Lick on Fri Jul 13, 2007 9:42 am; edited 2 times in total

#134318 - Lick - Thu Jul 12, 2007 1:19 pm

Oh by the way, this will be in DSLiveWeather the next release. For all you "/data/<app>" freaks! ;)
_________________
http://licklick.wordpress.com

#134320 - felix123 - Thu Jul 12, 2007 1:37 pm

Thank you!
_________________
Nintendo DS homebrew on Wikipedia

#134321 - spinal_cord - Thu Jul 12, 2007 1:40 pm

So this means that we will no longer need to put our files in a specific place? cool.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#134327 - OOPMan - Thu Jul 12, 2007 2:06 pm

Only if people start using it. Still, it's simple enough and a maximum depth of 3 will probably for enough for most apps and freaks who pack their devices full of stuff...
_________________
"My boot, your face..." - Attributed to OOPMan, Emperor of Eroticon VI

You can find my NDS homebrew projects here...

#134348 - tondopie - Thu Jul 12, 2007 5:11 pm

this sounds like a nice idea. Hopefully more people will use it and we can avoid cluttering up the root.
_________________
Development Blog: http://teendev.org
Homebrew Podcast: http://homebrewcast.net
Web Design: http://xtendesign.net

#134378 - Lick - Thu Jul 12, 2007 9:25 pm

spinal_cord wrote:
So this means that we will no longer need to put our files in a specific place? cool.

Well, you still have to put your files within the depth range, but indeed, you get more freedom and flexibility in how you arrange your folders and move them around.
Although, since static naming is important to finding the right folder/file, you can't rename them.

OOPMan wrote:
Only if people start using it.

Always a problem isn't it..!
Quote:
Still, it's simple enough and a maximum depth of 3 will probably for enough for most apps and freaks who pack their devices full of stuff...

The maximum depth of 3 referred to the demo project. In reality the developer can decide how deep the application scans. I recommend 3 or 4 for the depth. I don't think people actually go that deep every time they want to start homebrew, that would take too much time.
_________________
http://licklick.wordpress.com

#134465 - melw - Fri Jul 13, 2007 9:34 am

Cheers! Added this to my own project space for loading external configuration files... I had to make a few changes to get it working, though: using non-case sensitive string comparison and not adding trailing slashes if we're searching for a file, not a directory.

Here's a slightly updated/modified version:

Code:
bool fileLocator(char *start, char *target, bool isDir, int depth, char *result)
{
   struct stat st;
   DIR_ITER *dir = diropen(start);
   static char child[256];
   char temp[256];
   
   if (dir)
   {
      while (dirnext(dir, child, &st) == 0)
      {
         if (strlen(child) == 1 && child[0] == '.')
            continue;
                  
         if (strlen(child) == 2 && child[0] == '.' && child[1] == '.')
            continue;
         
         if (((st.st_mode & S_IFDIR) && isDir) || (!(st.st_mode & S_IFDIR) && !isDir) )
         {
            if (strcasecmp(target, child) == 0) // changed from strcmp to strcasecmp
            {
               strcpy(result, start);
               if (start[strlen(start)-1] != '/')
                  strcat(result, "/");

               strcat(result, child);
               if(isDir) // only add trailing slash if we're searching for a directory
                  strcat(result, "/");
            
               dirclose(dir);
               return true;
            }
         }
            
         if ((st.st_mode & S_IFDIR) && depth > 1)
         {
            strcpy(temp, start);
            if (start[strlen(start)-1] != '/')
               strcat(temp, "/");

            strcat(temp, child);
            strcat(temp, "/");
            
            if (fileLocator(temp, target, isDir, depth-1, result))
            {
               dirclose(dir);
               return true;
            }
         }
      }
   }
   
   dirclose(dir);
   return false;
}

#134466 - Dwedit - Fri Jul 13, 2007 9:38 am

Is this breadth first or depth first?
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#134468 - Lick - Fri Jul 13, 2007 9:38 am

Great work! I'll use your version then. ;D

I believe it's depth first. Code-wise this is the easiest because it doesn't require storing the directories in a list first. It just goes into directories as it finds them. I don't think performance is an issue in any case.
_________________
http://licklick.wordpress.com

#145788 - Dark Knight ez - Thu Nov 22, 2007 3:01 pm

My appologies for resurrecting a dead topic, but I wanted to confirm this small function is indeed license free without any strings attached whatsoever. /being overly careful with other people's code
_________________
AmplituDS website

#145794 - Lick - Thu Nov 22, 2007 4:09 pm

Well yes, my initial version is public domain, provided as-is.
_________________
http://licklick.wordpress.com

#145802 - Rajveer - Thu Nov 22, 2007 6:29 pm

I wanted to thank Lick and Melw for the code but I didn't want to bring up an old topic. I'll take this chance to say thanks guys!