Pārlūkot izejas kodu

✨ Enable 'M20 L' with LONG_FILENAME_HOST_SUPPORT (#22271)

Mihai 3 gadus atpakaļ
vecāks
revīzija
094caadf94
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
3 mainītis faili ar 68 papildinājumiem un 35 dzēšanām
  1. 1
    1
      Marlin/src/gcode/sd/M20.cpp
  2. 60
    32
      Marlin/src/sd/cardreader.cpp
  3. 7
    2
      Marlin/src/sd/cardreader.h

+ 1
- 1
Marlin/src/gcode/sd/M20.cpp Parādīt failu

@@ -33,7 +33,7 @@
33 33
 void GcodeSuite::M20() {
34 34
   if (card.flag.mounted) {
35 35
     SERIAL_ECHOLNPGM(STR_BEGIN_FILE_LIST);
36
-    card.ls();
36
+    card.ls(TERN_(LONG_FILENAME_HOST_SUPPORT, parser.boolval('L')));
37 37
     SERIAL_ECHOLNPGM(STR_END_FILE_LIST);
38 38
   }
39 39
   else

+ 60
- 32
Marlin/src/sd/cardreader.cpp Parādīt failu

@@ -258,54 +258,82 @@ void CardReader::selectByName(SdFile dir, const char * const match) {
258 258
   }
259 259
 }
260 260
 
261
-//
262
-// Recursive method to print all files within a folder in flat
263
-// DOS 8.3 format. This style of listing is the most compatible
264
-// with legacy hosts.
265
-//
266
-// This method recurses to unlimited depth and lists every
267
-// G-code file within the given parent. If the hierarchy is
268
-// very deep this can blow up the stack, so a 'depth' parameter
269
-// (as with printListingJSON) would be a good addition.
270
-//
271
-void CardReader::printListing(SdFile parent, const char * const prepend/*=nullptr*/) {
261
+/**
262
+ * Recursive method to print all files within a folder in flat
263
+ * DOS 8.3 format. This style of listing is the most compatible
264
+ * with legacy hosts.
265
+ *
266
+ * This method recurses to unlimited depth and lists all G-code
267
+ * files within the given parent. If the hierarchy is very deep
268
+ * this can blow up the stack, so a 'depth' parameter would be a
269
+ * good addition.
270
+ */
271
+void CardReader::printListing(
272
+  SdFile parent
273
+  OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames/*=false*/)
274
+  , const char * const prepend/*=nullptr*/
275
+  OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong/*=nullptr*/)
276
+) {
272 277
   dir_t p;
273 278
   while (parent.readDir(&p, longFilename) > 0) {
274 279
     if (DIR_IS_SUBDIR(&p)) {
275 280
 
276
-      // Get the short name for the item, which we know is a folder
277
-      char dosFilename[FILENAME_LENGTH];
281
+      size_t lenPrepend = prepend ? strlen(prepend) + 1 : 0;
282
+      // Allocate enough stack space for the full path including / separator
283
+      char path[lenPrepend + FILENAME_LENGTH];
284
+      if (prepend) {
285
+        strcpy(path, prepend);
286
+        path[lenPrepend - 1] = '/';
287
+      }
288
+      char* dosFilename = path + lenPrepend;
278 289
       createFilename(dosFilename, p);
279 290
 
280
-      // Allocate enough stack space for the full path to a folder, trailing slash, and nul
281
-      const bool prepend_is_empty = (!prepend || prepend[0] == '\0');
282
-      const int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(dosFilename) + 1 + 1;
283
-      char path[len];
284
-
285
-      // Append the FOLDERNAME12/ to the passed string.
286
-      // It contains the full path to the "parent" argument.
287
-      // We now have the full path to the item in this folder.
288
-      strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty
289
-      strcat(path, dosFilename);                      // FILENAME_LENGTH characters maximum
290
-      strcat(path, "/");                              // 1 character
291
-
292
-      // Serial.print(path);
293
-
294 291
       // Get a new directory object using the full path
295 292
       // and dive recursively into it.
296 293
       SdFile child; // child.close() in destructor
297 294
       if (child.open(&parent, dosFilename, O_READ))
298
-        printListing(child, path);
295
+        #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
296
+          if (includeLongNames) {
297
+            size_t lenPrependLong = prependLong ? strlen(prependLong) + 1 : 0;
298
+            // Allocate enough stack space for the full long path including / separator
299
+            char pathLong[lenPrependLong + strlen(longFilename) + 1];
300
+            if (prependLong) {
301
+              strcpy(pathLong, prependLong);
302
+              pathLong[lenPrependLong - 1] = '/';
303
+            }
304
+            strcpy(pathLong + lenPrependLong, longFilename);
305
+            printListing(child, /*includeLongNames=*/true, path, pathLong);
306
+          }
307
+          else
308
+        #endif
309
+            printListing(child, path);
299 310
       else {
300 311
         SERIAL_ECHO_MSG(STR_SD_CANT_OPEN_SUBDIR, dosFilename);
301 312
         return;
302 313
       }
303 314
     }
304 315
     else if (is_dir_or_gcode(p)) {
305
-      if (prepend) SERIAL_ECHO(prepend);
316
+      if (prepend) {
317
+        SERIAL_ECHO(prepend);
318
+        SERIAL_CHAR('/');
319
+      }
306 320
       SERIAL_ECHO(createFilename(filename, p));
307 321
       SERIAL_CHAR(' ');
308
-      SERIAL_ECHOLN(p.fileSize);
322
+      #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
323
+        if (!includeLongNames)
324
+      #endif
325
+          SERIAL_ECHOLN(p.fileSize);
326
+      #if ENABLED(LONG_FILENAME_HOST_SUPPORT)
327
+        else {
328
+          SERIAL_ECHO(p.fileSize);
329
+          SERIAL_CHAR(' ');
330
+          if (prependLong) {
331
+            SERIAL_ECHO(prependLong);
332
+            SERIAL_CHAR('/');
333
+          }
334
+          SERIAL_ECHOLN(longFilename[0] ? longFilename : "???");
335
+        }
336
+      #endif
309 337
     }
310 338
   }
311 339
 }
@@ -313,10 +341,10 @@ void CardReader::printListing(SdFile parent, const char * const prepend/*=nullpt
313 341
 //
314 342
 // List all files on the SD card
315 343
 //
316
-void CardReader::ls() {
344
+void CardReader::ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames/*=false*/)) {
317 345
   if (flag.mounted) {
318 346
     root.rewind();
319
-    printListing(root);
347
+    printListing(root OPTARG(LONG_FILENAME_HOST_SUPPORT, includeLongNames));
320 348
   }
321 349
 }
322 350
 

+ 7
- 2
Marlin/src/sd/cardreader.h Parādīt failu

@@ -199,7 +199,7 @@ public:
199 199
     FORCE_INLINE static void getfilename_sorted(const uint16_t nr) { selectFileByIndex(nr); }
200 200
   #endif
201 201
 
202
-  static void ls();
202
+  static void ls(TERN_(LONG_FILENAME_HOST_SUPPORT, bool includeLongNames=false));
203 203
 
204 204
   #if ENABLED(POWER_LOSS_RECOVERY)
205 205
     static bool jobRecoverFileExists();
@@ -330,7 +330,12 @@ private:
330 330
   static int countItems(SdFile dir);
331 331
   static void selectByIndex(SdFile dir, const uint8_t index);
332 332
   static void selectByName(SdFile dir, const char * const match);
333
-  static void printListing(SdFile parent, const char * const prepend=nullptr);
333
+  static void printListing(
334
+    SdFile parent
335
+    OPTARG(LONG_FILENAME_HOST_SUPPORT, const bool includeLongNames=false)
336
+    , const char * const prepend=nullptr
337
+    OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong=nullptr)
338
+  );
334 339
 
335 340
   #if ENABLED(SDCARD_SORT_ALPHA)
336 341
     static void flush_presort();

Notiek ielāde…
Atcelt
Saglabāt