3 #define MAX_FILE_LEN 256
4 #define MAX_FILE_COUNT 1000000
17 template<
class SDT,
class FileT>
24 void begin(
const char *startDir,
const char *extension,
25 const char *file_name_pattern) {
27 this->start_dir = startDir;
28 this->ext = extension;
29 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 (
Str(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 Str strFileTName(file_name);
184 && strFileTName.
matches(file_name_pattern)
186 LOGD(
"-> isValidAudioFile: '%s': %d", file_name, result);
195 static char name[MAX_FILE_LEN];
196 file.getName(name,MAX_FILE_LEN);
199 Str tmp(file.name());
205 return file.name()+pos;
211 #if defined(USE_SDFAT) || ESP_IDF_VERSION_MAJOR >= 4
212 LOGD(
"-> fileNamePath: %s",
fileName(file));
213 file_path_str = start_dir;
214 if (!file_path_str.endsWith(
"/")){
215 file_path_str +=
"/";
217 for (
int j=0; j<file_path_stack.size(); j++){
218 file_path_str += file_path_stack[j]+
"/";
221 static char name[MAX_FILE_LEN];
222 strncpy(name,
fileName(file), MAX_FILE_LEN);
223 file_path_str += name;
224 const char* result = file_path_str.c_str();
225 LOGD(
"<- fileNamePath: %s", result);
232 bool isHidden(FileT f){
241 FileT open(
const char* name){
245 if (!result.open(name)){
247 LOGE(
"File open error: %s", name);
249 LOGE(
"File open error: name is null");
254 return p_sd->open(name);