src/corelib/kernel/qcorecmdlineargs_p.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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)
       
    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<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 
       
   154 #else  // !Q_OS_WIN
       
   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
       
   165 
       
   166 QT_END_NAMESPACE
       
   167 
       
   168 #endif // QCORECMDLINEARGS_WIN_P_H