OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
io_utilities.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// I / O U T I L I T I E S
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "pch.h"
12
13namespace opennn
14{
15
17// Thread-safe positional reader. On POSIX uses pread(); on Windows uses
18// ReadFile with OVERLAPPED so the offset is per-call and not shared state.
20{
21public:
22 FileReader() = default;
24
25 FileReader(const FileReader&) = delete;
26 FileReader& operator=(const FileReader&) = delete;
29
31 void open(const filesystem::path& path);
32
34 void close();
35
37 [[nodiscard]] bool is_open() const;
38
43 void read_at(void* buffer, size_t bytes, uint64_t offset) const;
44
46 [[nodiscard]] uint64_t file_size() const;
47
48private:
49#if defined(_WIN32)
50 void* handle_ = nullptr;
51#else
52 int fd_ = -1;
53#endif
54};
55
56
58// Streaming writer that lands on a .tmp file and renames atomically when
59// finish_with_rename() succeeds. Drops the .tmp on destruction otherwise.
61{
62public:
63 FileWriter() = default;
65
66 FileWriter(const FileWriter&) = delete;
67 FileWriter& operator=(const FileWriter&) = delete;
70
72 void open(const filesystem::path& tmp_path);
73
75 [[nodiscard]] bool is_open() const;
76
78 void write(const void* buffer, size_t bytes);
79
81 void finish_with_rename(const filesystem::path& final_path);
82
84 void abort();
85
86private:
87 filesystem::path tmp_path_;
88 ofstream stream_;
89 bool finalized_ = false;
90};
91
92
94void atomic_rename(const filesystem::path& from, const filesystem::path& to);
95
96
98// CSV reader. Loads a file (or string) into a single buffer and tokenizes
99// each non-empty line into a vector of string_views into that buffer.
100// The buffer lives in Result, so the views stay valid as long as Result does.
101// Handles UTF-8 BOM, quoted fields (the separator inside quotes is ignored),
102// and CRLF line endings.
104{
105public:
106
108 struct Config
109 {
110 char separator = ',';
111 function<void(string_view)> line_validator;
112 };
113
115 struct Result
116 {
117 string buffer;
118 vector<vector<string_view>> rows;
119 };
120
122 explicit CsvReader(Config c) : config(std::move(c)) {}
123
125 [[nodiscard]] Result read(const filesystem::path& path) const;
126
128 [[nodiscard]] Result read_string(string_view csv_text) const;
129
130private:
131
132 Config config;
133
134 void parse(Result& out) const;
135};
136
137}
138
139// OpenNN: Open Neural Networks Library.
140// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
141// Licensed under the GNU Lesser General Public License v2.1 or later.
CsvReader(Config c)
Constructs a reader with the given configuration.
Definition io_utilities.h:122
Result read(const filesystem::path &path) const
Reads and parses the CSV file at the given path.
Result read_string(string_view csv_text) const
Parses a CSV string already held in memory.
void close()
Closes the underlying file handle, if any.
void read_at(void *buffer, size_t bytes, uint64_t offset) const
Reads bytes from a specific offset into the provided buffer (thread-safe).
bool is_open() const
Returns true while the underlying handle is open.
FileReader(const FileReader &)=delete
void open(const filesystem::path &path)
Opens the file at the given path for reading.
uint64_t file_size() const
Returns the total size of the open file in bytes.
FileReader & operator=(FileReader &&)=delete
FileReader(FileReader &&)=delete
FileReader & operator=(const FileReader &)=delete
FileReader()=default
FileWriter(FileWriter &&)=delete
FileWriter & operator=(const FileWriter &)=delete
void write(const void *buffer, size_t bytes)
Appends the given byte range to the open file.
void abort()
Closes and deletes the tmp file, discarding any written data.
FileWriter & operator=(FileWriter &&)=delete
FileWriter()=default
void open(const filesystem::path &tmp_path)
Opens a temporary file to which subsequent write() calls are appended.
bool is_open() const
Returns true while the underlying stream is open.
FileWriter(const FileWriter &)=delete
void finish_with_rename(const filesystem::path &final_path)
Closes the stream and atomically renames the tmp file to final_path.
Definition adaptive_moment_estimation.h:14
void atomic_rename(const filesystem::path &from, const filesystem::path &to)
Atomically renames a file, replacing the destination if needed.
Reader configuration: field separator and an optional per-line validator.
Definition io_utilities.h:109
function< void(string_view)> line_validator
Definition io_utilities.h:111
char separator
Definition io_utilities.h:110
Parsed CSV result; owns the source buffer that backs all row views.
Definition io_utilities.h:116
vector< vector< string_view > > rows
Definition io_utilities.h:118
string buffer
Definition io_utilities.h:117