MCPcopy Create free account
hub / github.com/RetroShare/RetroShare / string_parse_keyvals

Function string_parse_keyvals

retroshare-gui/src/util/stringutil.cpp:195–252  ·  view source on GitHub ↗

Parses a series of space-separated key[=value|="value"] tokens from * str and returns the mappings in a QHash. If str was unable * to be parsed, ok is set to false. */

Source from the content-addressed store, hash-verified

193 * <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
194 * to be parsed, <b>ok</b> is set to false. */
195QHash<QString,QString>
196string_parse_keyvals(const QString &str, bool *ok)
197{
198 int i, len;
199 bool tmp_ok;
200 QHash<QString,QString> keyvals;
201
202 i = 0;
203 len = str.length();
204 while (i < len && str[i].isSpace())
205 i++; /* Skip initial whitespace */
206 while (i < len) {
207 QString key, val;
208
209 while (i < len && !str[i].isSpace() && str[i] != '=')
210 key.append(str[i++]);
211
212 if (i < len && str[i] == '=') {
213 if (++i < len && str[i] == '\"') {
214 /* The value is wrapped in quotes */
215 val.append(str[i]);
216 while (++i < len) {
217 val.append(str[i]);
218 if (str[i] == '\\') {
219 if (++i == len)
220 goto error;
221 val.append(str[i]);
222 } else if (str[i] == '\"') {
223 i++;
224 break;
225 }
226 }
227 val = string_unescape(val, &tmp_ok);
228 if (!tmp_ok)
229 goto error;
230 keyvals.insert(key, val);
231 } else {
232 /* The value was not wrapped in quotes */
233 while (i < len && !str[i].isSpace())
234 val.append(str[i++]);
235 keyvals.insert(key, val);
236 }
237 } else {
238 /* The key had no value */
239 keyvals.insert(key, QString(""));
240 }
241 while (i < len && str[i].isSpace())
242 i++;
243 }
244 if (ok)
245 *ok = true;
246 return keyvals;
247
248error:
249 if (ok)
250 *ok = false;
251 return QHash<QString,QString>();
252}

Callers

nothing calls this directly

Calls 4

string_unescapeFunction · 0.85
appendMethod · 0.80
insertMethod · 0.80
QStringClass · 0.70

Tested by

no test coverage detected