Repository navigation
Expand file tree
/
Copy pathFile.mpp
More file actions
208 lines (186 loc) · 7.7 KB
/
Copy pathFile.mpp
File metadata and controls
208 lines (186 loc) · 7.7 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
export module CppUtils.FileSystem.File;
import std;
import CppUtils.Type;
export namespace CppUtils::FileSystem
{
struct Error
{
enum class Type
{
OpenFailed,
ReadFailed,
WriteFailed,
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()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
for (auto buffer = std::vector<char>(blockSize); file.good();)
{
file.read(std::ranges::data(buffer), static_cast<std::streamsize>(blockSize));
if (file.bad()) [[unlikely]]
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::ranges::data(buffer), bytesRead};
else
return std::as_bytes(std::span{std::ranges::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);
}
inline auto write(const std::filesystem::path& filePath, const Type::TriviallyCopyable auto& buffer, bool isAppend = false) -> std::expected<void, Error>
{
const auto openMode = isAppend ? (std::ios::binary | std::ios::app) : (std::ios::binary | std::ios::trunc);
auto file = std::ofstream{filePath, openMode};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
file.write(reinterpret_cast<const char*>(std::addressof(buffer)), sizeof(buffer));
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::WriteFailed, "Hardware write error"}};
return {};
}
inline auto append(const std::filesystem::path& filePath, const Type::TriviallyCopyable auto& buffer) -> std::expected<void, Error>
{
return write(filePath, buffer, true);
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline auto read(const std::filesystem::path& filePath) -> std::expected<T, Error>
{
auto file = std::ifstream{filePath, std::ios::binary};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
auto buffer = T{};
file.read(reinterpret_cast<char*>(std::addressof(buffer)), sizeof(buffer));
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::ReadFailed, "Hardware read error"}};
return buffer;
}
template<Type::TriviallyCopyable T>
inline auto writeVector(const std::filesystem::path& filePath, const std::vector<T>& vector, bool isAppend = false) -> std::expected<void, Error>
{
const auto openMode = isAppend ? (std::ios::binary | std::ios::app) : (std::ios::binary | std::ios::trunc);
auto file = std::ofstream{filePath, openMode};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
file.write(reinterpret_cast<const char*>(std::ranges::data(vector)), static_cast<std::streamsize>(std::ranges::size(vector) * sizeof(T)));
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::WriteFailed, "Hardware write error"}};
return {};
}
template<Type::TriviallyCopyable T>
inline auto appendVector(const std::filesystem::path& filePath, const std::vector<T>& vector) -> std::expected<void, Error>
{
return writeVector(filePath, vector, true);
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline auto readVector(const std::filesystem::path& filePath) -> std::expected<std::vector<T>, Error>
{
auto file = std::ifstream{filePath, std::ios::binary};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
auto errorCode = std::error_code{};
const auto fileSize = std::filesystem::file_size(filePath, errorCode);
if (errorCode) [[unlikely]]
return std::unexpected{Error{Error::Type::ReadFailed, "Failed to get file size"}};
const auto elementCount = fileSize / sizeof(T);
auto vector = std::vector<T>{};
vector.resize(elementCount);
if (elementCount > 0uz)
{
file.read(reinterpret_cast<char*>(std::ranges::data(vector)), static_cast<std::streamsize>(elementCount * sizeof(T)));
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::ReadFailed, "Hardware read error"}};
}
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, bool isAppend = false) -> std::expected<void, Error>
{
const auto openMode = isAppend ? (std::ios::out | std::ios::app) : (std::ios::out | std::ios::trunc);
auto file = std::ofstream{filePath, openMode};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
file << content;
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::WriteFailed, "Hardware write error"}};
return {};
}
inline auto append(const std::filesystem::path& filePath, std::string_view content) -> std::expected<void, Error>
{
return write(filePath, content, true);
}
[[nodiscard]] inline auto read(const std::filesystem::path& filePath) -> std::expected<std::string, Error>
{
auto file = std::ifstream{filePath};
if (not file.is_open()) [[unlikely]]
return std::unexpected{Error{Error::Type::OpenFailed, "Failed to open file"}};
auto content = std::string{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
if (file.bad()) [[unlikely]]
return std::unexpected{Error{Error::Type::ReadFailed, "Hardware read error"}};
return content;
}
}
}