+
+/*
+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(const wxChar *p,
+ const wxChar *allowedChars)
+{
+ wxString argName;
+
+ while ( *p && (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(const wxChar *p)
+{
+ return GetOptionName(p, wxCMD_LINE_CHARS_ALLOWED_BY_SHORT_OPTION);
+}
+
+static wxString GetLongOptionName(const wxChar *p)
+{
+ return GetOptionName(p, 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.
+ */
+
+/* static */
+wxArrayString wxCmdLineParser::ConvertStringToArgs(const wxChar *p)
+{
+ wxArrayString args;
+
+ wxString arg;
+ arg.reserve(1024);
+
+ bool isInsideQuotes = false;
+ for ( ;; )
+ {
+ // skip white space
+ while ( *p == _T(' ') || *p == _T('\t') )
+ p++;
+
+ // anything left?
+ if ( *p == _T('\0') )
+ break;
+
+ // parse this parameter
+ bool endParam = false;
+ bool lastBS = false;
+ for ( arg.clear(); !endParam; p++ )
+ {
+ switch ( *p )
+ {
+ case _T('"'):
+ 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
+ break;
+
+ case _T(' '):
+ case _T('\t'):
+ // backslash does *not* quote the space, only quotes do
+ if ( isInsideQuotes )
+ {
+ // skip assignment below
+ break;
+ }
+ // fall through
+
+ case _T('\0'):
+ endParam = true;
+
+ break;
+ }
+
+ if ( endParam )
+ {
+ break;
+ }
+
+ lastBS = *p == _T('\\');
+
+ arg += *p;
+ }
+
+ args.push_back(arg);
+ }
+
+ return args;
+}
+