1 | |
2 | #include "meinproc_common.h" |
3 | |
4 | #include "xslt.h" |
5 | |
6 | #include <QDir> |
7 | #include <QFileInfo> |
8 | |
9 | #include <cstdlib> |
10 | |
11 | CheckFileResult checkFile( const QString &checkFilename ) |
12 | { |
13 | const QFileInfo checkFile(checkFilename); |
14 | if (!checkFile.exists()) |
15 | { |
16 | return CheckFileDoesNotExist; |
17 | } |
18 | if (!checkFile.isFile()) |
19 | { |
20 | return CheckFileIsNotFile; |
21 | } |
22 | if (!checkFile.isReadable()) |
23 | { |
24 | return CheckFileIsNotReadable; |
25 | } |
26 | return CheckFileSuccess; |
27 | } |
28 | |
29 | CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs) |
30 | { |
31 | const QString pwd_buffer = QDir::currentPath(); |
32 | const QFileInfo file( checkFilename ); |
33 | |
34 | setenv( "XML_CATALOG_FILES" , catalogs.constData(), 1 ); |
35 | if ( QFileInfo( exe ).isExecutable() ) { |
36 | QDir::setCurrent( file.absolutePath() ); |
37 | QString cmd = exe; |
38 | cmd += " --valid --noout " ; |
39 | cmd += file.fileName(); |
40 | cmd += " 2>&1" ; |
41 | FILE *xmllint = popen( QFile::encodeName( cmd ).constData(), "r" ); |
42 | char buf[ 512 ]; |
43 | bool noout = true; |
44 | unsigned int n; |
45 | while ( ( n = fread(buf, 1, sizeof( buf ) - 1, xmllint ) ) ) { |
46 | noout = false; |
47 | buf[ n ] = '\0'; |
48 | fputs( buf, stderr ); |
49 | } |
50 | pclose( xmllint ); |
51 | QDir::setCurrent( pwd_buffer ); |
52 | if ( !noout ) |
53 | return CheckNoOut; |
54 | } else { |
55 | return CheckNoXmllint; |
56 | } |
57 | return CheckSuccess; |
58 | } |
59 | |
60 | void doOutput(QString output, bool usingStdOut, bool usingOutput, const QString &outputOption, bool replaceCharset) |
61 | { |
62 | if (output.indexOf( "<FILENAME " ) == -1 || usingStdOut || usingOutput ) |
63 | { |
64 | QFile file; |
65 | if ( usingStdOut ) { |
66 | file.open( stdout, QIODevice::WriteOnly ); |
67 | } else { |
68 | if ( usingOutput ) |
69 | file.setFileName( outputOption ); |
70 | else |
71 | file.setFileName( "index.html" ); |
72 | file.open(QIODevice::WriteOnly); |
73 | } |
74 | if (replaceCharset) replaceCharsetHeader( output ); |
75 | #ifdef Q_WS_WIN |
76 | QByteArray data = output.toUtf8(); |
77 | #else |
78 | QByteArray data = output.toLocal8Bit(); |
79 | #endif |
80 | file.write(data.data(), data.length()); |
81 | file.close(); |
82 | } else { |
83 | int index = 0; |
84 | while (true) { |
85 | index = output.indexOf("<FILENAME " , index); |
86 | if (index == -1) |
87 | break; |
88 | int filename_index = index + strlen("<FILENAME filename=\"" ); |
89 | |
90 | const QString filename = output.mid(filename_index, |
91 | output.indexOf("\"" , filename_index) - |
92 | filename_index); |
93 | |
94 | QString filedata = splitOut(output, index); |
95 | QFile file(filename); |
96 | file.open(QIODevice::WriteOnly); |
97 | if (replaceCharset) replaceCharsetHeader( filedata ); |
98 | const QByteArray data = fromUnicode( filedata ); |
99 | file.write(data.data(), data.length()); |
100 | file.close(); |
101 | |
102 | index += 8; |
103 | } |
104 | } |
105 | } |
106 | |