Repository navigation
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
69 lines (59 loc) · 2.24 KB
/
Copy pathfilesystem.cpp
File metadata and controls
69 lines (59 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "engine/framework/io/filesystem.h"
#include <fstream>
#include <sstream>
#include <stdexcept>
namespace engine::io {
bool is_existing_directory(const std::filesystem::path & path) {
std::error_code ec;
return std::filesystem::is_directory(path, ec);
}
bool is_existing_file(const std::filesystem::path & path) {
std::error_code ec;
return std::filesystem::is_regular_file(path, ec);
}
std::filesystem::path require_directory(const std::filesystem::path & path, std::string_view role) {
if (!is_existing_directory(path)) {
throw std::runtime_error("missing " + std::string(role) + " directory: " + path.string());
}
return std::filesystem::weakly_canonical(path);
}
std::filesystem::path require_file(const std::filesystem::path & path, std::string_view role) {
if (!is_existing_file(path)) {
throw std::runtime_error("missing " + std::string(role) + " file: " + path.string());
}
return std::filesystem::weakly_canonical(path);
}
std::optional<std::filesystem::path> find_first_existing(
const std::filesystem::path & root,
const std::vector<std::string> & relative_candidates) {
for (const auto & candidate : relative_candidates) {
const auto path = root / candidate;
if (is_existing_file(path) || is_existing_directory(path)) {
return std::filesystem::weakly_canonical(path);
}
}
return std::nullopt;
}
std::vector<std::filesystem::path> collect_existing(
const std::filesystem::path & root,
const std::vector<std::string> & relative_candidates) {
std::vector<std::filesystem::path> results;
results.reserve(relative_candidates.size());
for (const auto & candidate : relative_candidates) {
const auto path = root / candidate;
if (is_existing_file(path) || is_existing_directory(path)) {
results.push_back(std::filesystem::weakly_canonical(path));
}
}
return results;
}
std::string read_text_file(const std::filesystem::path & path) {
std::ifstream input(path, std::ios::binary);
if (!input) {
throw std::runtime_error("failed to open text file: " + path.string());
}
std::ostringstream buffer;
buffer << input.rdbuf();
return buffer.str();
}
} // namespace engine::io