28inline std::pair<std::vector<double>, std::vector<double>>
30 std::pair<std::vector<double>, std::vector<double>> out_list;
31 std::ifstream file(fname);
32 std::string line =
"";
33 while (getline(file, line) && (file.is_open())) {
36 if (line.at(0) ==
'!' || line.at(0) ==
'#')
38 if (line.size() >= 2 && line.at(0) ==
'/' && line.at(1) ==
'/')
40 std::stringstream ss(line);
43 out_list.first.push_back(x);
44 out_list.second.push_back(y);
52 for (
auto posi = input.find(
"/*"); posi != std::string::npos;
53 posi = input.find(
"/*")) {
54 auto posf = input.find(
"*/");
55 if (posf != std::string::npos) {
56 input = input.substr(0, posi) + input.substr(posf + 2);
58 input = input.substr(0, posi);
66 std::string lines =
"";
69 std::stringstream stream1(input);
70 while (std::getline(stream1, line,
'\n')) {
71 auto comm1 = line.find(
'!');
72 auto comm2 = line.find(
'#');
73 auto comm3 = line.find(
"//");
74 auto comm = std::min(comm1, std::min(comm2, comm3));
75 lines += line.substr(0, comm);
82 lines.erase(std::remove_if(lines.begin(), lines.end(),
83 [](
unsigned char x) { return x ==
' '; }),
86 lines.erase(std::remove_if(lines.begin(), lines.end(),
87 [](
unsigned char x) { return x ==
'\t'; }),
91 lines.erase(std::remove_if(lines.begin(), lines.end(),
92 [](
unsigned char x) { return x ==
'\''; }),
94 lines.erase(std::remove_if(lines.begin(), lines.end(),
95 [](
unsigned char x) { return x ==
'\"'; }),
102enum RoW { read, write };
108inline void open_binary(std::fstream &stream,
const std::string &fname,
112 stream.open(fname, std::ios_base::out | std::ios_base::binary);
115 stream.open(fname, std::ios_base::in | std::ios_base::binary);
118 std::cout <<
"\nFAIL 16 in FRW\n";
126 std::ifstream infile(fileName);
127 return infile.good();
133template <
typename T,
typename... Types>
134void rw_binary(std::fstream &stream, RoW row, std::vector<T> &value,
136 binary_rw_vec(stream, value, row);
137 if constexpr (
sizeof...(values) != 0)
143template <
typename T,
typename... Types>
144void rw_binary(std::fstream &stream, RoW row, T &value, Types &...values) {
145 if constexpr (std::is_same_v<T, std::string>)
146 binary_str_rw(stream, value, row);
148 binary_rw(stream, value, row);
149 if constexpr (
sizeof...(values) != 0)
155void binary_rw(std::fstream &stream, T &value, RoW row) {
158 stream.write(
reinterpret_cast<const char *
>(&value),
sizeof(T));
161 stream.read(
reinterpret_cast<char *
>(&value),
sizeof(T));
164 std::cout <<
"\nFAIL 32 in FRW\n";
170void binary_rw_vec(std::fstream &stream, std::vector<T> &value, RoW row) {
171 std::size_t size = value.size();
172 binary_rw(stream, size, row);
175 for (
auto &x : value) {
181inline void binary_str_rw(std::fstream &stream, std::string &value, RoW row) {
183 std::size_t temp_len = value.length();
184 stream.write(
reinterpret_cast<const char *
>(&temp_len),
185 sizeof(std::size_t));
186 stream.write(value.c_str(),
long(value.length()));
187 }
else if (row == read) {
188 std::size_t temp_len;
189 stream.read(
reinterpret_cast<char *
>(&temp_len),
sizeof(std::size_t));
190 char *tvalue =
new char[temp_len + 1];
191 stream.read(tvalue,
long(temp_len));
192 tvalue[temp_len] =
'\0';
196 std::cout <<
"\nFAIL 55 in FRW\n";
File read/write utilities: text parsing and binary I/O.
Definition FRW_fileReadWrite.hpp:23
std::pair< std::vector< double >, std::vector< double > > readFile_xy_PoV(const std::string &fname)
Reads (x, y) pairs from a file; returns a pair of vectors {xs, ys}. Lines beginning with '#',...
Definition FRW_fileReadWrite.hpp:29
void rw_binary(std::fstream &stream, RoW row, std::vector< T > &value, Types &...values)
Function (variadic): reads/writes data from/to binary file. Works for trivial (PoD) types,...
Definition FRW_fileReadWrite.hpp:134
void open_binary(std::fstream &stream, const std::string &fname, RoW row)
Opens a binary fstream for reading or writing according to row.
Definition FRW_fileReadWrite.hpp:108
bool file_exists(const std::string &fileName)
Returns true if the file at fileName exists and can be opened.
Definition FRW_fileReadWrite.hpp:123
std::string removeCommentsAndSpaces(const std::string &input)
Strips comments (#, !, //, /* */), spaces, tabs, and quote characters. Lines are squashed together; ...
Definition FRW_fileReadWrite.hpp:65
void removeBlockComments(std::string &input)
Removes C-style block comments (/* ... */) from a string in-place.
Definition FRW_fileReadWrite.hpp:51