Code: Select all
char** songList;
char** songTitle;
uint32_t Sys_SearchSongs()
{
DIR* pdir;
struct dirent *pent;
struct stat statbuf;
Log::Debug(" opening dir\n");
pdir = opendir("/ctb/songs/");
if(pdir)
{
uint32_t num = 0;
uint32_t current = 0;
// Go to the end
while(readdir(pdir) != NULL) num++;
if(num < 2)
{
Log::Debug("No songs found\n");
return 0;
}
num -= 2;
// Make enough space for pointers
songList = (char**)malloc(sizeof(char*) * num);
songTitle = (char**)malloc(sizeof(char*) * num);
rewinddir(pdir);
Log::Debug(" num songs found: %d\n", num);
while( (pent = readdir(pdir)) != NULL )
{
Log::Debug(" trying to parse stat for folder %s\n", pent->d_name);
stat(pent->d_name, &statbuf);
// Ignore the current and previous directory
if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
continue;
if(S_ISDIR(statbuf.st_mode))
{
Log::Debug(" loading song data for %s\n", pent->d_name);
// Add the dir to the list
if(current > num) continue;
// Store the data
songList[current] = (char*)malloc(sizeof(char) * 768);
songTitle[current] = (char*)malloc(sizeof(char) * 256);
strcpy(songList[current], pent->d_name);
sprintf(path, "/ctb/songs/%s/%s.txt", songList[current], songList[current]);
FILE* fp = fopen(path, "rb+");
if(fp == NULL || ferror(fp))
{
strcpy(songTitle[current], "<ERROR ENTRY>");
}
else
{
fgets(title, 256, fp);
fclose(fp);
strcpy(songTitle[current], title);
}
Log::Debug(" song %d: %s\n", current, songTitle[current]);
current ++;
}
}
closedir(pdir);
Log::Debug(" parsed %d songs\n", current);
return current;
}
return 0;
}