+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
+/* 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
+ arg.clear();
+ for ( ;; p++ )
+ {
+ // do we have a (lone) backslash?
+ bool isQuotedByBS = FALSE;
+ while ( *p == _T('\\') )
+ {
+ p++;
+
+ // if we have 2 backslashes in a row, output one
+ if ( isQuotedByBS )
+ {
+ arg += _T('\\');
+ isQuotedByBS = FALSE;
+ }
+ else // the next char is quoted
+ {
+ isQuotedByBS = TRUE;
+ }
+ }
+
+ bool skipChar = FALSE,
+ endParam = FALSE;
+ switch ( *p )
+ {
+ case _T('"'):
+ if ( !isQuotedByBS )
+ {
+ // don't put the quote itself in the arg
+ skipChar = TRUE;
+
+ isInsideQuotes = !isInsideQuotes;
+ }
+ //else: insert a literal quote
+
+ break;
+
+ case _T(' '):
+ case _T('\t'):
+ if ( isInsideQuotes || isQuotedByBS )
+ {
+ // preserve it, skip endParam below
+ break;
+ }
+ //else: fall through
+
+ case _T('\0'):
+ endParam = TRUE;
+ break;
+
+ default:
+ if ( isQuotedByBS )
+ {
+ // ignore backslash before an ordinary character - this
+ // is needed to properly handle the file names under
+ // Windows appearing in the command line
+ arg += _T('\\');
+ }
+ }
+
+ // end of argument?
+ if ( endParam )
+ break;
+
+ // otherwise copy this char to arg
+ if ( !skipChar )
+ {
+ arg += *p;
+ }
+ }
+
+ args.Add(arg);
+ }
+
+ return args;
+}
+