forked from LinuxDevon/dbc_parser_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
72 lines (52 loc) · 1.75 KB
/
Copy pathutils.cpp
File metadata and controls
72 lines (52 loc) · 1.75 KB
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
68
69
70
71
72
#include <libdbc/utils/utils.hpp>
#include <regex>
namespace utils {
std::istream& StreamHandler::get_line(std::istream& stream, std::string& line) {
std::string newline;
std::getline(stream, newline);
// Windows CRLF (\r\n)
if (newline.size() && newline[newline.size() - 1] == '\r') {
line = newline.substr(0, newline.size() - 1);
// MacOS LF (\r)
} else if (newline.size() && newline[newline.size()] == '\r') {
line = newline.replace(newline.size(), 1, "\n");
} else {
line = newline;
}
return stream;
}
std::istream& StreamHandler::get_next_non_blank_line(std::istream& stream, std::string& line) {
bool is_blank = true;
const std::regex whitespace_re("\\s*(.*)");
std::smatch match;
while (is_blank) {
utils::StreamHandler::get_line(stream, line);
std::regex_search(line, match, whitespace_re);
if ((!line.empty() && !match.empty()) || (stream.eof())) {
if ((match.length(1) > 0) || (stream.eof())) {
is_blank = false;
}
}
}
return stream;
}
std::istream& StreamHandler::skip_to_next_blank_line(std::istream& stream, std::string& line) {
bool line_is_empty = false;
const std::regex whitespace_re("\\s*(.*)");
std::smatch match;
while (!line_is_empty) {
utils::StreamHandler::get_line(stream, line);
std::regex_search(line, match, whitespace_re);
if ((match.length(1) == 0) || (stream.eof())) {
line_is_empty = true;
}
}
return stream;
}
std::string String::trim(const std::string& line) {
const char* WhiteSpace = " \t\v\r\n";
std::size_t start = line.find_first_not_of(WhiteSpace);
std::size_t end = line.find_last_not_of(WhiteSpace);
return start == end ? std::string() : line.substr(start, end - start + 1);
}
} // Namespace Utils