Clarify SD longname string size (#14148)

This commit is contained in:
Robby Candra 2019-05-28 07:50:48 +07:00 committed by Scott Lahteine
parent 5f42d3a8b4
commit 5515f88452
2 changed files with 16 additions and 11 deletions

View file

@ -81,10 +81,10 @@ uint8_t CardReader::workDirDepth;
char **CardReader::sortshort, **CardReader::sortnames; char **CardReader::sortshort, **CardReader::sortnames;
#else #else
char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; char CardReader::sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
#endif #endif
#elif DISABLED(SDSORT_USES_STACK) #elif DISABLED(SDSORT_USES_STACK)
char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN]; char CardReader::sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
#endif #endif
#if HAS_FOLDER_SORTING #if HAS_FOLDER_SORTING
@ -760,11 +760,13 @@ void CardReader::setroot() {
#endif #endif
#else #else
// Copy filenames into the static array // Copy filenames into the static array
#if SORTED_LONGNAME_MAXLEN != LONG_FILENAME_LENGTH #define _SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN)
#define SET_SORTNAME(I) do{ strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN); \ #if SORTED_LONGNAME_MAXLEN == LONG_FILENAME_LENGTH
sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0) // Short name sorting always use LONG_FILENAME_LENGTH with no trailing nul
#define SET_SORTNAME(I) _SET_SORTNAME(I)
#else #else
#define SET_SORTNAME(I) strncpy(sortnames[I], longest_filename(), SORTED_LONGNAME_MAXLEN) // Copy multiple name blocks. Add a nul for the longest case.
#define SET_SORTNAME(I) do{ _SET_SORTNAME(I); sortnames[I][SORTED_LONGNAME_MAXLEN] = '\0'; }while(0)
#endif #endif
#if ENABLED(SDSORT_CACHE_NAMES) #if ENABLED(SDSORT_CACHE_NAMES)
#define SET_SORTSHORT(I) strcpy(sortshort[I], filename) #define SET_SORTSHORT(I) strcpy(sortshort[I], filename)
@ -816,7 +818,7 @@ void CardReader::setroot() {
sortnames = new char*[fileCnt]; sortnames = new char*[fileCnt];
#endif #endif
#elif ENABLED(SDSORT_USES_STACK) #elif ENABLED(SDSORT_USES_STACK)
char sortnames[fileCnt][SORTED_LONGNAME_MAXLEN]; char sortnames[fileCnt][SORTED_LONGNAME_STORAGE];
#endif #endif
// Folder sorting needs 1 bit per entry for flags. // Folder sorting needs 1 bit per entry for flags.

View file

@ -175,9 +175,11 @@ private:
#endif #endif
#if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM) #if BOTH(SDSORT_USES_RAM, SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)
#define SORTED_LONGNAME_MAXLEN ((SDSORT_CACHE_VFATS) * (FILENAME_LENGTH) + 1) #define SORTED_LONGNAME_MAXLEN (SDSORT_CACHE_VFATS) * (FILENAME_LENGTH)
#define SORTED_LONGNAME_STORAGE (SORTED_LONGNAME_MAXLEN + 1)
#else #else
#define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH #define SORTED_LONGNAME_MAXLEN LONG_FILENAME_LENGTH
#define SORTED_LONGNAME_STORAGE SORTED_LONGNAME_MAXLEN
#endif #endif
// Cache filenames to speed up SD menus. // Cache filenames to speed up SD menus.
@ -189,10 +191,11 @@ private:
static char **sortshort, **sortnames; static char **sortshort, **sortnames;
#else #else
static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH]; static char sortshort[SDSORT_LIMIT][FILENAME_LENGTH];
static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
#endif #endif
#elif DISABLED(SDSORT_USES_STACK) #endif
static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_MAXLEN];
#if (ENABLED(SDSORT_CACHE_NAMES) && DISABLED(SDSORT_DYNAMIC_RAM)) || NONE(SDSORT_CACHE_NAMES, SDSORT_USES_STACK)
static char sortnames[SDSORT_LIMIT][SORTED_LONGNAME_STORAGE];
#endif #endif
// Folder sorting uses an isDir array when caching items. // Folder sorting uses an isDir array when caching items.