~neon/qt/qtactiveqt

« back to all changes in this revision

Viewing changes to tools/dumpcpp/main.cpp

  • Committer: Friedemann Kleint
  • Date: 2015-02-02 13:58:07 UTC
  • Revision ID: git-v1:b2c2c27ffa0cac8f5fa8d76574155c9298694b8b
dumpcpp: Refactor code handling options strings.

Change the type from QByteArray to QString, removing some
conversion code. Move nameSpace option parameter into main
and turn into a QString as well.

Task-number: QTBUG-41130
Change-Id: Ife42f099f725a83e91cc2b73b49cee3fa8c3898b
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
extern bool qax_dispatchEqualsIDispatch;
73
73
extern void qax_deleteMetaObject(QMetaObject *mo);
74
74
 
75
 
QByteArray nameSpace;
76
75
QMap<QByteArray, QByteArray> namespaceForType;
77
76
QVector<QByteArray> strings;
78
77
QHash<QByteArray, int> stringIndex; // Optimization, speeds up generation
95
94
    }
96
95
}
97
96
 
98
 
void writeHeader(QTextStream &out, const QByteArray &nameSpace, const QString &outFileName)
 
97
void writeHeader(QTextStream &out, const QString &nameSpace, const QString &outFileName)
99
98
{
100
99
    out << "#ifndef QAX_DUMPCPP_" << outFileName.toUpper() << "_H" << endl;
101
100
    out << "#define QAX_DUMPCPP_" << outFileName.toUpper() << "_H" << endl;
980
979
    return true;
981
980
}
982
981
 
983
 
bool generateTypeLibrary(const QByteArray &typeLib, const QByteArray &outname, ObjectCategory category)
 
982
bool generateTypeLibrary(QString typeLibFile, QString outname,
 
983
                         const QString &nameSpace, ObjectCategory category)
984
984
{
985
 
    QString typeLibFile(QString::fromLatin1(typeLib.constData()));
986
 
    typeLibFile = typeLibFile.replace(QLatin1Char('/'), QLatin1Char('\\'));
987
 
    QString cppFile(QString::fromLatin1(outname.constData()));
 
985
    typeLibFile.replace(QLatin1Char('/'), QLatin1Char('\\'));
988
986
 
989
987
    ITypeLib *typelib;
990
988
    LoadTypeLibEx(reinterpret_cast<const wchar_t *>(typeLibFile.utf16()), REGKIND_NONE, &typelib);
993
991
        return false;
994
992
    }
995
993
 
996
 
    QString libName;
997
 
    BSTR nameString;
998
 
    typelib->GetDocumentation(-1, &nameString, 0, 0, 0);
999
 
    libName = QString::fromWCharArray(nameString);
1000
 
    SysFreeString(nameString);
1001
 
    if (!nameSpace.isEmpty())
1002
 
        libName = QString::fromLocal8Bit(nameSpace);
 
994
    QString libName = nameSpace;
 
995
    if (libName.isEmpty()) {
 
996
        BSTR nameString = Q_NULLPTR;
 
997
        if (SUCCEEDED(typelib->GetDocumentation(-1, &nameString, 0, 0, 0))) {
 
998
            libName = QString::fromWCharArray(nameString);
 
999
            SysFreeString(nameString);
 
1000
        }
 
1001
    }
1003
1002
 
1004
1003
    QString libVersion(QLatin1String("1.0"));
1005
1004
 
1010
1009
        typelib->ReleaseTLibAttr(tlibattr);
1011
1010
    }
1012
1011
 
1013
 
    if (cppFile.isEmpty())
1014
 
        cppFile = libName.toLower();
 
1012
    if (outname.isEmpty())
 
1013
        outname = libName.toLower();
1015
1014
 
1016
 
    if (cppFile.isEmpty()) {
 
1015
    if (outname.isEmpty()) {
1017
1016
        qWarning("dumpcpp: no output filename provided, and cannot deduce output filename");
1018
1017
        return false;
1019
1018
    }
1026
1025
        return false;
1027
1026
    }
1028
1027
    QTextStream classImplOut(&classImplFile);
1029
 
    QFile implFile(cppFile + QLatin1String(".cpp"));
 
1028
    QFile implFile(outname + QLatin1String(".cpp"));
1030
1029
    QTextStream implOut(&implFile);
1031
1030
    if (!(category & (NoMetaObject|NoImplementation))) {
1032
1031
        if (!implFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
1044
1043
 
1045
1044
        implOut << "#define QAX_DUMPCPP_" << libName.toUpper() << "_NOINLINES" << endl;
1046
1045
 
1047
 
        implOut << "#include \"" << cppFile << ".h\"" << endl;
 
1046
        implOut << "#include \"" << outname << ".h\"" << endl;
1048
1047
        implOut << endl;
1049
1048
        implOut << "using namespace " << libName << ';' << endl;
1050
1049
        implOut << endl;
1051
1050
    }
1052
1051
 
1053
 
    QFile declFile(cppFile + QLatin1String(".h"));
 
1052
    QFile declFile(outname + QLatin1String(".h"));
1054
1053
    QTextStream declOut(&declFile);
1055
1054
    QByteArray classes;
1056
1055
    QTextStream classesOut(&classes, QIODevice::WriteOnly);
1073
1072
        declOut << "****************************************************************************/" << endl;
1074
1073
        declOut << endl;
1075
1074
 
1076
 
        QFileInfo cppFileInfo(cppFile);
1077
 
        writeHeader(declOut, libName.toLatin1(), cppFileInfo.fileName());
 
1075
        QFileInfo cppFileInfo(outname);
 
1076
        writeHeader(declOut, libName, cppFileInfo.fileName());
1078
1077
 
1079
1078
        UINT typeCount = typelib->GetTypeInfoCount();
1080
1079
        if (declFile.isOpen()) {
1492
1491
    } state;
1493
1492
    state = Default;
1494
1493
 
1495
 
    QByteArray outname;
1496
 
    QByteArray typeLib;
 
1494
    QString outname;
 
1495
    QString typeLib;
 
1496
    QString nameSpace;
1497
1497
 
1498
1498
    for (int a = 1; a < argc; ++a) {
1499
1499
        QByteArray arg(argv[a]);
1553
1553
            break;
1554
1554
 
1555
1555
        case Output:
1556
 
            outname = arg;
 
1556
            outname = QFile::decodeName(arg);
1557
1557
            state = Default;
1558
1558
            break;
1559
1559
 
1560
1560
        case NameSpace:
1561
 
            nameSpace = arg;
 
1561
            nameSpace = QString::fromLocal8Bit(arg);
1562
1562
            state = Default;
1563
1563
            break;
1564
1564
 
1565
1565
        case GetTypeLib:
1566
 
            typeLib = arg;
 
1566
            typeLib = QFile::decodeName(arg);
1567
1567
            state = Default;
1568
1568
            category = TypeLibID;
1569
1569
            break;
1574
1574
 
1575
1575
    if (category == TypeLibID) {
1576
1576
        QSettings settings(QLatin1String("HKEY_LOCAL_MACHINE\\Software\\Classes\\TypeLib\\") +
1577
 
                           QString::fromLatin1(typeLib.constData()), QSettings::NativeFormat);
1578
 
        typeLib = QByteArray();
 
1577
                           typeLib, QSettings::NativeFormat);
 
1578
        typeLib.clear();
1579
1579
        QStringList codes = settings.childGroups();
1580
1580
        for (int c = 0; c < codes.count(); ++c) {
1581
 
            typeLib = settings.value(QLatin1Char('/') + codes.at(c) + QLatin1String("/0/win32/.")).toByteArray();
1582
 
            if (QFile::exists(QString::fromLatin1(typeLib))) {
 
1581
            typeLib = settings.value(QLatin1Char('/') + codes.at(c) + QLatin1String("/0/win32/.")).toString();
 
1582
            if (QFile::exists(typeLib))
1583
1583
                break;
1584
 
            }
1585
1584
        }
1586
1585
 
1587
1586
        if (!typeLib.isEmpty())
1588
 
            fprintf(stdout, "\"%s\"\n", typeLib.data());
 
1587
            fprintf(stdout, "\"%s\"\n", qPrintable(typeLib));
1589
1588
        return 0;
1590
1589
    }
1591
1590
 
1599
1598
    }
1600
1599
 
1601
1600
    // not a file - search registry
1602
 
    if (!QFile::exists(QString::fromLatin1(typeLib.constData()))) {
 
1601
    if (!QFile::exists(typeLib)) {
1603
1602
        bool isObject = false;
1604
1603
        QSettings settings(QLatin1String("HKEY_LOCAL_MACHINE\\Software\\Classes"), QSettings::NativeFormat);
1605
1604
 
1606
1605
        // regular string and not a file - must be ProgID
1607
1606
        if (typeLib.at(0) != '{') {
1608
1607
            CLSID clsid;
1609
 
            if (CLSIDFromProgID(reinterpret_cast<const wchar_t *>(QString(QLatin1String(typeLib)).utf16()), &clsid) != S_OK) {
1610
 
                qWarning("dumpcpp: '%s' is not a type library and not a registered ProgID", typeLib.constData());
 
1608
            if (CLSIDFromProgID(reinterpret_cast<const wchar_t *>(typeLib.utf16()), &clsid) != S_OK) {
 
1609
                qWarning("dumpcpp: '%s' is not a type library and not a registered ProgID",
 
1610
                         qPrintable(typeLib));
1611
1611
                return -2;
1612
1612
            }
1613
 
            QUuid uuid(clsid);
1614
 
            typeLib = uuid.toString().toLatin1();
 
1613
            typeLib = QUuid(clsid).toString();
1615
1614
            isObject = true;
1616
1615
        }
1617
1616
 
1618
1617
        // check if CLSID
1619
1618
        if (!isObject) {
1620
 
            QVariant test = settings.value(QLatin1String("/CLSID/") +
1621
 
                                           QString::fromLatin1(typeLib.constData()) + QLatin1String("/."));
 
1619
            QVariant test = settings.value(QLatin1String("/CLSID/") + typeLib + QLatin1String("/."));
1622
1620
            isObject = test.isValid();
1623
1621
        }
1624
1622
 
1625
1623
        // search typelib ID for CLSID
1626
1624
        if (isObject)
1627
 
            typeLib = settings.value(QLatin1String("/CLSID/") +
1628
 
                                     QString::fromLatin1(typeLib.constData()) + QLatin1String("/Typelib/.")).toByteArray();
 
1625
            typeLib = settings.value(QLatin1String("/CLSID/") + typeLib
 
1626
                                     + QLatin1String("/Typelib/.")).toString();
1629
1627
 
1630
1628
        // interpret input as type library ID
1631
 
        QString key = QLatin1String("/TypeLib/") + QLatin1String(typeLib);
 
1629
        QString key = QLatin1String("/TypeLib/") + typeLib;
1632
1630
        settings.beginGroup(key);
1633
1631
        QStringList versions = settings.childGroups();
1634
1632
        QStringList codes;
1641
1639
        settings.endGroup();
1642
1640
 
1643
1641
        for (int c = 0; c < codes.count(); ++c) {
1644
 
            typeLib = settings.value(key + QLatin1Char('/') + codes.at(c) + QLatin1String("/win32/.")).toByteArray();
1645
 
            if (QFile::exists(QString::fromLatin1(typeLib.constData()))) {
 
1642
            typeLib = settings.value(key + QLatin1Char('/') + codes.at(c)
 
1643
                                     + QLatin1String("/win32/.")).toString();
 
1644
            if (QFile::exists(typeLib))
1646
1645
                break;
1647
 
            }
1648
1646
        }
1649
1647
    }
1650
1648
 
1651
 
    if (!QFile::exists(QString::fromLatin1(typeLib.constData()))) {
1652
 
        qWarning("dumpcpp: type library '%s' not found", typeLib.constData());
 
1649
    if (!QFile::exists(typeLib)) {
 
1650
        qWarning("dumpcpp: type library '%s' not found", qPrintable(typeLib));
1653
1651
        return -2;
1654
1652
    }
1655
1653
 
1656
 
    if (!generateTypeLibrary(typeLib, outname, (ObjectCategory)category)) {
1657
 
        qWarning("dumpcpp: error processing type library '%s'", typeLib.constData());
 
1654
    if (!generateTypeLibrary(typeLib, outname, nameSpace, (ObjectCategory)category)) {
 
1655
        qWarning("dumpcpp: error processing type library '%s'", qPrintable(typeLib));
1658
1656
        return -1;
1659
1657
    }
1660
1658