5 * Copyright (C) 2005-2013 Team XBMC
8 * This Program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This Program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with XBMC; see the file COPYING. If not, see
20 * <http://www.gnu.org/licenses/>.
25 #include "PlatformDefs.h"
28 #define _snprintf snprintf
35 class CmdLineArgs : public std::vector<char*>
40 // Save local copy of the command line string, because
41 // ParseCmdLine() modifies this string while parsing it.
42 PSZ cmdline = GetCommandLine();
43 m_cmdline = new char [strlen (cmdline) + 1];
46 strcpy (m_cmdline, cmdline);
55 CmdLineArgs (const int argc, const char **argv)
61 for (int i = 0 ; i<argc ; i++)
63 cmdline += std::string(argv[i]);
76 m_cmdline = new char [cmdline.length() + 1];
79 strcpy(m_cmdline, cmdline.c_str());
90 PSZ m_cmdline; // the command line string
92 ////////////////////////////////////////////////////////////////////////////////
93 // Parse m_cmdline into individual tokens, which are delimited by spaces. If a
94 // token begins with a quote, then that token is terminated by the next quote
95 // followed immediately by a space or terminator. This allows tokens to contain
97 // This input string: This "is" a ""test"" "of the parsing" alg"o"rithm.
98 // Produces these tokens: This, is, a, "test", of the parsing, alg"o"rithm
99 ////////////////////////////////////////////////////////////////////////////////
105 bool bInQuotes = false;
106 PSZ pargs = m_cmdline;
110 while (isspace (*pargs)) // skip leading whitespace
113 bInQuotes = (*pargs == QUOTE); // see if this token is quoted
115 if (bInQuotes) // skip leading quote
118 push_back (pargs); // store position of current token
121 // NOTE: Args are normally terminated by whitespace, unless the
122 // arg is quoted. That's why we handle the two cases separately,
123 // even though they are very similar.
126 // find next quote followed by a space or terminator
128 !(*pargs == QUOTE && (isspace (pargs[1]) || pargs[1] == TERM)))
132 *pargs = TERM; // terminate token
133 if (pargs[1]) // if quoted token not followed by a terminator
134 pargs += 2; // advance to next token
139 // skip to next non-whitespace character
140 while (*pargs && !isspace (*pargs))
142 if (*pargs && isspace (*pargs)) // end of token
144 *pargs = TERM; // terminate token
145 pargs++; // advance to next token or terminator
150 }; // class CmdLineArgs
153 #endif // CMDLINEARGS_H