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.dwFileAttribu
tes & FILE_ATTRIBUTE_DIRECTORY)
{
string full_path;
if(strcmp(file_data.cFileN
ame, ".") == 0 ||
strcmp(file_data.cFileName
, "..") == 0 ||
strstr(g_excluded_dirs.c_s
tr(), 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_st
r(), 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.cFileNa
me);
// 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_searc
h, &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