Skip to content

Commit 89dedb1

Browse files
committed
Add indexOf to AudioSourceIdx
1 parent 1eaa0b0 commit 89dedb1

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

src/AudioTools/Disk/AudioSourceIdxSD.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class AudioSourceIdxSD : public AudioSource {
110110
/// Provides the number of files (The max index is size()-1)
111111
long size() { return idx.size();}
112112

113+
/// Provides the index of the file with the given name
114+
int indexOf(const char* filename) {
115+
return idx.indexOf(filename);
116+
}
117+
113118
protected:
114119
#if defined(USE_SD_NO_NS)
115120
SDIndex<SDClass, File> idx{SD};

src/AudioTools/Disk/AudioSourceIdxSDFAT.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class AudioSourceIdxSDFAT : public AudioSource {
140140
/// Provides the number of files (The max index is size()-1)
141141
long size() { return idx.size(); }
142142

143+
/// Provides the index of the file with the given name
144+
int indexOf(const char* filename) {
145+
return idx.indexOf(filename);
146+
}
147+
143148
protected:
144149
SdSpiConfig *p_cfg = nullptr;
145150
AudioFs sd;

src/AudioTools/Disk/AudioSourceIdxSDMMC.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class AudioSourceIdxSDMMC : public AudioSource {
9898
/// Provides the number of files (The max index is size()-1)
9999
long size() { return idx.size();}
100100

101+
/// Provides the index of the file with the given name
102+
int indexOf(const char* filename) {
103+
return idx.indexOf(filename);
104+
}
105+
101106
protected:
102107
SDIndex<fs::SDMMCFS,fs::File> idx{SD_MMC};
103108
File file;

src/AudioTools/Disk/SDIndex.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,59 @@ class SDIndex {
9595
return found ? result.c_str() : nullptr;
9696
}
9797

98+
/// Find the index of a filename
99+
int indexOf(const char* filename) {
100+
if (filename == nullptr) {
101+
LOGE("filename is null");
102+
return -1;
103+
}
104+
105+
FileT idxfile = p_sd->open(idx_path.c_str());
106+
if (idxfile.available() == 0) {
107+
LOGE("Index file is empty");
108+
idxfile.close();
109+
return -1;
110+
}
111+
112+
int count = 0;
113+
String searchName(filename);
114+
115+
while (idxfile.available() > 0) {
116+
result = idxfile.readStringUntil('\n');
117+
118+
// result c-string
119+
char *c_str = (char *)result.c_str();
120+
// remove potential cr character
121+
int lastPos = result.length() - 1;
122+
if (lastPos >= 0 && c_str[lastPos] == 13) {
123+
c_str[lastPos] = 0;
124+
}
125+
126+
LOGD("Comparing %d: '%s' with '%s'", count, c_str, filename);
127+
128+
// Check if the filename matches (with or without path)
129+
String currentFile(c_str);
130+
if (currentFile == searchName ||
131+
currentFile.endsWith("/" + searchName) ||
132+
currentFile.endsWith(searchName)) {
133+
LOGD("Found '%s' at index %d", filename, count);
134+
idxfile.close();
135+
return count;
136+
}
137+
138+
count++;
139+
}
140+
141+
// Update max_idx if we've counted all entries
142+
if (max_idx == -1) {
143+
max_idx = count;
144+
}
145+
146+
idxfile.close();
147+
LOGD("File '%s' not found in index", filename);
148+
return -1;
149+
}
150+
98151
long size() {
99152
if (max_idx == -1) {
100153
FileT idxfile = p_sd->open(idx_path.c_str());

0 commit comments

Comments
 (0)