|
@@ -11,8 +11,7 @@
|
11
|
11
|
|
12
|
12
|
CardReader::CardReader()
|
13
|
13
|
{
|
14
|
|
- #if defined(SDCARD_SORT_ALPHA) && SORT_USES_MORE_RAM
|
15
|
|
- sortnames = NULL;
|
|
14
|
+ #ifdef SDCARD_SORT_ALPHA
|
16
|
15
|
sort_count = 0;
|
17
|
16
|
#endif
|
18
|
17
|
filesize = 0;
|
|
@@ -37,19 +36,15 @@ CardReader::CardReader()
|
37
|
36
|
autostart_atmillis=millis()+5000;
|
38
|
37
|
}
|
39
|
38
|
|
40
|
|
-char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
|
|
39
|
+char *createFilename(char *buffer, const dir_t &p) //buffer>12characters
|
41
|
40
|
{
|
42
|
41
|
char *pos=buffer;
|
43
|
|
- for (uint8_t i = 0; i < 11; i++)
|
44
|
|
- {
|
45
|
|
- if (p.name[i] == ' ')continue;
|
46
|
|
- if (i == 8)
|
47
|
|
- {
|
48
|
|
- *pos++='.';
|
49
|
|
- }
|
50
|
|
- *pos++=p.name[i];
|
|
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];
|
51
|
46
|
}
|
52
|
|
- *pos++=0;
|
|
47
|
+ *pos++ = 0;
|
53
|
48
|
return buffer;
|
54
|
49
|
}
|
55
|
50
|
|
|
@@ -59,7 +54,7 @@ void CardReader::lsDive(const char *prepend,SdFile parent)
|
59
|
54
|
dir_t p;
|
60
|
55
|
uint8_t cnt=0;
|
61
|
56
|
|
62
|
|
- while (parent.readDir(p, diveFilename) > 0)
|
|
57
|
+ while (parent.readDir(p, longFilename) > 0)
|
63
|
58
|
{
|
64
|
59
|
if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
|
65
|
60
|
{
|
|
@@ -96,8 +91,8 @@ void CardReader::lsDive(const char *prepend,SdFile parent)
|
96
|
91
|
{
|
97
|
92
|
if (p.name[0] == DIR_NAME_FREE) break;
|
98
|
93
|
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
|
99
|
|
- if (diveFilename[0] != '\0' &&
|
100
|
|
- (diveFilename[0] == '.' || diveFilename[0] == '_')) continue;
|
|
94
|
+ if (longFilename[0] != '\0' &&
|
|
95
|
+ (longFilename[0] == '.' || longFilename[0] == '_')) continue;
|
101
|
96
|
if ( p.name[0] == '.')
|
102
|
97
|
{
|
103
|
98
|
if ( p.name[1] != '.')
|
|
@@ -556,21 +551,20 @@ void CardReader::closefile(bool store_location)
|
556
|
551
|
|
557
|
552
|
}
|
558
|
553
|
|
559
|
|
-void CardReader::getfilename(const uint8_t nr)
|
|
554
|
+void CardReader::getfilename(const uint16_t nr)
|
560
|
555
|
{
|
561
|
|
- #if defined(SDCARD_SORT_ALPHA) && SORT_USES_MORE_RAM
|
|
556
|
+ #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
|
562
|
557
|
if (nr < sort_count) {
|
563
|
|
- strcpy(diveFilename, sortnames[nr]);
|
|
558
|
+ strcpy(longFilename, sortnames[nr]);
|
|
559
|
+ filenameIsDir = isDir[nr];
|
564
|
560
|
return;
|
565
|
561
|
}
|
566
|
562
|
#endif
|
567
|
|
-
|
568
|
563
|
curDir=&workDir;
|
569
|
564
|
lsAction=LS_GetFilename;
|
570
|
565
|
nrFiles=nr;
|
571
|
566
|
curDir->rewind();
|
572
|
567
|
lsDive("",*curDir);
|
573
|
|
-
|
574
|
568
|
}
|
575
|
569
|
|
576
|
570
|
uint16_t CardReader::getnrfilenames()
|
|
@@ -631,12 +625,8 @@ void CardReader::updir()
|
631
|
625
|
/**
|
632
|
626
|
* Get the name of a file in the current directory by sort-index
|
633
|
627
|
*/
|
634
|
|
-void CardReader::getfilename_sorted(const uint8_t nr) {
|
635
|
|
- #if SORT_USES_MORE_RAM
|
636
|
|
- getfilename(nr < sort_count ? sort_order[nr] : nr);
|
637
|
|
- #else
|
638
|
|
- getfilename(nr < SORT_LIMIT ? sort_order[nr] : nr);
|
639
|
|
- #endif
|
|
628
|
+void CardReader::getfilename_sorted(const uint16_t nr) {
|
|
629
|
+ getfilename(nr < sort_count ? sort_order[nr] : nr);
|
640
|
630
|
}
|
641
|
631
|
|
642
|
632
|
/**
|
|
@@ -656,68 +646,73 @@ void CardReader::presort()
|
656
|
646
|
|
657
|
647
|
if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
|
658
|
648
|
|
659
|
|
- #if SORT_USES_MORE_RAM
|
660
|
|
- sortnames = (char**)malloc(fileCnt * sizeof(char*));
|
661
|
|
- sort_count = fileCnt;
|
662
|
|
- #elif SORT_USES_RAM
|
663
|
|
- char *sortnames[fileCnt];
|
664
|
|
- #if FOLDER_SORTING != 0
|
665
|
|
- uint8_t isdir[fileCnt];
|
|
649
|
+ #if SORT_USES_RAM
|
|
650
|
+ #if SORT_USES_MORE_RAM
|
|
651
|
+ sortnames = (char**)calloc(fileCnt, sizeof(char*));
|
|
652
|
+ #else
|
|
653
|
+ char *sortnames[fileCnt];
|
666
|
654
|
#endif
|
667
|
655
|
#else
|
668
|
|
- char sortname[LONG_FILENAME_LENGTH+1];
|
|
656
|
+ char name1[LONG_FILENAME_LENGTH+1];
|
|
657
|
+ #endif
|
|
658
|
+
|
|
659
|
+ #if FOLDER_SORTING != 0
|
|
660
|
+ #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
|
661
|
+ isDir = (uint8_t*)calloc(fileCnt, sizeof(uint8_t));
|
|
662
|
+ #else
|
|
663
|
+ uint8_t isDir[fileCnt];
|
|
664
|
+ #endif
|
669
|
665
|
#endif
|
670
|
666
|
|
|
667
|
+ sort_count = fileCnt;
|
|
668
|
+ sort_order = new uint8_t[fileCnt];
|
|
669
|
+
|
671
|
670
|
if (fileCnt > 1) {
|
672
|
671
|
|
673
|
|
- // Init sort order [and get filenames]
|
674
|
|
- for (int i=0; i<fileCnt; i++) {
|
675
|
|
- int byte=i/8, bit=1<<(i%8);
|
|
672
|
+ // Init sort order. If using RAM then read all filenames now.
|
|
673
|
+ for (uint16_t i=0; i<fileCnt; i++) {
|
676
|
674
|
sort_order[i] = i;
|
677
|
675
|
#if SORT_USES_RAM
|
678
|
676
|
getfilename(i);
|
679
|
|
- char *name = diveFilename[0] ? diveFilename : filename;
|
680
|
|
- // SERIAL_ECHOPGM("--- ");
|
681
|
|
- // SERIAL_ECHOLN(name);
|
682
|
|
- sortnames[i] = (char*)malloc(strlen(name) + 1);
|
683
|
|
- strcpy(sortnames[i], name);
|
|
677
|
+ sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
|
|
678
|
+ // char out[30];
|
|
679
|
+ // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
|
|
680
|
+ // SERIAL_ECHOLN(out);
|
684
|
681
|
#if FOLDER_SORTING != 0
|
685
|
|
- isdir[i] = filenameIsDir;
|
|
682
|
+ isDir[i] = filenameIsDir;
|
686
|
683
|
#endif
|
687
|
684
|
#endif
|
688
|
685
|
}
|
689
|
686
|
|
690
|
687
|
// Bubble Sort
|
691
|
|
- for (uint8_t i=fileCnt; --i;) {
|
|
688
|
+ for (uint16_t i=fileCnt; --i;) {
|
692
|
689
|
bool cmp, didSwap = false;
|
693
|
|
- for (uint8_t j=0; j<i; ++j) {
|
694
|
|
- int s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
|
|
690
|
+ for (uint16_t j=0; j<i; ++j) {
|
|
691
|
+ uint16_t s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
|
695
|
692
|
#if SORT_USES_RAM
|
696
|
693
|
#if FOLDER_SORTING != 0
|
697
|
|
- cmp = (isdir[o1] == isdir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isdir[FOLDER_SORTING > 0 ? o1 : o2];
|
|
694
|
+ cmp = (isDir[o1] == isDir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isDir[FOLDER_SORTING > 0 ? o1 : o2];
|
698
|
695
|
#else
|
699
|
|
- cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0);
|
|
696
|
+ cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
|
700
|
697
|
#endif
|
701
|
698
|
#else
|
702
|
699
|
getfilename(o1);
|
|
700
|
+ strcpy(name1, longFilename[0] ? longFilename : filename);
|
703
|
701
|
#if FOLDER_SORTING != 0
|
704
|
702
|
bool dir1 = filenameIsDir;
|
705
|
703
|
#endif
|
706
|
|
- char *name = diveFilename[0] ? diveFilename : filename;
|
707
|
|
- strcpy(sortname, name);
|
708
|
704
|
getfilename(o2);
|
709
|
|
- name = diveFilename[0] ? diveFilename : filename;
|
|
705
|
+ char *name2 = longFilename[0] ? longFilename : filename;
|
710
|
706
|
#if FOLDER_SORTING != 0
|
711
|
|
- cmp = (dir1 == filenameIsDir) ? (strcasecmp(sortname, name) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
|
|
707
|
+ cmp = (dir1 == filenameIsDir) ? (strcasecmp(name1, name2) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
|
712
|
708
|
#else
|
713
|
|
- cmp = strcasecmp(sortname, name) > 0);
|
|
709
|
+ cmp = strcasecmp(name1, name2) > 0;
|
714
|
710
|
#endif
|
715
|
711
|
#endif
|
716
|
712
|
if (cmp) {
|
717
|
|
- // SERIAL_ECHOPGM("Swap ");
|
718
|
|
- // SERIAL_ECHOLN(sortnames[o1]);
|
719
|
|
- // SERIAL_ECHOPGM(" for ");
|
720
|
|
- // SERIAL_ECHOLN(sortnames[o2]);
|
|
713
|
+ // char out[LONG_FILENAME_LENGTH*2+20];
|
|
714
|
+ // sprintf_P(out, PSTR("Swap %i %s for %i %s"), o1, sortnames[o1], o2, sortnames[o2]);
|
|
715
|
+ // SERIAL_ECHOLN(out);
|
721
|
716
|
sort_order[s1] = o2;
|
722
|
717
|
sort_order[s2] = o1;
|
723
|
718
|
didSwap = true;
|
|
@@ -727,30 +722,32 @@ void CardReader::presort()
|
727
|
722
|
}
|
728
|
723
|
|
729
|
724
|
#if SORT_USES_RAM && !SORT_USES_MORE_RAM
|
730
|
|
- for (int i=0; i < fileCnt; ++i) free(sortnames[i]);
|
|
725
|
+ for (uint16_t i=0; i<fileCnt; ++i) free(sortnames[i]);
|
731
|
726
|
#endif
|
732
|
727
|
}
|
733
|
728
|
else {
|
734
|
729
|
sort_order[0] = 0;
|
|
730
|
+ #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
|
731
|
+ sortnames = (char**)malloc(sizeof(char*));
|
|
732
|
+ isDir = (uint8_t*)malloc(sizeof(uint8_t));
|
|
733
|
+ getfilename(0);
|
|
734
|
+ sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
|
|
735
|
+ isDir[0] = filenameIsDir;
|
|
736
|
+ #endif
|
735
|
737
|
}
|
736
|
738
|
|
737
|
739
|
}
|
738
|
740
|
}
|
739
|
741
|
|
740
|
742
|
void CardReader::flush_presort() {
|
741
|
|
- #if SORT_USES_MORE_RAM
|
742
|
|
- if (sort_count > 0) {
|
743
|
|
- for (int i=0; i < sort_count; ++i) {
|
744
|
|
- free(sortnames[i]);
|
745
|
|
- sort_order[i] = i;
|
746
|
|
- }
|
|
743
|
+ if (sort_count > 0) {
|
|
744
|
+ #if SORT_USES_RAM && SORT_USES_MORE_RAM
|
|
745
|
+ for (uint8_t i=0; i<sort_count; ++i) free(sortnames[i]);
|
747
|
746
|
free(sortnames);
|
748
|
|
- sortnames = NULL;
|
749
|
|
- sort_count = 0;
|
750
|
|
- }
|
751
|
|
- #else
|
752
|
|
- for (int i=SORT_LIMIT; --i;) sort_order[i] = i;
|
753
|
|
- #endif
|
|
747
|
+ #endif
|
|
748
|
+ delete sort_order;
|
|
749
|
+ sort_count = 0;
|
|
750
|
+ }
|
754
|
751
|
}
|
755
|
752
|
|
756
|
753
|
#endif // SDCARD_SORT_ALPHA
|