-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathfile.cpp
38 lines (32 loc) · 1.03 KB
/
file.cpp
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
#include "file.hpp"
FileError::FileError(const std::string& description)
: BaseError{"File error", description}
{
}
auto openInput(const std::string& filename) -> std::ifstream
{
if (auto is = std::ifstream{filename, std::ios::binary})
return is;
else
throw FileError{"could not open input file " + filename};
}
auto loadStream(std::istream& is, std::size_t size) -> std::vector<std::uint8_t>
{
auto content = std::vector<std::uint8_t>{};
auto it = std::istreambuf_iterator{is};
for (auto i = std::size_t{}; i < size && it != std::istreambuf_iterator<char>{}; i++, ++it)
content.push_back(*it);
return content;
}
auto loadFile(const std::string& filename, std::size_t size) -> std::vector<std::uint8_t>
{
auto is = openInput(filename);
return loadStream(is, size);
}
auto openOutput(const std::string& filename) -> std::ofstream
{
if (auto os = std::ofstream{filename, std::ios::binary})
return os;
else
throw FileError{"could not open output file " + filename};
}