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 "file.h"
#include <fstream>
namespace fs = std::filesystem;
using namespace std::string_literals;
std::string Reichwein::File::getFile(const fs::path& filename)
{
std::ifstream file(filename.string(), std::ios::in | std::ios::binary);
if (file.is_open()) {
file.seekg(0, std::ios::end);
std::ifstream::pos_type fileSize = file.tellg();
if (fileSize < 0) { // on pseudo-files (e.g. /proc/*), this doesn't work. Use a slower workaround:
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
file.seekg(0, std::ios::beg);
std::string bytes(fileSize, ' ');
file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
return bytes;
} else {
throw std::runtime_error("Opening "s + filename.string() + " for reading");
}
}
void Reichwein::File::setFile(const fs::path& filename, const std::string& s)
{
Reichwein::File::setFile(filename, s.data(), s.size());
}
void Reichwein::File::setFile(const fs::path& filename, const char* data, size_t size)
{
std::ofstream file(filename.string(), std::ios::out | std::ios::binary);
if (file.is_open()) {
file.write(data, size);
} else {
throw std::runtime_error("Opening "s + filename.string() + " for writing");
}
}
void Reichwein::File::setFile(const fs::path& filename, const std::vector<uint8_t>& data)
{
Reichwein::File::setFile(filename, reinterpret_cast<const char*>(data.data()), data.size());
}
|