Advertisement

01.04.2005 at 06:23PM PST, ID: 21262051
[x]
Attachment Details

non-recursive directory searching

Asked by boodabelly in C++ Programming Language

Tags: recursive

I have the following code and I would like to remove the global variables by recursing the directories without recursive function calls (and avoid passing the parameters everytime in the recursive calls).  It is pretty basic win32 code and nothing serious, just playing around with my mp3 collection basically.

My collection is organized into a structure that will not change and I do not care enough to make this program flexible enough to accomodate anything else, this is just for fun.  I am also reading the mp3/ogg info into a data structure (but that code is not present, obviously)

data structure:
typedef struct song_tag
{
      string title;
      string artist;
      string album;
      string year;
      string comment;
      string track;
      string genre;

} SONG_TAG;

typedef struct mp3_song
{
      string title;
      SONG_TAG tag;

} MP3_SONG;

typedef struct mp3_album
{
      string title;
      MP3_SONG *p_next;

} MP3_ALBUM;

typedef struct mp3_band
{
      string title;
      MP3_ALBUM *p_next;

} MP3_BAND;

typedef struct mp3_collection
{
      string title;
      MP3_BAND *p_next;

} MP3_COLLECTION;

the globals:

string g_collection_m3u_path;
string g_band_m3u_path;
string g_m3u_new_dir;
string g_excluded_dirs;
FILE *g_band_m3u = NULL;
FILE *g_album_m3u = NULL;

function body:

      WIN32_FIND_DATA file_data;
      HANDLE h_search;
      char new_path[MAX_PATH];

      
      SetCurrentDirectory(search_dir.c_str());
      h_search = FindFirstFile(SEARCH_EXT, &file_data);

      if(h_search == INVALID_HANDLE_VALUE)
      {
            return false;
      }

      do
      {
            sprintf(new_path, "%s\\%s", search_dir.c_str(), file_data.cFileName);
                        
            if(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                  string full_path;


                  if(strcmp(file_data.cFileName, ".") == 0 ||
                     strcmp(file_data.cFileName, "..") == 0 ||
                     strstr(g_excluded_dirs.c_str(), file_data.cFileName))
                  {
                        continue;
                  }

                  if(dir_level == DIR_LEVEL_M3U_BAND)
                  {
                        // create the directory, but do not check for error
                        // will return NULL if directory already exists
                        g_band_m3u_path = g_m3u_new_dir;
                        g_band_m3u_path += "\\";
                        g_band_m3u_path += file_data.cFileName;
                        CreateDirectory(g_band_m3u_path.c_str(), NULL);

                        // create \<new dir>\_all.m3u
                        full_path = g_m3u_new_dir;
                        full_path += "\\";
                        full_path += GLOBAL_M3U;
                        g_band_m3u = fopen(full_path.c_str(), "a+");

                        if(!g_band_m3u)
                        {
                              return false;
                        }

                        search_directories_global(new_path, dir_level + 1);
                        fclose(g_band_m3u);
                        g_band_m3u = NULL;
                  }
                  else if(dir_level == DIR_LEVEL_M3U_ALBUM)
                  {
                        full_path = g_band_m3u_path;
                        full_path += file_data.cFileName;
                        full_path += PLAY_LIST_EXT;
                        g_album_m3u = fopen(full_path.c_str(), "a+");

                        if(!g_album_m3u)
                        {
                              return false;
                        }

                        search_directories_global(new_path, dir_level + 1);
                        fclose(g_album_m3u);
                        g_album_m3u = NULL;
                  }
            }
            else if(dir_level == DIR_LEVEL_SONG)
            {
                  string file_ext;

                  
                  file_ext = file_ext(file_data.cFileName);
                  
                        // MEDIA_EXTS = 'mp3ogg', do not output all of the cover names if it exists
                  if(strstr(MEDIA_EXTS, file_ext.c_str()) != NULL)
                  {
                        fprintf(g_album_m3u, "%s\n", new_path);
                        fprintf(g_band_m3u, "%s\n", new_path);
                  }
            }

      } while(FindNextFile(h_search, &file_data) && h_search != INVALID_HANDLE_VALUE);

Summary of the above function:
recurses directories and creates a playlist for:
each band;
each album of each band;
entire collection
Start Free Trial
 
Keywords: non-recursive directory searching
 
Loading Advertisement...
 
[+][-]01.04.2005 at 11:29PM PST, ID: 12959976

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C++ Programming Language
Tags: recursive
Sign Up Now!
Solution Provided By: nonubik
Participating Experts: 3
Solution Grade: A
 
 
[+][-]01.05.2005 at 12:39AM PST, ID: 12960246

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.05.2005 at 12:41AM PST, ID: 12960253

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.05.2005 at 05:34AM PST, ID: 12961620

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.05.2005 at 06:02AM PST, ID: 12961829

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.05.2005 at 06:48AM PST, ID: 12962291

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.05.2005 at 06:52AM PST, ID: 12962348

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32