Loading...
Searching...
No Matches
FileManager.inl
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5/*
6 * IOManager.inl
7 *
8 * Created on: 17 Jun 2020
9 * Author: Maxim Masterov
10 * Company: SURFSara
11 */
12
13#ifndef MANAGERS_IO_FILEMANAGER_INL_
14#define MANAGERS_IO_FILEMANAGER_INL_
15
16#include <cstdlib>
17
18namespace gpr {
19namespace io {
20
21inline std::vector<std::string> FileManager::splitString(const std::string &str,
22 std::string _regex)
23{
24 std::regex re(_regex);
25 std::sregex_token_iterator re_it(str.begin(), str.end(), re, -1);
26 return std::vector<std::string>(re_it, std::sregex_token_iterator());
27}
28
29template <typename T>
30void FileManager::convertStringToValue(
31 std::map<std::string, std::vector<std::string> > &input_data,
32 gpr::KeyValuePair<T> &parameters)
33{
34 if (input_data.find(parameters.key) != input_data.end()) {
35 if (typeid(parameters.value) == typeid(int))
36 parameters.value = std::atoi(input_data[parameters.key][0].c_str());
37 if (typeid(parameters.value) == typeid(double))
38 parameters.value = std::atof(input_data[parameters.key][0].c_str());
39 if (typeid(parameters.value) == typeid(bool)) {
40 const std::string &value = input_data[parameters.key][0];
41 parameters.value = value == "true" || value == "1";
42 }
43 }
44}
45
46template <typename T>
47void FileManager::convertStringToString(
48 std::map<std::string, std::vector<std::string> > &input_data,
49 gpr::KeyValuePair<T> &parameters)
50{
51 if (input_data.find(parameters.key) != input_data.end()) {
52 parameters.value = input_data[parameters.key][0];
53 }
54}
55
56template <typename T>
57void FileManager::convertStringToValueArray(
58 std::map<std::string, std::vector<std::string> > &input_data,
59 gpr::KeyValuePair<T> &parameters)
60{
61 if (input_data.find(parameters.key) != input_data.end()) {
62 // The destination is a fixed-extent array (cell_dimensions is
63 // double[9], dist_sp double[4], param_trans double[2]); the source is
64 // however many comma-separated fields the input file happens to carry.
65 // Copy the overlap and report the surplus rather than writing past the
66 // member into its neighbours.
67 const size_t capacity = std::extent<T>::value;
68 const std::vector<std::string> &values = input_data[parameters.key];
69 const size_t count = std::min(capacity, values.size());
70 for (size_t n = 0; n < count; ++n) {
71 if (typeid(parameters.value[0]) == typeid(int))
72 parameters.value[n] = std::atoi(values[n].c_str());
73 if (typeid(parameters.value[0]) == typeid(double))
74 parameters.value[n] = std::atof(values[n].c_str());
75 }
76 if (values.size() > capacity) {
78 err_man << "Key '" << parameters.key << "' supplies "
79 << values.size() << " values for an array of " << capacity
80 << "; extra values ignored.\n";
81 }
82 }
83}
84
85template <typename T>
86void FileManager::convertStringToStringArray(
87 std::map<std::string, std::vector<std::string> > &input_data,
88 gpr::KeyValuePair<T> &parameters)
89{
90 if (input_data.find(parameters.key) != input_data.end()) {
91 for (size_t n = 0; n < input_data[parameters.key].size(); ++n) {
92 parameters.value[n] = input_data[parameters.key][n];
93 }
94 }
95}
96
97template <typename T>
98void FileManager::readDataFile(const std::string file_name,
99 std::map<std::string, T *> &data)
100{
101 std::ifstream is;
102 std::string line;
103 char split_char = ' ';
104
105 is.open(file_name.c_str(), std::ios::in);
106
107 if (!is.is_open()) {
108 gpr::io::ErrorManager err_man;
109 err_man << "Input file '" << file_name << "' cannot be opened.\n";
110 return;
111 }
112
113 // Read the file by data sets:
114 // first - field name and dimensions
115 // second - values
116 while (std::getline(is, line)) {
117 if (!line.empty()) {
118 std::vector<std::string> split_line;
119 std::string key = "";
120
121 split_line = splitString(line, std::string(1, split_char));
122
123 // A header is "<name> <rows> <cols>". Anything shorter is not one:
124 // skip it rather than indexing the split out of range.
125 if (split_line.size() < 3) continue;
126
127 key = split_line[0];
128
129 if (data.find(key) != data.end()) {
130 T *field = data[key];
131 // Dimensions are attacker-controlled text. Reject anything
132 // negative or unparseable before it reaches resize(), where
133 // Index_t's unsignedness would turn -1 into 4294967295.
134 const long rows_in = std::atol(split_line[1].c_str());
135 const long cols_in = std::atol(split_line[2].c_str());
136 if (rows_in < 0 || cols_in < 0) {
137 gpr::io::ErrorManager err_man;
138 err_man << "Field '" << key << "' declares negative "
139 << "dimensions (" << rows_in << " x " << cols_in
140 << "); skipped.\n";
141 continue;
142 }
143 const gpr::Index_t num_elts_i =
144 static_cast<gpr::Index_t>(rows_in);
145 const gpr::Index_t num_elts_j =
146 static_cast<gpr::Index_t>(cols_in);
147
148 field->resize(num_elts_i, num_elts_j);
149
150 // Get values
151 if (!std::getline(is, line) || line.empty()) {
152 gpr::io::ErrorManager err_man;
153 err_man << "Field '" << key
154 << "' has no value line; left zero-filled.\n";
155 continue;
156 }
157
158 split_line.clear();
159 split_line = splitString(line, std::string(1, split_char));
160
161 // Stop at the declared extent: a value line longer than
162 // rows*cols must not run off the end of the field, and a
163 // zero column count would leave counter_j pinned below.
164 const size_t capacity = static_cast<size_t>(num_elts_i) *
165 static_cast<size_t>(num_elts_j);
166 const size_t count = std::min(capacity, split_line.size());
167 gpr::Index_t counter_i = 0;
168 gpr::Index_t counter_j = 0;
169 for (size_t n = 0; n < count; ++n) {
170 field->operator()(counter_i, counter_j) =
171 std::atof(split_line[n].c_str());
172 ++counter_j;
173 if (counter_j == num_elts_j) {
174 ++counter_i;
175 counter_j = 0;
176 }
177 }
178 if (split_line.size() > capacity) {
179 gpr::io::ErrorManager err_man;
180 err_man << "Field '" << key << "' declares " << num_elts_i
181 << " x " << num_elts_j << " but supplies "
182 << split_line.size()
183 << " values; extra values ignored.\n";
184 }
185 }
186 }
187 }
188
189 is.close();
190
191 // Print if needed
192 // for(std::pair<std::string, gpr::Field<T>*> elt : data) {
193 // std::cout << elt.first << "\n";
194 // for(Index_t n = 0; n < elt.second->getDimensions(); ++n)
195 // for(Index_t m = 0; m < elt.second->getSize(n); ++m)
196 // std::cout << elt.second->operator()(n, m) << " ";
197 // std::cout << "\n";
198 // }
199}
200
201template <typename T>
202bool FileManager::readFromPlainFile(const std::string file_name, T &field)
203{
204 std::ifstream in_stream;
205 in_stream.open(file_name, std::ios::in);
206
207 if (!in_stream.is_open()) {
209 err << "Error! Reference data file '" << file_name << "' not found!\n";
210 return false;
211 }
212
213 // A short or malformed file leaves the tail of `field` holding whatever it
214 // held before, which reads downstream as data. Report the truncation.
215 decltype(field.size()) n = 0;
216 for (; n < field.size(); ++n) {
217 if (!(in_stream >> *(field.data() + n))) break;
218 }
219 in_stream.close();
220 if (n != field.size()) {
222 err << "Error! File '" << file_name << "' supplied " << n << " of "
223 << field.size() << " expected values.\n";
224 return false;
225 }
226 return true;
227}
228
229template <typename T>
230bool FileManager::writePlainFile(const std::string file_name, const T &field,
231 std::ios::openmode mode)
232{
233 std::ofstream out_stream;
234
235 out_stream.open(file_name, mode);
236
237 if (!out_stream.is_open()) {
239 err << "Error! Can't create file '" << file_name << "'!\n";
240 return false;
241 }
242
243 for (Eigen::Index n = 0; n < field.size(); ++n) {
244 out_stream << *(field.data() + n) << "\n";
245 }
246 out_stream.close();
247 return true;
248}
249
250} /* namespace io */
251} /* namespace gpr */
252
253#endif /* MANAGERS_IO_FILEMANAGER_INL_ */
Responsible for errors handling.
bool writePlainFile(const std::string file_name, const T &field, std::ios::openmode mode)
bool readFromPlainFile(const std::string file_name, T &field)
Read plain ASCII data file. This function reads data line-by-line from a plain text file....
void readDataFile(const std::string file_name, std::map< std::string, T * > &data)
Read data file. The method reads data file written in ASCII format. The values of data should point t...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
uint32_t Index_t
Definition Macros.h:17
Structure of a string key and template value.
Definition Structures.h:35
std::string key
Definition Structures.h:36