forked from pisa-engine/pisa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpp
More file actions
52 lines (42 loc) · 1.37 KB
/
Copy pathio.cpp
File metadata and controls
52 lines (42 loc) · 1.37 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
#include <fstream>
#include <fmt/format.h>
#include "io.hpp"
namespace pisa::io {
NoSuchFile::NoSuchFile(std::string const& file)
: m_message(fmt::format("No such file: {}", file)) {}
[[nodiscard]] auto NoSuchFile::what() const noexcept -> char const* {
return m_message.c_str();
}
auto resolve_path(std::string const& file) -> std::filesystem::path {
std::filesystem::path p(file);
if (not std::filesystem::exists(p)) {
throw NoSuchFile(file);
}
return p;
}
auto read_string_vector(std::string const& filename) -> std::vector<std::string> {
std::vector<std::string> vec;
std::ifstream is(filename);
std::string line;
while (std::getline(is, line)) {
vec.push_back(std::move(line));
}
return vec;
}
auto load_data(std::string const& data_file) -> std::vector<char> {
std::vector<char> data;
std::ifstream in(data_file.c_str(), std::ios::binary);
in.seekg(0, std::ios::end);
std::streamsize size = in.tellg();
in.seekg(0, std::ios::beg);
data.resize(size);
if (not in.read(data.data(), size)) {
throw std::runtime_error("Failed reading " + data_file);
}
return data;
}
void write_data(std::string const& data_file, std::span<std::byte const> bytes) {
std::ofstream os(data_file);
os.write(reinterpret_cast<char const*>(bytes.data()), bytes.size());
}
} // namespace pisa::io