-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparam_string.cc
More file actions
332 lines (262 loc) · 7.5 KB
/
param_string.cc
File metadata and controls
332 lines (262 loc) · 7.5 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <cstdio>
#include <cstring>
#include <cctype>
#include "param_string.h"
bool ParamString::skipParameters(FILE *inf) {
int len = readFirstLine(inf);
if (len < 0) return false;
return 0 == fseek(inf, len, SEEK_CUR);
}
bool ParamString::readParameters(FILE *inf) {
readFirstLine(inf);
params.clear();
string line, name, value;
while (true) {
if (!readLine(inf, line)) return false;
if (line.length() == 0) break;
const char *str = line.c_str();
// extract the name--everything until the equals sign
name = readString(str, "=");
// skip this line if there is no equals or no name
if (name.length() == 0 || *str != '=') continue;
str++;
// extract the value--everything until the end of the line
value = readString(str, "");
params[name] = value;
}
return true;
}
bool ParamString::writeParameters(FILE *outf) {
string content;
map<string,string>::const_iterator iter;
for (iter = params.begin(); iter != params.end(); iter++) {
content.append(formatString(iter->first.c_str(), FORMAT_STRING_LHS));
content.push_back('=');
content.append(formatString(iter->second.c_str(), FORMAT_STRING_RHS));
content.push_back('\n');
}
content.push_back('\n');
size_t size = content.length();
if (fprintf(outf, PARAM_STRING_FIRST_WORD " %llu\n", (long long unsigned) size) < 0) return false;
if (fputs(content.c_str(), outf) == EOF) return false;
return true;
}
bool ParamString::parseInt(const char *s, int &result) {
return 1 == sscanf(s, "%d", &result);
}
bool ParamString::parseFloat(const char *s, float &result) {
return 1 == sscanf(s, "%f", &result);
}
bool ParamString::parseList(const char *s, vector<string> &result) {
result.resize(0);
s = skipWhitespace(s);
if (*s != '[') return false;
s++;
while(true) {
string word = readString(s, ",]");
if (*s == '\0') return false;
result.push_back(word);
if (*s == ']') break;
s++;
}
return true;
}
bool ParamString::parseMap(const char *s, map<string,string> &result) {
result.clear();
s = skipWhitespace(s);
if (*s != '{') return false;
s++;
string name, value;
while(true) {
name = readString(s, "=");
if (*s == '\0') return false;
s++;
value = readString(s, ",}");
if (*s == '\0') return false;
result[name] = value;
if (*s == '}') break;
s++;
}
return true;
}
string ParamString::formatInt(int i) {
char buf[20];
#ifdef _WIN32
sprintf_s(buf, (sizeof buf) - 1, "%d", i);
#else
snprintf(buf, sizeof buf, "%d", i);
#endif
return string(buf);
}
string ParamString::formatFloat(float f) {
char buf[20];
#ifdef _WIN32
sprintf_s(buf, (sizeof buf) - 1, "%.8g", f);
#else
snprintf(buf, sizeof buf, "%.8g", f);
#endif
return string(buf);
}
string ParamString::formatList(const vector<string> &v) {
string result;
result.push_back('[');
for (size_t i=0; i < v.size(); i++) {
if (i > 0) result.push_back(',');
result.append(formatString(v[i].c_str()));
}
result.push_back(']');
return result;
}
string ParamString::formatMap(const map<string,string> &m) {
string result;
map<string,string>::const_iterator iter;
bool first = true;
result.push_back('{');
for (iter = m.begin(); iter != m.end(); iter++) {
if (!first) result.push_back(',');
first = false;
result.append(formatString(iter->first.c_str()));
result.push_back('=');
result.append(formatString(iter->second.c_str()));
}
result.push_back('}');
return result;
}
void ParamString::setFloatList(const char *name, const vector<float> &floats) {
vector<string> strings(floats.size());
for (size_t i=0; i < floats.size(); i++)
strings[i] = formatFloat(floats[i]);
setList(name, strings);
}
bool ParamString::get(const char *name, string &value) {
map<string,string>::iterator p = params.find(name);
if (p == params.end()) return false;
value = p->second;
return true;
}
bool ParamString::getInt(const char *name, int &value) {
string s;
if (!get(name, s)) return false;
return parseInt(s.c_str(), value);
}
bool ParamString::getFloat(const char *name, float &value) {
string s;
if (!get(name, s)) return false;
return parseFloat(s.c_str(), value);
}
bool ParamString::getList(const char *name, vector<string> &value) {
string s;
if (!get(name, s)) return false;
return parseList(s.c_str(), value);
}
bool ParamString::getFloatList(const char *name, vector<float> &values) {
vector<string> strings;
if (!getList(name, strings)) return false;
values.resize(strings.size());
for (size_t i=0; i < strings.size(); i++) {
float f;
if (!parseFloat(strings[i].c_str(), f)) return false;
values[i] = f;
}
return true;
}
bool ParamString::getMap(const char *name, map<string,string> &values) {
string s;
if (!get(name, s)) return false;
return parseMap(s.c_str(), values);
}
/** If the string starts with a doublequote, read until the matching
doublequote. Otherwise, read until the end of the string, or until
one of the characters in endChars is reached.
Advances 'inputStr' by the number of characters consumed.
*/
string ParamString::readString(const char *&inputStr, const char *endChars) {
string result;
const char *p = skipWhitespace(inputStr);
// read until matching doublequote
if (*p == '"') {
p++;
while (*p && *p != '"') {
if (*p == '\\') {
switch (p[1]) {
case '\\': result.push_back('\\'); p++; break;
case '"': result.push_back('='); p++; break;
case 'n': result.push_back('\n'); p++; break;
case 'r': result.push_back('\r'); p++; break;
case 't': result.push_back('\t'); p++; break;
default:
// not a recognized escape, leave the backslash
result.push_back(*p);
}
} else {
result.push_back(*p);
}
p++;
}
if (*p == '"') p++;
inputStr = p;
}
// read until an endChar
else {
const char *e = strpbrk(p, endChars);
if (e == NULL)
e = p + strlen(p);
result.append(p, e);
// trim trailing whitespace
while (result.length() > 0 && isspace(result[result.length()-1]))
result.resize(result.length()-1);
inputStr = e;
}
return result;
}
string ParamString::formatString(const char *s, FormatStringEscapeType esc) {
// enclose in doublequotes if it contains unprintables or
// param-file punctuation characters
const char *endChars;
if (esc == FORMAT_STRING_LHS) endChars = "\"\n\t =";
else if (esc == FORMAT_STRING_RHS) endChars = "\"\n\t ";
else endChars = "\"\n\t ,[]{}=";
if (!strpbrk(s, endChars)) return string(s);
// escape doublequotes, newlines, and tab characters
// for the rest, it's sufficient to enclose them in doublequotes
string result;
const char *p = s;
result.push_back('"');
while (*p) {
if (*p == '"') {
result.append("\\\"");
} else if (*p == '\n') {
result.append("\\n");
} else if (*p == '\t') {
result.append("\\t");
} else {
result.push_back(*p);
}
p++;
}
result.push_back('"');
return result;
}
const char *ParamString::skipWhitespace(const char *s) {
while (isspace(*s)) s++;
return s;
}
int ParamString::readFirstLine(FILE *inf) {
string line;
int len;
if (!readLine(inf, line)) return -1;
if (1 != sscanf(line.c_str(), PARAM_STRING_FIRST_WORD " %d", &len))
return -2;
if (len < 0) return -3;
return len;
}
bool ParamString::readLine(FILE *inf, string &line) {
line.clear();
int c;
while (true) {
c = getc(inf);
if (c == EOF || c == '\n') break;
line.push_back((char)c);
}
return c != EOF || !line.empty();
}