@@ -91,8 +91,8 @@ int16_t CardReader::nrItems = -1;
91
91
92
92
int16_t CardReader::sort_count;
93
93
#if ENABLED(SDSORT_GCODE)
94
- bool CardReader::sort_alpha;
95
- int CardReader::sort_folders;
94
+ SortFlag CardReader::sort_alpha;
95
+ int8_t CardReader::sort_folders;
96
96
// bool CardReader::sort_reverse;
97
97
#endif
98
98
@@ -160,8 +160,8 @@ CardReader::CardReader() {
160
160
#if ENABLED(SDCARD_SORT_ALPHA)
161
161
sort_count = 0 ;
162
162
#if ENABLED(SDSORT_GCODE)
163
- sort_alpha = true ;
164
- sort_folders = FOLDER_SORTING ;
163
+ sort_alpha = TERN (SDSORT_REVERSE, AS_REV, AS_FWD) ;
164
+ sort_folders = SDSORT_FOLDERS ;
165
165
// sort_reverse = false;
166
166
#endif
167
167
#endif
@@ -993,7 +993,7 @@ void CardReader::selectFileByIndex(const int16_t nr) {
993
993
if (nr < sort_count) {
994
994
strcpy (filename, sortshort[nr]);
995
995
strcpy (longFilename, sortnames[nr]);
996
- flag.filenameIsDir = IS_DIR (nr);
996
+ TERN_ (HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR (nr) );
997
997
setBinFlag (strcmp_P (strrchr (filename, ' .' ), PSTR (" .BIN" )) == 0 );
998
998
return ;
999
999
}
@@ -1011,7 +1011,7 @@ void CardReader::selectFileByName(const char * const match) {
1011
1011
if (strcasecmp (match, sortshort[nr]) == 0 ) {
1012
1012
strcpy (filename, sortshort[nr]);
1013
1013
strcpy (longFilename, sortnames[nr]);
1014
- flag.filenameIsDir = IS_DIR (nr);
1014
+ TERN_ (HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR (nr) );
1015
1015
setBinFlag (strcmp_P (strrchr (filename, ' .' ), PSTR (" .BIN" )) == 0 );
1016
1016
return ;
1017
1017
}
@@ -1163,7 +1163,7 @@ void CardReader::cdroot() {
1163
1163
* Get the name of a file in the working directory by sort-index
1164
1164
*/
1165
1165
void CardReader::selectFileByIndexSorted (const int16_t nr) {
1166
- selectFileByIndex (TERN1 (SDSORT_GCODE, sort_alpha) && (nr < sort_count) ? sort_order[nr] : nr);
1166
+ selectFileByIndex (SortFlag ( TERN1 (SDSORT_GCODE, sort_alpha != AS_OFF) ) && (nr < sort_count) ? sort_order[nr] : nr);
1167
1167
}
1168
1168
1169
1169
#if ENABLED(SDSORT_USES_RAM)
@@ -1210,7 +1210,7 @@ void CardReader::cdroot() {
1210
1210
int16_t fileCnt = get_num_items ();
1211
1211
1212
1212
// Sorting may be turned off
1213
- if (TERN0 (SDSORT_GCODE, ! sort_alpha)) return ;
1213
+ if (TERN0 (SDSORT_GCODE, sort_alpha == AS_OFF )) return ;
1214
1214
1215
1215
// If there are files, sort up to the limit
1216
1216
if (fileCnt > 0 ) {
@@ -1239,9 +1239,9 @@ void CardReader::cdroot() {
1239
1239
// Folder sorting needs 1 bit per entry for flags.
1240
1240
#if HAS_FOLDER_SORTING
1241
1241
#if ENABLED(SDSORT_DYNAMIC_RAM)
1242
- isDir = new uint8_t [(fileCnt + 7 ) >> 3 ];
1242
+ isDir = new uint8_t [(fileCnt + 7 ) >> 3 ]; // Allocate space with 'new'
1243
1243
#elif ENABLED(SDSORT_USES_STACK)
1244
- uint8_t isDir[(fileCnt + 7 ) >> 3 ];
1244
+ uint8_t isDir[(fileCnt + 7 ) >> 3 ]; // Use stack in this scope
1245
1245
#endif
1246
1246
#endif
1247
1247
@@ -1291,18 +1291,18 @@ void CardReader::cdroot() {
1291
1291
const int16_t o2 = sort_order[j + 1 ];
1292
1292
1293
1293
// Compare names from the array or just the two buffered names
1294
- # if ENABLED(SDSORT_USES_RAM)
1295
- # define _SORT_CMP_NODIR () ( strcasecmp(sortnames[o1], sortnames[o2] ) > 0 )
1296
- # else
1297
- # define _SORT_CMP_NODIR () (strcasecmp(name1, name2) > 0 )
1298
- #endif
1294
+ auto _sort_cmp_file = []( char * const n1, char * const n2) -> bool {
1295
+ const bool sort = strcasecmp (n1, n2 ) > 0 ;
1296
+ return ( TERN (SDSORT_GCODE, card. sort_alpha == AS_REV, ENABLED (SDSORT_REVERSE))) ? !sort : sort;
1297
+ };
1298
+ #define _SORT_CMP_FILE () _sort_cmp_file(TERN(SDSORT_USES_RAM, sortnames[o1], name1), TERN(SDSORT_USES_RAM, sortnames[o2], name2))
1299
1299
1300
1300
#if HAS_FOLDER_SORTING
1301
1301
#if ENABLED(SDSORT_USES_RAM)
1302
1302
// Folder sorting needs an index and bit to test for folder-ness.
1303
- #define _SORT_CMP_DIR (fs ) (IS_DIR(o1) == IS_DIR(o2) ? _SORT_CMP_NODIR () : IS_DIR(fs > 0 ? o1 : o2))
1303
+ #define _SORT_CMP_DIR (fs ) (IS_DIR(o1) == IS_DIR(o2) ? _SORT_CMP_FILE () : IS_DIR(fs > 0 ? o1 : o2))
1304
1304
#else
1305
- #define _SORT_CMP_DIR (fs ) ((dir1 == flag.filenameIsDir) ? _SORT_CMP_NODIR () : (fs > 0 ? dir1 : !dir1))
1305
+ #define _SORT_CMP_DIR (fs ) ((dir1 == flag.filenameIsDir) ? _SORT_CMP_FILE () : (fs > 0 ? dir1 : !dir1))
1306
1306
#endif
1307
1307
#endif
1308
1308
@@ -1318,12 +1318,12 @@ void CardReader::cdroot() {
1318
1318
if (
1319
1319
#if HAS_FOLDER_SORTING
1320
1320
#if ENABLED(SDSORT_GCODE)
1321
- sort_folders ? _SORT_CMP_DIR (sort_folders) : _SORT_CMP_NODIR ()
1321
+ sort_folders ? _SORT_CMP_DIR (sort_folders) : _SORT_CMP_FILE ()
1322
1322
#else
1323
- _SORT_CMP_DIR (FOLDER_SORTING )
1323
+ _SORT_CMP_DIR (SDSORT_FOLDERS )
1324
1324
#endif
1325
1325
#else
1326
- _SORT_CMP_NODIR ()
1326
+ _SORT_CMP_FILE ()
1327
1327
#endif
1328
1328
) {
1329
1329
// Reorder the index, indicate that sorting happened
@@ -1357,12 +1357,14 @@ void CardReader::cdroot() {
1357
1357
#if ENABLED(SDSORT_DYNAMIC_RAM)
1358
1358
sortnames = new char *[1 ];
1359
1359
sortshort = new char *[1 ];
1360
- isDir = new uint8_t [1 ];
1361
1360
#endif
1362
1361
selectFileByIndex (0 );
1363
1362
SET_SORTNAME (0 );
1364
1363
SET_SORTSHORT (0 );
1365
- isDir[0 ] = flag.filenameIsDir ;
1364
+ #if ALL(HAS_FOLDER_SORTING, SDSORT_DYNAMIC_RAM)
1365
+ isDir = new uint8_t [1 ];
1366
+ isDir[0 ] = flag.filenameIsDir ;
1367
+ #endif
1366
1368
#endif
1367
1369
}
1368
1370
0 commit comments