Reduce lsDive stack usage to the minimum

This commit is contained in:
Scott Lahteine 2015-07-16 12:10:38 -07:00 committed by Richard Wackerbarth
parent fba4e6241b
commit f3c1a6b19c

View file

@ -56,22 +56,28 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
// If the entry is a directory and the action is LS_SerialPrint // If the entry is a directory and the action is LS_SerialPrint
if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
// Allocate enough stack space for the full path to a folder, trailing slash, and nul
int len = strlen(prepend) + FILENAME_LENGTH + 1 + 1;
char path[len];
// Get the short name for the item, which we know is a folder // Get the short name for the item, which we know is a folder
char lfilename[FILENAME_LENGTH]; char lfilename[FILENAME_LENGTH];
createFilename(lfilename, p); createFilename(lfilename, p);
// Allocate enough stack space for the full path to a folder, trailing slash, and nul
boolean prepend_is_empty = (prepend[0] == '\0');
int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1;
char path[len];
// Append the FOLDERNAME12/ to the passed string. // Append the FOLDERNAME12/ to the passed string.
// It contains the full path to the "parent" argument. // It contains the full path to the "parent" argument.
// We now have the full path to the item in this folder. // We now have the full path to the item in this folder.
if (prepend_is_empty) {
path[0] = '/'; // a root slash if prepend is empty
path[1] = '\0';
}
else
path[0] = '\0'; path[0] = '\0';
if (prepend[0] == '\0') strcat(path, "/"); // a root slash if prepend is empty
strcat(path, prepend); strcat(path, prepend); // 1 character minimum
strcat(path, lfilename); strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
strcat(path, "/"); strcat(path, "/"); // 1 character
// Serial.print(path); // Serial.print(path);