-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.mpp
More file actions
174 lines (154 loc) · 5.74 KB
/
File.mpp
File metadata and controls
174 lines (154 loc) · 5.74 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
export module CppUtils.FileSystem.File;
import std;
import CppUtils.Type;
export namespace CppUtils::FileSystem
{
struct Error
{
enum class Type
{
OpenFailed,
ReadFailed,
CallbackError
} type;
std::string_view message;
};
inline auto forFiles(
const std::filesystem::path& directoryPath,
auto&& function,
bool recursively = false) -> void
{
if (not std::filesystem::exists(directoryPath) or not std::filesystem::is_directory(directoryPath))
return;
if (recursively)
{
for (const auto& directoryEntry : std::filesystem::recursive_directory_iterator(directoryPath))
if (const auto& path = directoryEntry.path(); not std::filesystem::is_directory(path))
function(path);
}
else
for (const auto& directoryEntry : std::filesystem::directory_iterator(directoryPath))
if (const auto& path = directoryEntry.path(); not std::filesystem::is_directory(path))
function(path);
}
inline auto forFilesWithExtension(
const std::filesystem::path& directoryPath,
std::string_view ext,
auto&& function,
bool recursively = false) -> void
{
forFiles(directoryPath, [&ext, function = std::forward<decltype(function)>(function)](auto&& filePath) -> void {
if (filePath.extension() == ext)
function(filePath);
}, recursively);
}
template<class T = std::span<const std::byte>>
inline auto readByBlocks(
const std::filesystem::path& filePath,
auto&& function,
std::size_t blockSize = 4'096) -> std::expected<void, Error>
{
auto file = std::ifstream{filePath, std::ios::binary};
if (not file.is_open())
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
for (auto buffer = std::vector<char>(blockSize); file.good();)
{
file.read(std::data(buffer), static_cast<std::streamsize>(blockSize));
if (file.bad())
return std::unexpected{Error{Error::Type::ReadFailed, "Hardware read error"}};
const auto bytesRead = static_cast<std::size_t>(file.gcount());
if (bytesRead == 0)
break;
auto block = [&]() {
if constexpr (std::same_as<T, std::string_view>)
return std::string_view{std::data(buffer), bytesRead};
else
return std::as_bytes(std::span{std::data(buffer), bytesRead});
}();
using ResultType = std::invoke_result_t<decltype(function), decltype(block)>;
if constexpr (Type::Specializes<ResultType, std::expected>)
{
if (auto result = function(block); not result)
return std::unexpected{Error{Error::Type::CallbackError, result.error()}};
}
else
function(block);
if (bytesRead < blockSize)
break;
}
return {};
}
namespace Binary
{
[[nodiscard]] inline auto readByBlocks(const std::filesystem::path& filePath, auto&& function, std::size_t blockSize = 4'096)
{
return FileSystem::readByBlocks<std::span<const std::byte>>(filePath, std::forward<decltype(function)>(function), blockSize);
}
template<Type::TriviallyCopyable T>
inline auto write(const std::filesystem::path& filePath, const T& buffer) -> void
{
auto file = std::ofstream{filePath, std::ios::binary};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
file.write(reinterpret_cast<const char*>(std::addressof(buffer)), sizeof(buffer));
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline auto read(const std::filesystem::path& filePath) -> T
{
auto file = std::ifstream{filePath, std::ios::binary};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
auto buffer = T{};
file.read(reinterpret_cast<char*>(std::addressof(buffer)), sizeof(buffer));
return buffer;
}
template<Type::TriviallyCopyable T>
inline auto writeVector(const std::filesystem::path& filePath, const std::vector<T>& vector) -> void
{
auto file = std::ofstream{filePath, std::ios::binary};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
file.write(reinterpret_cast<const char*>(std::data(vector)), static_cast<std::streamsize>(std::size(vector) * sizeof(T)));
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline auto readVector(const std::filesystem::path& filePath) -> std::vector<T>
{
auto file = std::ifstream{filePath, std::ios::binary};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
const auto nbElements = std::filesystem::file_size(filePath) / sizeof(T);
auto vector = std::vector<T>{};
vector.resize(nbElements);
file.read(reinterpret_cast<char*>(std::data(vector)), static_cast<std::streamsize>(nbElements * sizeof(T)));
return vector;
}
}
namespace String
{
[[nodiscard]] inline auto readByBlocks(const std::filesystem::path& filePath, auto&& function, std::size_t blockSize = 4'096)
{
return FileSystem::readByBlocks<std::string_view>(filePath, std::forward<decltype(function)>(function), blockSize);
}
inline auto write(const std::filesystem::path& filePath, std::string_view content) -> void
{
auto file = std::ofstream{filePath};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
file << content;
}
inline auto append(const std::filesystem::path& filePath, std::string_view content) -> void
{
auto file = std::ofstream{filePath, std::ios::app};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
file << content;
}
[[nodiscard]] inline auto read(const std::filesystem::path& filePath) -> std::string
{
auto file = std::ifstream{filePath};
if (not file.is_open())
throw std::runtime_error{"Failed to open " + filePath.string() + " file"};
return std::string{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
}
}
}