config: abstract fs utils to fix esp32 builds

This commit is contained in:
Torrie Fischer
2023-12-20 09:19:37 +01:00
parent 6797889b4c
commit ce65e7e7f0
3 changed files with 91 additions and 48 deletions

57
src/FsUtils.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "FsUtils.h"
filename_iterator::filename_iterator()
: suffix(NULL),
valid(false)
{}
filename_iterator::filename_iterator(const char* path, const char* suffix)
#ifdef BOARD_ESP8266
: dir(LittleFS.openDir(path)),
#endif
#ifdef BOARD_ESP32
: dir(LittleFS.open(path)),
#endif
valid(true),
suffix(suffix)
{
next();
}
void
filename_iterator::next()
{
if (!valid) {
return;
}
int extPos = -1;
do {
#ifdef BOARD_ESP8266
valid = dir.next();
if (valid) {
String fname = dir.fileName();
extPos = fname.lastIndexOf(suffix);
if (extPos != -1) {
ret = fname.substring(0, extPos);
}
}
#endif
#ifdef BOARD_ESP32
File next = dir.openNextFile();
valid = (bool)next;
if (valid && !next.isDirectory()) {
String fname = next.name();
extPos = fname.lastIndexOf(suffix);
if (extPos != -1) {
ret = fname.substring(0, extPos);
}
}
#endif
} while (valid && extPos == -1);
}
filename_iterator& filename_iterator::operator++()
{
next();
return *this;
}