01-14-2015 09:22 AM
Hello,
I want to retrieve all files in a directory, and ran into this sample code:
This is the function that does the retrieve action: GetFilesAndDirectories(). Its content is as follow:
void GetFilesAndDirectories(char dir[], int* numItems, int parentItem)
{
char fileName[MAX_PATHNAME_LEN], searchPath[MAX_PATHNAME_LEN];
int error = 0;
strcpy (searchPath, dir);
strcat (searchPath, "\\*");
if (!GetFirstFile (searchPath, 1, 1, 0, 0, 0, 0, fileName)) // has at least one file
{
InsertListItem (panelHandle, PANEL_LISTBOX, -1, fileName, (*numItems)++);
while (!GetNextFile (fileName))
{
InsertListItem (panelHandle, PANEL_LISTBOX, -1, fileName, (*numItems)++);
}
}
if (!GetFirstFile (searchPath, 0, 0, 0, 0, 0, 1, fileName)) // has at least one directory
{
char dirNamesSaved[MAX_NUM_DIR][MAX_PATHNAME_LEN];
int numDir = 0, i;
strcpy (dirNamesSaved[numDir++], fileName);
while (!GetNextFile (fileName) && numDir<MAX_NUM_DIR) // first save all the directory names
{
strcpy (dirNamesSaved[numDir++], fileName);
}
for (i=0; i<numDir; i++)
{
char displayName[MAX_PATHNAME_LEN];
Fmt (displayName, "%s<%s%s", fileName, " (Directory)");
InsertListItem (panelHandle, PANEL_LISTBOX, -1, displayName, (*numItems)++);
MakePathname (dir, dirNamesSaved[i], searchPath);
GetFilesAndDirectories(searchPath, numItems, (*numItems)-1);
}
}
}
I then modify that function into one that I can use to retrieve either files or folders, and populate a ring object with the result. I call it Ring_Populate(). Its content is as follow:
void Ring_Populate (int panel, int TargetRing, char TargetFolder[], int Directory_Bool, int *Iteration_Now, int Iteration_Prev)
{
char FileName [260]; //standard = 260 max char. w/ null char.
char Folder [260];
strcpy (Folder, TargetFolder); //copy string
strcat (Folder, "*"); //add wildcard character, * or ?
switch (Directory_Bool)
{
case 0: //file
if (!GetFirstFile(Folder, 1, 1, 0, 0, 0, 0, FileName)) //has at least 1 file
{
InsertListItem (panel, TargetRing, -1, FileName, (*Iteration_Now)++);
while (!GetNextFile(FileName))
{
InsertListItem (panel, TargetRing, -1, FileName, (*Iteration_Now)++);
}
}
break;
default: //folder
if (!GetFirstFile(Folder, 0, 0, 0, 0, 0, 1, FileName)) //has at least 1 folder
{
int Folder_No = 0;
int Folder_Max = 50; //max 50 folders
char Folder_List [50][260]; //name 260 max
strcpy (Folder_List [Folder_No++], FileName); //copy first element then ++
while ( (!GetNextFile(FileName)) && (Folder_No < Folder_Max) )
{
strcpy (Folder_List [Folder_No++], FileName); //copy all folder names
}
for (int i = 0; i < Folder_No; i++)
{
char modFolder [260];
//Fmt (modFolder, "%s<%s%s", FileName, " (DIR)");
//InsertListItem (panel, TargetRing, -1, modFolder, (*Iteration_Now)++);
//MakePathname (TargetFolder, Folder_List [i], Folder); //recycle
//Ring_Populate (panel, TargetRing, TargetFolder, 1, Iteration_Now, (*Iteration_Now) - 1);
Fmt (modFolder, "%s<%s%s", Folder_List [i], " (DIR)");
InsertListItem (panel, TargetRing, -1, modFolder, i);
}
}
break;
}
}
It works fine. However, the section that I commented out (modified from the sample code, which works) gives me stack overflow.
//Fmt (modFolder, "%s<%s%s", FileName, " (DIR)");
//InsertListItem (panel, TargetRing, -1, modFolder, (*Iteration_Now)++);
//MakePathname (TargetFolder, Folder_List [i], Folder); //recycle
//Ring_Populate (panel, TargetRing, TargetFolder, 1, Iteration_Now, (*Iteration_Now) - 1);
This is the original code:
char displayName[MAX_PATHNAME_LEN];
Fmt (displayName, "%s<%s%s", fileName, " (Directory)");
InsertListItem (panelHandle, PANEL_LISTBOX, -1, displayName, (*numItems)++);
MakePathname (dir, dirNamesSaved[i], searchPath);
GetFilesAndDirectories(searchPath, numItems, (*numItems)-1);
Question:
1. How do I get stack overflow? My code is essentially the same.
2. My modified code works fine retrieving the list of folders. I do not see the need to call the function recursively. Can anyone explain?