-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
78 lines (71 loc) · 2 KB
/
utils.cpp
File metadata and controls
78 lines (71 loc) · 2 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
73
74
75
76
77
78
#include "Utils.h"
#include <sstream>
#include <algorithm>
#include <cctype>
namespace Utils {
std::string trim(const std::string& s) {
size_t a = 0;
while (a < s.size() && std::isspace(static_cast<unsigned char>(s[a]))) ++a;
size_t b = s.size();
while (b > a && std::isspace(static_cast<unsigned char>(s[b-1]))) --b;
return s.substr(a, b - a);
}
int stoiSafe(const std::string& s, int fallback) {
try {
return std::stoi(s);
} catch (...) {
return fallback;
}
}
std::string escapeCSV(const std::string& s) {
bool needQuotes = s.find(',') != std::string::npos || s.find('"') != std::string::npos;
std::string out = s;
// double quotes escape
size_t pos = 0;
while ((pos = out.find('"', pos)) != std::string::npos) {
out.replace(pos, 1, "\"\"");
pos += 2;
}
if (needQuotes) {
out = "\"" + out + "\"";
}
return out;
}
// Very small CSV parser for known format
std::vector<std::string> splitCSV(const std::string& line) {
std::vector<std::string> result;
std::string cur;
bool inQuotes = false;
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
if (inQuotes) {
if (c == '"') {
if (i + 1 < line.size() && line[i+1] == '"') {
cur.push_back('"'); // escaped quote
++i;
} else {
inQuotes = false; // closing quote
}
} else {
cur.push_back(c);
}
} else {
if (c == ',') {
result.push_back(cur);
cur.clear();
} else if (c == '"') {
inQuotes = true;
} else {
cur.push_back(c);
}
}
}
result.push_back(cur);
return result;
}
std::string unescapeCSV(const std::string& s) {
// Our splitCSV already returns unescaped content for quotes,
// but ensure trimming
return trim(s);
}
} // namespace Utils