3 #include "AudioTools/CoreAudio/AudioBasic/Str.h"
5 #define MAX_FILE_LEN 256
6 #define MAX_FILE_COUNT 1000000
20 template <
class SDT,
class FileT>
25 void begin(
const char *startDir,
const char *extension,
26 const char *file_name_pattern) {
28 this->start_dir = startDir;
29 this->ext = extension;
30 this->file_name_pattern = file_name_pattern;
36 if (max_idx != -1 && idx > max_idx) {
44 if (!found)
return nullptr;
45 return result.c_str();
51 requested_idx = MAX_FILE_COUNT;
62 const char *start_dir;
71 const char *ext =
nullptr;
72 const char *file_name_pattern =
nullptr;
76 if (dirname ==
nullptr)
return;
77 LOGD(
"listDir: %s", dirname);
78 FileT root = open(dirname);
80 LOGE(
"Open failed: %s", dirname);
84 if (!isDirectory(root)) {
85 LOGD(
"Is not directory: %s", dirname);
89 if (
StrView(dirname).startsWith(
".")) {
90 LOGD(
"Invalid file: %s", dirname);
94 if (isDirectory(root)) {
98 FileT file = openNext(root);
99 while (file && !found) {
100 if (isDirectory(file)) {
102 LOGD(
"name: %s", name.c_str());
110 LOGD(
"File %s at index: %d", fn, actual_idx);
111 if (actual_idx == requested_idx) {
116 LOGD(
"Ignoring %s", fn);
119 file = openNext(root);
122 if (!found && file_path_stack.size() == 0) {
123 max_idx = actual_idx;
128 void rewind(FileT &f) {
137 bool isDirectory(FileT &f) {
142 result = f.isDirectory();
144 LOGD(
"isDirectory %s: %d",
fileName(f), result);
148 FileT openNext(FileT &dir) {
152 if (!result.openNext(&dir, O_READ)) {
153 LOGD(
"No next file");
157 return dir.openNextFile();
161 void pushPath(
const char *name) {
163 LOGD(
"pushPath: %s", name);
164 String nameStr(name);
165 file_path_stack.push_back(nameStr);
171 file_path_stack.pop_back(str);
172 LOGD(
"popPath: %s", str.c_str());
177 const char *file_name =
fileName(file);
178 if (file.isDirectory()) {
179 LOGD(
"-> isValidAudioFile: '%s': %d", file_name,
false);
182 StrView strFileTName(file_name);
184 strFileTName.
matches(file_name_pattern) && !isHidden(file);
185 LOGD(
"-> isValidAudioFile: '%s': %d", file_name, result);
193 static char name[MAX_FILE_LEN];
194 file.getName(name, MAX_FILE_LEN);
203 return file.name() + pos;
209 #if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
210 LOGD(
"-> fileNamePath: %s",
fileName(file));
211 file_path_str = start_dir;
212 if (!file_path_str.endsWith(
"/")) {
213 file_path_str +=
"/";
215 for (
int j = 0; j < file_path_stack.size(); j++) {
216 file_path_str += file_path_stack[j] +
"/";
219 static char name[MAX_FILE_LEN];
220 strncpy(name,
fileName(file), MAX_FILE_LEN);
221 file_path_str += name;
222 const char *result = file_path_str.c_str();
223 LOGD(
"<- fileNamePath: %s", result);
230 bool isHidden(FileT &f) {
238 FileT open(
const char *name) {
242 if (!result.open(name)) {
243 if (name !=
nullptr) {
244 LOGE(
"File open error: %s", name);
246 LOGE(
"File open error: name is null");
251 return p_sd->open(name);