+
+/*
+Returns a string which is equal to the string pointed to by p, but up to the
+point where p contains an character that's not allowed.
+Allowable characters are letters and numbers, and characters pointed to by
+the parameter allowedChars.
+
+For example, if p points to "abcde-@-_", and allowedChars is "-_",
+this function returns "abcde-".
+*/
+static wxString GetOptionName(wxString::const_iterator p,
+ wxString::const_iterator end,
+ const wxChar *allowedChars)
+{
+ wxString argName;
+
+ while ( p != end && (wxIsalnum(*p) || wxStrchr(allowedChars, *p)) )
+ {
+ argName += *p++;
+ }
+
+ return argName;
+}
+
+// Besides alphanumeric characters, short and long options can
+// have other characters.
+
+// A short option additionally can have these
+#define wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("_?")
+
+// A long option can have the same characters as a short option and a '-'.
+#define wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION \
+ wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION wxT("-")
+
+static wxString GetShortOptionName(wxString::const_iterator p,
+ wxString::const_iterator end)
+{
+ return GetOptionName(p, end, wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION);
+}
+
+static wxString GetLongOptionName(wxString::const_iterator p,
+ wxString::const_iterator end)
+{
+ return GetOptionName(p, end, wxCMD_LINE_CHARS_ALLOWED_BY_LONG_OPTION);
+}
+
+#endif // wxUSE_CMDLINE_PARSER
+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
+/*
+ This function is mainly used under Windows (as under Unix we always get the
+ command line arguments as argc/argv anyhow) and so it tries to follow
+ Windows conventions for the command line handling, not Unix ones. For
+ instance, backslash is not special except when it precedes double quote when
+ it does quote it.
+
+ TODO: Rewrite this to follow the even more complicated rule used by Windows
+ CommandLineToArgv():
+
+ * A string of backslashes not followed by a quotation mark has no special
+ meaning.
+ * An even number of backslashes followed by a quotation mark is treated as
+ pairs of protected backslashes, followed by a word terminator.
+ * An odd number of backslashes followed by a quotation mark is treated as
+ pairs of protected backslashes, followed by a protected quotation mark.
+
+ See http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx
+
+ It could also be useful to provide a converse function which is also
+ non-trivial, see
+
+ http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
+ */
+
+/* static */
+wxArrayString
+wxCmdLineParser::ConvertStringToArgs(const wxString& cmdline,
+ wxCmdLineSplitType type)
+{
+ wxArrayString args;
+
+ wxString arg;
+ arg.reserve(1024);
+
+ const wxString::const_iterator end = cmdline.end();
+ wxString::const_iterator p = cmdline.begin();
+
+ for ( ;; )
+ {
+ // skip white space
+ while ( p != end && (*p == ' ' || *p == '\t') )
+ ++p;
+
+ // anything left?
+ if ( p == end )
+ break;
+
+ // parse this parameter
+ bool lastBS = false,
+ isInsideQuotes = false;
+ wxChar chDelim = '\0';
+ for ( arg.clear(); p != end; ++p )
+ {
+ const wxChar ch = *p;
+
+ if ( type == wxCMD_LINE_SPLIT_DOS )
+ {
+ if ( ch == '"' )
+ {
+ if ( !lastBS )
+ {
+ isInsideQuotes = !isInsideQuotes;
+
+ // don't put quote in arg
+ continue;
+ }
+ //else: quote has no special meaning but the backslash
+ // still remains -- makes no sense but this is what
+ // Windows does
+ }
+ // note that backslash does *not* quote the space, only quotes do
+ else if ( !isInsideQuotes && (ch == ' ' || ch == '\t') )
+ {
+ ++p; // skip this space anyhow
+ break;
+ }
+
+ lastBS = !lastBS && ch == '\\';
+ }
+ else // type == wxCMD_LINE_SPLIT_UNIX
+ {
+ if ( !lastBS )
+ {
+ if ( isInsideQuotes )
+ {
+ if ( ch == chDelim )
+ {
+ isInsideQuotes = false;
+
+ continue; // don't use the quote itself
+ }
+ }
+ else // not in quotes and not escaped
+ {
+ if ( ch == '\'' || ch == '"' )
+ {
+ isInsideQuotes = true;
+ chDelim = ch;
+
+ continue; // don't use the quote itself
+ }
+
+ if ( ch == ' ' || ch == '\t' )
+ {
+ ++p; // skip this space anyhow
+ break;
+ }
+ }
+
+ lastBS = ch == '\\';
+ if ( lastBS )
+ continue;
+ }
+ else // escaped by backslash, just use as is
+ {
+ lastBS = false;
+ }
+ }
+
+ arg += ch;
+ }
+
+ args.push_back(arg);
+ }
+
+ return args;
+}