~ubuntu-branches/ubuntu/precise/kde4libs/precise-proposed

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "meinproc_common.h"

#include "xslt.h"

#include <QDir>
#include <QFileInfo>

#include <cstdlib>

CheckFileResult checkFile( const QString &checkFilename )
{
    const QFileInfo checkFile(checkFilename);
    if (!checkFile.exists())
    {
        return CheckFileDoesNotExist;
    }
    if (!checkFile.isFile())
    {
        return CheckFileIsNotFile;
    }
    if (!checkFile.isReadable())
    {
        return CheckFileIsNotReadable;
    }
    return CheckFileSuccess;
}

CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs)
{
    const QString pwd_buffer = QDir::currentPath();
    const QFileInfo file( checkFilename );

    setenv( "XML_CATALOG_FILES", catalogs.constData(), 1 );
    if ( QFileInfo( exe ).isExecutable() ) {
        QDir::setCurrent( file.absolutePath() );
        QString cmd = exe;
        cmd += " --valid --noout ";
        cmd += file.fileName();
        cmd += " 2>&1";
        FILE *xmllint = popen( QFile::encodeName( cmd ).constData(), "r" );
        char buf[ 512 ];
        bool noout = true;
        unsigned int n;
        while ( ( n = fread(buf, 1, sizeof( buf ) - 1, xmllint ) ) ) {
            noout = false;
            buf[ n ] = '\0';
            fputs( buf, stderr );
        }
        pclose( xmllint );
        QDir::setCurrent( pwd_buffer );
        if ( !noout )
            return CheckNoOut;
    } else {
        return CheckNoXmllint;
    }
    return CheckSuccess;
}

void doOutput(QString output, bool usingStdOut, bool usingOutput, const QString &outputOption, bool replaceCharset)
{
    if (output.indexOf( "<FILENAME " ) == -1 || usingStdOut || usingOutput )
    {
        QFile file;
        if ( usingStdOut ) {
            file.open( stdout, QIODevice::WriteOnly );
        } else {
            if ( usingOutput )
                file.setFileName( outputOption );
            else
                file.setFileName( "index.html" );
            file.open(QIODevice::WriteOnly);
        }
        if (replaceCharset) replaceCharsetHeader( output );
#ifdef Q_WS_WIN
        QByteArray data = output.toUtf8();
#else
        QByteArray data = output.toLocal8Bit();
#endif
        file.write(data.data(), data.length());
        file.close();
    } else {
        int index = 0;
        while (true) {
            index = output.indexOf("<FILENAME ", index);
            if (index == -1)
                break;
            int filename_index = index + strlen("<FILENAME filename=\"");

            const QString filename = output.mid(filename_index,
                                            output.indexOf("\"", filename_index) -
                                            filename_index);

            QString filedata = splitOut(output, index);
            QFile file(filename);
            file.open(QIODevice::WriteOnly);
            if (replaceCharset) replaceCharsetHeader( filedata );
            const QByteArray data = fromUnicode( filedata );
            file.write(data.data(), data.length());
            file.close();

            index += 8;
        }
    }
}