13#ifndef MANAGERS_IO_FILEMANAGER_INL_
14#define MANAGERS_IO_FILEMANAGER_INL_
21inline std::vector<std::string> FileManager::splitString(
const std::string &str,
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());
30void FileManager::convertStringToValue(
31 std::map<std::string, std::vector<std::string> > &input_data,
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";
47void FileManager::convertStringToString(
48 std::map<std::string, std::vector<std::string> > &input_data,
51 if (input_data.find(parameters.
key) != input_data.end()) {
52 parameters.
value = input_data[parameters.
key][0];
57void FileManager::convertStringToValueArray(
58 std::map<std::string, std::vector<std::string> > &input_data,
61 if (input_data.find(parameters.
key) != input_data.end()) {
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());
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";
86void FileManager::convertStringToStringArray(
87 std::map<std::string, std::vector<std::string> > &input_data,
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];
99 std::map<std::string, T *> &data)
103 char split_char =
' ';
105 is.open(file_name.c_str(), std::ios::in);
109 err_man <<
"Input file '" << file_name <<
"' cannot be opened.\n";
116 while (std::getline(is, line)) {
118 std::vector<std::string> split_line;
119 std::string key =
"";
121 split_line = splitString(line, std::string(1, split_char));
125 if (split_line.size() < 3)
continue;
129 if (data.find(key) != data.end()) {
130 T *
field = data[key];
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) {
138 err_man <<
"Field '" << key <<
"' declares negative "
139 <<
"dimensions (" << rows_in <<
" x " << cols_in
148 field->resize(num_elts_i, num_elts_j);
151 if (!std::getline(is, line) || line.empty()) {
153 err_man <<
"Field '" << key
154 <<
"' has no value line; left zero-filled.\n";
159 split_line = splitString(line, std::string(1, split_char));
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());
169 for (
size_t n = 0; n < count; ++n) {
170 field->operator()(counter_i, counter_j) =
171 std::atof(split_line[n].c_str());
173 if (counter_j == num_elts_j) {
178 if (split_line.size() > capacity) {
180 err_man <<
"Field '" << key <<
"' declares " << num_elts_i
181 <<
" x " << num_elts_j <<
" but supplies "
183 <<
" values; extra values ignored.\n";
204 std::ifstream in_stream;
205 in_stream.open(file_name, std::ios::in);
207 if (!in_stream.is_open()) {
209 err <<
"Error! Reference data file '" << file_name <<
"' not found!\n";
215 decltype(
field.size()) n = 0;
216 for (; n <
field.size(); ++n) {
217 if (!(in_stream >> *(
field.data() + n)))
break;
220 if (n !=
field.size()) {
222 err <<
"Error! File '" << file_name <<
"' supplied " << n <<
" of "
223 <<
field.size() <<
" expected values.\n";
231 std::ios::openmode mode)
233 std::ofstream out_stream;
235 out_stream.open(file_name, mode);
237 if (!out_stream.is_open()) {
239 err <<
"Error! Can't create file '" << file_name <<
"'!\n";
243 for (Eigen::Index n = 0; n <
field.size(); ++n) {
244 out_stream << *(
field.data() + n) <<
"\n";
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.
Structure of a string key and template value.