1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
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
59
QT_BEGIN_NAMESPACE
60
61
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
62
63
QT_BEGIN_INCLUDE_NAMESPACE
64
#include "QtCore/qvector.h"
65
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
66
#  include "qt_windows.h"
67
#endif
68
QT_END_INCLUDE_NAMESPACE
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
                    p++;
107
                    if (*p == Char('\"') || *p == Char('\''))
108
                        ;                        // yes
109
                    else
110
                        p--;                        // treat \ literally
111
                } else {
112
                    if (!quote && (*p == Char('\"') || *p == Char('\''))) {        // " or ' quote
113
                        quote = *p++;
114
                        continue;
115
                    } else if (QChar((short)(*p)).isSpace() && !quote)
116
                        break;
117
                }
118
                if (*p)
119
                    *r++ = *p++;
120
            }
121
            if (*p && p < p_end)
122
                p++;
123
            *r = Char('\0');
124
125
            if (argc >= (int)argv.size()-1)        // expand array
126
                argv.resize(argv.size()*2);
127
            argv[argc++] = start;
128
        }
129
    }
130
    argv[argc] = 0;
131
132
    return argv;
133
}
134
135
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
136
static inline QStringList qWinCmdArgs(QString cmdLine) // not const-ref: this might be modified
137
{
138
    QStringList args;
139
140
    int argc = 0;
141
    QVector<wchar_t*> argv = qWinCmdLine<wchar_t>((wchar_t *)cmdLine.utf16(), cmdLine.length(), argc);
142
    for (int a = 0; a < argc; ++a) {
143
        args << QString::fromWCharArray(argv[a]);
144
    }
145
146
    return args;
147
}
148
149
static inline QStringList qCmdLineArgs(int argc, char *argv[])
150
{
151
    Q_UNUSED(argc)
152
    Q_UNUSED(argv)
153
    QString cmdLine = QString::fromWCharArray(GetCommandLine());
154
    return qWinCmdArgs(cmdLine);
155
}
156
#endif
157
#else  // !Q_OS_WIN || !Q_OS_SYMBIAN
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 || Q_OS_SYMBIAN
168
169
QT_END_NAMESPACE
170
171
#endif // QCORECMDLINEARGS_WIN_P_H