Skip to content

Commit

Permalink
We don't use the root dir member in embedded fs, and fix iterating on…
Browse files Browse the repository at this point in the history
… the root path.
  • Loading branch information
vector-of-bool committed Sep 7, 2018
1 parent e94373e commit 692890b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
23 changes: 11 additions & 12 deletions CMakeRC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -430,26 +426,29 @@ function(cmrc_add_resource_library name)

namespace {

std::pair<const cmrc::detail::index_type*, const cmrc::detail::directory*>
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;
$<JOIN:$<TARGET_PROPERTY:@libname@,CMRC_MAKE_DIRS>,
>
$<JOIN:$<TARGET_PROPERTY:@libname@,CMRC_MAKE_FILES>,
>
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@
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/iterate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down

0 comments on commit 692890b

Please sign in to comment.