1
/*
2
 * This file is part of the API Extractor project.
3
 *
4
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
5
 *
6
 * Contact: PySide team <contact@pyside.org>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * version 2 as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 * 02110-1301 USA
21
 *
22
 */
23
24
#include "fileout.h"
25
#include "reporthandler.h"
26
27
#include <QtCore/QTextCodec>
28
#include <QtCore/QFileInfo>
29
#include <QtCore/QDir>
30
31
#include <cstdio>
32
33
bool FileOut::dummy = false;
34
bool FileOut::diff = false;
35
36
#ifdef Q_OS_LINUX
37
const char* colorDelete = "\033[31m";
38
const char* colorAdd = "\033[32m";
39
const char* colorInfo = "\033[36m";
40
const char* colorReset = "\033[0m";
41
#else
42
const char* colorDelete = "";
43
const char* colorAdd = "";
44
const char* colorInfo = "";
45
const char* colorReset = "";
46
#endif
47
48
FileOut::FileOut(QString n):
49
        name(n),
50
        stream(&tmp),
51
        isDone(false)
52
{}
53
54
static int* lcsLength(QList<QByteArray> a, QList<QByteArray> b)
55
{
56
    const int height = a.size() + 1;
57
    const int width = b.size() + 1;
58
59
    int *res = new int[width * height];
60
61
    for (int row = 0; row < height; row++)
62
        res[width * row] = 0;
63
64
    for (int col = 0; col < width; col++)
65
        res[col] = 0;
66
67
    for (int row = 1; row < height; row++) {
68
        for (int col = 1; col < width; col++) {
69
            if (a[row-1] == b[col-1])
70
                res[width * row + col] = res[width * (row-1) + col-1] + 1;
71
            else
72
                res[width * row + col] = qMax(res[width * row     + col-1],
73
                                              res[width * (row-1) + col]);
74
        }
75
    }
76
    return res;
77
}
78
79
enum Type {
80
    Add,
81
    Delete,
82
    Unchanged
83
};
84
85
struct Unit
86
{
87
    Unit(Type type, int pos) :
88
            type(type),
89
            start(pos),
90
            end(pos) {}
91
92
    Type type;
93
    int start;
94
    int end;
95
96
    void print(QList<QByteArray> a, QList<QByteArray> b)
97
    {
98
            if (type == Unchanged) {
99
                if ((end - start) > 9) {
100
                    for (int i = start; i <= start + 2; i++)
101
                        std::printf("  %s\n", a[i].data());
102
                    std::printf("%s=\n= %d more lines\n=%s\n", colorInfo, end - start - 6, colorReset);
103
                    for (int i = end - 2; i <= end; i++)
104
                        std::printf("  %s\n", a[i].data());
105
                } else {
106
                    for (int i = start; i <= end; i++)
107
                        std::printf("  %s\n", a[i].data());
108
                }
109
            } else if (type == Add) {
110
                std::printf("%s", colorAdd);
111
                for (int i = start; i <= end; i++)
112
                    std::printf("+ %s\n", b[i].data());
113
                std::printf("%s", colorReset);
114
            } else if (type == Delete) {
115
                std::printf("%s", colorDelete);
116
                for (int i = start; i <= end; i++)
117
                    std::printf("- %s\n", a[i].data());
118
                std::printf("%s", colorReset);
119
            }
120
    }
121
};
122
123
static QList<Unit*> *unitAppend(QList<Unit*> *res, Type type, int pos)
124
{
125
    if (!res) {
126
        res = new QList<Unit*>;
127
        res->append(new Unit(type, pos));
128
        return res;
129
    }
130
131
    Unit *last = res->last();
132
    if (last->type == type)
133
        last->end = pos;
134
    else
135
        res->append(new Unit(type, pos));
136
137
    return res;
138
}
139
140
static QList<Unit*> *diffHelper(int *lcs, QList<QByteArray> a, QList<QByteArray> b, int row, int col)
141
{
142
    if (row > 0 && col > 0 && (a[row-1] == b[col-1])) {
143
        return unitAppend(diffHelper(lcs, a, b, row - 1, col - 1), Unchanged, row - 1);
144
    } else {
145
        int width = b.size() + 1;
146
        if ((col > 0)
147
            && (row == 0 || lcs[width * row + col-1] >= lcs[width *(row-1) + col])) {
148
            return unitAppend(diffHelper(lcs, a, b, row, col - 1), Add, col - 1);
149
        } else if ((row > 0)
150
            && (col == 0 || lcs[width * row + col-1] < lcs[width *(row-1) + col])) {
151
            return unitAppend(diffHelper(lcs, a, b, row - 1, col), Delete, row - 1);
152
        }
153
    }
154
    delete lcs;
155
    return 0;
156
}
157
158
static void diff(QList<QByteArray> a, QList<QByteArray> b)
159
{
160
    QList<Unit*> *res = diffHelper(lcsLength(a, b), a, b, a.size(), b.size());
161
    for (int i = 0; i < res->size(); i++) {
162
        Unit *unit = res->at(i);
163
        unit->print(a, b);
164
        delete(unit);
165
    }
166
    delete(res);
167
}
168
169
170
bool FileOut::done()
171
{
172
    Q_ASSERT(!isDone);
173
    if (name.isEmpty())
174
        return false;
175
176
    isDone = true;
177
    bool fileEqual = false;
178
    QFile fileRead(name);
179
    QFileInfo info(fileRead);
180
    stream.flush();
181
    QByteArray original;
182
    if (info.exists() && (diff || (info.size() == tmp.size()))) {
183
        if (!fileRead.open(QIODevice::ReadOnly)) {
184
            ReportHandler::warning(QString("failed to open file '%1' for reading")
185
                                   .arg(fileRead.fileName()));
186
            return false;
187
        }
188
189
        original = fileRead.readAll();
190
        fileRead.close();
191
        fileEqual = (original == tmp);
192
    }
193
194
    if (!fileEqual) {
195
        if (!FileOut::dummy) {
196
            QDir dir(info.absolutePath());
197
            if (!dir.mkpath(dir.absolutePath())) {
198
                ReportHandler::warning(QString("unable to create directory '%1'")
199
                                       .arg(dir.absolutePath()));
200
                return false;
201
            }
202
203
            QFile fileWrite(name);
204
            if (!fileWrite.open(QIODevice::WriteOnly)) {
205
                ReportHandler::warning(QString("failed to open file '%1' for writing")
206
                                       .arg(fileWrite.fileName()));
207
                return false;
208
            }
209
            QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
210
            stream.setDevice(&fileWrite);
211
            stream << tmp;
212
        }
213
        if (diff) {
214
            std::printf("%sFile: %s%s\n", colorInfo, qPrintable(name), colorReset);
215
            ::diff(original.split('\n'), tmp.split('\n'));
216
            std::printf("\n");
217
        }
218
        return true;
219
    }
220
    return false;
221
}