qpatch.cpp 4.25 KB
Newer Older
1

Roberto Raggi's avatar
Roberto Raggi committed
2
3
4
5
6
//#include <QCoreApplication>
#include <QStringList>
#include <QFile>
#include <QVector>
#include <QTextStream>
7
8
9
10
#include <iostream>

int main(int argc, char *argv[])
{
Roberto Raggi's avatar
Roberto Raggi committed
11
    if (argc != 4) {
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
12
        std::cerr << "Usage: qpatch file.list oldQtDir newQtDir" << std::endl;
13
14
15
        return EXIT_FAILURE;
    }

Roberto Raggi's avatar
Roberto Raggi committed
16
17
18
    const QByteArray files = argv[1];
    const QByteArray qtDirPath = argv[2];
    const QByteArray newQtPath = argv[3];
19
20
21
22
23
24
25

    if (qtDirPath.size() < newQtPath.size()) {
        std::cerr << "qpatch: error: newQtDir needs to be less than " << qtDirPath.size() << " characters."
                << std::endl;
        return EXIT_FAILURE;
    }

Roberto Raggi's avatar
Roberto Raggi committed
26
    QFile fn(QFile::decodeName(files));
27
28
29
30
31
    if (! fn.open(QFile::ReadOnly)) {
        std::cerr << "qpatch: error: file not found" << std::endl;
        return EXIT_FAILURE;
    }

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
32
33
34
35
    QStringList filesToPatch, textFilesToPatch;
    bool readingTextFilesToPatch = false;

    // read the input file
36
    QTextStream in(&fn);
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
37

38
    forever {
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
39
        const QString line = in.readLine();
40
41
42
43

        if (line.isNull())
            break;

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
44
45
46
47
48
        else if (line.isEmpty())
            continue;

        else if (line.startsWith(QLatin1String("%%")))
            readingTextFilesToPatch = true;
49

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
50
51
52
53
54
55
        else if (readingTextFilesToPatch)
            textFilesToPatch.append(line);

        else
            filesToPatch.append(line);
    }
56
57

    foreach (QString fileName, filesToPatch) {
Roberto Raggi's avatar
Roberto Raggi committed
58
        QString prefix = QFile::decodeName(newQtPath);
59
60
61
62
63
64
65
66

        if (! prefix.endsWith(QLatin1Char('/')))
            prefix += QLatin1Char('/');

        fileName.prepend(prefix);

        QFile file(fileName);
        if (! file.open(QFile::ReadOnly)) {
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
67
            std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
68
69
70
71
72
            continue;
        }

        const QByteArray source = file.readAll();
        file.close();
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
73

74
75
        int index = 0;

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
76
77
78
79
        if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
            std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
            continue;
        }
80
81
82
83
84
85
86

        forever {
            int start = source.indexOf(qtDirPath, index);
            if (start == -1)
                break;

            int endOfString = start;
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
87
88
89
90
            for (; endOfString < source.size(); ++endOfString) {
                if (! source.at(endOfString))
                    break;
            }
91
92
93

            ++endOfString; // include the '\0'

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
94
95
            if (index != start)
                file.write(source.constData() + index, start - index);
96
97
98
99

            int length = endOfString - start;
            QVector<char> s;

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
100
            for (const char *x = newQtPath.constData(); x != newQtPath.constEnd(); ++x)
101
102
103
104
                s.append(*x);

            const int qtDirPathLength = qtDirPath.size();

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
105
106
            for (const char *x = source.constData() + start + qtDirPathLength;
                    x != source.constData() + endOfString; ++x)
107
108
109
110
111
112
113
                s.append(*x);

            const int oldSize = s.size();

            for (int i = oldSize; i < length; ++i)
                s.append('\0');

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
114
115
116
117
118
119
#if 0
            std::cout << "replace string: " << source.mid(start, endOfString - start).constData()
                    << " with: " << s.constData() << std::endl;
#endif

            file.write(s.constData(), s.size());
120
121
122
123

            index = endOfString;
        }

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
124
125
126
        if (index != source.size())
            file.write(source.constData() + index, source.size() - index);
    }
127

Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
128
    foreach (QString fileName, textFilesToPatch) {
Roberto Raggi's avatar
Roberto Raggi committed
129
        QString prefix = QFile::decodeName(newQtPath);
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
130
131
132
133
134
135
136
137
138
139

        if (! prefix.endsWith(QLatin1Char('/')))
            prefix += QLatin1Char('/');

        fileName.prepend(prefix);

        QFile file(fileName);
        if (! file.open(QFile::ReadOnly)) {
            std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
            continue;
140
        }
Roberto Raggi's avatar
bugfix  
Roberto Raggi committed
141
142
143
144
145
146
147
148
149
150
151

        QByteArray source = file.readAll();
        file.close();

        if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
            std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
            continue;
        }

        source.replace(qtDirPath, newQtPath);
        file.write(source);
152
153
154
155
    }

    return 0;
}