File: icoutils_wrestool.cpp

package info (click to toggle)
kio-extras 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,720 kB
  • ctags: 3,303
  • sloc: cpp: 26,172; ansic: 5,034; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (91 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
/*
    icoutils_wrestool.cpp - Extract Microsoft Window icons and images using icoutils package

    Copyright (c) 2009-2010 by Pali Rohár <pali.rohar@gmail.com>

    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU General Public                   *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include "icoutils.h"

#include <QList>
#include <QPair>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QProcess>
#include <QSet>

#define abs(n) ( ( n < 0 ) ? -n : n )
typedef QPair < QString, int > IconInExe;

bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, const QString &outputFileName, const qint32 iconNumber)
{

    QProcess wrestool;

    wrestool.start("wrestool", QStringList() << "-l" << inputFileName);
    wrestool.waitForFinished();

    if ( wrestool.exitCode() != 0 )
        return false;

    const QStringList output = QString(wrestool.readAll()).split('\n');

    QRegExp regExp("--type=(.*) --name=(.*) --language=(.*) \\[(.*)\\]");

    // If we specify number of icon, use only group icons (Windows use only group icons)
    if ( iconNumber > 0 )
        regExp.setPattern("--type=(14) --name=(.*) --language=(.*) \\[(.*)\\]");

    QList <IconInExe> icons;

    // First try use group icons (type 14, default first for windows executables), then icons (type 3), then group cursors (type 12) and finaly cursors (type 1)
    // Note: Last icon (type 3) could be in higher resolution

    // Group Icons
    foreach ( const QString &line, output )
        if ( regExp.indexIn(line) != -1 && regExp.cap(1).toInt() == 14 )
            icons << qMakePair(regExp.cap(2), 14);

    // Icons
    foreach ( const QString &line, output )
        if ( regExp.indexIn(line) != -1 && regExp.cap(1).toInt() == 3 )
            icons << qMakePair(regExp.cap(2), 3);

    if ( icons.isEmpty() )
        return false;

    if ( iconNumber > 0 && icons.size() >= iconNumber )
        icons = QList <IconInExe> () << icons.at(iconNumber+1);

    foreach ( const IconInExe &icon, icons )
    {

        QString name = icon.first;
        int type = icon.second;

        if ( name.at(0) == '\'' )
            name = name.mid(1, name.size()-2);

        QFile(outputFileName).resize(0);

        wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName << "-o" << outputFileName);
        wrestool.waitForFinished();

        if ( wrestool.exitCode() == 0 && QFile(outputFileName).size() != 0 )
            return true;

    }

    return false;

}