commandlinetokenizer.cpp
1 //------------------------------------------------------------------------------
2 // commandlinetokenizer.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "commandlinetokenizer.h"
24 
25 #include "scanner.h"
26 
27 #ifdef Q_OS_WIN32
28  #include <windows.h>
29 #endif
30 
31 class CommandLineTokenizer::PrivData
32 {
33 public:
34  #ifdef Q_OS_WIN32
35  static QStringList tokenize(const QString &cmdLine)
36  {
37  if (cmdLine.isEmpty())
38  {
39  // CommandLineToArgvW() returns path to current executable
40  // if lpCmdLine argument is an empty string. We don't want that
41  // here.
42  return QStringList();
43  }
44  int numArgs = 0;
45  LPCWSTR winapiCmdLine = (LPCWSTR)cmdLine.utf16();
46  LPWSTR* winapiTokens = CommandLineToArgvW(winapiCmdLine, &numArgs);
47 
48  if (winapiTokens == NULL)
49  {
50  return QStringList();
51  }
52 
53  QStringList result;
54  for (int i = 0; i < numArgs; ++i)
55  {
56  // Conversion to "ushort*" seems to work for LPWSTR.
57  result << QString::fromUtf16((const ushort*)winapiTokens[i]);
58  }
59  LocalFree(winapiTokens);
60  return result;
61  }
62  #else
63  static QStringList tokenize(const QString &cmdLine)
64  {
65  QStringList result;
66  Scanner sc(cmdLine.toAscii().constData(), cmdLine.length());
67  while (sc.nextString())
68  {
69  result << sc->str();
70  }
71  return result;
72  }
73  #endif
74 };
75 
76 QStringList CommandLineTokenizer::tokenize(const QString &cmdLine)
77 {
78  return PrivData::tokenize(cmdLine);
79 }
Scanner reads scripts by checking individual tokens.
Definition: scanner.h:75