-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelperFunction.cpp
67 lines (52 loc) · 1.8 KB
/
HelperFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "HelperFunction.h"
std::vector<std::string> HelperFunction::read_line_from_file(std::string path) {
// Read all lines from any file in the system and return a vector of files.
std::vector<std::string> lines;
std::fstream file(path.c_str());
if (file.fail()) {
std::cerr << "\n\nERROR: Can't open the file\n\n";
return lines;
}
std::string line;
while (getline(file, line)) {
if ((int)line.size() == 0)
continue;
lines.push_back(line);
}
file.close();
return lines; // return a vector of files.
}
void HelperFunction::write_line_on_file(std::string path, std::vector<std::string> lines, int append) const {
std::ios_base::openmode statue = std::ios::in | std::ios::out | std::ios::app; // append
if (!append)
statue = std::ios::in | std::ios::out | std::ios::trunc; // overwrite
std::fstream file(path.c_str(), statue);
if (file.fail()) {
std::cerr << "\n\nERROR: Can't open the file\n\n";
return;
}
for (const auto& line : lines)
file << line << '\n';
file.close();
}
std::vector<std::string> HelperFunction::split_string(std::string line, char delimiter) {
// split line from a file to vector of string.
std::vector<std::string> fields;
std::string s = line;
int pos = 0;
std::string substr;
while ((pos = (int)s.find(delimiter)) != -1) {
substr = s.substr(0, pos);
fields.push_back(substr);
s.erase(0, pos + 1);
}
fields.push_back(s);
return fields;
}
int HelperFunction::to_int(std::string str) {
// read number from line from any file and return it.
std::istringstream iss(str);
int number;
iss >> number;
return number;
}