22 SDIndex(SDT &sd) { p_sd = &sd; };
23 void begin(
const char *startDir,
const char *extension,
24 const char *file_name_pattern,
bool setupIndex =
true) {
26 this->start_dir = startDir;
27 this->ext = extension;
28 this->file_name_pattern = file_name_pattern;
29 idx_path = filePathString(startDir,
"idx.txt");
30 idx_defpath = filePathString(startDir,
"idx-def.txt");
31 int idx_file_size = indexFileTSize();
32 LOGI(
"Index file size: %d", idx_file_size);
34 String(startDir) +
"|" + extension +
"|" + file_name_pattern;
35 String keyOld = getIndexDef();
36 if (setupIndex && (keyNew != keyOld || idx_file_size == 0)) {
37 FileT idxfile = p_sd->open(idx_path.c_str(), FILE_WRITE);
38 LOGW(
"Creating index file");
40 LOGI(
"Indexing completed");
47 void ls(
Print &p,
const char *startDir,
const char *extension,
48 const char *file_name_pattern =
"*") {
50 this->ext = extension;
51 this->file_name_pattern = file_name_pattern;
53 file_path_stack.clear();
54 file_path_str.clear();
60 if (max_idx >= 0 && idx > max_idx) {
61 LOGE(
"idx %d > size %d", idx, max_idx);
65 FileT idxfile = p_sd->open(idx_path.c_str());
68 if (idxfile.available() == 0) {
69 LOGE(
"Index file is empty");
73 while (idxfile.available() > 0 && !found) {
74 result = idxfile.readStringUntil(
'\n');
77 char *c_str = (
char *)result.c_str();
79 int lastPos = result.length() - 1;
80 if (c_str[lastPos] == 13) {
84 LOGD(
"%d -> %s", count, c_str);
95 return found ? result.c_str() :
nullptr;
100 FileT idxfile = p_sd->open(idx_path.c_str());
103 while (idxfile.available() > 0) {
104 result = idxfile.readStringUntil(
'\n');
106 char *c_str = (
char *)result.c_str();
120 List<String> file_path_stack;
121 String file_path_str;
122 const char *start_dir;
124 const char *ext =
nullptr;
125 const char *file_name_pattern =
nullptr;
128 String filePathString(
const char *name,
const char *suffix) {
129 String result = name;
130 return result.endsWith(
"/") ? result + suffix : result +
"/" + suffix;
135 LOGD(
"listDir: %s", dirname);
136 FileT root = open(dirname);
138 LOGE(
"Open failed: %s", dirname);
142 if (!isDirectory(root)) {
143 LOGD(
"Is not directory: %s", dirname);
147 if (
StrView(dirname).startsWith(
".")) {
148 LOGD(
"Invalid file: %s", dirname);
154 FileT file = openNext(root);
156 if (isDirectory(file)) {
158 LOGD(
"name: %s", name.c_str());
160 listDir(idxfile, name.c_str());
164 LOGD(
"Adding file to index: %s", fn);
167 LOGD(
"Ignoring %s", fn);
170 file = openNext(root);
175 bool isDirectory(FileT &f) {
180 result = f.isDirectory();
182 LOGD(
"isDirectory %s: %d",
fileName(f), result);
186 FileT openNext(FileT &dir) {
190 if (!result.openNext(&dir, O_READ)) {
191 LOGD(
"No next file");
195 return dir.openNextFile();
199 void pushPath(
const char *name) {
200 LOGD(
"pushPath: %s", name);
201 LOGD(
"pushPath: %s", name);
202 String nameStr(name);
203 file_path_stack.push_back(nameStr);
209 file_path_stack.pop_back(str);
210 LOGD(
"popPath: %s", str.c_str());
216 const char *file_name =
fileName(file);
217 if (file.isDirectory()) {
218 LOGD(
"-> isValidAudioFile: '%s': %d", file_name,
false);
221 StrView strFileTName(file_name);
223 strFileTName.
matches(file_name_pattern) && !isHidden(file);
224 LOGD(
"-> isValidAudioFile: '%s': %d", file_name, result);
228 String getIndexDef() {
229 FileT idxdef = p_sd->open(idx_defpath.c_str());
230 String key1 = idxdef.readString();
234 void saveIndexDef(String keyNew) {
235 FileT idxdef = p_sd->open(idx_defpath.c_str(), FILE_WRITE);
236 idxdef.write((
const uint8_t *)keyNew.c_str(), keyNew.length());
240 size_t indexFileTSize() {
241 FileT idxfile = p_sd->open(idx_path.c_str());
242 size_t result = idxfile.size();
247 void rewind(FileT &f) {
260 static char name[MAX_FILE_LEN];
261 file.getName(name, MAX_FILE_LEN);
270 return file.name() + pos;
276#if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
277 LOGD(
"-> fileNamePath: %s",
fileName(file));
278 file_path_str = start_dir;
279 if (!file_path_str.endsWith(
"/")) {
280 file_path_str +=
"/";
282 for (
int j = 0; j < file_path_stack.size(); j++) {
283 file_path_str += file_path_stack[j] +
"/";
286 static char name[MAX_FILE_LEN];
287 strncpy(name,
fileName(file), MAX_FILE_LEN);
288 file_path_str += name;
289 const char *result = file_path_str.c_str();
290 LOGD(
"<- fileNamePath: %s", result);
297 bool isHidden(FileT &f) {
305 FileT open(
const char *name) {
309 if (!result.open(name)) {
310 LOGE(
"FileT open error: %s", name);
314 return p_sd->open(name,
"r");