KDocTools

meinproc_common.cpp
1
2#include "meinproc_common.h"
3
4#include "docbookxslt.h"
5#include "docbookxslt_p.h"
6
7#include <QDir>
8#include <QFileInfo>
9
10#include <cstdlib>
11
12#ifdef Q_CC_MSVC
13static inline FILE *popen(const char *command, const char *mode)
14{
15 return _popen(command, mode);
16}
17
18static inline int pclose(FILE *file)
19{
20 return _pclose(file);
21}
22#endif
23
24CheckFileResult checkFile(const QString &checkFilename)
25{
26 const QFileInfo checkFile(checkFilename);
27 if (!checkFile.exists()) {
28 return CheckFileDoesNotExist;
29 }
30 if (!checkFile.isFile()) {
31 return CheckFileIsNotFile;
32 }
33 if (!checkFile.isReadable()) {
34 return CheckFileIsNotReadable;
35 }
36 return CheckFileSuccess;
37}
38
39CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs)
40{
41 const QString pwd_buffer = QDir::currentPath();
42 const QFileInfo file(checkFilename);
43
44 qputenv("XML_CATALOG_FILES", catalogs);
45 if (QFileInfo(exe).isExecutable()) {
46 QDir::setCurrent(file.absolutePath());
47 QString cmd = exe;
48 cmd += QStringLiteral(" --valid --noout ");
49 cmd += file.fileName();
50 cmd += QStringLiteral(" 2>&1");
51 FILE *xmllint = popen(QFile::encodeName(cmd).constData(), "r");
52 char buf[512];
53 bool noout = true;
54 size_t n;
55 while ((n = fread(buf, 1, sizeof(buf) - 1, xmllint))) {
56 noout = false;
57 buf[n] = '\0';
58 fputs(buf, stderr);
59 }
60 pclose(xmllint);
61 QDir::setCurrent(pwd_buffer);
62 if (!noout) {
63 return CheckNoOut;
64 }
65 } else {
66 return CheckNoXmllint;
67 }
68 return CheckSuccess;
69}
70
71void doOutput(QString output, bool usingStdOut, bool usingOutput, const QString &outputOption, bool replaceCharset)
72{
73 if (output.indexOf(QStringLiteral("<FILENAME ")) == -1 || usingStdOut || usingOutput) {
74 QFile file;
75 if (usingStdOut) {
76 file.open(stdout, QIODevice::WriteOnly);
77 } else {
78 if (usingOutput) {
79 file.setFileName(outputOption);
80 } else {
81 file.setFileName(QStringLiteral("index.html"));
82 }
84 }
85 if (replaceCharset) {
86 replaceCharsetHeader(output);
87 }
88#ifdef Q_OS_WIN
89 QByteArray data = output.toUtf8();
90#else
91 QByteArray data = output.toLocal8Bit();
92#endif
93 file.write(data.data(), data.length());
94 file.close();
95 } else {
96 int index = 0;
97 while (true) {
98 index = output.indexOf(QStringLiteral("<FILENAME "), index);
99 if (index == -1) {
100 break;
101 }
102 int filename_index = index + qstrlen("<FILENAME filename=\"");
103
104 const QString filename = output.mid(filename_index, output.indexOf(QLatin1Char('\"'), filename_index) - filename_index);
105
106 QString filedata = splitOut(output, index);
107 QFile file(filename);
109 if (replaceCharset) {
110 replaceCharsetHeader(filedata);
111 }
112 const QByteArray data = fromUnicode(filedata);
113 file.write(data.data(), data.length());
114 file.close();
115
116 index += 8;
117 }
118 }
119}
char * data()
qsizetype length() const const
QString currentPath()
bool setCurrent(const QString &path)
QByteArray encodeName(const QString &fileName)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
void setFileName(const QString &name)
virtual void close() override
qint64 write(const QByteArray &data)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString mid(qsizetype position, qsizetype n) const const
QByteArray toLocal8Bit() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.