From ce65e7e7f0ec6b811bb418aa56e0b9940685d513 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Wed, 20 Dec 2023 09:19:37 +0100 Subject: [PATCH] config: abstract fs utils to fix esp32 builds --- src/Config.h | 49 +----------------------------------------- src/FsUtils.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/FsUtils.h | 33 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 48 deletions(-) create mode 100644 src/FsUtils.cpp create mode 100644 src/FsUtils.h diff --git a/src/Config.h b/src/Config.h index 64d5b7c..6633e63 100644 --- a/src/Config.h +++ b/src/Config.h @@ -2,7 +2,7 @@ #include #include "JsonCoordinateMapping.h" #include -#include +#include "FsUtils.h" class Configuration { public: @@ -59,55 +59,8 @@ struct ConfigService: public Task { const CoordinateMapping* coordMap() const { return &m_jsonMap; } const char* loadedProfile() const; void overrideProfile(const char* profileName); - const char* getConfigValue(const char* key) const; const std::vector& commands() const override; - struct filename_iterator: public std::iterator { - Dir dir; - String ret; - bool valid; - const char* suffix; - explicit filename_iterator() : suffix(NULL), valid(false) {} - explicit filename_iterator(const char* path, const char* suffix) : dir(LittleFS.openDir(path)), valid(true), suffix(suffix) { - next(); - } - - void next() { - if (!valid) { - return; - } - int extPos = -1; - do { - valid = dir.next(); - Log.info("valid %F", valid); - if (valid) { - String fname = dir.fileName(); - extPos = fname.lastIndexOf(suffix); - Log.info("compare %s %d", fname.c_str(), extPos); - if (extPos != -1) { - ret = fname.substring(0, extPos); - Log.info("found %s", ret.c_str()); - } - } - } while (valid && extPos == -1); - } - - filename_iterator& operator++() { - next(); - return *this; - } - - filename_iterator& operator++(int) {filename_iterator ret = *this; ++(*this); return ret;} - bool operator==(const filename_iterator &other) const { return valid == other.valid;} - bool operator!=(const filename_iterator &other) const { return !(*this == other); } - const char* operator*() const { - if (!valid) { - return NULL; - } - return ret.c_str(); - } - }; - filename_iterator mapsBegin() const { return filename_iterator("/maps/", ".json"); } filename_iterator mapsEnd() const { return filename_iterator(); } diff --git a/src/FsUtils.cpp b/src/FsUtils.cpp new file mode 100644 index 0000000..4b94941 --- /dev/null +++ b/src/FsUtils.cpp @@ -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; +} diff --git a/src/FsUtils.h b/src/FsUtils.h new file mode 100644 index 0000000..c01a917 --- /dev/null +++ b/src/FsUtils.h @@ -0,0 +1,33 @@ +#pragma once +#include + +#include + +struct filename_iterator: public std::iterator { + public: + filename_iterator(); + filename_iterator(const char* path, const char* suffix); + void next(); + filename_iterator& operator++(); + filename_iterator& operator++(int) {filename_iterator ret = *this; ++(*this); return ret;} + bool operator==(const filename_iterator &other) const { return valid == other.valid;} + bool operator!=(const filename_iterator &other) const { return !(*this == other); } + const char* operator*() const { + if (!valid) { + return NULL; + } + return ret.c_str(); + } + + private: + String ret; + bool valid; + const char* suffix; + +#ifdef BOARD_ESP8266 + Dir dir; +#endif +#ifdef BOARD_ESP32 + File dir; +#endif +};