Repository navigation
Expand file tree
/
Copy pathArgUtils.cpp
More file actions
311 lines (235 loc) · 9.56 KB
/
Copy pathArgUtils.cpp
File metadata and controls
311 lines (235 loc) · 9.56 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//-----------------------------------------------------------------------------
// This file is part of the the QParser2 project. You can find the complete
// project here: https://github.com/robdunn4/QSpice/
//-----------------------------------------------------------------------------
#include "ArgUtils.h"
#include <iomanip>
#include <regex>
#include <sstream>
#include <stdexcept>
// TODO: Revisit parse() static methods. Originally intended to be called by
// constructors, but that seems a little clunky.
// ============================================================================
// Point implementation
// ============================================================================
// Point::Point() : x(0), y(0) {}
//
// Point::Point(int x_, int y_) : x(x_), y(y_) {}
Point Point::add(const Point &other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator+(const Point &other) const {
return Point(x + other.x, y + other.y);
}
Point Point::operator-(const Point &other) const {
return Point(x - other.x, y - other.y);
}
// do we use this? if so, do we need an operator-= as well?
Point &Point::operator+=(const Point &other) {
x += other.x;
y += other.y;
return *this;
}
// ============================================================================
// ArgInt implementation
// ============================================================================
ArgInt::ArgInt() : value(0) {}
ArgInt::ArgInt(int val) : value(val) {}
ArgInt::ArgInt(const std::string &str) : value(parse(str)) {}
ArgInt::ArgInt(const ArgInt &other) : value(other.value) {}
int ArgInt::getValue() const { return value; }
ArgInt::operator int() const { return value; }
int ArgInt::parse(const std::string &str) {
try {
size_t pos;
int value = std::stoi(str, &pos);
// Check if entire string was consumed
if (pos != str.length()) {
throw std::invalid_argument("Invalid integer format");
}
return value;
} catch ([[maybe_unused]] const std::exception &e) {
throw std::invalid_argument("Failed to parse integer: " + str);
}
}
std::string ArgInt::toString() const { return std::to_string(value); }
std::string ArgInt::toString(int value) { return std::to_string(value); }
// ============================================================================
// ArgFloat implementation
// ============================================================================
ArgFloat::ArgFloat() : value(0.0f) {}
ArgFloat::ArgFloat(float val) : value(val) {}
ArgFloat::ArgFloat(const std::string &str) : value(parse(str)) {}
ArgFloat::ArgFloat(const ArgFloat &other) : value(other.value) {}
float ArgFloat::getValue() const { return value; }
ArgFloat::operator float() const { return value; }
float ArgFloat::parse(const std::string &str) {
try {
size_t pos;
float value = std::stof(str, &pos);
if (pos != str.length()) {
throw std::invalid_argument("Invalid float format");
}
return value;
} catch ([[maybe_unused]] const std::exception &e) {
throw std::invalid_argument("Failed to parse float: " + str);
}
}
std::string ArgFloat::toString(int precision) const {
std::ostringstream oss;
// oss << std::fixed << std::setprecision(precision) << value;
oss << value;
return oss.str();
}
std::string ArgFloat::toString(float value, int precision) {
std::ostringstream oss;
// oss << std::fixed << std::setprecision(precision) << value;
oss << value;
return oss.str();
}
// ============================================================================
// ArgHex implementation
// Note: Apparently hex values should be lower case.
// ============================================================================
ArgHex::ArgHex() : value(0) {}
ArgHex::ArgHex(int val) : value(val) {}
ArgHex::ArgHex(const std::string &str) : value(parse(str)) {}
ArgHex::ArgHex(const ArgHex &other) : value(other.value) {}
int ArgHex::getValue() const { return value; }
ArgHex::operator int() const { return value; }
int ArgHex::parse(const std::string &str) {
try {
// Ensure string starts with "0x" or "0X"
if (str.length() < 3 ||
(str.substr(0, 2) != "0x" && str.substr(0, 2) != "0X")) {
throw std::invalid_argument("Hex string must start with 0x");
}
size_t pos;
int value = std::stoi(str, &pos, 16);
if (pos != str.length()) {
throw std::invalid_argument("Invalid hex format");
}
return value;
} catch ([[maybe_unused]] const std::exception &e) {
throw std::invalid_argument("Failed to parse hex: " + str);
}
}
std::string ArgHex::toString(int width) const {
std::ostringstream oss;
oss << "0x" << std::setfill('0') << std::setw(width) << std::hex
<< std::nouppercase << value;
return oss.str();
}
std::string ArgHex::toString(int value, int width) {
std::ostringstream oss;
oss << "0x" << std::setfill('0') << std::setw(width) << std::hex
<< std::nouppercase << value;
return oss.str();
}
// ============================================================================
// ArgBool implementation
// ============================================================================
ArgBool::ArgBool() : value(false) {}
ArgBool::ArgBool(bool val) : value(val) {}
ArgBool::ArgBool(const std::string &str) : value(parse(str)) {}
ArgBool::ArgBool(const ArgBool &other) : value(other.value) {}
bool ArgBool::getValue() const { return value; }
ArgBool::operator bool() const { return value; }
bool ArgBool::parse(const std::string &str) {
if (str == "true") return true;
if (str == "false") return false;
throw std::invalid_argument("Failed to parse bool: " + str);
}
std::string ArgBool::toString() const { return value ? "true" : "false"; }
std::string ArgBool::toString(bool value) { return value ? "true" : "false"; }
// ============================================================================
// ArgString implementation
// ============================================================================
ArgString::ArgString() : value("") {}
ArgString::ArgString(const std::string &str) : value(str) {}
ArgString::ArgString(const char *cstr) : value(cstr) {}
ArgString::ArgString(const ArgString &other) : value(other.value) {}
std::string ArgString::getValue() const { return value; }
ArgString::operator std::string() const { return value; }
std::string ArgString::parse(const std::string &str) { return str; }
std::string ArgString::toString() const { return value; }
std::string ArgString::toString(const std::string &value) { return value; }
// split special cases (e.g., ItemNet and ItemPin) where there may be two
// trailing quoted strings, one of which is optional. the tokenizer will not
// split these into separate arguments, so we need to do it for the special
// cases.
ArgString &ArgString::splitQuoted(ArgString &secondPart) {
auto pos = value.find(' ');
if (pos != std::string_view::npos) {
secondPart = std::string(value.substr(pos + 1));
value = std::string(value.substr(0, pos));
}
return *this;
}
// when constructing ItemBase-derived classes via constructors in code, the
// developer should not be required to include quotes around string arguments.
// ArgString &ArgString::quote() {
// value = "\"" + value + "\"";
// return *this;
ArgString ArgString::quote() const {
return ArgString("\"" + value + "\"");
}
std::string ArgString::toAsciiString() const {
std::string ascii;
for (size_t i = 0; i < value.size(); ++i) {
if (static_cast<unsigned char>(value[i]) > 0x7F) continue;
ascii += value[i];
}
return ascii;
}
// ============================================================================
// ArgString implementation (adds quoting + ASCII cleanup)
// ============================================================================
//// TODO: Revert back to only ArgString base class; for now, undo quoting
//// std::string ArgString::toString() const { return "\"" + value + "\""; }
// std::string ArgString::toString() const { return value; }
//
// std::string ArgString::toAsciiString() const {
// std::string ascii;
// for (size_t i = 0; i < value.size(); ++i) {
// if (static_cast<unsigned char>(value[i]) > 0x7F) continue;
// ascii += value[i];
// }
// return ascii;
// }
// ============================================================================
// ArgPoint implementation
// ============================================================================
ArgPoint::ArgPoint() : value() {}
ArgPoint::ArgPoint(const Point &p) : value(p) {}
ArgPoint::ArgPoint(int x, int y) : value(x, y) {}
ArgPoint::ArgPoint(const std::string &str) : value(parse(str)) {}
ArgPoint::ArgPoint(const ArgPoint &other) : value(other.value) {}
Point ArgPoint::getValue() const { return value; }
ArgPoint::operator Point() const { return value; }
Point ArgPoint::parse(const std::string &str) {
// Pattern matches: '(', integer (with optional sign), ',',
// integer (with optional sign), ')' - NO whitespace allowed
static const std::regex pattern(R"(\((-?\d+),(-?\d+)\))");
std::smatch matches;
if (!std::regex_match(str, matches, pattern)) {
throw std::invalid_argument("Invalid point format: " + str);
}
try {
int x = std::stoi(matches[1].str());
int y = std::stoi(matches[2].str());
return Point(x, y);
} catch ([[maybe_unused]] const std::exception &e) {
throw std::invalid_argument("Failed to parse point values: " + str);
}
}
std::string ArgPoint::toString() const {
std::ostringstream oss;
oss << "(" << value.x << "," << value.y << ")";
return oss.str();
}
std::string ArgPoint::toString(const Point &point) {
std::ostringstream oss;
oss << "(" << point.x << "," << point.y << ")";
return oss.str();
}