Qt 4.8
qcorecmdlineargs_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QCORECMDLINEARGS_P_H
43 #define QCORECMDLINEARGS_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include "QtCore/qstring.h"
57 #include "QtCore/qstringlist.h"
58 
60 
61 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
62 
64 #include "QtCore/qvector.h"
65 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
66 # include "qt_windows.h"
67 #endif
69 
70 // template implementation of the parsing algorithm
71 // this is used from qcoreapplication_win.cpp and the tools (rcc, uic...)
72 
73 template<typename Char>
74 static QVector<Char*> qWinCmdLine(Char *cmdParam, int length, int &argc)
75 {
76  QVector<Char*> argv(8);
77  Char *p = cmdParam;
78  Char *p_end = p + length;
79 
80  argc = 0;
81 
82  while (*p && p < p_end) { // parse cmd line arguments
83  while (QChar((short)(*p)).isSpace()) // skip white space
84  p++;
85  if (*p && p < p_end) { // arg starts
86  int quote;
87  Char *start, *r;
88  if (*p == Char('\"') || *p == Char('\'')) { // " or ' quote
89  quote = *p;
90  start = ++p;
91  } else {
92  quote = 0;
93  start = p;
94  }
95  r = start;
96  while (*p && p < p_end) {
97  if (quote) {
98  if (*p == quote) {
99  p++;
100  if (QChar((short)(*p)).isSpace())
101  break;
102  quote = 0;
103  }
104  }
105  if (*p == '\\') { // escape char?
106  if (*(p+1) == quote)
107  p++;
108  } else {
109  if (!quote && (*p == Char('\"') || *p == Char('\''))) { // " or ' quote
110  quote = *p++;
111  continue;
112  } else if (QChar((short)(*p)).isSpace() && !quote)
113  break;
114  }
115  if (*p)
116  *r++ = *p++;
117  }
118  if (*p && p < p_end)
119  p++;
120  *r = Char('\0');
121 
122  if (argc >= (int)argv.size()-1) // expand array
123  argv.resize(argv.size()*2);
124  argv[argc++] = start;
125  }
126  }
127  argv[argc] = 0;
128 
129  return argv;
130 }
131 
132 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
133 static inline QStringList qWinCmdArgs(QString cmdLine) // not const-ref: this might be modified
134 {
135  QStringList args;
136 
137  int argc = 0;
138  QVector<wchar_t*> argv = qWinCmdLine<wchar_t>((wchar_t *)cmdLine.utf16(), cmdLine.length(), argc);
139  for (int a = 0; a < argc; ++a) {
140  args << QString::fromWCharArray(argv[a]);
141  }
142 
143  return args;
144 }
145 
146 static inline QStringList qCmdLineArgs(int argc, char *argv[])
147 {
148  Q_UNUSED(argc)
149  Q_UNUSED(argv)
150  QString cmdLine = QString::fromWCharArray(GetCommandLine());
151  return qWinCmdArgs(cmdLine);
152 }
153 #endif
154 #else // !Q_OS_WIN || !Q_OS_SYMBIAN
155 
156 static inline QStringList qCmdLineArgs(int argc, char *argv[])
157 {
158  QStringList args;
159  for (int i = 0; i != argc; ++i)
160  args += QString::fromLocal8Bit(argv[i]);
161  return args;
162 }
163 
164 #endif // Q_OS_WIN || Q_OS_SYMBIAN
165 
167 
168 #endif // QCORECMDLINEARGS_WIN_P_H
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
static QStringList qWinCmdArgs(QString cmdLine)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
long ASN1_INTEGER_get ASN1_INTEGER * a
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
ushort Char
static QVector< Char * > qWinCmdLine(Char *cmdParam, int length, int &argc)
static QStringList qCmdLineArgs(int argc, char *argv[])
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290