proparserutils.h 8.59 KB
Newer Older
con's avatar
con committed
1 2 3 4 5 6 7 8
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9 10 11
**
** Non-Open Source Usage
**
con's avatar
con committed
12 13 14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15 16 17 18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19 20 21 22 23 24 25 26 27 28
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
29 30 31 32
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
33

con's avatar
con committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#ifndef PROPARSERUTILS_H
#define PROPARSERUTILS_H

#include <QtCore/QDir>
#include <QtCore/QLibraryInfo>

QT_BEGIN_NAMESPACE

// Pre- and postcondition macros
#define PRE(cond) do {if(!(cond))qt_assert(#cond,__FILE__,__LINE__);} while (0)
#define POST(cond) do {if(!(cond))qt_assert(#cond,__FILE__,__LINE__);} while (0)

// This struct is from qmake, but we are not using everything.
struct Option
{
    //simply global convenience
    //static QString libtool_ext;
    //static QString pkgcfg_ext;
    //static QString prf_ext;
    //static QString prl_ext;
    //static QString ui_ext;
    //static QStringList h_ext;
    //static QStringList cpp_ext;
    //static QString h_moc_ext;
    //static QString cpp_moc_ext;
    //static QString obj_ext;
    //static QString lex_ext;
    //static QString yacc_ext;
    //static QString h_moc_mod;
    //static QString cpp_moc_mod;
    //static QString lex_mod;
    //static QString yacc_mod;
    static QString dir_sep;
    static QString dirlist_sep;
    static QString qmakespec;
    static QChar field_sep;

    enum TARG_MODE { TARG_UNIX_MODE, TARG_WIN_MODE, TARG_MACX_MODE, TARG_MAC9_MODE, TARG_QNX6_MODE };
    static TARG_MODE target_mode;
    //static QString pro_ext;
    //static QString res_ext;

    static void init()
    {
#ifdef Q_OS_WIN
        Option::dirlist_sep = QLatin1Char(';');
        Option::dir_sep = QLatin1Char('\\');
#else
        Option::dirlist_sep = QLatin1Char(':');
        Option::dir_sep = QLatin1Char(QLatin1Char('/'));
#endif
        Option::qmakespec = QString::fromLatin1(qgetenv("QMAKESPEC").data());
        Option::field_sep = QLatin1Char(' ');
    }
};
#if defined(Q_OS_WIN32)
Option::TARG_MODE Option::target_mode = Option::TARG_WIN_MODE;
#elif defined(Q_OS_MAC)
Option::TARG_MODE Option::target_mode = Option::TARG_MACX_MODE;
#elif defined(Q_OS_QNX6)
Option::TARG_MODE Option::target_mode = Option::TARG_QNX6_MODE;
#else
Option::TARG_MODE Option::target_mode = Option::TARG_UNIX_MODE;
#endif

QString Option::qmakespec;
QString Option::dirlist_sep;
QString Option::dir_sep;
QChar Option::field_sep;

static void unquote(QString *string)
{
    PRE(string);
    if ( (string->startsWith(QLatin1Char('\"')) && string->endsWith(QLatin1Char('\"')))
        || (string->startsWith(QLatin1Char('\'')) && string->endsWith(QLatin1Char('\''))) )
    {
        string->remove(0,1);
        string->remove(string->length() - 1,1);
    }
}

static void insertUnique(QHash<QString, QStringList> *map,
    const QString &key, const QStringList &value, bool unique = true)
{
    QStringList &sl = (*map)[key];
    if (!unique) {
        sl += value;
    } else {
        for (int i = 0; i < value.count(); ++i) {
            if (!sl.contains(value.at(i)))
                sl.append(value.at(i));
        }
    }
}

/*
  See ProFileEvaluator::Private::visitProValue(...)

static QStringList replaceInList(const QStringList &varList, const QRegExp &regexp,
                           const QString &replace, bool global)
{
    QStringList resultList = varList;

    for (QStringList::Iterator varit = resultList.begin(); varit != resultList.end();) {
        if (varit->contains(regexp)) {
            *varit = varit->replace(regexp, replace);
            if (varit->isEmpty())
                varit = resultList.erase(varit);
            else
                ++varit;
            if (!global)
                break;
        } else {
            ++varit;
        }
    }
    return resultList;
}
*/

inline QStringList splitPathList(const QString paths)
{
    return paths.split(Option::dirlist_sep);
}

static QStringList split_arg_list(QString params)
{
    int quote = 0;
    QStringList args;

    const ushort LPAREN = '(';
    const ushort RPAREN = ')';
    const ushort SINGLEQUOTE = '\'';
    const ushort DOUBLEQUOTE = '"';
    const ushort COMMA = ',';
    const ushort SPACE = ' ';
    //const ushort TAB = '\t';

    ushort unicode;
    const QChar *params_data = params.data();
    const int params_len = params.length();
    int last = 0;
hjk's avatar
hjk committed
176
    while (last < params_len && ((params_data+last)->unicode() == SPACE
con's avatar
con committed
177 178
                                /*|| (params_data+last)->unicode() == TAB*/))
        ++last;
hjk's avatar
hjk committed
179
    for (int x = last, parens = 0; x <= params_len; x++) {
con's avatar
con committed
180
        unicode = (params_data+x)->unicode();
hjk's avatar
hjk committed
181 182
        if (x == params_len) {
            while (x && (params_data+(x-1))->unicode() == SPACE)
con's avatar
con committed
183 184
                --x;
            QString mid(params_data+last, x-last);
hjk's avatar
hjk committed
185 186
            if (quote) {
                if (mid[0] == quote && mid[(int)mid.length()-1] == quote)
con's avatar
con committed
187 188 189 190 191 192
                    mid = mid.mid(1, mid.length()-2);
                quote = 0;
            }
            args << mid;
            break;
        }
hjk's avatar
hjk committed
193
        if (unicode == LPAREN) {
con's avatar
con committed
194
            --parens;
hjk's avatar
hjk committed
195
        } else if (unicode == RPAREN) {
con's avatar
con committed
196
            ++parens;
hjk's avatar
hjk committed
197
        } else if (quote && unicode == quote) {
con's avatar
con committed
198
            quote = 0;
hjk's avatar
hjk committed
199
        } else if (!quote && (unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE)) {
con's avatar
con committed
200
            quote = unicode;
hjk's avatar
hjk committed
201
        } else if (!parens && !quote && unicode == COMMA) {
con's avatar
con committed
202 203 204
            QString mid = params.mid(last, x - last).trimmed();
            args << mid;
            last = x+1;
hjk's avatar
hjk committed
205
            while (last < params_len && ((params_data+last)->unicode() == SPACE
con's avatar
con committed
206 207 208 209
                                        /*|| (params_data+last)->unicode() == TAB*/))
                ++last;
        }
    }
hjk's avatar
hjk committed
210
    for (int i = 0; i < args.count(); i++)
con's avatar
con committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        unquote(&args[i]);
    return args;
}

static QStringList split_value_list(const QString &vals, bool do_semicolon=false)
{
    QString build;
    QStringList ret;
    QStack<QChar> quote;

    const QChar LPAREN = QLatin1Char('(');
    const QChar RPAREN = QLatin1Char(')');
    const QChar SINGLEQUOTE = QLatin1Char('\'');
    const QChar DOUBLEQUOTE = QLatin1Char('"');
    const QChar BACKSLASH = QLatin1Char('\\');
    const QChar SEMICOLON = QLatin1Char(';');

    const QChar *vals_data = vals.data();
    const int vals_len = vals.length();
hjk's avatar
hjk committed
230
    for (int x = 0, parens = 0; x < vals_len; x++) {
con's avatar
con committed
231 232 233 234 235 236
        QChar c = vals_data[x];
        if (x != vals_len-1 && c == BACKSLASH &&
           vals_data[x+1].unicode() == '\'' || vals_data[x+1] == DOUBLEQUOTE) {
            build += vals_data[x++]; // get that 'escape'
        } else if (!quote.isEmpty() && c == quote.top()) {
            quote.pop();
hjk's avatar
hjk committed
237
        } else if (c == SINGLEQUOTE || c == DOUBLEQUOTE) {
con's avatar
con committed
238
            quote.push(c);
hjk's avatar
hjk committed
239
        } else if (c == RPAREN) {
con's avatar
con committed
240
            --parens;
hjk's avatar
hjk committed
241
        } else if (c == LPAREN) {
con's avatar
con committed
242 243 244
            ++parens;
        }

hjk's avatar
hjk committed
245
        if (!parens && quote.isEmpty() && ((do_semicolon && c == SEMICOLON) ||
con's avatar
con committed
246 247 248 249 250 251 252
                                          vals_data[x] == Option::field_sep)) {
            ret << build;
            build.clear();
        } else {
            build += vals_data[x];
        }
    }
hjk's avatar
hjk committed
253
    if (!build.isEmpty())
con's avatar
con committed
254 255 256 257 258 259 260 261 262 263 264
        ret << build;
    return ret;
}

static QStringList qmake_mkspec_paths()
{
    QStringList ret;
    const QString concat = QDir::separator() + QString(QLatin1String("mkspecs"));
    QByteArray qmakepath = qgetenv("QMAKEPATH");
    if (!qmakepath.isEmpty()) {
        const QStringList lst = splitPathList(QString::fromLocal8Bit(qmakepath));
hjk's avatar
hjk committed
265
        for (QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
con's avatar
con committed
266 267 268 269 270 271 272 273 274 275
            ret << ((*it) + concat);
    }
    ret << QLibraryInfo::location(QLibraryInfo::DataPath) + concat;

    return ret;
}

QT_END_NAMESPACE

#endif // PROPARSERUTILS_H