[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r1852: Add some methods for parsing a series of space-separated "ke (in trunk: . src/util)
Author: edmanm
Date: 2007-08-22 21:06:04 -0400 (Wed, 22 Aug 2007)
New Revision: 1852
Modified:
trunk/
trunk/src/util/string.cpp
trunk/src/util/string.h
Log:
r2024@adrastea: edmanm | 2007-08-22 21:01:02 -0400
Add some methods for parsing a series of space-separated "key=value" tokens
and returning a QHash of the key-to-value mappings.
Property changes on: trunk
___________________________________________________________________
svk:merge ticket from /vidalia/local/trunk [r2024] on 54b3572a-7227-0410-958f-53ecd705b71a
Modified: trunk/src/util/string.cpp
===================================================================
--- trunk/src/util/string.cpp 2007-08-22 16:32:34 UTC (rev 1851)
+++ trunk/src/util/string.cpp 2007-08-23 01:06:04 UTC (rev 1852)
@@ -130,3 +130,89 @@
return hex;
}
+/** Given a quoted string <b>str</b>, this function returns an unquoted,
+ * unescaped string. <b>str</b> must start and end with an unescaped quote. */
+QString
+string_unescape(const QString str, bool *ok)
+{
+ QString out;
+
+ /* The string must start and end with an unescaped dquote */
+ if (str.length() < 2 || !str.startsWith("\"") || !str.endsWith("\"") ||
+ (str.endsWith("\\\"") && !str.endsWith("\\\\\""))) {
+ if (ok)
+ *ok = false;
+ return QString();
+ }
+ for (int i = 1; i < str.length()-1; i++) {
+ if (str[i] == '\\')
+ i++;
+ out.append(str[i]);
+ }
+ if (ok)
+ *ok = true;
+ return out;
+}
+
+/** Parses a series of space-separated key[=value|="value"] tokens from
+ * <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
+ * to be parsed, <b>ok</b> is set to false. */
+QHash<QString,QString>
+string_parse_keyvals(const QString str, bool *ok)
+{
+ int i, len;
+ bool tmp_ok;
+ QHash<QString,QString> keyvals;
+
+ i = 0;
+ len = str.length();
+ while (i < len && str[i].isSpace())
+ i++; /* Skip initial whitespace */
+ while (i < len) {
+ QString key, val;
+
+ while (i < len && !str[i].isSpace() && str[i] != '=')
+ key.append(str[i++]);
+
+ if (i < len && str[i] == '=') {
+ if (++i < len && str[i] == '\"') {
+ /* The value is wrapped in quotes */
+ val.append(str[i]);
+ while (++i < len) {
+ val.append(str[i]);
+ if (str[i] == '\\') {
+ if (++i == len)
+ goto error;
+ val.append(str[i]);
+ } else if (str[i] == '\"') {
+ i++;
+ break;
+ }
+ }
+ val = string_unescape(val, &tmp_ok);
+ if (!tmp_ok)
+ goto error;
+ keyvals.insert(key, val);
+ } else {
+ /* The value was not wrapped in quotes */
+ while (i < len && !str[i].isSpace())
+ val.append(str[i++]);
+ keyvals.insert(key, val);
+ }
+ } else {
+ /* The key had no value */
+ keyvals.insert(key, QString(""));
+ }
+ while (i < len && str[i].isSpace())
+ i++;
+ }
+ if (ok)
+ *ok = true;
+ return keyvals;
+
+error:
+ if (ok)
+ *ok = false;
+ return QHash<QString,QString>();
+}
+
Modified: trunk/src/util/string.h
===================================================================
--- trunk/src/util/string.h 2007-08-22 16:32:34 UTC (rev 1851)
+++ trunk/src/util/string.h 2007-08-23 01:06:04 UTC (rev 1852)
@@ -29,7 +29,9 @@
#define __STRING_H
#include <QStringList>
+#include <QHash>
+
/** Creates a QStringList from the array of C strings. */
QStringList char_array_to_stringlist(char **arr, int len);
@@ -55,5 +57,14 @@
* util.c. See LICENSE for details on Tor's license. */
QString base16_encode(const QByteArray buf);
+/** Given a quoted string <b>str</b>, this function returns an unquoted,
+ * unescaped string. <b>str</b> must start and end with an unescaped quote. */
+QString string_unescape(const QString str, bool *ok = 0);
+
+/** Parses a series of space-separated key[=value|="value"] tokens from
+ * <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
+ * to be parsed, <b>ok</b> is set to false. */
+QHash<QString,QString> string_parse_keyvals(const QString str, bool *ok = 0);
+
#endif