57
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the QtCore module of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
10 ** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11 ** accordance with the Qt Commercial License Agreement provided with the
|
|
12 ** Software or, alternatively, in accordance with the terms contained in
|
|
13 ** a written agreement between you and Nokia.
|
|
14 **
|
|
15 ** GNU Lesser General Public License Usage
|
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
17 ** General Public License version 2.1 as published by the Free Software
|
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
19 ** packaging of this file. Please review the following information to
|
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
22 **
|
|
23 ** In addition, as a special exception, Nokia gives you certain
|
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL
|
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|
26 ** package.
|
|
27 **
|
|
28 ** GNU General Public License Usage
|
|
29 ** Alternatively, this file may be used under the terms of the GNU
|
|
30 ** General Public License version 3.0 as published by the Free Software
|
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the
|
|
32 ** packaging of this file. Please review the following information to
|
|
33 ** ensure the GNU General Public License version 3.0 requirements will be
|
|
34 ** met: http://www.gnu.org/copyleft/gpl.html.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
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
|
|
59 QT_BEGIN_NAMESPACE
|
|
60
|
|
61 #if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
|
|
62
|
|
63 QT_BEGIN_INCLUDE_NAMESPACE
|
|
64 #include "QtCore/qvector.h"
|
|
65 #include "qt_windows.h"
|
|
66 QT_END_INCLUDE_NAMESPACE
|
|
67
|
|
68 // template implementation of the parsing algorithm
|
|
69 // this is used from qcoreapplication_win.cpp and the tools (rcc, uic...)
|
|
70
|
|
71 template<typename Char>
|
|
72 static QVector<Char*> qWinCmdLine(Char *cmdParam, int length, int &argc)
|
|
73 {
|
|
74 QVector<Char*> argv(8);
|
|
75 Char *p = cmdParam;
|
|
76 Char *p_end = p + length;
|
|
77
|
|
78 argc = 0;
|
|
79
|
|
80 while (*p && p < p_end) { // parse cmd line arguments
|
|
81 while (QChar((short)(*p)).isSpace()) // skip white space
|
|
82 p++;
|
|
83 if (*p && p < p_end) { // arg starts
|
|
84 int quote;
|
|
85 Char *start, *r;
|
|
86 if (*p == Char('\"') || *p == Char('\'')) { // " or ' quote
|
|
87 quote = *p;
|
|
88 start = ++p;
|
|
89 } else {
|
|
90 quote = 0;
|
|
91 start = p;
|
|
92 }
|
|
93 r = start;
|
|
94 while (*p && p < p_end) {
|
|
95 if (quote) {
|
|
96 if (*p == quote) {
|
|
97 p++;
|
|
98 if (QChar((short)(*p)).isSpace())
|
|
99 break;
|
|
100 quote = 0;
|
|
101 }
|
|
102 }
|
|
103 if (*p == '\\') { // escape char?
|
|
104 p++;
|
|
105 if (*p == Char('\"') || *p == Char('\''))
|
|
106 ; // yes
|
|
107 else
|
|
108 p--; // treat \ literally
|
|
109 } else {
|
|
110 if (!quote && (*p == Char('\"') || *p == Char('\''))) { // " or ' quote
|
|
111 quote = *p++;
|
|
112 continue;
|
|
113 } else if (QChar((short)(*p)).isSpace() && !quote)
|
|
114 break;
|
|
115 }
|
|
116 if (*p)
|
|
117 *r++ = *p++;
|
|
118 }
|
|
119 if (*p && p < p_end)
|
|
120 p++;
|
|
121 *r = Char('\0');
|
|
122
|
|
123 if (argc >= (int)argv.size()-1) // expand array
|
|
124 argv.resize(argv.size()*2);
|
|
125 argv[argc++] = start;
|
|
126 }
|
|
127 }
|
|
128 argv[argc] = 0;
|
|
129
|
|
130 return argv;
|
|
131 }
|
|
132
|
|
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<ushort*> argv = qWinCmdLine<ushort>((ushort*)cmdLine.utf16(), cmdLine.length(), argc);
|
|
139 for (int a = 0; a < argc; ++a) {
|
|
140 args << QString::fromUtf16(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 = QT_WA_INLINE(
|
|
151 QString::fromUtf16((unsigned short*)GetCommandLineW()),
|
|
152 QString::fromLocal8Bit(GetCommandLineA())
|
|
153 );
|
|
154 return qWinCmdArgs(cmdLine);
|
|
155 }
|
|
156
|
|
157 #else // !Q_OS_WIN
|
|
158
|
|
159 static inline QStringList qCmdLineArgs(int argc, char *argv[])
|
|
160 {
|
|
161 QStringList args;
|
|
162 for (int i = 0; i != argc; ++i)
|
|
163 args += QString::fromLocal8Bit(argv[i]);
|
|
164 return args;
|
|
165 }
|
|
166
|
|
167 #endif // Q_OS_WIN
|
|
168
|
|
169 QT_END_NAMESPACE
|
|
170
|
|
171 #endif // QCORECMDLINEARGS_WIN_P_H
|