Skip to content

Commit ddc82b8

Browse files
committed
📝 Document diveToFile, printListing
1 parent 87a9437 commit ddc82b8

File tree

3 files changed

+73
-52
lines changed

3 files changed

+73
-52
lines changed

Marlin/src/inc/SanityCheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
23042304
+ (DISABLED(IS_LEGACY_TFT) && ENABLED(TFT_GENERIC)) \
23052305
+ (ENABLED(IS_LEGACY_TFT) && COUNT_ENABLED(TFT_320x240, TFT_320x240_SPI, TFT_480x320, TFT_480x320_SPI)) \
23062306
+ COUNT_ENABLED(ANYCUBIC_LCD_I3MEGA, ANYCUBIC_LCD_CHIRON, ANYCUBIC_TFT35) \
2307-
+ COUNT_ENABLED(DGUS_LCD_UI_ORIGIN, DGUS_LCD_UI_FYSETC, DGUS_LCD_UI_HIPRECY,DGUS_LCD_UI_MKS) \
2307+
+ COUNT_ENABLED(DGUS_LCD_UI_ORIGIN, DGUS_LCD_UI_FYSETC, DGUS_LCD_UI_HIPRECY, DGUS_LCD_UI_MKS) \
23082308
+ COUNT_ENABLED(ENDER2_STOCKDISPLAY, CR10_STOCKDISPLAY, DWIN_CREALITY_LCD) \
23092309
+ COUNT_ENABLED(FYSETC_MINI_12864_X_X, FYSETC_MINI_12864_1_2, FYSETC_MINI_12864_2_0, FYSETC_MINI_12864_2_1, FYSETC_GENERIC_12864_1_1) \
23102310
+ COUNT_ENABLED(LCD_SAINSMART_I2C_1602, LCD_SAINSMART_I2C_2004) \

Marlin/src/sd/cardreader.cpp

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,14 @@ void CardReader::selectByName(SdFile dir, const char * const match) {
261261
}
262262

263263
//
264-
// Recursive method to list all files within a folder
264+
// Recursive method to print all files within a folder in flat
265+
// DOS 8.3 format. This style of listing is the most compatible
266+
// with legacy hosts.
267+
//
268+
// This method recurses to unlimited depth and lists every
269+
// G-code file within the given parent. If the hierarchy is
270+
// very deep this can blow up the stack, so a 'depth' parameter
271+
// (as with printListingJSON) would be a good addition.
265272
//
266273
void CardReader::printListing(SdFile parent, const char * const prepend/*=nullptr*/) {
267274
dir_t p;
@@ -288,17 +295,17 @@ void CardReader::printListing(SdFile parent, const char * const prepend/*=nullpt
288295

289296
// Get a new directory object using the full path
290297
// and dive recursively into it.
291-
SdFile child;
292-
if (!child.open(&parent, dosFilename, O_READ))
298+
SdFile child; // child.close() in destructor
299+
if (child.open(&parent, dosFilename, O_READ))
300+
printListing(child, path);
301+
else {
293302
SERIAL_ECHO_MSG(STR_SD_CANT_OPEN_SUBDIR, dosFilename);
294-
295-
printListing(child, path);
296-
// close() is done automatically by destructor of SdFile
303+
return;
304+
}
297305
}
298306
else if (is_dir_or_gcode(p)) {
299-
createFilename(filename, p);
300307
if (prepend) SERIAL_ECHO(prepend);
301-
SERIAL_ECHO(filename);
308+
SERIAL_ECHO(createFilename(filename, p));
302309
SERIAL_CHAR(' ');
303310
SERIAL_ECHOLN(p.fileSize);
304311
}
@@ -342,7 +349,7 @@ void CardReader::ls() {
342349
// Go to the next segment
343350
while (path[++i]) { }
344351

345-
// SERIAL_ECHOPGM("Looking for segment: "); SERIAL_ECHOLN(segment);
352+
//SERIAL_ECHOLNPAIR("Looking for segment: ", segment);
346353

347354
// Find the item, setting the long filename
348355
diveDir.rewind();
@@ -720,14 +727,14 @@ void CardReader::removeFile(const char * const name) {
720727

721728
//abortFilePrintNow();
722729

723-
SdFile *curDir;
724-
const char * const fname = diveToFile(false, curDir, name);
730+
SdFile *itsDirPtr;
731+
const char * const fname = diveToFile(false, itsDirPtr, name);
725732
if (!fname) return;
726733

727734
#if ENABLED(SDCARD_READONLY)
728735
SERIAL_ECHOLNPAIR("Deletion failed (read-only), File: ", fname, ".");
729736
#else
730-
if (file.remove(curDir, fname)) {
737+
if (file.remove(itsDirPtr, fname)) {
731738
SERIAL_ECHOLNPAIR("File deleted:", fname);
732739
sdpos = 0;
733740
TERN_(SDCARD_SORT_ALPHA, presort());
@@ -870,98 +877,101 @@ uint16_t CardReader::countFilesInWorkDir() {
870877
* - If update_cwd was 'true' the workDir now points to the file's directory.
871878
*
872879
* Returns a pointer to the last segment (filename) of the given DOS 8.3 path.
880+
* On exit, inDirPtr contains an SdFile reference to the file's directory.
873881
*
874882
* A nullptr result indicates an unrecoverable error.
883+
*
884+
* NOTE: End the path with a slash to dive to a folder. In this case the
885+
* returned filename will be blank (points to the end of the path).
875886
*/
876-
const char* CardReader::diveToFile(const bool update_cwd, SdFile* &diveDir, const char * const path, const bool echo/*=false*/) {
887+
const char* CardReader::diveToFile(const bool update_cwd, SdFile* &inDirPtr, const char * const path, const bool echo/*=false*/) {
877888
DEBUG_SECTION(est, "diveToFile", true);
878889

879890
// Track both parent and subfolder
880891
static SdFile newDir1, newDir2;
881-
SdFile *sub = &newDir1, *startDir;
892+
SdFile *sub = &newDir1, *startDirPtr;
882893

883894
// Parsing the path string
884-
const char *item_name_adr = path;
895+
const char *atom_ptr = path;
885896

886897
DEBUG_ECHOLNPAIR(" path = '", path, "'");
887898

888899
if (path[0] == '/') { // Starting at the root directory?
889-
diveDir = &root;
890-
item_name_adr++;
891-
DEBUG_ECHOLNPAIR(" CWD to root: ", hex_address((void*)diveDir));
900+
inDirPtr = &root;
901+
atom_ptr++;
902+
DEBUG_ECHOLNPAIR(" CWD to root: ", hex_address((void*)inDirPtr));
892903
if (update_cwd) workDirDepth = 0; // The cwd can be updated for the benefit of sub-programs
893904
}
894905
else
895-
diveDir = &workDir; // Dive from workDir (as set by the UI)
906+
inDirPtr = &workDir; // Dive from workDir (as set by the UI)
896907

897-
startDir = diveDir;
908+
startDirPtr = inDirPtr;
898909

899-
DEBUG_ECHOLNPAIR(" startDir = ", hex_address((void*)startDir));
910+
DEBUG_ECHOLNPAIR(" startDirPtr = ", hex_address((void*)startDirPtr));
900911

901-
while (item_name_adr) {
912+
while (atom_ptr) {
902913
// Find next subdirectory delimiter
903-
char * const name_end = strchr(item_name_adr, '/');
914+
char * const name_end = strchr(atom_ptr, '/');
904915

905916
// Last atom in the path? Item found.
906-
if (name_end <= item_name_adr) break;
917+
if (name_end <= atom_ptr) break;
907918

908-
// Set subDirName
909-
const uint8_t len = name_end - item_name_adr;
919+
// Isolate the next subitem name
920+
const uint8_t len = name_end - atom_ptr;
910921
char dosSubdirname[len + 1];
911-
strncpy(dosSubdirname, item_name_adr, len);
922+
strncpy(dosSubdirname, atom_ptr, len);
912923
dosSubdirname[len] = 0;
913924

914925
if (echo) SERIAL_ECHOLN(dosSubdirname);
915926

916927
DEBUG_ECHOLNPAIR(" sub = ", hex_address((void*)sub));
917928

918-
// Open diveDir (closing first)
929+
// Open inDirPtr (closing first)
919930
sub->close();
920-
if (!sub->open(diveDir, dosSubdirname, O_READ)) {
931+
if (!sub->open(inDirPtr, dosSubdirname, O_READ)) {
921932
openFailed(dosSubdirname);
922-
item_name_adr = nullptr;
933+
atom_ptr = nullptr;
923934
break;
924935
}
925936

926-
// Close diveDir if not at starting-point
927-
if (diveDir != startDir) {
928-
DEBUG_ECHOLNPAIR(" closing diveDir: ", hex_address((void*)diveDir));
929-
diveDir->close();
937+
// Close inDirPtr if not at starting-point
938+
if (inDirPtr != startDirPtr) {
939+
DEBUG_ECHOLNPAIR(" closing inDirPtr: ", hex_address((void*)inDirPtr));
940+
inDirPtr->close();
930941
}
931942

932-
// diveDir now subDir
933-
diveDir = sub;
934-
DEBUG_ECHOLNPAIR(" diveDir = sub: ", hex_address((void*)diveDir));
943+
// inDirPtr now subDir
944+
inDirPtr = sub;
945+
DEBUG_ECHOLNPAIR(" inDirPtr = sub: ", hex_address((void*)inDirPtr));
935946

936947
// Update workDirParents and workDirDepth
937948
if (update_cwd) {
938949
DEBUG_ECHOLNPAIR(" update_cwd");
939950
if (workDirDepth < MAX_DIR_DEPTH)
940-
workDirParents[workDirDepth++] = *diveDir;
951+
workDirParents[workDirDepth++] = *inDirPtr;
941952
}
942953

943954
// Point sub at the other scratch object
944-
sub = (diveDir != &newDir1) ? &newDir1 : &newDir2;
955+
sub = (inDirPtr != &newDir1) ? &newDir1 : &newDir2;
945956
DEBUG_ECHOLNPAIR(" swapping sub = ", hex_address((void*)sub));
946957

947958
// Next path atom address
948-
item_name_adr = name_end + 1;
959+
atom_ptr = name_end + 1;
949960
}
950961

951962
if (update_cwd) {
952-
workDir = *diveDir;
953-
DEBUG_ECHOLNPAIR(" final workDir = ", hex_address((void*)diveDir));
963+
workDir = *inDirPtr;
964+
DEBUG_ECHOLNPAIR(" final workDir = ", hex_address((void*)inDirPtr));
954965
flag.workDirIsRoot = (workDirDepth == 0);
955966
TERN_(SDCARD_SORT_ALPHA, presort());
956967
}
957968

958-
DEBUG_ECHOLNPAIR(" returning string ", item_name_adr ?: "nullptr");
959-
return item_name_adr;
969+
DEBUG_ECHOLNPAIR(" returning string ", atom_ptr ?: "nullptr");
970+
return atom_ptr;
960971
}
961972

962973
void CardReader::cd(const char * relpath) {
963-
SdFile newDir;
964-
SdFile *parent = workDir.isOpen() ? &workDir : &root;
974+
SdFile newDir, *parent = &getWorkDir();
965975

966976
if (newDir.open(parent, relpath, O_READ)) {
967977
workDir = newDir;

Marlin/src/sd/cardreader.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ class CardReader {
111111
static void mount();
112112
static void release();
113113
static inline bool isMounted() { return flag.mounted; }
114-
static void ls();
115114

116115
// Handle media insert/remove
117116
static void manage_media();
@@ -176,21 +175,32 @@ class CardReader {
176175
return 0;
177176
}
178177

179-
// Helper for open and remove
180-
static const char* diveToFile(const bool update_cwd, SdFile* &curDir, const char * const path, const bool echo=false);
178+
/**
179+
* Dive down to a relative or absolute path.
180+
* Relative paths apply to the workDir.
181+
*
182+
* update_cwd: Pass 'true' to update the workDir on success.
183+
* inDirPtr: On exit your pointer points to the target SdFile.
184+
* A nullptr indicates failure.
185+
* path: Start with '/' for abs path. End with '/' to get a folder ref.
186+
* echo: Set 'true' to print the path throughout the loop.
187+
*/
188+
static const char* diveToFile(const bool update_cwd, SdFile* &inDirPtr, const char * const path, const bool echo=false);
181189

182190
#if ENABLED(SDCARD_SORT_ALPHA)
183191
static void presort();
184192
static void getfilename_sorted(const uint16_t nr);
185193
#if ENABLED(SDSORT_GCODE)
186-
FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
187-
FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
194+
FORCE_INLINE static void setSortOn(bool b) { sort_alpha = b; presort(); }
195+
FORCE_INLINE static void setSortFolders(int i) { sort_folders = i; presort(); }
188196
//FORCE_INLINE static void setSortReverse(bool b) { sort_reverse = b; }
189197
#endif
190198
#else
191199
FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
192200
#endif
193201

202+
static void ls();
203+
194204
#if ENABLED(POWER_LOSS_RECOVERY)
195205
static bool jobRecoverFileExists();
196206
static void openJobRecoveryFile(const bool read);
@@ -199,6 +209,7 @@ class CardReader {
199209

200210
// Current Working Dir - Set by cd, cdup, cdroot, and diveToFile(true, ...)
201211
static inline char* getWorkDirName() { workDir.getDosName(filename); return filename; }
212+
static inline SdFile& getWorkDir() { return workDir.isOpen() ? workDir : root; }
202213

203214
// Print File stats
204215
static inline uint32_t getFileSize() { return filesize; }

0 commit comments

Comments
 (0)