-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathread_file.hpp
46 lines (39 loc) · 1.1 KB
/
read_file.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <filesystem>
#include <fstream>
#include <system_error>
#include <qtils/outcome.hpp>
namespace kagome {
template <typename T>
concept StandardLayoutPointer =
std::is_standard_layout_v<std::remove_pointer_t<T>>;
template <typename T>
concept ByteContainer = //
requires(T t, std::streampos pos) {
{ t.data() } -> StandardLayoutPointer;
{ t.size() } -> std::convertible_to<std::streamsize>;
{ t.resize(pos) };
{ t.clear() };
};
template <ByteContainer Out>
outcome::result<void> readFile(Out &out, const std::filesystem::path &path) {
std::ifstream file{path, std::ios::binary | std::ios::ate};
if (not file.good()) {
out.clear();
return std::errc{errno};
}
out.resize(file.tellg());
file.seekg(0);
file.read(reinterpret_cast<char *>(out.data()), out.size());
if (not file.good()) {
out.clear();
return std::errc{errno};
}
return outcome::success();
}
} // namespace kagome