|
@@ -40,24 +40,43 @@ char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
|
40
|
40
|
return buffer;
|
41
|
41
|
}
|
42
|
42
|
|
|
43
|
+/**
|
|
44
|
+ * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction:
|
|
45
|
+ * LS_Count - Add +1 to nrFiles for every file within the parent
|
|
46
|
+ * LS_GetFilename - Get the filename of the file indexed by nrFiles
|
|
47
|
+ * LS_SerialPrint - Print the full path of each file to serial output
|
|
48
|
+ */
|
43
|
49
|
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
|
44
|
50
|
dir_t p;
|
45
|
51
|
uint8_t cnt = 0;
|
46
|
52
|
|
|
53
|
+ // Read the next entry from a directory
|
47
|
54
|
while (parent.readDir(p, longFilename) > 0) {
|
48
|
|
- if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // hence LS_SerialPrint
|
49
|
|
- char path[FILENAME_LENGTH*2];
|
|
55
|
+
|
|
56
|
+ // If the entry is a directory and the action is LS_SerialPrint
|
|
57
|
+ if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) {
|
|
58
|
+
|
|
59
|
+ // Allocate enough stack space for the full path to a folder
|
|
60
|
+ int len = strlen(prepend) + FILENAME_LENGTH + 1;
|
|
61
|
+ char path[len];
|
|
62
|
+
|
|
63
|
+ // Get the short name for the item, which we know is a folder
|
50
|
64
|
char lfilename[FILENAME_LENGTH];
|
51
|
65
|
createFilename(lfilename, p);
|
52
|
66
|
|
53
|
|
- path[0] = 0;
|
54
|
|
- if (prepend[0] == 0) strcat(path, "/"); //avoid leading / if already in prepend
|
|
67
|
+ // Append the FOLDERNAME12/ to the passed string.
|
|
68
|
+ // It contains the full path to the "parent" argument.
|
|
69
|
+ // We now have the full path to the item in this folder.
|
|
70
|
+ path[0] = '\0';
|
|
71
|
+ if (prepend[0] == '\0') strcat(path, "/"); // a root slash if prepend is empty
|
55
|
72
|
strcat(path, prepend);
|
56
|
73
|
strcat(path, lfilename);
|
57
|
74
|
strcat(path, "/");
|
58
|
75
|
|
59
|
|
- //Serial.print(path);
|
|
76
|
+ // Serial.print(path);
|
60
|
77
|
|
|
78
|
+ // Get a new directory object using the full path
|
|
79
|
+ // and dive recursively into it.
|
61
|
80
|
SdFile dir;
|
62
|
81
|
if (!dir.open(parent, lfilename, O_READ)) {
|
63
|
82
|
if (lsAction == LS_SerialPrint) {
|
|
@@ -67,14 +86,13 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
67
|
86
|
}
|
68
|
87
|
}
|
69
|
88
|
lsDive(path, dir);
|
70
|
|
- //close done automatically by destructor of SdFile
|
|
89
|
+ // close() is done automatically by destructor of SdFile
|
71
|
90
|
}
|
72
|
91
|
else {
|
73
|
92
|
char pn0 = p.name[0];
|
74
|
93
|
if (pn0 == DIR_NAME_FREE) break;
|
75
|
94
|
if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
|
76
|
|
- char lf0 = longFilename[0];
|
77
|
|
- if (lf0 == '.') continue;
|
|
95
|
+ if (longFilename[0] == '.') continue;
|
78
|
96
|
|
79
|
97
|
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
|
80
|
98
|
|
|
@@ -82,24 +100,27 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
|
82
|
100
|
|
83
|
101
|
if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
|
84
|
102
|
|
85
|
|
- //if (cnt++ != nr) continue;
|
86
|
|
- createFilename(filename, p);
|
87
|
|
- if (lsAction == LS_SerialPrint) {
|
88
|
|
- SERIAL_PROTOCOL(prepend);
|
89
|
|
- SERIAL_PROTOCOLLN(filename);
|
90
|
|
- }
|
91
|
|
- else if (lsAction == LS_Count) {
|
92
|
|
- nrFiles++;
|
93
|
|
- }
|
94
|
|
- else if (lsAction == LS_GetFilename) {
|
95
|
|
- if (match != NULL) {
|
96
|
|
- if (strcasecmp(match, filename) == 0) return;
|
97
|
|
- }
|
98
|
|
- else if (cnt == nrFiles) return;
|
99
|
|
- cnt++;
|
|
103
|
+ switch (lsAction) {
|
|
104
|
+ case LS_Count:
|
|
105
|
+ nrFiles++;
|
|
106
|
+ break;
|
|
107
|
+ case LS_SerialPrint:
|
|
108
|
+ createFilename(filename, p);
|
|
109
|
+ SERIAL_PROTOCOL(prepend);
|
|
110
|
+ SERIAL_PROTOCOLLN(filename);
|
|
111
|
+ break;
|
|
112
|
+ case LS_GetFilename:
|
|
113
|
+ createFilename(filename, p);
|
|
114
|
+ if (match != NULL) {
|
|
115
|
+ if (strcasecmp(match, filename) == 0) return;
|
|
116
|
+ }
|
|
117
|
+ else if (cnt == nrFiles) return;
|
|
118
|
+ cnt++;
|
|
119
|
+ break;
|
100
|
120
|
}
|
|
121
|
+
|
101
|
122
|
}
|
102
|
|
- }
|
|
123
|
+ } // while readDir
|
103
|
124
|
}
|
104
|
125
|
|
105
|
126
|
void CardReader::ls() {
|