tools/linguist/shared/proparserutils.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2006-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the Qt Linguist of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef PROPARSERUTILS_H
00025 #define PROPARSERUTILS_H
00026 
00027 #include <QtCore/QLibraryInfo>
00028 
00029 // Pre- and postcondition macros
00030 #define PRE(cond) do {if(!(cond))qt_assert(#cond,__FILE__,__LINE__);} while (0)
00031 #define POST(cond) do {if(!(cond))qt_assert(#cond,__FILE__,__LINE__);} while (0)
00032 
00033 // This struct is from qmake, but we are not using everything.
00034 struct Option
00035 {
00036     //simply global convenience
00037     //static QString libtool_ext;
00038     //static QString pkgcfg_ext;
00039     //static QString prf_ext;
00040     //static QString prl_ext;
00041     //static QString ui_ext;
00042     //static QStringList h_ext;
00043     //static QStringList cpp_ext;
00044     //static QString h_moc_ext;
00045     //static QString cpp_moc_ext;
00046     //static QString obj_ext;
00047     //static QString lex_ext;
00048     //static QString yacc_ext;
00049     //static QString h_moc_mod;
00050     //static QString cpp_moc_mod;
00051     //static QString lex_mod;
00052     //static QString yacc_mod;
00053     static QString dir_sep;
00054     static QString dirlist_sep;
00055     static QString qmakespec;
00056     
00057     enum TARG_MODE { TARG_UNIX_MODE, TARG_WIN_MODE, TARG_MACX_MODE, TARG_MAC9_MODE, TARG_QNX6_MODE };
00058     static TARG_MODE target_mode;
00059     //static QString pro_ext;
00060     //static QString res_ext;
00061     //static char field_sep;
00062 
00063     static void init()
00064     {
00065 #ifdef Q_OS_WIN
00066         Option::dirlist_sep = ';';
00067         Option::dir_sep = QLatin1Char('\\');
00068 #else
00069         Option::dirlist_sep = ':';
00070         Option::dir_sep = QLatin1Char('/');
00071 #endif
00072         Option::qmakespec = qgetenv("QMAKESPEC");
00073     }
00074 };
00075 #if defined(Q_OS_WIN32)
00076 Option::TARG_MODE Option::target_mode = Option::TARG_WIN_MODE;
00077 #elif defined(Q_OS_MAC)
00078 Option::TARG_MODE Option::target_mode = Option::TARG_MACX_MODE;
00079 #elif defined(Q_OS_QNX6)
00080 Option::TARG_MODE Option::target_mode = Option::TARG_QNX6_MODE;
00081 #else
00082 Option::TARG_MODE Option::target_mode = Option::TARG_UNIX_MODE;
00083 #endif
00084 
00085 QString Option::qmakespec;
00086 QString Option::dirlist_sep;
00087 QString Option::dir_sep;
00088 
00089 static void unquote(QString *string)
00090 {
00091     PRE(string);
00092     if ( (string->startsWith(QLatin1Char('\"')) && string->endsWith(QLatin1Char('\"')))
00093         || (string->startsWith(QLatin1Char('\'')) && string->endsWith(QLatin1Char('\''))) )
00094     {
00095         string->remove(0,1);
00096         string->remove(string->length() - 1,1);
00097     }
00098 }
00099 
00100 
00101 static void insertUnique(QMap<QByteArray, QStringList> *map, const QByteArray &key, const QString &value, bool unique = true)
00102 {
00103     QStringList &sl = (*map)[key];
00104     if (!unique || (unique && !sl.contains(value))) {
00105         sl.append(value);
00106     }
00107 }
00108 
00109 inline QStringList splitPathList(const QString paths) { return paths.split(Option::dirlist_sep); }
00110 
00111 static QStringList split_arg_list(QString params)
00112 {
00113     int quote = 0;
00114     QStringList args;
00115 
00116     const ushort LPAREN = '(';
00117     const ushort RPAREN = ')';
00118     const ushort SINGLEQUOTE = '\'';
00119     const ushort DOUBLEQUOTE = '"';
00120     const ushort COMMA = ',';
00121     const ushort SPACE = ' ';
00122     //const ushort TAB = '\t';
00123 
00124     ushort unicode;
00125     const QChar *params_data = params.data();
00126     const int params_len = params.length();
00127     int last = 0;
00128     while(last < params_len && ((params_data+last)->unicode() == SPACE
00129                                 /*|| (params_data+last)->unicode() == TAB*/))
00130         ++last;
00131     for(int x = last, parens = 0; x <= params_len; x++) {
00132         unicode = (params_data+x)->unicode();
00133         if(x == params_len) {
00134             while(x && (params_data+(x-1))->unicode() == SPACE)
00135                 --x;
00136             QString mid(params_data+last, x-last);
00137             if(quote) {
00138                 if(mid[0] == quote && mid[(int)mid.length()-1] == quote)
00139                     mid = mid.mid(1, mid.length()-2);
00140                 quote = 0;
00141             }
00142             args << mid;
00143             break;
00144         }
00145         if(unicode == LPAREN) {
00146             --parens;
00147         } else if(unicode == RPAREN) {
00148             ++parens;
00149         } else if(quote && unicode == quote) {
00150             quote = 0;
00151         } else if(!quote && (unicode == SINGLEQUOTE || unicode == DOUBLEQUOTE)) {
00152             quote = unicode;
00153         } else if(!parens && !quote && unicode == COMMA) {
00154             QString mid = params.mid(last, x - last).trimmed();
00155             args << mid;
00156             last = x+1;
00157             while(last < params_len && ((params_data+last)->unicode() == SPACE
00158                                         /*|| (params_data+last)->unicode() == TAB*/))
00159                 ++last;
00160         }
00161     }
00162     for(int i = 0; i < args.count(); i++)
00163         unquote(&args[i]);
00164     return args;
00165 }
00166 
00167 
00168 static QStringList qmake_mkspec_paths()
00169 {
00170     QStringList ret;
00171     const QString concat = QDir::separator() + QString("mkspecs");
00172     QByteArray qmakepath = qgetenv("QMAKEPATH");
00173     if (!qmakepath.isEmpty()) {
00174         const QStringList lst = splitPathList(QString::fromLocal8Bit(qmakepath));
00175         for(QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
00176             ret << ((*it) + concat);
00177     }
00178     ret << QLibraryInfo::location(QLibraryInfo::DataPath) + concat;
00179 
00180     return ret;
00181 }
00182 
00183 
00184 static QString getPropertyValue(const QString &v)
00185 {
00186     if(v == "QT_INSTALL_PREFIX")
00187         return QLibraryInfo::location(QLibraryInfo::PrefixPath);
00188     else if(v == "QT_INSTALL_DATA")
00189         return QLibraryInfo::location(QLibraryInfo::DataPath);
00190     else if(v == "QT_INSTALL_DOCS")
00191         return QLibraryInfo::location(QLibraryInfo::DocumentationPath);
00192     else if(v == "QT_INSTALL_HEADERS")
00193         return QLibraryInfo::location(QLibraryInfo::HeadersPath);
00194     else if(v == "QT_INSTALL_LIBS")
00195         return QLibraryInfo::location(QLibraryInfo::LibrariesPath);
00196     else if(v == "QT_INSTALL_BINS")
00197         return QLibraryInfo::location(QLibraryInfo::BinariesPath);
00198     else if(v == "QT_INSTALL_PLUGINS")
00199         return QLibraryInfo::location(QLibraryInfo::PluginsPath);
00200     else if(v == "QT_INSTALL_TRANSLATIONS")
00201         return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
00202     else if(v == "QT_INSTALL_CONFIGURATION")
00203         return QLibraryInfo::location(QLibraryInfo::SettingsPath);
00204     else if(v == "QT_INSTALL_EXAMPLES")
00205         return QLibraryInfo::location(QLibraryInfo::ExamplesPath);
00206     else if(v == "QT_INSTALL_DEMOS")
00207         return QLibraryInfo::location(QLibraryInfo::DemosPath);
00208     else if(v == "QMAKE_MKSPECS")
00209         return qmake_mkspec_paths().join(Option::dirlist_sep);
00210     else if(v == "QMAKE_VERSION")
00211         return QLatin1String("1.0");        //###
00212         //return qmake_version();
00213 #ifdef QT_VERSION_STR
00214     else if(v == "QT_VERSION")
00215         return QT_VERSION_STR;
00216 #endif
00217     return QLatin1String("UNKNOWN");        //###
00218 }
00219 
00220 
00221 #endif // PROPARSERUTILS_H
00222 

Generated on Thu Mar 15 12:01:50 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1