Browse Source

Reduce lsDive stack usage to the minimum

Scott Lahteine 10 years ago
parent
commit
f3c1a6b19c
1 changed files with 15 additions and 9 deletions
  1. 15
    9
      Marlin/cardreader.cpp

+ 15
- 9
Marlin/cardreader.cpp View File

@@ -56,22 +56,28 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
56 56
     // If the entry is a directory and the action is LS_SerialPrint
57 57
     if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
58 58
 
59
-      // Allocate enough stack space for the full path to a folder, trailing slash, and nul
60
-      int len = strlen(prepend) + FILENAME_LENGTH + 1 + 1;
61
-      char path[len];
62
-
63 59
       // Get the short name for the item, which we know is a folder
64 60
       char lfilename[FILENAME_LENGTH];
65 61
       createFilename(lfilename, p);
66 62
 
63
+      // Allocate enough stack space for the full path to a folder, trailing slash, and nul
64
+      boolean prepend_is_empty = (prepend[0] == '\0');
65
+      int len = strlen(prepend) + (prepend_is_empty ? 1 : 0) + strlen(lfilename) + 1;
66
+      char path[len];
67
+
67 68
       // Append the FOLDERNAME12/ to the passed string.
68 69
       // It contains the full path to the "parent" argument.
69 70
       // We now have the full path to the item in this folder.
70
-      path[0] = '\0';
71
-      if (prepend[0] == '\0') strcat(path, "/"); // a root slash if prepend is empty
72
-      strcat(path, prepend);
73
-      strcat(path, lfilename);
74
-      strcat(path, "/");
71
+      if (prepend_is_empty) {
72
+        path[0] = '/'; // a root slash if prepend is empty
73
+        path[1] = '\0';
74
+      }
75
+      else
76
+        path[0] = '\0';
77
+
78
+      strcat(path, prepend);   // 1 character minimum
79
+      strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum
80
+      strcat(path, "/");       // 1 character
75 81
 
76 82
       // Serial.print(path);
77 83
 

Loading…
Cancel
Save