-
Notifications
You must be signed in to change notification settings - Fork 0
/
configoption.cpp
286 lines (236 loc) · 5.3 KB
/
configoption.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
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
// Copyright (C) 2014-2016 Eric Hebert (ayebear)
// This code is licensed under MIT, see LICENSE.txt for details.
#include "configoption.h"
namespace cfg
{
Option::OptionVector Option::emptyVector;
Option::Option(const std::string& data)
{
setString(data);
}
Option::Option(const Option& data)
{
operator=(data);
}
void Option::reset()
{
removeRange();
options.reset();
operator=(0);
}
bool Option::operator=(const char* data)
{
return setString(data);
}
bool Option::operator=(const std::string& data)
{
return setString(data);
}
Option& Option::operator=(const Option& data)
{
text = data.text;
integer = data.integer;
decimal = data.decimal;
boolean = data.boolean;
quotes = data.quotes;
minEnabled = data.minEnabled;
maxEnabled = data.maxEnabled;
rangeMin = data.rangeMin;
rangeMax = data.rangeMax;
if (data.options)
options = std::make_unique<OptionVector>(*data.options);
else
options.reset();
return *this;
}
bool Option::setString(const std::string& data)
{
std::istringstream stream(data);
double value{};
// Try to parse a value from the string
bool success = (stream >> value) && (static_cast<size_t>(stream.gcount()) == data.size());
// Only set the value if it's in range
if (isInRange(value))
{
decimal = value;
integer = decimal;
text = data;
// Check for a boolean value
auto lowerStr = strlib::toLower(data);
bool isTrue = (lowerStr == "true");
bool isFalse = (lowerStr == "false");
// Determine if quotes are necessary
quotes = !(success || isFalse || isTrue);
// Convert to a boolean ("true" means true, or any non-zero value)
boolean = (success ? (decimal != 0) : isTrue);
return true;
}
return false;
}
const std::string& Option::toString() const
{
return text;
}
std::string Option::toStringWithQuotes() const
{
// Automatically append quotes to the string if it originally had them
return (quotes ? ('"' + text + '"') : text);
}
int Option::toInt() const
{
return integer;
}
long Option::toLong() const
{
return static_cast<long>(integer);
}
float Option::toFloat() const
{
return static_cast<float>(decimal);
}
double Option::toDouble() const
{
return decimal;
}
bool Option::toBool() const
{
return boolean;
}
char Option::toChar() const
{
return static_cast<char>(integer);
}
void Option::get(std::string& val) const
{
val = text;
}
void Option::get(long& val) const
{
val = integer;
}
void Option::get(double& val) const
{
val = decimal;
}
void Option::get(bool& val) const
{
val = boolean;
}
Option::operator const std::string&() const
{
return text;
}
void Option::setQuotes(bool setting)
{
quotes = setting;
}
bool Option::hasQuotes()
{
return quotes;
}
void Option::setMin(double minimum)
{
rangeMin = minimum;
minEnabled = true;
}
void Option::setMax(double maximum)
{
rangeMax = maximum;
maxEnabled = true;
}
void Option::setRange(double minimum, double maximum)
{
setMin(minimum);
setMax(maximum);
}
void Option::removeRange()
{
minEnabled = false;
maxEnabled = false;
}
Option& Option::push(const Option& opt)
{
if (!options)
options = std::make_unique<OptionVector>();
options->push_back(opt);
return options->back();
}
void Option::pop()
{
if (options && !options->empty())
options->pop_back();
}
Option& Option::operator[](unsigned pos)
{
return ((*options)[pos]);
}
Option& Option::back()
{
return options->back();
}
unsigned Option::size() const
{
return (options ? options->size() : 0);
}
void Option::clear()
{
options.reset();
}
Option::OptionVector::iterator Option::begin()
{
if (options)
return options->begin();
return emptyVector.begin();
}
Option::OptionVector::iterator Option::end()
{
if (options)
return options->end();
return emptyVector.end();
}
Option::OptionVector::const_iterator Option::cbegin() const
{
if (options)
return options->cbegin();
return emptyVector.cbegin();
}
Option::OptionVector::const_iterator Option::cend() const
{
if (options)
return options->cend();
return emptyVector.cend();
}
std::string Option::buildArrayString(const std::string& indentStr) const
{
// Continue building array strings until the option is just a single element and not an array
if (options)
{
std::string nextIndentStr(indentStr); //
//std::string nextIndentStr(indentStr + '\t'); //
// Build the array string
std::string arrayStr("{"); //std::string arrayStr("{\n");
unsigned arraySize = options->size();
for (unsigned i = 0; i < arraySize; ++i)
{
arrayStr += nextIndentStr;
arrayStr += (*options)[i].buildArrayString(nextIndentStr);
if (i < arraySize - 1)
arrayStr += ", "; //arrayStr += ",\n";
}
arrayStr += indentStr + '}'; //arrayStr += '\n' + indentStr + '}';
return arrayStr;
}
else
return toStringWithQuotes();
}
bool Option::isInRange(double num)
{
return ((!minEnabled || num >= rangeMin) &&
(!maxEnabled || num <= rangeMax));
}
std::ostream& operator<<(std::ostream& stream, const Option& option)
{
stream << option.toString();
return stream;
}
}