22template <std::size_t I = 0,
typename... Tp>
23inline typename std::enable_if<I ==
sizeof...(Tp),
void>::type
24stringstreamVectorIntoTuple(
const std::vector<std::string> &,
25 std::tuple<Tp...> &) {}
27template <std::size_t I = 0,
typename... Tp>
28 inline typename std::enable_if <
29 I<
sizeof...(Tp),
void>::type
30 stringstreamVectorIntoTuple(
const std::vector<std::string> &lst,
31 std::tuple<Tp...> &t) {
33 std::cerr <<
"\nFAIL 34 in FRW: list shorter than tuple\n";
34 std::stringstream(lst[I]) >> std::get<I>(t);
35 stringstreamVectorIntoTuple<I + 1, Tp...>(lst, t);
39inline std::vector<std::string> readInputFile_byEntry(
const std::string &fname)
44 std::vector<std::string> entry_list;
45 std::ifstream file(fname);
47 while (getline(file, line) && (file.is_open())) {
48 std::stringstream ss(line);
51 if (entry.at(0) ==
'!' || entry.at(0) ==
'#')
53 if (entry.size() >= 2 && entry.at(0) ==
'/' && entry.at(1) ==
'/')
55 entry_list.push_back(entry);
62inline std::vector<std::string> readInputFile_byLine(
const std::string &fname)
66 std::vector<std::string> entry_list;
67 std::ifstream file(fname);
69 while (getline(file, line) && (file.is_open())) {
72 if (line.at(0) ==
'!' || line.at(0) ==
'#')
74 if (line.size() >= 2 && line.at(0) ==
'/' && line.at(1) ==
'/')
76 entry_list.push_back(line);
82inline std::vector<std::pair<double, double>>
83readFile_xy_VoP(
const std::string &fname)
88 std::vector<std::pair<double, double>> out_list;
89 std::ifstream file(fname);
90 std::string line =
"";
91 while (getline(file, line) && (file.is_open())) {
94 if (line.at(0) ==
'!' || line.at(0) ==
'#')
96 if (line.size() >= 2 && line.at(0) ==
'/' && line.at(1) ==
'/')
98 std::stringstream ss(line);
101 out_list.emplace_back(x, y);
106inline std::pair<std::vector<double>, std::vector<double>>
107readFile_xy_PoV(
const std::string &fname)
112 std::pair<std::vector<double>, std::vector<double>> out_list;
113 std::ifstream file(fname);
114 std::string line =
"";
115 while (getline(file, line) && (file.is_open())) {
118 if (line.at(0) ==
'!' || line.at(0) ==
'#')
120 if (line.size() >= 2 && line.at(0) ==
'/' && line.at(1) ==
'/')
122 std::stringstream ss(line);
125 out_list.first.push_back(x);
126 out_list.second.push_back(y);
132inline void writeFile_xy(
const std::vector<double> &x,
133 const std::vector<double> &y,
134 const std::string &fname) {
136 if (x.size() != y.size()) {
137 std::cout <<
"Warning 139 in FRW: trying to write {x,y} vectors of "
138 "different lengths!\n";
140 std::ofstream file(fname);
141 auto max = std::min(x.size(), y.size());
142 for (
auto i = 0ul; i < max; ++i) {
143 file << x[i] <<
" " << y[i] <<
"\n";
151 std::ifstream f(fname);
154 std::ostringstream ss;
162inline void removeBlockComments(std::string &input) {
163 for (
auto posi = input.find(
"/*"); posi != std::string::npos;
164 posi = input.find(
"/*")) {
165 auto posf = input.find(
"*/");
166 if (posf != std::string::npos) {
167 input = input.substr(0, posi) + input.substr(posf + 2);
169 input = input.substr(0, posi);
174inline std::string removeCommentsAndSpaces(
const std::string &input)
177 std::string lines =
"";
180 std::stringstream stream1(input);
181 while (std::getline(stream1, line,
'\n')) {
182 auto comm1 = line.find(
'!');
183 auto comm2 = line.find(
'#');
184 auto comm3 = line.find(
"//");
185 auto comm = std::min(comm1, std::min(comm2, comm3));
186 lines += line.substr(0, comm);
190 removeBlockComments(lines);
193 lines.erase(std::remove_if(lines.begin(), lines.end(),
194 [](
unsigned char x) { return x ==
' '; }),
197 lines.erase(std::remove_if(lines.begin(), lines.end(),
198 [](
unsigned char x) { return x ==
'\t'; }),
202 lines.erase(std::remove_if(lines.begin(), lines.end(),
203 [](
unsigned char x) { return x ==
'\''; }),
205 lines.erase(std::remove_if(lines.begin(), lines.end(),
206 [](
unsigned char x) { return x ==
'\"'; }),
213inline std::vector<std::pair<std::string, std::string>>
214splitInput_byBraces(
const std::string &input) {
216 std::vector<std::pair<std::string, std::string>> output;
218 auto lines = removeCommentsAndSpaces(input);
220 auto find_close = [&lines](
auto open) {
221 auto next_open = lines.find(
'{', open + 1);
222 auto next_close = lines.find(
'}', open + 1);
223 auto next = std::min(next_open, next_close);
224 if (next == next_close || next == std::string::npos)
228 next_open = lines.find(
'{', next + 1);
229 next_close = lines.find(
'}', next + 1);
230 next = std::min(next_open, next_close);
231 if (next == next_close)
233 if (next == next_open)
236 next = lines.find(
'}', next + 1);
240 std::size_t previous_end = 0;
242 auto beg = lines.find(
'{', previous_end);
243 auto end = find_close(beg);
244 if (beg == std::string::npos)
246 if (end == std::string::npos) {
247 std::cerr <<
"\nFAIL 114 in FRW: Bad file format (missing '}'?)\n";
250 auto identifier = lines.substr(previous_end, beg - previous_end);
251 auto options = lines.substr(beg + 1, end - beg - 1);
252 output.push_back(std::make_pair(identifier, options));
253 previous_end = end + 1;
260inline std::vector<std::string> splitInput_bySemiColon(
const std::string &input)
263 std::vector<std::string> entry_list;
265 auto lines = removeCommentsAndSpaces(input);
277 std::size_t start = (input.size() > 0 && input[0] ==
'{') ? 1 : 0;
282 const auto bkt = input.find(
'{', start);
283 const auto semic = input.find(
';', start);
284 const auto bracketQ = bkt < semic;
288 const auto end = bracketQ ? input.find(
"};", start) + 1 : semic;
289 if (end == std::string::npos)
291 entry_list.push_back(input.substr(start, end - start));
299template <
typename... Tp>
300void setInputParameters(
const std::string &infile, std::tuple<Tp...> &tp) {
301 auto input = readInputFile_byEntry(infile);
302 if (
sizeof...(Tp) > input.size()) {
306 std::cerr <<
"\nFail 71 in FRW: Wrong number of input parameters? "
307 <<
"Reading from file: " << infile <<
". Expected "
308 <<
sizeof...(Tp) <<
" arguments"
309 <<
", but got " << input.size() <<
".\n";
312 stringstreamVectorIntoTuple(input, tp);
316enum RoW { read, write };
321inline void open_binary(std::fstream &stream,
const std::string &fname,
325 stream.open(fname, std::ios_base::out | std::ios_base::binary);
328 stream.open(fname, std::ios_base::in | std::ios_base::binary);
331 std::cout <<
"\nFAIL 16 in FRW\n";
335inline bool file_exists(
const std::string &fileName) {
338 std::ifstream infile(fileName);
339 return infile.good();
345template <
typename T,
typename... Types>
346void rw_binary(std::fstream &stream, RoW row, std::vector<T> &value,
348 binary_rw_vec(stream, value, row);
349 if constexpr (
sizeof...(values) != 0)
355template <
typename T,
typename... Types>
356void rw_binary(std::fstream &stream, RoW row, T &value, Types &...values) {
357 if constexpr (std::is_same_v<T, std::string>)
358 binary_str_rw(stream, value, row);
360 binary_rw(stream, value, row);
361 if constexpr (
sizeof...(values) != 0)
367void binary_rw(std::fstream &stream, T &value, RoW row) {
370 stream.write(
reinterpret_cast<const char *
>(&value),
sizeof(T));
373 stream.read(
reinterpret_cast<char *
>(&value),
sizeof(T));
376 std::cout <<
"\nFAIL 32 in FRW\n";
382void binary_rw_vec(std::fstream &stream, std::vector<T> &value, RoW row) {
383 std::size_t size = value.size();
384 binary_rw(stream, size, row);
387 for (
auto &x : value) {
393inline void binary_str_rw(std::fstream &stream, std::string &value, RoW row) {
395 std::size_t temp_len = value.length();
396 stream.write(
reinterpret_cast<const char *
>(&temp_len),
397 sizeof(std::size_t));
398 stream.write(value.c_str(),
long(value.length()));
399 }
else if (row == read) {
400 std::size_t temp_len;
401 stream.read(
reinterpret_cast<char *
>(&temp_len),
sizeof(std::size_t));
402 char *tvalue =
new char[temp_len + 1];
403 stream.read(tvalue,
long(temp_len));
404 tvalue[temp_len] =
'\0';
408 std::cout <<
"\nFAIL 55 in FRW\n";