Skip to content

Commit

Permalink
Add get_as_string() and get_as_raw_ptr()
Browse files Browse the repository at this point in the history
This change adds support for directly fetching resource data as std::string
as well as raw "const char *" combined with std::size_t. This allows
cleaner code in use cases where the existing file interface is
overkill.
  • Loading branch information
technoyes committed Aug 28, 2023
1 parent 952ffdd commit fc3b502
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
21 changes: 19 additions & 2 deletions CMakeRC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public:
: _index(&index)
{}

file open(const std::string& path) const {
const detail::file_data& _get_file_data(const std::string& path) const {
auto entry_ptr = _get(path);
if (!entry_ptr || !entry_ptr->is_file()) {
#ifdef CMRC_NO_EXCEPTIONS
Expand All @@ -350,10 +350,27 @@ public:
throw std::system_error(make_error_code(std::errc::no_such_file_or_directory), path);
#endif
}
auto& dat = entry_ptr->as_file();
return entry_ptr->as_file();
}

file open(const std::string& path) const {
auto& dat = _get_file_data(path);
return file{dat.begin_ptr, dat.end_ptr};
}

std::string get_as_string(const std::string& path) const {
auto& dat = _get_file_data(path);
auto& f = file{dat.begin_ptr, dat.end_ptr};
return std::string{dat.begin_ptr, f.size()};
}

const char *get_as_raw_ptr(const std::string& path, std::size_t& file_size) const {
auto& dat = _get_file_data(path);
auto& f = file{dat.begin_ptr, dat.end_ptr};
file_size = f.size();
return dat.begin_ptr;
}

bool is_file(const std::string& path) const noexcept {
auto entry_ptr = _get(path);
return entry_ptr && entry_ptr->is_file();
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ statically allocated resource library data.
- `open(const std::string& path) -> cmrc::file` - Opens and returns a
non-directory `file` object at `path`, or throws `std::system_error()` on
error.
- `get_as_string(const std::string& path) -> std::string` - Returns the
non-directory data at `path` as a `std::string`, or throws `std::system_error()`
on error.
- `get_as_raw_ptr(const std::string& path, std::size_t& file_size) -> const char *` -
Returns the non-directory data at `path` as a raw `const char *` pointer and updates
the supplied `file_size` reference with the data size, or throws
`std::system_error()` on error.
- `is_file(const std::string& path) -> bool` - Returns `true` if the given
`path` names a regular file, `false` otherwise.
- `is_directory(const std::string& path) -> bool` - Returns `true` if the given
Expand Down Expand Up @@ -179,6 +186,10 @@ the library using `cmrc_add_resources` with the name of the library and the
paths to any additional resources that you wish to compile in. This way you can
lazily add resources to the library as your configure script runs.
Resources are always stored with an extra trailing `null` byte to facilitate
the direct use of data as C strings if needed. The `null` byte is not a part of
the actual data and does not count towards its size.
Both `cmrc_add_resource_library` and `cmrc_add_resources` take two additional
keyword parameters:
Expand Down
14 changes: 14 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ cmrc_add_test(
subdir_a/subdir_b/file_b.txt
)

cmrc_add_test(
NAME simple_string
PASS_REGEX "^Hello, world!"
RESOURCES
hello.txt
)

cmrc_add_test(
NAME simple_raw_ptr
PASS_REGEX "^Hello, world!"
RESOURCES
hello.txt
)

cmrc_add_test(
NAME flower
RESOURCES flower.jpg
Expand Down
12 changes: 12 additions & 0 deletions tests/simple_raw_ptr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <cmrc/cmrc.hpp>

#include <iostream>

CMRC_DECLARE(simple_raw_ptr);

int main() {
auto fs = cmrc::simple_raw_ptr::get_filesystem();
std::size_t sz;
const char *data = fs.get_as_raw_ptr("hello.txt", sz);
std::cout << std::string(data, sz) << '\n';
}
11 changes: 11 additions & 0 deletions tests/simple_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <cmrc/cmrc.hpp>

#include <iostream>

CMRC_DECLARE(simple_string);

int main() {
auto fs = cmrc::simple_string::get_filesystem();
std::string a_string = fs.get_as_string("hello.txt");
std::cout << a_string << '\n';
}

0 comments on commit fc3b502

Please sign in to comment.