Skip to content

Commit 9f48916

Browse files
committed
AudioSourceVector: support delete of entries
1 parent c17091c commit 9f48916

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/AudioTools/Disk/AudioSource.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class NamePrinter : public Print {
229229
};
230230

231231
/**
232-
* @brief Audio Data Source managing a Vector of (file) names with minimal RAM
232+
* @brief Flexible Audio Data Source using a Vector of (file) names with minimal RAM
233233
* usage. Files are stored with separated path index and name to minimize memory
234234
* consumption. Identical paths are stored only once in a shared path registry.
235235
* This class is a template to support multiple SD libraries and other Streams.
@@ -365,6 +365,43 @@ class AudioSourceVector : public AudioSource, public PathNamesRegistry {
365365

366366
}
367367

368+
/// Remove a file by full path
369+
bool deleteName(const char* nameWithPath) {
370+
TRACED();
371+
if (nameWithPath == nullptr) return false;
372+
373+
int idx = indexOf(nameWithPath);
374+
if (idx >= 0) {
375+
LOGI("deleteName: '%s' at index %d", nameWithPath, idx);
376+
return deleteIndex(idx);
377+
}
378+
379+
LOGW("deleteName: File not found: '%s'", nameWithPath);
380+
return false;
381+
}
382+
383+
/// Remove a file by index
384+
bool deleteIndex(size_t idx) {
385+
TRACED();
386+
if (idx >= files.size()) {
387+
LOGW("deleteIndex: Invalid index: %d (size: %d)", (int)idx, files.size());
388+
return false;
389+
}
390+
391+
LOGI("deleteIndex: Removing file at index %d", (int)idx);
392+
files.erase(files.begin() + idx);
393+
394+
// Adjust current_index if necessary
395+
if (current_index >= (int)idx) {
396+
current_index--;
397+
if (current_index < 0 && !files.empty()) {
398+
current_index = 0;
399+
}
400+
}
401+
402+
return true;
403+
}
404+
368405
/// Add multiple files at once
369406
template <typename T, size_t N>
370407
void addNames(T (&nameArray)[N]) {

0 commit comments

Comments
 (0)