-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUIProperties.cpp
More file actions
308 lines (238 loc) · 6.28 KB
/
Copy pathGUIProperties.cpp
File metadata and controls
308 lines (238 loc) · 6.28 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
#include "GUI.h"
#include <cassert>
using namespace RTE;
GUIProperties::GUIProperties(const std::string& Name) {
m_Name = Name;
m_VariableList.clear();
}
GUIProperties::GUIProperties() {
m_Name = "";
m_VariableList.clear();
}
GUIProperties::~GUIProperties() {
Clear();
}
void GUIProperties::Clear() {
// Free the list
std::vector<PropVariable*>::iterator it;
for (it = m_VariableList.begin(); it != m_VariableList.end(); it++) {
PropVariable* p = *it;
// Free the property
delete p;
}
m_VariableList.clear();
}
void GUIProperties::AddVariable(const std::string& Variable, const std::string& Value) {
// If this property already exists, just update it
std::string Val;
if (GetValue(Variable, &Val)) {
SetValue(Variable, Value);
return;
}
PropVariable* Prop = new PropVariable;
Prop->m_Name = Variable;
Prop->m_Value = Value;
m_VariableList.push_back(Prop);
}
void GUIProperties::AddVariable(const std::string& Variable, char* Value) {
std::string Val = Value;
AddVariable(Variable, Val);
}
void GUIProperties::AddVariable(const std::string& Variable, int Value) {
char buf[32];
std::snprintf(buf, sizeof(buf), "%i", Value);
std::string Val(buf);
AddVariable(Variable, Val);
}
void GUIProperties::AddVariable(const std::string& Variable, bool Value) {
std::string Val = Value ? "True" : "False";
AddVariable(Variable, Val);
}
bool GUIProperties::SetValue(const std::string& Variable, const std::string& Value) {
// Find the property
std::vector<PropVariable*>::iterator it;
for (it = m_VariableList.begin(); it != m_VariableList.end(); it++) {
PropVariable* p = *it;
// Matching name?
if (stricmp(p->m_Name.c_str(), Variable.c_str()) == 0) {
p->m_Value = Value;
return true;
}
}
// Not set
return false;
}
bool GUIProperties::SetValue(const std::string& Variable, int Value) {
char buf[64];
std::snprintf(buf, sizeof(buf), "%i", Value);
return SetValue(Variable, buf);
}
void GUIProperties::Update(GUIProperties* Props, bool Add) {
assert(Props);
std::vector<PropVariable*>::iterator it1;
for (it1 = Props->m_VariableList.begin(); it1 != Props->m_VariableList.end(); it1++) {
const PropVariable* Src = *it1;
// Set the variable
if (!SetValue(Src->m_Name, Src->m_Value) && Add) {
AddVariable(Src->m_Name, Src->m_Value);
}
}
}
bool GUIProperties::GetValue(const std::string& Variable, std::string* Value) {
// Find the property
std::vector<PropVariable*>::iterator it;
for (it = m_VariableList.begin(); it != m_VariableList.end(); it++) {
const PropVariable* p = *it;
// Matching name?
if (stricmp(p->m_Name.c_str(), Variable.c_str()) == 0) {
*Value = p->m_Value;
return true;
}
}
// Not found
return false;
}
int GUIProperties::GetValue(const std::string& Variable, std::string* Array, int MaxArraySize) {
assert(Array);
std::string Value;
// Get the string
if (!GetValue(Variable, &Value)) {
return 0;
}
// Create a c string version of the value for tokenizing
char* str = new char[Value.length() + 1];
if (!str) {
return 0;
}
// Tokenize the string
strcpy(str, Value.c_str());
char* tok = strtok(str, ",");
int count = 0;
while (tok && count < MaxArraySize) {
Array[count++] = GUIUtil::TrimString(tok);
tok = strtok(0, ",");
}
delete[] str;
// Return the number of elements read
return count;
}
int GUIProperties::GetValue(const std::string& Variable, int* Array, int MaxArraySize) {
assert(Array);
std::string Value;
// Get the string
if (!GetValue(Variable, &Value)) {
return 0;
}
// Create a c string version of the value for tokenizing
char* str = new char[Value.length() + 1];
if (!str) {
return 0;
}
// Tokenize the string
strcpy(str, Value.c_str());
char* tok = strtok(str, ",");
int count = 0;
while (tok && count < MaxArraySize) {
Array[count++] = atoi(tok);
tok = strtok(0, ",");
}
delete[] str;
// Return the number of elements read
return count;
}
bool GUIProperties::GetValue(const std::string& Variable, int* Value) {
assert(Value);
std::string val;
// Get the string
if (!GetValue(Variable, &val)) {
return false;
}
*Value = atoi(val.c_str());
// Found the value
return true;
}
bool GUIProperties::GetValue(const std::string& Variable, unsigned long* Value) {
assert(Value);
std::string val;
// Get the string
if (!GetValue(Variable, &val)) {
return false;
}
*Value = atol(val.c_str());
// Found the value
return true;
}
bool GUIProperties::GetValue(const std::string& Variable, bool* Value) {
assert(Value);
std::string val;
// Get the string
if (!GetValue(Variable, &val)) {
return false;
}
// Default is false
*Value = false;
// Convert the string into a boolean
if (stricmp(val.c_str(), "true") == 0) {
*Value = true;
}
// Found the value
return true;
}
std::string GUIProperties::GetName() const {
return m_Name;
}
std::string GUIProperties::ToString() {
std::string OutString = "";
// Go through each value
std::vector<PropVariable*>::iterator it;
for (it = m_VariableList.begin(); it != m_VariableList.end(); it++) {
const PropVariable* V = *it;
OutString += V->m_Name;
OutString.append(" = ");
OutString += V->m_Value;
OutString.append("\n");
}
return OutString;
}
int GUIProperties::GetCount() const {
return m_VariableList.size();
}
bool GUIProperties::GetVariable(int Index, std::string* Name, std::string* Value) {
// Check for a bad index
if (Index < 0 || Index >= m_VariableList.size()) {
return false;
}
const PropVariable* P = (PropVariable*)m_VariableList.at(Index);
if (Name) {
*Name = P->m_Name;
}
if (Value) {
*Value = P->m_Value;
}
return true;
}
bool GUIProperties::SetVariable(int Index, const std::string& Name, const std::string& Value) {
// Check for a bad index
if (Index < 0 || Index >= m_VariableList.size()) {
return false;
}
PropVariable* P = m_VariableList.at(Index);
P->m_Name = Name;
P->m_Value = Value;
return true;
}
void GUIProperties::Sort(bool Ascending) {
// Simple bubble sort
for (int i = 0; i < m_VariableList.size(); i++) {
for (int j = 0; j < m_VariableList.size() - 1 - i; j++) {
PropVariable* V = m_VariableList.at(j);
PropVariable* V2 = m_VariableList.at(j + 1);
if ((V->m_Name.compare(V2->m_Name) > 0 && Ascending) || (V->m_Name.compare(V2->m_Name) < 0 && !Ascending)) {
// Swap em
PropVariable temp = *V;
*V = *V2;
*V2 = temp;
}
}
}
}