Loading...
Searching...
No Matches
io_utilities.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
7
8#include <type_traits>
9#include <functional>
10
11namespace opennn
12{
13
14void download_if_missing(const filesystem::path&, const string&);
15void download_files_if_missing(const filesystem::path& directory,
16 string_view base_url,
17 const vector<string_view>& filenames);
18
19string read_text_file(const filesystem::path&);
20
21vector<filesystem::path> list_files(const filesystem::path& directory,
22 std::function<bool(const filesystem::path&)> predicate);
23vector<filesystem::path> list_directories(const filesystem::path& directory,
24 std::function<bool(const filesystem::path&)> predicate);
25bool is_file_current(const filesystem::path& file,
26 const vector<filesystem::path>& sources,
27 uintmax_t expected_size = 0);
28
30{
31public:
32 FileReader() = default;
34
35 FileReader(const FileReader&) = delete;
36 FileReader& operator=(const FileReader&) = delete;
39
40 void open(const filesystem::path&);
41 void close();
42 bool is_open() const;
43
44 void read_at(span<std::byte>, uint64_t offset) const;
45
46 template <typename T, size_t Extent>
47 requires (!is_const_v<T> && is_trivially_copyable_v<T>)
48 void read_at(const span<T, Extent> buffer, const uint64_t offset) const
49 {
50 read_at(span<std::byte>(as_writable_bytes(buffer)), offset);
51 }
52
53 uint64_t file_size() const;
54
55private:
56#if defined(_WIN32)
57 void* handle_ = nullptr;
58#else
59 int fd_ = -1;
60#endif
61};
62
64 const vector<Index>& sample_indices,
65 Index samples_number,
66 uint64_t record_values,
67 Index source_offset,
68 Index values_number,
69 span<float> output,
70 Index output_stride,
71 Index output_offset,
72 string_view context);
73
75{
76public:
77 FileWriter() = default;
79
80 FileWriter(const FileWriter&) = delete;
81 FileWriter& operator=(const FileWriter&) = delete;
84
85 void open(const filesystem::path&);
86
87 void write(span<const std::byte>);
88
89 template <typename T, size_t Extent>
90 requires is_trivially_copyable_v<remove_const_t<T>>
91 void write(const span<T, Extent> buffer)
92 {
93 write(span<const std::byte>(as_bytes(buffer)));
94 }
95
96 void finish_with_rename(const filesystem::path&);
97
98private:
99 filesystem::path tmp_path_;
100 ofstream stream_;
101};
102
103template <typename T>
104concept RawStorable = is_trivially_copyable_v<T>;
105
106bool read_binary_value(istream& stream, RawStorable auto& value)
107{
108 return bool(stream.read(reinterpret_cast<char*>(&value), sizeof(value)));
109}
110
111bool read_binary_values(istream& stream, RawStorable auto&... values)
112{
113 return (read_binary_value(stream, values) && ...);
114}
115
116void write_binary_value(FileWriter& writer, const RawStorable auto& value)
117{
118 writer.write(span(&value, 1));
119}
120
121inline bool read_binary_string(istream& stream, string& value)
122{
123 uint64_t size = 0;
124 if (!read_binary_value(stream, size)
125 || size > uint64_t(numeric_limits<streamsize>::max()))
126 return false;
127
128 value.resize(size_t(size));
129 return size == 0 || bool(stream.read(value.data(), streamsize(size)));
130}
131
132inline void write_binary_string(FileWriter& writer, const string& value)
133{
134 write_binary_value(writer, uint64_t(value.size()));
135 writer.write(span(value));
136}
137
139{
140public:
141 FileMapping() = default;
143
144 FileMapping(const FileMapping&) = delete;
147 FileMapping& operator=(FileMapping&&) noexcept;
148
149 bool map(const filesystem::path&);
150 void reset();
151
152 const char* data() const { return data_; }
153 size_t size() const { return size_; }
154
155private:
156 const char* data_ = nullptr;
157 size_t size_ = 0;
158#if defined(_WIN32)
159 void* file_handle_ = nullptr;
160 void* mapping_handle_ = nullptr;
161#else
162 int fd_ = -1;
163#endif
164};
165
166}
FileMapping & operator=(const FileMapping &)=delete
const char * data() const
Definition io_utilities.h:152
bool map(const filesystem::path &)
size_t size() const
Definition io_utilities.h:153
FileMapping(const FileMapping &)=delete
FileMapping(FileMapping &&) noexcept
Definition io_utilities.h:30
bool is_open() const
FileReader(const FileReader &)=delete
void read_at(span< std::byte >, uint64_t offset) const
uint64_t file_size() const
void read_at(const span< T, Extent > buffer, const uint64_t offset) const
Definition io_utilities.h:48
FileReader & operator=(FileReader &&)=delete
void open(const filesystem::path &)
FileReader(FileReader &&)=delete
FileReader & operator=(const FileReader &)=delete
FileReader()=default
Definition io_utilities.h:75
void open(const filesystem::path &)
FileWriter(FileWriter &&)=delete
FileWriter & operator=(const FileWriter &)=delete
void write(span< const std::byte >)
FileWriter & operator=(FileWriter &&)=delete
void finish_with_rename(const filesystem::path &)
void write(const span< T, Extent > buffer)
Definition io_utilities.h:91
FileWriter()=default
FileWriter(const FileWriter &)=delete
Definition io_utilities.h:104
Definition configuration.h:10
vector< filesystem::path > list_directories(const filesystem::path &directory, std::function< bool(const filesystem::path &)> predicate)
void write_binary_value(FileWriter &writer, const RawStorable auto &value)
Definition io_utilities.h:116
bool is_file_current(const filesystem::path &file, const vector< filesystem::path > &sources, uintmax_t expected_size=0)
vector< filesystem::path > list_files(const filesystem::path &directory, std::function< bool(const filesystem::path &)> predicate)
void download_files_if_missing(const filesystem::path &directory, string_view base_url, const vector< string_view > &filenames)
void download_if_missing(const filesystem::path &, const string &)
void read_int32_batch(const FileReader &, const vector< Index > &sample_indices, Index samples_number, uint64_t record_values, Index source_offset, Index values_number, span< float > output, Index output_stride, Index output_offset, string_view context)
void write_binary_string(FileWriter &writer, const string &value)
Definition io_utilities.h:132
string read_text_file(const filesystem::path &)
bool read_binary_value(istream &stream, RawStorable auto &value)
Definition io_utilities.h:106
bool read_binary_values(istream &stream, RawStorable auto &... values)
Definition io_utilities.h:111
bool read_binary_string(istream &stream, string &value)
Definition io_utilities.h:121