|
@@ -11,9 +11,6 @@
|
11
|
11
|
|
12
|
12
|
CardReader::CardReader()
|
13
|
13
|
{
|
14
|
|
- #ifdef SDCARD_SORT_ALPHA
|
15
|
|
- sort_count = 0;
|
16
|
|
- #endif
|
17
|
14
|
filesize = 0;
|
18
|
15
|
sdpos = 0;
|
19
|
16
|
sdprinting = false;
|
|
@@ -36,15 +33,19 @@ CardReader::CardReader()
|
36
|
33
|
autostart_atmillis=millis()+5000;
|
37
|
34
|
}
|
38
|
35
|
|
39
|
|
-char *createFilename(char *buffer, const dir_t &p) //buffer>12characters
|
|
36
|
+char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
|
40
|
37
|
{
|
41
|
38
|
char *pos=buffer;
|
42
|
|
- for (uint8_t i = 0; i < 11; i++) {
|
43
|
|
- if (p.name[i] == ' ') continue;
|
44
|
|
- if (i == 8) *pos++ = '.';
|
45
|
|
- *pos++ = p.name[i];
|
|
39
|
+ for (uint8_t i = 0; i < 11; i++)
|
|
40
|
+ {
|
|
41
|
+ if (p.name[i] == ' ')continue;
|
|
42
|
+ if (i == 8)
|
|
43
|
+ {
|
|
44
|
+ *pos++='.';
|
|
45
|
+ }
|
|
46
|
+ *pos++=p.name[i];
|
46
|
47
|
}
|
47
|
|
- *pos++ = 0;
|
|
48
|
+ *pos++=0;
|
48
|
49
|
return buffer;
|
49
|
50
|
}
|
50
|
51
|
|
|
@@ -52,15 +53,15 @@ char *createFilename(char *buffer, const dir_t &p) //buffer>12characters
|
52
|
53
|
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/)
|
53
|
54
|
{
|
54
|
55
|
dir_t p;
|
55
|
|
- uint8_t cnt=0;
|
|
56
|
+ uint8_t cnt=0;
|
56
|
57
|
|
57
|
58
|
while (parent.readDir(p, longFilename) > 0)
|
58
|
59
|
{
|
59
|
60
|
if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
|
60
|
61
|
{
|
61
|
62
|
|
62
|
|
- char path[FILENAME_LENGTH*2];
|
63
|
|
- char lfilename[FILENAME_LENGTH];
|
|
63
|
+ char path[13*2];
|
|
64
|
+ char lfilename[13];
|
64
|
65
|
createFilename(lfilename,p);
|
65
|
66
|
|
66
|
67
|
path[0]=0;
|
|
@@ -86,6 +87,8 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
86
|
87
|
}
|
87
|
88
|
lsDive(path,dir);
|
88
|
89
|
//close done automatically by destructor of SdFile
|
|
90
|
+
|
|
91
|
+
|
89
|
92
|
}
|
90
|
93
|
else
|
91
|
94
|
{
|
|
@@ -97,7 +100,8 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
97
|
100
|
|
98
|
101
|
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
|
99
|
102
|
filenameIsDir=DIR_IS_SUBDIR(&p);
|
100
|
|
-
|
|
103
|
+
|
|
104
|
+
|
101
|
105
|
if(!filenameIsDir)
|
102
|
106
|
{
|
103
|
107
|
if(p.name[8]!='G') continue;
|
|
@@ -121,6 +125,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
121
|
125
|
}
|
122
|
126
|
else if (cnt == nrFiles) return;
|
123
|
127
|
cnt++;
|
|
128
|
+
|
124
|
129
|
}
|
125
|
130
|
}
|
126
|
131
|
}
|
|
@@ -129,6 +134,9 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
129
|
134
|
void CardReader::ls()
|
130
|
135
|
{
|
131
|
136
|
lsAction=LS_SerialPrint;
|
|
137
|
+ if(lsAction==LS_Count)
|
|
138
|
+ nrFiles=0;
|
|
139
|
+
|
132
|
140
|
root.rewind();
|
133
|
141
|
lsDive("",root);
|
134
|
142
|
}
|
|
@@ -175,9 +183,6 @@ void CardReader::initsd()
|
175
|
183
|
}
|
176
|
184
|
workDir=root;
|
177
|
185
|
curDir=&root;
|
178
|
|
- #ifdef SDCARD_SORT_ALPHA
|
179
|
|
- presort();
|
180
|
|
- #endif
|
181
|
186
|
/*
|
182
|
187
|
if(!workDir.openRoot(&volume))
|
183
|
188
|
{
|
|
@@ -194,10 +199,8 @@ void CardReader::setroot()
|
194
|
199
|
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
|
195
|
200
|
}*/
|
196
|
201
|
workDir=root;
|
|
202
|
+
|
197
|
203
|
curDir=&workDir;
|
198
|
|
- #ifdef SDCARD_SORT_ALPHA
|
199
|
|
- presort();
|
200
|
|
- #endif
|
201
|
204
|
}
|
202
|
205
|
void CardReader::release()
|
203
|
206
|
{
|
|
@@ -210,9 +213,6 @@ void CardReader::startFileprint()
|
210
|
213
|
if(cardOK)
|
211
|
214
|
{
|
212
|
215
|
sdprinting = true;
|
213
|
|
- #ifdef SDCARD_SORT_ALPHA
|
214
|
|
- flush_presort();
|
215
|
|
- #endif
|
216
|
216
|
}
|
217
|
217
|
}
|
218
|
218
|
|
|
@@ -241,7 +241,7 @@ void CardReader::getAbsFilename(char *t)
|
241
|
241
|
while(*t!=0 && cnt< MAXPATHNAMELENGTH)
|
242
|
242
|
{t++;cnt++;} //crawl counter forward.
|
243
|
243
|
}
|
244
|
|
- if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
|
|
244
|
+ if(cnt<MAXPATHNAMELENGTH-13)
|
245
|
245
|
file.getFilename(t);
|
246
|
246
|
else
|
247
|
247
|
t[0]=0;
|
|
@@ -311,7 +311,7 @@ void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
|
311
|
311
|
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
312
|
312
|
if(dirname_end>0 && dirname_end>dirname_start)
|
313
|
313
|
{
|
314
|
|
- char subdirname[FILENAME_LENGTH];
|
|
314
|
+ char subdirname[13];
|
315
|
315
|
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
316
|
316
|
subdirname[dirname_end-dirname_start]=0;
|
317
|
317
|
SERIAL_ECHOLN(subdirname);
|
|
@@ -408,7 +408,7 @@ void CardReader::removeFile(char* name)
|
408
|
408
|
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
409
|
409
|
if(dirname_end>0 && dirname_end>dirname_start)
|
410
|
410
|
{
|
411
|
|
- char subdirname[FILENAME_LENGTH];
|
|
411
|
+ char subdirname[13];
|
412
|
412
|
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
413
|
413
|
subdirname[dirname_end-dirname_start]=0;
|
414
|
414
|
SERIAL_ECHOLN(subdirname);
|
|
@@ -446,9 +446,6 @@ void CardReader::removeFile(char* name)
|
446
|
446
|
SERIAL_PROTOCOLPGM("File deleted:");
|
447
|
447
|
SERIAL_PROTOCOLLN(fname);
|
448
|
448
|
sdpos = 0;
|
449
|
|
- #ifdef SDCARD_SORT_ALPHA
|
450
|
|
- presort();
|
451
|
|
- #endif
|
452
|
449
|
}
|
453
|
450
|
else
|
454
|
451
|
{
|
|
@@ -564,14 +561,6 @@ void CardReader::closefile(bool store_location)
|
564
|
561
|
|
565
|
562
|
void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
|
566
|
563
|
{
|
567
|
|
- #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
|
568
|
|
- if (nr < sort_count) {
|
569
|
|
- strcpy(filename, sortshort[nr]);
|
570
|
|
- strcpy(longFilename, sortnames[nr]);
|
571
|
|
- filenameIsDir = isDir[nr];
|
572
|
|
- return;
|
573
|
|
- }
|
574
|
|
- #endif
|
575
|
564
|
curDir=&workDir;
|
576
|
565
|
lsAction=LS_GetFilename;
|
577
|
566
|
nrFiles=nr;
|
|
@@ -595,7 +584,7 @@ void CardReader::chdir(const char * relpath)
|
595
|
584
|
{
|
596
|
585
|
SdFile newfile;
|
597
|
586
|
SdFile *parent=&root;
|
598
|
|
-
|
|
587
|
+
|
599
|
588
|
if(workDir.isOpen())
|
600
|
589
|
parent=&workDir;
|
601
|
590
|
|
|
@@ -613,164 +602,21 @@ void CardReader::chdir(const char * relpath)
|
613
|
602
|
workDirParents[0]=*parent;
|
614
|
603
|
}
|
615
|
604
|
workDir=newfile;
|
616
|
|
- #ifdef SDCARD_SORT_ALPHA
|
617
|
|
- presort();
|
618
|
|
- #endif
|
619
|
605
|
}
|
620
|
606
|
}
|
621
|
607
|
|
622
|
608
|
void CardReader::updir()
|
623
|
609
|
{
|
624
|
|
- if (workDirDepth > 0)
|
|
610
|
+ if(workDirDepth > 0)
|
625
|
611
|
{
|
626
|
612
|
--workDirDepth;
|
627
|
613
|
workDir = workDirParents[0];
|
|
614
|
+ int d;
|
628
|
615
|
for (int d = 0; d < workDirDepth; d++)
|
629
|
616
|
workDirParents[d] = workDirParents[d+1];
|
630
|
|
- #ifdef SDCARD_SORT_ALPHA
|
631
|
|
- presort();
|
632
|
|
- #endif
|
633
|
|
- }
|
634
|
|
-}
|
635
|
|
-
|
636
|
|
-#ifdef SDCARD_SORT_ALPHA
|
637
|
|
-
|
638
|
|
-/**
|
639
|
|
- * Get the name of a file in the current directory by sort-index
|
640
|
|
- */
|
641
|
|
-void CardReader::getfilename_sorted(const uint16_t nr) {
|
642
|
|
- getfilename(nr < sort_count ? sort_order[nr] : nr);
|
643
|
|
-}
|
644
|
|
-
|
645
|
|
-/**
|
646
|
|
- * Read all the files and produce a sort key
|
647
|
|
- *
|
648
|
|
- * We can do this in 3 ways...
|
649
|
|
- * - Minimal RAM: Read two filenames at a time sorting along...
|
650
|
|
- * - Some RAM: Buffer the directory and return filenames from RAM
|
651
|
|
- * - Some RAM: Buffer the directory just for this sort
|
652
|
|
- */
|
653
|
|
-void CardReader::presort()
|
654
|
|
-{
|
655
|
|
- flush_presort();
|
656
|
|
-
|
657
|
|
- uint16_t fileCnt = getnrfilenames();
|
658
|
|
- if (fileCnt > 0) {
|
659
|
|
-
|
660
|
|
- if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
|
661
|
|
-
|
662
|
|
- #if SORT_USES_RAM
|
663
|
|
- #if SORT_USES_MORE_RAM
|
664
|
|
- sortshort = (char**)calloc(fileCnt, sizeof(char*));
|
665
|
|
- sortnames = (char**)calloc(fileCnt, sizeof(char*));
|
666
|
|
- #else
|
667
|
|
- char *sortnames[fileCnt];
|
668
|
|
- #endif
|
669
|
|
- #else
|
670
|
|
- char name1[LONG_FILENAME_LENGTH+1];
|
671
|
|
- #endif
|
672
|
|
-
|
673
|
|
- #if FOLDER_SORTING != 0
|
674
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
675
|
|
- isDir = (uint8_t*)calloc(fileCnt, sizeof(uint8_t));
|
676
|
|
- #else
|
677
|
|
- uint8_t isDir[fileCnt];
|
678
|
|
- #endif
|
679
|
|
- #endif
|
680
|
|
-
|
681
|
|
- sort_order = new uint8_t[fileCnt];
|
682
|
|
-
|
683
|
|
- if (fileCnt > 1) {
|
684
|
|
-
|
685
|
|
- // Init sort order. If using RAM then read all filenames now.
|
686
|
|
- for (uint16_t i=0; i<fileCnt; i++) {
|
687
|
|
- sort_order[i] = i;
|
688
|
|
- #if SORT_USES_RAM
|
689
|
|
- getfilename(i);
|
690
|
|
- sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
|
691
|
|
- #if SORT_USES_MORE_RAM
|
692
|
|
- sortshort[i] = strdup(filename);
|
693
|
|
- #endif
|
694
|
|
- // char out[30];
|
695
|
|
- // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
|
696
|
|
- // SERIAL_ECHOLN(out);
|
697
|
|
- #if FOLDER_SORTING != 0
|
698
|
|
- isDir[i] = filenameIsDir;
|
699
|
|
- #endif
|
700
|
|
- #endif
|
701
|
|
- }
|
702
|
|
-
|
703
|
|
- // Bubble Sort
|
704
|
|
- for (uint16_t i=fileCnt; --i;) {
|
705
|
|
- bool cmp, didSwap = false;
|
706
|
|
- for (uint16_t j=0; j<i; ++j) {
|
707
|
|
- uint16_t s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
|
708
|
|
- #if SORT_USES_RAM
|
709
|
|
- #if FOLDER_SORTING != 0
|
710
|
|
- cmp = (isDir[o1] == isDir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isDir[FOLDER_SORTING > 0 ? o1 : o2];
|
711
|
|
- #else
|
712
|
|
- cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
|
713
|
|
- #endif
|
714
|
|
- #else
|
715
|
|
- getfilename(o1);
|
716
|
|
- strcpy(name1, longFilename[0] ? longFilename : filename);
|
717
|
|
- #if FOLDER_SORTING != 0
|
718
|
|
- bool dir1 = filenameIsDir;
|
719
|
|
- #endif
|
720
|
|
- getfilename(o2);
|
721
|
|
- char *name2 = longFilename[0] ? longFilename : filename;
|
722
|
|
- #if FOLDER_SORTING != 0
|
723
|
|
- cmp = (dir1 == filenameIsDir) ? (strcasecmp(name1, name2) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
|
724
|
|
- #else
|
725
|
|
- cmp = strcasecmp(name1, name2) > 0;
|
726
|
|
- #endif
|
727
|
|
- #endif
|
728
|
|
- if (cmp) {
|
729
|
|
- sort_order[s1] = o2;
|
730
|
|
- sort_order[s2] = o1;
|
731
|
|
- didSwap = true;
|
732
|
|
- }
|
733
|
|
- }
|
734
|
|
- if (!didSwap) break;
|
735
|
|
- }
|
736
|
|
-
|
737
|
|
- #if SORT_USES_RAM && !SORT_USES_MORE_RAM
|
738
|
|
- for (uint16_t i=0; i<fileCnt; ++i) free(sortnames[i]);
|
739
|
|
- #endif
|
740
|
|
- }
|
741
|
|
- else {
|
742
|
|
- sort_order[0] = 0;
|
743
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
744
|
|
- sortnames = (char**)malloc(sizeof(char*));
|
745
|
|
- sortshort = (char**)malloc(sizeof(char*));
|
746
|
|
- isDir = (uint8_t*)malloc(sizeof(uint8_t));
|
747
|
|
- getfilename(0);
|
748
|
|
- sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
|
749
|
|
- sortshort[0] = strdup(filename);
|
750
|
|
- isDir[0] = filenameIsDir;
|
751
|
|
- #endif
|
752
|
|
- }
|
753
|
|
-
|
754
|
|
- sort_count = fileCnt;
|
755
|
|
- }
|
756
|
|
-}
|
757
|
|
-
|
758
|
|
-void CardReader::flush_presort() {
|
759
|
|
- if (sort_count > 0) {
|
760
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
761
|
|
- for (uint8_t i=0; i<sort_count; ++i) {
|
762
|
|
- free(sortshort[i]);
|
763
|
|
- free(sortnames[i]);
|
764
|
|
- }
|
765
|
|
- free(sortshort);
|
766
|
|
- free(sortnames);
|
767
|
|
- #endif
|
768
|
|
- delete sort_order;
|
769
|
|
- sort_count = 0;
|
770
|
617
|
}
|
771
|
618
|
}
|
772
|
619
|
|
773
|
|
-#endif // SDCARD_SORT_ALPHA
|
774
|
620
|
|
775
|
621
|
void CardReader::printingHasFinished()
|
776
|
622
|
{
|
|
@@ -794,9 +640,6 @@ void CardReader::printingHasFinished()
|
794
|
640
|
enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
|
795
|
641
|
}
|
796
|
642
|
autotempShutdown();
|
797
|
|
- #ifdef SDCARD_SORT_ALPHA
|
798
|
|
- presort();
|
799
|
|
- #endif
|
800
|
643
|
}
|
801
|
644
|
}
|
802
|
645
|
#endif //SDSUPPORT
|