|
@@ -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)
|
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)
|
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
|
{
|
|
@@ -98,10 +101,11 @@ void CardReader::lsDive(const char *prepend,SdFile parent)
|
98
|
101
|
if ( p.name[1] != '.')
|
99
|
102
|
continue;
|
100
|
103
|
}
|
101
|
|
-
|
|
104
|
+
|
102
|
105
|
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
|
103
|
106
|
filenameIsDir=DIR_IS_SUBDIR(&p);
|
104
|
|
-
|
|
107
|
+
|
|
108
|
+
|
105
|
109
|
if(!filenameIsDir)
|
106
|
110
|
{
|
107
|
111
|
if(p.name[8]!='G') continue;
|
|
@@ -120,8 +124,10 @@ void CardReader::lsDive(const char *prepend,SdFile parent)
|
120
|
124
|
}
|
121
|
125
|
else if(lsAction==LS_GetFilename)
|
122
|
126
|
{
|
123
|
|
- if (cnt == nrFiles) return;
|
|
127
|
+ if(cnt==nrFiles)
|
|
128
|
+ return;
|
124
|
129
|
cnt++;
|
|
130
|
+
|
125
|
131
|
}
|
126
|
132
|
}
|
127
|
133
|
}
|
|
@@ -130,6 +136,9 @@ void CardReader::lsDive(const char *prepend,SdFile parent)
|
130
|
136
|
void CardReader::ls()
|
131
|
137
|
{
|
132
|
138
|
lsAction=LS_SerialPrint;
|
|
139
|
+ if(lsAction==LS_Count)
|
|
140
|
+ nrFiles=0;
|
|
141
|
+
|
133
|
142
|
root.rewind();
|
134
|
143
|
lsDive("",root);
|
135
|
144
|
}
|
|
@@ -168,9 +177,6 @@ void CardReader::initsd()
|
168
|
177
|
}
|
169
|
178
|
workDir=root;
|
170
|
179
|
curDir=&root;
|
171
|
|
- #ifdef SDCARD_SORT_ALPHA
|
172
|
|
- presort();
|
173
|
|
- #endif
|
174
|
180
|
/*
|
175
|
181
|
if(!workDir.openRoot(&volume))
|
176
|
182
|
{
|
|
@@ -187,10 +193,8 @@ void CardReader::setroot()
|
187
|
193
|
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
|
188
|
194
|
}*/
|
189
|
195
|
workDir=root;
|
|
196
|
+
|
190
|
197
|
curDir=&workDir;
|
191
|
|
- #ifdef SDCARD_SORT_ALPHA
|
192
|
|
- presort();
|
193
|
|
- #endif
|
194
|
198
|
}
|
195
|
199
|
void CardReader::release()
|
196
|
200
|
{
|
|
@@ -203,7 +207,6 @@ void CardReader::startFileprint()
|
203
|
207
|
if(cardOK)
|
204
|
208
|
{
|
205
|
209
|
sdprinting = true;
|
206
|
|
- flush_presort();
|
207
|
210
|
}
|
208
|
211
|
}
|
209
|
212
|
|
|
@@ -232,7 +235,7 @@ void CardReader::getAbsFilename(char *t)
|
232
|
235
|
while(*t!=0 && cnt< MAXPATHNAMELENGTH)
|
233
|
236
|
{t++;cnt++;} //crawl counter forward.
|
234
|
237
|
}
|
235
|
|
- if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
|
|
238
|
+ if(cnt<MAXPATHNAMELENGTH-13)
|
236
|
239
|
file.getFilename(t);
|
237
|
240
|
else
|
238
|
241
|
t[0]=0;
|
|
@@ -302,7 +305,7 @@ void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
|
302
|
305
|
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
303
|
306
|
if(dirname_end>0 && dirname_end>dirname_start)
|
304
|
307
|
{
|
305
|
|
- char subdirname[FILENAME_LENGTH];
|
|
308
|
+ char subdirname[13];
|
306
|
309
|
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
307
|
310
|
subdirname[dirname_end-dirname_start]=0;
|
308
|
311
|
SERIAL_ECHOLN(subdirname);
|
|
@@ -398,7 +401,7 @@ void CardReader::removeFile(char* name)
|
398
|
401
|
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
|
399
|
402
|
if(dirname_end>0 && dirname_end>dirname_start)
|
400
|
403
|
{
|
401
|
|
- char subdirname[FILENAME_LENGTH];
|
|
404
|
+ char subdirname[13];
|
402
|
405
|
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
|
403
|
406
|
subdirname[dirname_end-dirname_start]=0;
|
404
|
407
|
SERIAL_ECHOLN(subdirname);
|
|
@@ -436,9 +439,6 @@ void CardReader::removeFile(char* name)
|
436
|
439
|
SERIAL_PROTOCOLPGM("File deleted:");
|
437
|
440
|
SERIAL_PROTOCOLLN(fname);
|
438
|
441
|
sdpos = 0;
|
439
|
|
- #ifdef SDCARD_SORT_ALPHA
|
440
|
|
- presort();
|
441
|
|
- #endif
|
442
|
442
|
}
|
443
|
443
|
else
|
444
|
444
|
{
|
|
@@ -552,21 +552,14 @@ void CardReader::closefile(bool store_location)
|
552
|
552
|
|
553
|
553
|
}
|
554
|
554
|
|
555
|
|
-void CardReader::getfilename(const uint16_t nr)
|
|
555
|
+void CardReader::getfilename(const uint8_t nr)
|
556
|
556
|
{
|
557
|
|
- #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
|
558
|
|
- if (nr < sort_count) {
|
559
|
|
- strcpy(filename, sortshort[nr]);
|
560
|
|
- strcpy(longFilename, sortnames[nr]);
|
561
|
|
- filenameIsDir = isDir[nr];
|
562
|
|
- return;
|
563
|
|
- }
|
564
|
|
- #endif
|
565
|
557
|
curDir=&workDir;
|
566
|
558
|
lsAction=LS_GetFilename;
|
567
|
559
|
nrFiles=nr;
|
568
|
560
|
curDir->rewind();
|
569
|
561
|
lsDive("",*curDir);
|
|
562
|
+
|
570
|
563
|
}
|
571
|
564
|
|
572
|
565
|
uint16_t CardReader::getnrfilenames()
|
|
@@ -584,7 +577,7 @@ void CardReader::chdir(const char * relpath)
|
584
|
577
|
{
|
585
|
578
|
SdFile newfile;
|
586
|
579
|
SdFile *parent=&root;
|
587
|
|
-
|
|
580
|
+
|
588
|
581
|
if(workDir.isOpen())
|
589
|
582
|
parent=&workDir;
|
590
|
583
|
|
|
@@ -602,164 +595,21 @@ void CardReader::chdir(const char * relpath)
|
602
|
595
|
workDirParents[0]=*parent;
|
603
|
596
|
}
|
604
|
597
|
workDir=newfile;
|
605
|
|
- #ifdef SDCARD_SORT_ALPHA
|
606
|
|
- presort();
|
607
|
|
- #endif
|
608
|
598
|
}
|
609
|
599
|
}
|
610
|
600
|
|
611
|
601
|
void CardReader::updir()
|
612
|
602
|
{
|
613
|
|
- if (workDirDepth > 0)
|
|
603
|
+ if(workDirDepth > 0)
|
614
|
604
|
{
|
615
|
605
|
--workDirDepth;
|
616
|
606
|
workDir = workDirParents[0];
|
|
607
|
+ int d;
|
617
|
608
|
for (int d = 0; d < workDirDepth; d++)
|
618
|
609
|
workDirParents[d] = workDirParents[d+1];
|
619
|
|
- #ifdef SDCARD_SORT_ALPHA
|
620
|
|
- presort();
|
621
|
|
- #endif
|
622
|
|
- }
|
623
|
|
-}
|
624
|
|
-
|
625
|
|
-#ifdef SDCARD_SORT_ALPHA
|
626
|
|
-
|
627
|
|
-/**
|
628
|
|
- * Get the name of a file in the current directory by sort-index
|
629
|
|
- */
|
630
|
|
-void CardReader::getfilename_sorted(const uint16_t nr) {
|
631
|
|
- getfilename(nr < sort_count ? sort_order[nr] : nr);
|
632
|
|
-}
|
633
|
|
-
|
634
|
|
-/**
|
635
|
|
- * Read all the files and produce a sort key
|
636
|
|
- *
|
637
|
|
- * We can do this in 3 ways...
|
638
|
|
- * - Minimal RAM: Read two filenames at a time sorting along...
|
639
|
|
- * - Some RAM: Buffer the directory and return filenames from RAM
|
640
|
|
- * - Some RAM: Buffer the directory just for this sort
|
641
|
|
- */
|
642
|
|
-void CardReader::presort()
|
643
|
|
-{
|
644
|
|
- flush_presort();
|
645
|
|
-
|
646
|
|
- uint16_t fileCnt = getnrfilenames();
|
647
|
|
- if (fileCnt > 0) {
|
648
|
|
-
|
649
|
|
- if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
|
650
|
|
-
|
651
|
|
- #if SORT_USES_RAM
|
652
|
|
- #if SORT_USES_MORE_RAM
|
653
|
|
- sortshort = (char**)calloc(fileCnt, sizeof(char*));
|
654
|
|
- sortnames = (char**)calloc(fileCnt, sizeof(char*));
|
655
|
|
- #else
|
656
|
|
- char *sortnames[fileCnt];
|
657
|
|
- #endif
|
658
|
|
- #else
|
659
|
|
- char name1[LONG_FILENAME_LENGTH+1];
|
660
|
|
- #endif
|
661
|
|
-
|
662
|
|
- #if FOLDER_SORTING != 0
|
663
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
664
|
|
- isDir = (uint8_t*)calloc(fileCnt, sizeof(uint8_t));
|
665
|
|
- #else
|
666
|
|
- uint8_t isDir[fileCnt];
|
667
|
|
- #endif
|
668
|
|
- #endif
|
669
|
|
-
|
670
|
|
- sort_order = new uint8_t[fileCnt];
|
671
|
|
-
|
672
|
|
- if (fileCnt > 1) {
|
673
|
|
-
|
674
|
|
- // Init sort order. If using RAM then read all filenames now.
|
675
|
|
- for (uint16_t i=0; i<fileCnt; i++) {
|
676
|
|
- sort_order[i] = i;
|
677
|
|
- #if SORT_USES_RAM
|
678
|
|
- getfilename(i);
|
679
|
|
- sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
|
680
|
|
- #if SORT_USES_MORE_RAM
|
681
|
|
- sortshort[i] = strdup(filename);
|
682
|
|
- #endif
|
683
|
|
- // char out[30];
|
684
|
|
- // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
|
685
|
|
- // SERIAL_ECHOLN(out);
|
686
|
|
- #if FOLDER_SORTING != 0
|
687
|
|
- isDir[i] = filenameIsDir;
|
688
|
|
- #endif
|
689
|
|
- #endif
|
690
|
|
- }
|
691
|
|
-
|
692
|
|
- // Bubble Sort
|
693
|
|
- for (uint16_t i=fileCnt; --i;) {
|
694
|
|
- bool cmp, didSwap = false;
|
695
|
|
- for (uint16_t j=0; j<i; ++j) {
|
696
|
|
- uint16_t s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
|
697
|
|
- #if SORT_USES_RAM
|
698
|
|
- #if FOLDER_SORTING != 0
|
699
|
|
- cmp = (isDir[o1] == isDir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isDir[FOLDER_SORTING > 0 ? o1 : o2];
|
700
|
|
- #else
|
701
|
|
- cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
|
702
|
|
- #endif
|
703
|
|
- #else
|
704
|
|
- getfilename(o1);
|
705
|
|
- strcpy(name1, longFilename[0] ? longFilename : filename);
|
706
|
|
- #if FOLDER_SORTING != 0
|
707
|
|
- bool dir1 = filenameIsDir;
|
708
|
|
- #endif
|
709
|
|
- getfilename(o2);
|
710
|
|
- char *name2 = longFilename[0] ? longFilename : filename;
|
711
|
|
- #if FOLDER_SORTING != 0
|
712
|
|
- cmp = (dir1 == filenameIsDir) ? (strcasecmp(name1, name2) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
|
713
|
|
- #else
|
714
|
|
- cmp = strcasecmp(name1, name2) > 0;
|
715
|
|
- #endif
|
716
|
|
- #endif
|
717
|
|
- if (cmp) {
|
718
|
|
- sort_order[s1] = o2;
|
719
|
|
- sort_order[s2] = o1;
|
720
|
|
- didSwap = true;
|
721
|
|
- }
|
722
|
|
- }
|
723
|
|
- if (!didSwap) break;
|
724
|
|
- }
|
725
|
|
-
|
726
|
|
- #if SORT_USES_RAM && !SORT_USES_MORE_RAM
|
727
|
|
- for (uint16_t i=0; i<fileCnt; ++i) free(sortnames[i]);
|
728
|
|
- #endif
|
729
|
|
- }
|
730
|
|
- else {
|
731
|
|
- sort_order[0] = 0;
|
732
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
733
|
|
- sortnames = (char**)malloc(sizeof(char*));
|
734
|
|
- sortshort = (char**)malloc(sizeof(char*));
|
735
|
|
- isDir = (uint8_t*)malloc(sizeof(uint8_t));
|
736
|
|
- getfilename(0);
|
737
|
|
- sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
|
738
|
|
- sortshort[0] = strdup(filename);
|
739
|
|
- isDir[0] = filenameIsDir;
|
740
|
|
- #endif
|
741
|
|
- }
|
742
|
|
-
|
743
|
|
- sort_count = fileCnt;
|
744
|
|
- }
|
745
|
|
-}
|
746
|
|
-
|
747
|
|
-void CardReader::flush_presort() {
|
748
|
|
- if (sort_count > 0) {
|
749
|
|
- #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
750
|
|
- for (uint8_t i=0; i<sort_count; ++i) {
|
751
|
|
- free(sortshort[i]);
|
752
|
|
- free(sortnames[i]);
|
753
|
|
- }
|
754
|
|
- free(sortshort);
|
755
|
|
- free(sortnames);
|
756
|
|
- #endif
|
757
|
|
- delete sort_order;
|
758
|
|
- sort_count = 0;
|
759
|
610
|
}
|
760
|
611
|
}
|
761
|
612
|
|
762
|
|
-#endif // SDCARD_SORT_ALPHA
|
763
|
613
|
|
764
|
614
|
void CardReader::printingHasFinished()
|
765
|
615
|
{
|
|
@@ -783,9 +633,6 @@ void CardReader::printingHasFinished()
|
783
|
633
|
enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
|
784
|
634
|
}
|
785
|
635
|
autotempShutdown();
|
786
|
|
- #ifdef SDCARD_SORT_ALPHA
|
787
|
|
- presort();
|
788
|
|
- #endif
|
789
|
636
|
}
|
790
|
637
|
}
|
791
|
638
|
#endif //SDSUPPORT
|