From 98542d0c0bad45aaa4f6ce3242640576be7dd9d9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 7 Mar 2020 11:53:39 +1100 Subject: [PATCH] AP_ROMFS: added directory listing interface --- libraries/AP_ROMFS/AP_ROMFS.cpp | 18 ++++++++++++++++++ libraries/AP_ROMFS/AP_ROMFS.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/libraries/AP_ROMFS/AP_ROMFS.cpp b/libraries/AP_ROMFS/AP_ROMFS.cpp index 9f6a05097b..f87915e520 100644 --- a/libraries/AP_ROMFS/AP_ROMFS.cpp +++ b/libraries/AP_ROMFS/AP_ROMFS.cpp @@ -113,3 +113,21 @@ void AP_ROMFS::free(const uint8_t *data) ::free(const_cast(data)); #endif } + +/* + directory listing interface. Start with ofs=0. Returns pathnames + that match dirname prefix. Ends with nullptr return when no more + files found +*/ +const char *AP_ROMFS::dir_list(const char *dirname, uint16_t &ofs) +{ + const size_t dlen = strlen(dirname); + for ( ; ofs < ARRAY_SIZE(files); ofs++) { + if (strncmp(dirname, files[ofs].filename, dlen) == 0 && + files[ofs].filename[dlen] == '/') { + // found one + return files[ofs++].filename; + } + } + return nullptr; +} diff --git a/libraries/AP_ROMFS/AP_ROMFS.h b/libraries/AP_ROMFS/AP_ROMFS.h index 213f033d24..4dc6458b5c 100644 --- a/libraries/AP_ROMFS/AP_ROMFS.h +++ b/libraries/AP_ROMFS/AP_ROMFS.h @@ -16,6 +16,13 @@ public: // free returned data static void free(const uint8_t *data); + /* + directory listing interface. Start with ofs=0. Returns pathnames + that match dirname prefix. Ends with nullptr return when no more + files found + */ + static const char *dir_list(const char *dirname, uint16_t &ofs); + private: // find an embedded file static const uint8_t *find_file(const char *name, uint32_t &size);