From 8ebefe6d3578394a88dd0eb751d49048ca70ee2c Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Wed, 26 Nov 2014 08:51:31 -0800
Subject: [PATCH] Completed SORT_USES_MORE_RAM implementation

For the MORE_RAM option we need to buffer both the short and long
names, even though long names are sometimes redundant. Worst case, all
the names are max length. We can save some RAM by not storing these. We
could save more RAM by only storing the visible part of the long name.
---
 Marlin/cardreader.cpp | 19 +++++++++++++++++--
 Marlin/cardreader.h   |  1 +
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/Marlin/cardreader.cpp b/Marlin/cardreader.cpp
index 5d465f47a4..cf0b5176a0 100644
--- a/Marlin/cardreader.cpp
+++ b/Marlin/cardreader.cpp
@@ -203,6 +203,7 @@ void CardReader::startFileprint()
   if(cardOK)
   {
     sdprinting = true;
+    flush_presort();
   }
 }
 
@@ -555,6 +556,7 @@ void CardReader::getfilename(const uint16_t nr)
 {
   #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
     if (nr < sort_count) {
+      strcpy(filename, sortshort[nr]);
       strcpy(longFilename, sortnames[nr]);
       filenameIsDir = isDir[nr];
       return;
@@ -648,6 +650,7 @@ void CardReader::presort()
 
     #if SORT_USES_RAM
       #if SORT_USES_MORE_RAM
+        sortshort = (char**)calloc(fileCnt, sizeof(char*));
         sortnames = (char**)calloc(fileCnt, sizeof(char*));
       #else
         char *sortnames[fileCnt];
@@ -664,7 +667,6 @@ void CardReader::presort()
       #endif
     #endif
 
-    sort_count = fileCnt;
     sort_order = new uint8_t[fileCnt];
 
     if (fileCnt > 1) {
@@ -675,6 +677,9 @@ void CardReader::presort()
         #if SORT_USES_RAM
           getfilename(i);
           sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
+          #if SORT_USES_MORE_RAM
+            sortshort[i] = strdup(filename);
+          #endif
           // char out[30];
           // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
           // SERIAL_ECHOLN(out);
@@ -729,20 +734,27 @@ void CardReader::presort()
       sort_order[0] = 0;
       #if SORT_USES_RAM && SORT_USES_MORE_RAM
         sortnames = (char**)malloc(sizeof(char*));
+        sortshort = (char**)malloc(sizeof(char*));
         isDir = (uint8_t*)malloc(sizeof(uint8_t));
         getfilename(0);
         sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
+        sortshort[0] = strdup(filename);
         isDir[0] = filenameIsDir;
       #endif
     }
 
+    sort_count = fileCnt;
   }
 }
 
 void CardReader::flush_presort() {
   if (sort_count > 0) {
     #if SORT_USES_RAM && SORT_USES_MORE_RAM
-      for (uint8_t i=0; i<sort_count; ++i) free(sortnames[i]);
+      for (uint8_t i=0; i<sort_count; ++i) {
+        free(sortshort[i]);
+        free(sortnames[i]);
+      }
+      free(sortshort);
       free(sortnames);
     #endif
     delete sort_order;
@@ -774,6 +786,9 @@ void CardReader::printingHasFinished()
           enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
       }
       autotempShutdown();
+      #ifdef SDCARD_SORT_ALPHA
+        presort();
+      #endif
     }
 }
 #endif //SDSUPPORT
diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h
index 6a74fe0661..fd8635a5bd 100644
--- a/Marlin/cardreader.h
+++ b/Marlin/cardreader.h
@@ -73,6 +73,7 @@ private:
   uint16_t sort_count;
   uint8_t *sort_order;
   #if SORT_USES_MORE_RAM
+    char **sortshort;
     char **sortnames;
     uint8_t *isDir;
   #endif