Line data Source code
1 : #include "filesystem.hpp"
2 :
3 : #include <fstream>
4 :
5 : #include "result.hpp"
6 :
7 : namespace wren::utils::fs {
8 :
9 0 : auto file_exists_in_dir(const std::filesystem::path& root,
10 : const std::filesystem::path& file)
11 : -> expected<std::filesystem::path> {
12 0 : if (std::filesystem::exists(root / file)) {
13 0 : return root / file;
14 : }
15 :
16 0 : for (const auto& dir : std::filesystem::recursive_directory_iterator(root)) {
17 0 : if (std::filesystem::exists(dir / file)) {
18 0 : return dir / file;
19 : }
20 : }
21 :
22 0 : return std::unexpected(
23 0 : std::make_error_code(std::errc::no_such_file_or_directory));
24 0 : }
25 :
26 0 : auto read_file_to_string(const std::filesystem::path& path) -> std::string {
27 0 : std::ifstream file(path);
28 0 : std::stringstream result;
29 0 : result << file.rdbuf();
30 0 : return result.str();
31 0 : }
32 :
33 0 : auto read_file_to_bin(const std::filesystem::path& path)
34 : -> std::vector<uint8_t> {
35 0 : std::ifstream file(path, std::ios::binary);
36 :
37 0 : std::streampos size;
38 0 : file.seekg(0, std::ios::end);
39 0 : size = file.tellg();
40 0 : file.seekg(0, std::ios::beg);
41 :
42 0 : std::vector<uint8_t> buf;
43 0 : buf.reserve(size);
44 :
45 0 : buf.insert(buf.begin(), std::istreambuf_iterator<char>(file),
46 0 : std::istreambuf_iterator<char>());
47 :
48 0 : return buf;
49 0 : }
50 :
51 : } // namespace wren::utils::fs
|