Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tools/linguist/lupdate/main.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the Qt Linguist of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    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.
     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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
    4040****************************************************************************/
    4141
    42 #include "translator.h"
    43 #include "translatortools.h"
    44 #include "profileevaluator.h"
     42#include "lupdate.h"
     43
     44#include <translator.h>
     45#include <profileevaluator.h>
     46#include <proreader.h>
    4547
    4648#include <QtCore/QCoreApplication>
     
    5355#include <QtCore/QTextCodec>
    5456
     57#include <iostream>
     58
    5559static QString m_defaultExtensions;
    5660
    57 static void printOut(const QString & out)
     61static void printErr(const QString & out)
    5862{
    5963    qWarning("%s", qPrintable(out));
    6064}
    6165
     66static void printOut(const QString & out)
     67{
     68    std::cerr << qPrintable(out);
     69}
     70
    6271static void recursiveFileInfoList(const QDir &dir,
    63     const QStringList &nameFilters, QDir::Filters filter, bool recursive,
     72    const QSet<QString> &nameFilters, QDir::Filters filter,
    6473    QFileInfoList *fileinfolist)
    6574{
    66     if (recursive)
    67         filter |= QDir::AllDirs;
    68     QFileInfoList entries = dir.entryInfoList(nameFilters, filter);
    69 
    70     QFileInfoList::iterator it;
    71     for (it = entries.begin(); it != entries.end(); ++it) {
    72         QString fname = it->fileName();
    73         if (fname != QLatin1String(".") && fname != QLatin1String("..")) {
    74             if (it->isDir())
    75                 recursiveFileInfoList(QDir(it->absoluteFilePath()), nameFilters, filter, recursive, fileinfolist);
    76             else
    77                 fileinfolist->append(*it);
    78         }
    79     }
     75    foreach (const QFileInfo &fi, dir.entryInfoList(filter))
     76        if (fi.isDir())
     77            recursiveFileInfoList(QDir(fi.absoluteFilePath()), nameFilters, filter, fileinfolist);
     78        else if (nameFilters.contains(fi.suffix()))
     79            fileinfolist->append(fi);
    8080}
    8181
     
    8484    printOut(QObject::tr(
    8585        "Usage:\n"
    86         "    lupdate [options] [project-file]\n"
     86        "    lupdate [options] [project-file]...\n"
    8787        "    lupdate [options] [source-file|path]... -ts ts-files\n\n"
    88         "lupdate is part of Qt's Linguist tool chain. It can be used as a\n"
    89         "stand-alone tool to create XML based translations files in the .ts\n"
    90         "format from translatable messages in C++ and Java source code.\n\n"
    91         "lupdate can also merge such messages into existing .ts files.\n\n"
     88        "lupdate is part of Qt's Linguist tool chain. It extracts translatable\n"
     89        "messages from Qt UI files, C++, Java and JavaScript/QtScript source code.\n"
     90        "Extracted messages are stored in textual translation source files (typically\n"
     91        "Qt TS XML). New and modified messages can be merged into existing TS files.\n\n"
    9292        "Options:\n"
    9393        "    -help  Display this information and exit.\n"
     
    103103        "           Do not explain what is being done.\n"
    104104        "    -no-sort\n"
    105         "           Do not sort contexts in .ts files.\n"
     105        "           Do not sort contexts in TS files.\n"
    106106        "    -no-recursive\n"
    107107        "           Do not recursively scan the following directories.\n"
    108108        "    -recursive\n"
    109         "           Recursively scan the following directories.\n"
     109        "           Recursively scan the following directories (default).\n"
     110        "    -I <includepath> or -I<includepath>\n"
     111        "           Additional location to look for include files.\n"
     112        "           May be specified multiple times.\n"
    110113        "    -locations {absolute|relative|none}\n"
    111         "           Specify/override how source code references are saved in ts files.\n"
     114        "           Specify/override how source code references are saved in TS files.\n"
    112115        "           Default is absolute.\n"
    113116        "    -no-ui-lines\n"
    114         "           Do not record line numbers in references to .ui files.\n"
     117        "           Do not record line numbers in references to UI files.\n"
    115118        "    -disable-heuristic {sametext|similartext|number}\n"
    116119        "           Disable the named merge heuristic. Can be specified multiple times.\n"
     
    119122        "           file syntax but different file suffix\n"
    120123        "    -source-language <language>[_<region>]\n"
    121         "           Specify/override the language of the source strings. Defaults to\n"
    122         "           POSIX if not specified and the file does not name it yet.\n"
     124        "           Specify the language of the source strings for new files.\n"
     125        "           Defaults to POSIX if not specified.\n"
    123126        "    -target-language <language>[_<region>]\n"
    124         "           Specify/override the language of the translation.\n"
    125         "           The target language is guessed from the file name if this option\n"
    126         "           is not specified and the file contents name no language yet.\n"
     127        "           Specify the language of the translations for new files.\n"
     128        "           Guessed from the file name if not specified.\n"
    127129        "    -version\n"
    128130        "           Display the version of lupdate and exit.\n"
     
    143145        if (QFile(fileName).exists()) {
    144146            if (!tor.load(fileName, cd, QLatin1String("auto"))) {
    145                 printOut(cd.error());
     147                printErr(cd.error());
    146148                *fail = true;
    147149                continue;
     
    166168            if (!targetLanguage.isEmpty())
    167169                tor.setLanguageCode(targetLanguage);
     170            else
     171                tor.setLanguageCode(Translator::guessLanguageCodeFromFileName(fileName));
    168172            if (!sourceLanguage.isEmpty())
    169173                tor.setSourceLanguageCode(sourceLanguage);
     
    179183            printOut(QObject::tr("Updating '%1'...\n").arg(fn));
    180184
     185        UpdateOptions theseOptions = options;
    181186        if (tor.locationsType() == Translator::NoLocations) // Could be set from file
    182             options |= NoLocations;
    183         Translator out = merge(tor, fetchedTor, options, err);
     187            theseOptions |= NoLocations;
     188        Translator out = merge(tor, fetchedTor, theseOptions, err);
    184189        if (!codecForTr.isEmpty())
    185190            out.setCodecName(codecForTr);
     
    198203        out.stripEmptyContexts();
    199204
     205        out.normalizeTranslations(cd);
     206        if (!cd.errors().isEmpty()) {
     207            printErr(cd.error());
     208            cd.clearErrors();
     209        }
    200210        if (!out.save(fileName, cd, QLatin1String("auto"))) {
    201             printOut(cd.error());
     211            printErr(cd.error());
    202212            *fail = true;
    203213        }
     
    217227    QStringList tsFileNames;
    218228    QStringList proFiles;
     229    QMultiHash<QString, QString> allCSources;
     230    QSet<QString> projectRoots;
    219231    QStringList sourceFiles;
     232    QStringList includePath;
    220233    QString targetLanguage;
    221234    QString sourceLanguage;
     
    230243
    231244    QString extensions = m_defaultExtensions;
    232     QStringList extensionsNameFilters;
     245    QSet<QString> extensionsNameFilters;
    233246
    234247    for (int  i = 1; i < argc; ++i) {
     
    343356            proFiles += args[i];
    344357            numFiles++;
     358            continue;
     359        } else if (arg.startsWith(QLatin1String("-I"))) {
     360            if (arg.length() == 2) {
     361                ++i;
     362                if (i == argc) {
     363                    qWarning("The -I option should be followed by a path.");
     364                    return 1;
     365                }
     366                includePath += args[i];
     367            } else {
     368                includePath += args[i].mid(2);
     369            }
    345370            continue;
    346371        } else if (arg.startsWith(QLatin1String("-")) && arg != QLatin1String("-")) {
     
    388413                    printOut(QObject::tr("Scanning directory '%1'...").arg(arg));
    389414                QDir dir = QDir(fi.filePath());
     415                projectRoots.insert(dir.absolutePath() + QLatin1Char('/'));
    390416                if (extensionsNameFilters.isEmpty()) {
    391                     extensions = extensions.trimmed();
    392                     // Remove the potential dot in front of each extension
    393                     if (extensions.startsWith(QLatin1Char('.')))
    394                         extensions.remove(0,1);
    395                     extensions.replace(QLatin1String(",."), QLatin1String(","));
    396 
    397                     extensions.insert(0, QLatin1String("*."));
    398                     extensions.replace(QLatin1Char(','), QLatin1String(",*."));
    399                     extensionsNameFilters = extensions.split(QLatin1Char(','));
     417                    foreach (QString ext, extensions.split(QLatin1Char(','))) {
     418                        ext = ext.trimmed();
     419                        if (ext.startsWith(QLatin1Char('.')))
     420                            ext.remove(0, 1);
     421                        extensionsNameFilters.insert(ext);
     422                    }
    400423                }
    401424                QDir::Filters filters = QDir::Files | QDir::NoSymLinks;
     425                if (recursiveScan)
     426                    filters |= QDir::AllDirs | QDir::NoDotAndDotDot;
    402427                QFileInfoList fileinfolist;
    403                 recursiveFileInfoList(dir, extensionsNameFilters, filters,
    404                     recursiveScan, &fileinfolist);
    405                 QFileInfoList::iterator ii;
    406                 QString fn;
    407                 for (ii = fileinfolist.begin(); ii != fileinfolist.end(); ++ii) {
    408                     // Make sure the path separator is stored with '/' in the ts file
    409                     sourceFiles << ii->canonicalFilePath().replace(QLatin1Char('\\'), QLatin1Char('/'));
     428                recursiveFileInfoList(dir, extensionsNameFilters, filters, &fileinfolist);
     429                int scanRootLen = dir.absolutePath().length();
     430                foreach (const QFileInfo &fi, fileinfolist) {
     431                    QString fn = QDir::cleanPath(fi.absoluteFilePath());
     432                    sourceFiles << fn;
     433
     434                    if (!fn.endsWith(QLatin1String(".java"))
     435                        && !fn.endsWith(QLatin1String(".ui"))
     436                        && !fn.endsWith(QLatin1String(".js"))
     437                        && !fn.endsWith(QLatin1String(".qs"))) {
     438                        int offset = 0;
     439                        int depth = 0;
     440                        do {
     441                            offset = fn.lastIndexOf(QLatin1Char('/'), offset - 1);
     442                            QString ffn = fn.mid(offset + 1);
     443                            allCSources.insert(ffn, fn);
     444                        } while (++depth < 3 && offset > scanRootLen);
     445                    }
    410446                }
    411447            } else {
    412                 sourceFiles << fi.canonicalFilePath().replace(QLatin1Char('\\'), QLatin1Char('/'));
     448                sourceFiles << QDir::cleanPath(fi.absoluteFilePath());;
    413449            }
    414450        }
    415451    } // for args
    416452
     453    foreach (const QString &proFile, proFiles)
     454        projectRoots.insert(QDir::cleanPath(QFileInfo(proFile).absolutePath()) + QLatin1Char('/'));
    417455
    418456    bool firstPass = true;
     
    422460        cd.m_defaultContext = defaultContext;
    423461        cd.m_noUiLines = options & NoUiLines;
     462        cd.m_projectRoots = projectRoots;
     463        cd.m_includePath = includePath;
     464        cd.m_allCSources = allCSources;
    424465
    425466        QStringList tsFiles = tsFileNames;
     
    451492            }
    452493
    453             evaluateProFile(visitor, &variables);
     494            cd.m_includePath += visitor.values(QLatin1String("INCLUDEPATH"));
     495
     496            evaluateProFile(visitor, &variables, pfi.absolutePath());
    454497
    455498            sourceFiles = variables.value("SOURCES");
     
    459502                codecForTr = tmp.first().toLatin1();
    460503                fetchedTor.setCodecName(codecForTr);
     504                cd.m_outputCodec = codecForTr;
    461505            }
    462506            tmp = variables.value("CODECFORSRC");
     
    473517        }
    474518
     519        QStringList sourceFilesCpp;
    475520        for (QStringList::iterator it = sourceFiles.begin(); it != sourceFiles.end(); ++it) {
    476             if (it->endsWith(QLatin1String(".java"), Qt::CaseInsensitive)) {
    477                 cd.m_sourceFileName = *it;
    478                 fetchedTor.load(*it, cd, QLatin1String("java"));
    479                 //fetchtr_java(*it, &fetchedTor, defaultContext, true, codecForSource);
    480             }
    481             else if (it->endsWith(QLatin1String(".ui"), Qt::CaseInsensitive)) {
    482                 fetchedTor.load(*it, cd, QLatin1String("ui"));
    483                 //fetchedTor.load(*it + QLatin1String(".h"), cd, QLatin1String("cpp"));
    484                 //fetchtr_ui(*it, &fetchedTor, defaultContext, true);
    485                 //fetchtr_cpp(*it + QLatin1String(".h"), &fetchedTor,
    486                 //             defaultContext, false, codecForSource);
    487             }
     521            if (it->endsWith(QLatin1String(".java"), Qt::CaseInsensitive))
     522                loadJava(fetchedTor, *it, cd);
     523            else if (it->endsWith(QLatin1String(".ui"), Qt::CaseInsensitive)
     524                     || it->endsWith(QLatin1String(".jui"), Qt::CaseInsensitive))
     525                loadUI(fetchedTor, *it, cd);
    488526            else if (it->endsWith(QLatin1String(".js"), Qt::CaseInsensitive)
    489                 || it->endsWith(QLatin1String(".qs"), Qt::CaseInsensitive)) {
    490                 fetchedTor.load(*it, cd, QLatin1String("js"));
    491             } else {
    492                 fetchedTor.load(*it, cd, QLatin1String("cpp"));
    493                 //fetchtr_cpp(*it, &fetchedTor, defaultContext, true, codecForSource);
    494             }
    495         }
     527                     || it->endsWith(QLatin1String(".qs"), Qt::CaseInsensitive))
     528                loadQScript(fetchedTor, *it, cd);
     529            else
     530                sourceFilesCpp << *it;
     531        }
     532        loadCPP(fetchedTor, sourceFilesCpp, cd);
    496533        if (!cd.error().isEmpty())
    497534            printOut(cd.error());
Note: See TracChangeset for help on using the changeset viewer.