From 692890b61220c4889ad84a0bac076e9c4d3101c1 Mon Sep 17 00:00:00 2001 From: vector-of-bool Date: Fri, 7 Sep 2018 00:52:59 -0600 Subject: [PATCH] We don't use the root dir member in embedded fs, and fix iterating on the root path. --- CMakeRC.cmake | 23 +++++++++++------------ tests/CMakeLists.txt | 2 +- tests/iterate.cpp | 3 +++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CMakeRC.cmake b/CMakeRC.cmake index 8871ba5..1b231b7 100644 --- a/CMakeRC.cmake +++ b/CMakeRC.cmake @@ -269,7 +269,7 @@ inline std::string normalize_path(std::string path) { while (path.find("/") == 0) { path.erase(path.begin()); } - while (path.rfind("/") == path.size() - 1) { + while (!path.empty() && (path.rfind("/") == path.size() - 1)) { path.pop_back(); } auto off = path.npos; @@ -320,7 +320,6 @@ using directory_iterator = detail::directory::iterator; class embedded_filesystem { // Never-null: const cmrc::detail::index_type* _index; - const cmrc::detail::directory* _root; const detail::file_or_directory* _get(std::string path) const { path = detail::normalize_path(path); auto found = _index->find(path); @@ -332,12 +331,9 @@ class embedded_filesystem { } public: - embedded_filesystem(const detail::index_type& index, const cmrc::detail::directory& dir) + explicit embedded_filesystem(const detail::index_type& index) : _index(&index) - , _root(&dir) - { - (void)_root; - } + {} file open(const std::string& path) const { auto entry_ptr = _get(path); @@ -430,26 +426,29 @@ function(cmrc_add_resource_library name) namespace { - std::pair - get_root_dir() { + const cmrc::detail::index_type& + get_root_index() { static cmrc::detail::directory root_directory_; + static cmrc::detail::file_or_directory root_directory_fod{root_directory_}; static cmrc::detail::index_type root_index; + root_index.emplace("", &root_directory_fod); struct dir_inl { class cmrc::detail::directory& directory; }; dir_inl root_directory_dir{root_directory_}; + (void)root_directory_dir; $, > $, > - return std::make_pair(&root_index, &root_directory_); + return root_index; } } cmrc::embedded_filesystem get_filesystem() { - static auto pair = get_root_dir(); - return cmrc::embedded_filesystem{*pair.first, *pair.second}; + static auto& index = get_root_index(); + return cmrc::embedded_filesystem{index}; } } // @ARG_NAMESPACE@ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a0b8c86..1a55c79 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,7 +68,7 @@ cmrc_add_test( cmrc_add_test( NAME iterate - PASS_REGEX "^file_a.txt\nfile_b.txt\n$" + PASS_REGEX "^subdir_a\nfile_a.txt\nfile_b.txt\n$" RESOURCES subdir_a/subdir_b/file_a.txt subdir_a/subdir_b/file_b.txt diff --git a/tests/iterate.cpp b/tests/iterate.cpp index af137a4..728995a 100644 --- a/tests/iterate.cpp +++ b/tests/iterate.cpp @@ -6,6 +6,9 @@ CMRC_DECLARE(iterate); int main() { auto fs = cmrc::iterate::get_filesystem(); + for (auto&& entry : fs.iterate_directory("")) { + std::cout << entry.filename() << '\n'; + } for (auto&& entry : fs.iterate_directory("subdir_a/subdir_b")) { std::cout << entry.filename() << '\n'; }