-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSV.cppm
More file actions
355 lines (328 loc) · 11.1 KB
/
CSV.cppm
File metadata and controls
355 lines (328 loc) · 11.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
module;
/* C++ language support */
#include <concepts>
#include <utility>
#include <generator>
/* IO and filesystem */
#include <filesystem>
#include <fstream>
#include <iostream>
/* C++ containers */
#include <vector>
#include <span>
#include <string>
#include <string_view>
export module CSV;
import base;
template <typename T>
concept parsable_from_string =
std::convertible_to<std::string, T> || (
std::is_default_constructible_v<T> && requires(T t) { std::stringstream{} >> t; }
);
template <typename T>
concept parsable_to_string = requires(T const& t) { std::format("{}", t); };
export class CSV
{
public:
class record_view
: private std::span<std::string const>
{
private:
using base = std::span<std::string const>;
protected:
friend CSV;
using base::base;
public:
using base::size;
template <parsable_from_string T>
[[nodiscard]] auto
parse_as(size_t index) const -> T
{
if (index >= this->size()) {
throw LibraryException("Index out of bounds");
}
if constexpr (std::convertible_to<std::string, T>) {
return T{this->base::operator[](index)};
} else {
T result;
std::stringstream ss(this->base::operator[](index));
ss >> result;
if (ss.fail()) {
throw LibraryException("Error parsing field");
}
return result;
}
}
};
using string = std::string;
private:
using record = std::vector<string>;
static constexpr inline auto s_openmode = std::ios_base::binary | std::ios_base::in;
std::filesystem::path m_filepath{};
mutable std::ifstream m_file{};
std::ifstream::pos_type m_record_start_pos{};
record m_header{};
mutable std::vector<record> m_records{};
bool m_modified{};
public:
CSV() = delete;
CSV(const CSV&) = delete;
CSV(CSV&&) noexcept = default;
auto operator=(const CSV&) -> CSV& = delete;
auto operator=(CSV&&) noexcept -> CSV& = default;
~CSV()
{
try {
if (this->modified()) {
this->save_changes();
}
} catch (std::exception const& e) {
std::cerr << std::format("Error during destruction: {}\n", e.what());
std::terminate();
} catch (...) {
std::cerr << "Unknown error during destruction\n";
std::terminate();
}
}
CSV(
std::filesystem::path filepath,
std::string_view expected_header = ""
) : m_filepath(std::move(filepath))
{
namespace fs = std::filesystem;
if (!fs::exists(this->m_filepath)) {
std::ofstream(this->m_filepath, std::ios_base::binary).close();
}
this->m_file.open(this->m_filepath, s_openmode);
if (!this->m_file.is_open()) {
throw LibraryException("Cannot open file for reading");
}
bool const file_empty = fs::file_size(m_filepath) == 0;
bool const expected_empty = expected_header.empty();
if (file_empty && expected_empty) {
throw LibraryException("Empty file and empty header");
}
if (!file_empty) {
std::string header_line;
std::getline(this->m_file, header_line);
this->m_file >> std::ws;
this->m_record_start_pos = this->m_file.tellg();
if (!expected_empty && header_line != expected_header) {
throw LibraryException("Header mismatch");
}
this->m_header = parse_record_line(std::move(header_line));
} else {
this->m_header = parse_record_line(std::string{expected_header});
this->m_modified = true;
this->save_changes();
}
}
private:
[[nodiscard]] static auto parse_record_line(string record_line) -> record
{
while (!record_line.empty() && !std::isgraph(record_line.back())) {
record_line.pop_back();
}
record record{};
std::istringstream ss(std::move(record_line));
ss >> std::ws;
for (string field_str; std::getline(ss, field_str, ','); field_str.clear()) {
record.push_back(std::move(field_str));
}
if (!ss.eof() && ss.fail()) {
throw LibraryException("Error parsing record line");
}
return record;
}
[[nodiscard]] auto parsed_record_count() const -> size_t { return this->m_records.size(); }
[[nodiscard]] auto traverse_file() const -> std::generator<record>
{
#define assert(...) \
if (!(__VA_ARGS__)) throw LibraryException("Assertion failed: " #__VA_ARGS__)
if (this->all_parsed()) {
co_return;
}
assert(this->m_record_start_pos != 0);
assert(this->m_file.tellg() >= this->m_record_start_pos);
for (std::string record_line; std::getline(this->m_file, record_line); record_line.clear()) {
if (this->m_file.fail()) {
throw LibraryException("Error reading file");
}
this->m_file >> std::ws;
co_yield parse_record_line(std::move(record_line));
}
co_return;
#undef assert
}
void checkout_to(std::filesystem::path const& new_filepath, std::ifstream::pos_type new_record_start_pos) noexcept
{
namespace fs = std::filesystem;
try {
this->m_file.close();
fs::path old_filepath = this->m_filepath;
old_filepath += ".old";
fs::remove(old_filepath);
fs::rename(this->m_filepath, old_filepath);
fs::rename(new_filepath, this->m_filepath);
this->m_file.open(this->m_filepath, s_openmode);
if (!this->m_file.is_open()) {
throw LibraryException("Cannot open file for reading/writing");
}
this->m_file.seekg(new_record_start_pos);
this->m_record_start_pos = new_record_start_pos;
} catch (std::exception const& e) {
std::cerr << std::format("Error during checkout: {}\n", e.what());
std::cerr << "Terminating program...\n";
std::terminate();
} catch (...) {
std::cerr << "Unknown error during checkout\n";
std::cerr << "Terminating program...\n";
std::terminate();
}
}
public:
[[nodiscard]] auto get_header() const -> record_view { return this->m_header; }
[[nodiscard]] auto field_count() const -> size_t { return this->m_header.size(); }
[[nodiscard]] auto record_count() const -> size_t
{
if (!this->all_parsed()) {
throw LibraryException("Records not fully parsed");
}
return this->m_records.size();
}
[[nodiscard]] auto all_parsed() const -> bool { return this->m_file.eof(); }
[[nodiscard]] auto modified() const -> bool { return this->m_modified; }
[[nodiscard]] auto traverse_records(size_t from = 0) const -> std::generator<record_view>
{
for (auto const& existing_record : this->m_records | std::views::drop(from)) {
co_yield existing_record;
}
for (size_t index = this->parsed_record_count(); auto new_record : this->traverse_file()) {
this->m_records.push_back(std::move(new_record));
if (index < from) {
continue;
}
co_yield this->m_records.back();
}
co_return;
}
void parse_all_records()
{
for ([[maybe_unused]] auto const& _ : this->traverse_records(this->parsed_record_count())) {}
}
/* Clear all records. A checkout point */
void clear_records()
{
this->m_modified = true;
this->m_records.clear();
this->save_changes();
}
template <parsable_to_string... Ts>
void append_record(Ts&&... fields)
{
if (sizeof...(Ts) != this->field_count()) {
throw LibraryException("Field count mismatch");
}
if (!this->modified() || !this->all_parsed()) this->parse_all_records();
this->m_modified = true;
this->m_records.push_back({std::format("{}", std::forward<Ts>(fields))...});
}
template <parsable_to_string T>
void modify_at(size_t record_index, size_t field_index, T&& new_value)
{
if (this->all_parsed() && record_index >= this->record_count()) {
throw LibraryException("Record index out of bounds: `record_index` >= `record_count`");
}
if (!this->all_parsed() && record_index >= this->parsed_record_count()) {
throw LibraryException("Record index out of bounds: current record has not been parsed");
}
if (field_index >= this->field_count()) {
throw LibraryException("Field index out of bounds");
}
this->m_modified = true;
this->m_records[record_index][field_index] = std::format("{}", std::forward<T>(new_value));
}
template <parsable_to_string T>
void modify_at(size_t record_index, std::string_view field_name, T&& new_value)
{
auto const field_pos = std::ranges::find(this->m_header, field_name);
if (field_pos == this->m_header.end()) {
throw LibraryException("Field name not found");
}
auto const field_index = std::distance(this->m_header.begin(), field_pos);
this->modify_at(record_index, field_index, std::forward<T>(new_value));
}
template <parsable_to_string... Ts>
void modify_at(size_t record_index, Ts&&... new_values)
{
if (sizeof...(Ts) != this->field_count()) {
throw LibraryException("Field count mismatch");
}
if (this->all_parsed() && record_index >= this->record_count()) {
throw LibraryException("Record index out of bounds: `record_index` >= `record_count`");
}
if (!this->all_parsed() && record_index >= this->parsed_record_count()) {
throw LibraryException("Record index out of bounds: current record has not been parsed");
}
this->m_modified = true;
auto& record = this->m_records[record_index];
[&]<std::size_t... Is>(std::index_sequence<Is...>)
{
((record[Is] = std::format("{}", std::forward<Ts>(new_values))), ...);
}(std::index_sequence_for<Ts...>{});
}
void discard_changes() noexcept
{
try {
this->m_records.clear();
this->m_file.clear();
this->m_file.seekg(this->m_record_start_pos);
this->m_modified = false;
} catch (std::exception const& e) {
std::cerr << std::format("Error during discarding changes: {}\n", e.what());
std::cerr << "Terminating program...\n";
std::terminate();
} catch (...) {
std::cerr << "Unknown error during discarding changes\n";
std::cerr << "Terminating program...\n";
std::terminate();
}
}
/* Write changes to a new file. A checkout point */
void save_changes()
{
namespace fs = std::filesystem;
if (!this->modified()) return;
/* Create new file to save changes */
fs::path new_filepath = this->m_filepath;
new_filepath += ".new";
std::ofstream new_file(new_filepath, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
if (!new_file.is_open()) {
throw LibraryException("Cannot open file for saving changes");
}
/* Write changes to new file */
/* Write the header line */
for (bool is_first = true; auto const& field : this->m_header) {
auto const str = std::format("{}{}", is_first ? "" : ",", field);
new_file << str;
is_first = false;
}
new_file << '\n';
/* Get the position of the beginning of records */
auto const new_record_start_pos = new_file.tellp();
/* Write the records */
for (auto const& record : this->m_records) {
for (bool is_first = true; auto const& field : record) {
auto const str = std::format("{}{}", is_first ? "" : ",", field);
new_file << str;
is_first = false;
}
new_file << '\n';
}
new_file.close();
/* Finished. Clear the flag. */
this->m_modified = false;
/* Replace old file with new file */
this->checkout_to(new_filepath, new_record_start_pos);
}
}; // class CSV