Changeset View
Changeset View
Standalone View
Standalone View
thumbnail/icoutils_wrestool.cpp
Show All 11 Lines | 1 | /* | |||
---|---|---|---|---|---|
12 | * * | 12 | * * | ||
13 | ************************************************************************* | 13 | ************************************************************************* | ||
14 | */ | 14 | */ | ||
15 | 15 | | |||
16 | #include "icoutils.h" | 16 | #include "icoutils.h" | ||
17 | 17 | | |||
18 | #include <QList> | 18 | #include <QList> | ||
19 | #include <QPair> | 19 | #include <QPair> | ||
20 | #include <QRegExp> | 20 | #include <QRegularExpression> | ||
21 | #include <QString> | 21 | #include <QString> | ||
22 | #include <QStringList> | 22 | #include <QStringList> | ||
23 | #include <QFile> | 23 | #include <QFile> | ||
24 | #include <QProcess> | 24 | #include <QProcess> | ||
25 | #include <QSet> | 25 | #include <QSet> | ||
26 | 26 | | |||
27 | #define abs(n) ( ( n < 0 ) ? -n : n ) | 27 | #define abs(n) ( ( n < 0 ) ? -n : n ) | ||
28 | typedef QPair < QString, int > IconInExe; | 28 | typedef QPair < QString, int > IconInExe; | ||
29 | 29 | | |||
30 | bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, QIODevice *outputDevice, const qint32 iconNumber) | 30 | bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, QIODevice *outputDevice, const qint32 iconNumber) | ||
31 | { | 31 | { | ||
32 | 32 | | |||
33 | QProcess wrestool; | 33 | QProcess wrestool; | ||
34 | 34 | | |||
35 | wrestool.start("wrestool", QStringList() << "-l" << inputFileName); | 35 | wrestool.start("wrestool", QStringList() << "-l" << inputFileName); | ||
36 | wrestool.waitForFinished(); | 36 | wrestool.waitForFinished(); | ||
37 | 37 | | |||
38 | if ( wrestool.exitCode() != 0 ) | 38 | if ( wrestool.exitCode() != 0 ) | ||
39 | return false; | 39 | return false; | ||
40 | 40 | | |||
41 | const QStringList output = QString(wrestool.readAll()).split('\n'); | 41 | QList<IconInExe> icons; | ||
42 | 42 | | |||
43 | QRegExp regExp("--type=(.*) --name=(.*) --language=(.*) \\[(.*)\\]"); | 43 | const QString output = QString::fromUtf8(wrestool.readAll()); | ||
44 | 44 | | |||
45 | // If we specify number of icon, use only group icons (Windows use only group icons) | 45 | // 16 bit binaries don't have "--language" | ||
46 | if ( iconNumber > 0 ) | 46 | const QRegularExpression regExp(QStringLiteral("--type=(\\d+) (?:--name=(.*) --language=(.*)|--name=(.*)) \\[.*\\]")); | ||
47 | regExp.setPattern("--type=(14) --name=(.*) --language=(.*) \\[(.*)\\]"); | | |||
48 | 47 | | |||
49 | QList <IconInExe> icons; | 48 | auto matches = regExp.globalMatch(output); | ||
50 | 49 | | |||
51 | // First try use group icons (type 14, default first for windows executables), then icons (type 3), then group cursors (type 12) and finally cursors (type 1) | 50 | while (matches.hasNext()) { | ||
52 | // Note: Last icon (type 3) could be in higher resolution | 51 | const auto match = matches.next(); | ||
53 | 52 | | |||
54 | // Group Icons | 53 | const int type = match.capturedRef(1).toInt(); | ||
55 | for (const QString &line : output) { | 54 | if (type != 14) { | ||
56 | if ( regExp.indexIn(line) != -1 && regExp.cap(1).toInt() == 14 ) | 55 | continue; | ||
57 | icons << qMakePair(regExp.cap(2), 14); | | |||
58 | } | 56 | } | ||
59 | 57 | | |||
60 | // Icons | 58 | QString name = match.captured(2); | ||
61 | for (const QString &line : output) { | 59 | if (name.isEmpty()) { | ||
62 | if ( regExp.indexIn(line) != -1 && regExp.cap(1).toInt() == 3 ) | 60 | name = match.captured(4); | ||
63 | icons << qMakePair(regExp.cap(2), 3); | 61 | } | ||
62 | | ||||
63 | icons << qMakePair(name, type); | ||||
64 | } | 64 | } | ||
65 | 65 | | |||
66 | if ( icons.isEmpty() ) | 66 | if ( icons.isEmpty() ) | ||
67 | return false; | 67 | return false; | ||
68 | 68 | | |||
69 | if ( iconNumber > 0 && icons.size() >= iconNumber ) | 69 | // iconNumber 0 is ambiguous... | ||
70 | icons = QList <IconInExe> () << icons.at(iconNumber+1); | 70 | if (iconNumber > 0 && iconNumber < icons.count()) { | ||
71 | icons = {icons.at(iconNumber)}; | ||||
72 | } | ||||
71 | 73 | | |||
72 | for (const IconInExe &icon : qAsConst(icons)) { | 74 | for (const IconInExe &icon : qAsConst(icons)) { | ||
73 | 75 | | |||
74 | QString name = icon.first; | 76 | QString name = icon.first; | ||
75 | int type = icon.second; | 77 | int type = icon.second; | ||
76 | 78 | | |||
77 | if ( name.at(0) == '\'' ) | 79 | if ( name.at(0) == '\'' ) | ||
78 | name = name.mid(1, name.size()-2); | 80 | name = name.mid(1, name.size()-2); | ||
apol: startsWith() | |||||
79 | 81 | | |||
80 | wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName); | 82 | wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName); | ||
81 | wrestool.waitForFinished(); | 83 | wrestool.waitForFinished(); | ||
82 | 84 | | |||
83 | if (wrestool.exitCode() != 0) { | 85 | if (wrestool.exitCode() != 0) { | ||
84 | return false; | 86 | return false; | ||
85 | } | 87 | } | ||
86 | 88 | | |||
Show All 12 Lines |
startsWith()