[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r2483: Add a method for parsing a QStringList of (possibly quoted a (in vidalia/trunk: . src/util)
Author: edmanm
Date: 2008-04-05 00:09:18 -0400 (Sat, 05 Apr 2008)
New Revision: 2483
Modified:
vidalia/trunk/
vidalia/trunk/src/util/stringutil.cpp
vidalia/trunk/src/util/stringutil.h
Log:
r309@lysithea: edmanm | 2008-04-04 23:57:46 -0400
Add a method for parsing a QStringList of (possibly quoted and escaped) command
line arguments from a QString. Also add a method for converting back to a
(possibly quoted and escaped) QString from a QStringList of arguments.
Property changes on: vidalia/trunk
___________________________________________________________________
svk:merge ticket from /local/vidalia/trunk [r309] on 90112fd6-a33b-4cea-8d39-48ff1d78625c
Modified: vidalia/trunk/src/util/stringutil.cpp
===================================================================
--- vidalia/trunk/src/util/stringutil.cpp 2008-04-05 04:09:14 UTC (rev 2482)
+++ vidalia/trunk/src/util/stringutil.cpp 2008-04-05 04:09:18 UTC (rev 2483)
@@ -126,13 +126,13 @@
string_escape(const QString &str)
{
QString out;
- out.append('\"');
+ out.append("\"");
for (int i = 0; i < str.length(); i++) {
if (str[i] == '\"' || str[i] == '\\')
out.append('\\');
out.append(str[i]);
}
- out.append('\"');
+ out.append("\"");
return out;
}
@@ -222,6 +222,76 @@
return QHash<QString,QString>();
}
+/** Parses a series of command line arguments from <b>str</b>. If <b>str</b>
+ * was unable to be parse, <b>ok</b> is set to false. */
+QStringList
+string_parse_arguments(const QString &str, bool *ok)
+{
+ QStringList args;
+ int i, len;
+ bool tmp_ok;
+
+ i = 0;
+ len = str.length();
+ while (i < len && str[i].isSpace())
+ i++; /* Skip initial whitespace */
+ while (i < len) {
+ QString arg;
+
+ if (str[i] == '\"') {
+ /* The value is wrapped in quotes */
+ arg.append(str[i]);
+ while (++i < len) {
+ arg.append(str[i]);
+ if (str[i] == '\\') {
+ if (++i == len)
+ goto error;
+ arg.append(str[i]);
+ } else if (str[i] == '\"') {
+ i++;
+ break;
+ }
+ }
+ arg = string_unescape(arg, &tmp_ok);
+ if (!tmp_ok)
+ goto error;
+ args << arg;
+ } else {
+ /* The value was not wrapped in quotes */
+ while (i < len && !str[i].isSpace())
+ arg.append(str[i++]);
+ args << arg;
+ }
+ while (i < len && str[i].isSpace())
+ i++;
+ }
+
+ if (ok)
+ *ok = true;
+ return args;
+
+error:
+ if (ok)
+ *ok = false;
+ return QStringList();
+}
+
+/** Formats the list of command line arguments in <b>args</b> as a string.
+ * Arguments that contain ' ', '\', or '"' tokens will be escaped and
+ * wrapped in double quotes. */
+QString
+string_format_arguments(const QStringList &args)
+{
+ QStringList out;
+ foreach (QString arg, args) {
+ if (arg.contains("\"") || arg.contains("\\") || arg.contains(" "))
+ out << string_escape(arg);
+ else
+ out << arg;
+ }
+ return out.join(" ");
+}
+
/** Returns true if <b>str</b> is a valid hexademical string. Returns false
* otherwise. */
bool
Modified: vidalia/trunk/src/util/stringutil.h
===================================================================
--- vidalia/trunk/src/util/stringutil.h 2008-04-05 04:09:14 UTC (rev 2482)
+++ vidalia/trunk/src/util/stringutil.h 2008-04-05 04:09:18 UTC (rev 2483)
@@ -60,6 +60,15 @@
* to be parsed, <b>ok</b> is set to false. */
QHash<QString,QString> string_parse_keyvals(const QString &str, bool *ok = 0);
+/** Parses a series of command line arguments from <b>str</b>. If <b>str</b>
+ * was unable to be parse, <b>ok</b> is set to false. */
+QStringList string_parse_arguments(const QString &str, bool *ok = 0);
+
+/** Formats the list of command line arguments in <b>args</b> as a string.
+ * Arguments that contain ' ', '\', or '"' tokens will be escaped and wrapped
+ * in double quotes. */
+QString string_format_arguments(const QStringList &args);
+
/** Returns true if <b>str</b> is a valid hexademical string. Returns false
* otherwise. */
bool string_is_hex(const QString &str);