File indexing completed on 2025-02-16 13:03:31
0001 0002 #include "meinproc_common.h" 0003 0004 #include "docbookxslt.h" 0005 #include "docbookxslt_p.h" 0006 0007 #include <QDir> 0008 #include <QFileInfo> 0009 0010 #include <cstdlib> 0011 0012 #ifdef Q_CC_MSVC 0013 static inline FILE *popen(const char *command, const char *mode) 0014 { 0015 return _popen(command, mode); 0016 } 0017 0018 static inline int pclose(FILE *file) 0019 { 0020 return _pclose(file); 0021 } 0022 #endif 0023 0024 CheckFileResult checkFile(const QString &checkFilename) 0025 { 0026 const QFileInfo checkFile(checkFilename); 0027 if (!checkFile.exists()) { 0028 return CheckFileDoesNotExist; 0029 } 0030 if (!checkFile.isFile()) { 0031 return CheckFileIsNotFile; 0032 } 0033 if (!checkFile.isReadable()) { 0034 return CheckFileIsNotReadable; 0035 } 0036 return CheckFileSuccess; 0037 } 0038 0039 CheckResult check(const QString &checkFilename, const QString &exe, const QByteArray &catalogs) 0040 { 0041 const QString pwd_buffer = QDir::currentPath(); 0042 const QFileInfo file(checkFilename); 0043 0044 qputenv("XML_CATALOG_FILES", catalogs); 0045 if (QFileInfo(exe).isExecutable()) { 0046 QDir::setCurrent(file.absolutePath()); 0047 QString cmd = exe; 0048 cmd += QStringLiteral(" --valid --noout "); 0049 cmd += file.fileName(); 0050 cmd += QStringLiteral(" 2>&1"); 0051 FILE *xmllint = popen(QFile::encodeName(cmd).constData(), "r"); 0052 char buf[512]; 0053 bool noout = true; 0054 size_t n; 0055 while ((n = fread(buf, 1, sizeof(buf) - 1, xmllint))) { 0056 noout = false; 0057 buf[n] = '\0'; 0058 fputs(buf, stderr); 0059 } 0060 pclose(xmllint); 0061 QDir::setCurrent(pwd_buffer); 0062 if (!noout) { 0063 return CheckNoOut; 0064 } 0065 } else { 0066 return CheckNoXmllint; 0067 } 0068 return CheckSuccess; 0069 } 0070 0071 void doOutput(QString output, bool usingStdOut, bool usingOutput, const QString &outputOption, bool replaceCharset) 0072 { 0073 if (output.indexOf(QStringLiteral("<FILENAME ")) == -1 || usingStdOut || usingOutput) { 0074 QFile file; 0075 if (usingStdOut) { 0076 file.open(stdout, QIODevice::WriteOnly); 0077 } else { 0078 if (usingOutput) { 0079 file.setFileName(outputOption); 0080 } else { 0081 file.setFileName(QStringLiteral("index.html")); 0082 } 0083 file.open(QIODevice::WriteOnly); 0084 } 0085 if (replaceCharset) { 0086 replaceCharsetHeader(output); 0087 } 0088 #ifdef Q_OS_WIN 0089 QByteArray data = output.toUtf8(); 0090 #else 0091 QByteArray data = output.toLocal8Bit(); 0092 #endif 0093 file.write(data.data(), data.length()); 0094 file.close(); 0095 } else { 0096 int index = 0; 0097 while (true) { 0098 index = output.indexOf(QStringLiteral("<FILENAME "), index); 0099 if (index == -1) { 0100 break; 0101 } 0102 int filename_index = index + qstrlen("<FILENAME filename=\""); 0103 0104 const QString filename = output.mid(filename_index, output.indexOf(QLatin1Char('\"'), filename_index) - filename_index); 0105 0106 QString filedata = splitOut(output, index); 0107 QFile file(filename); 0108 file.open(QIODevice::WriteOnly); 0109 if (replaceCharset) { 0110 replaceCharsetHeader(filedata); 0111 } 0112 const QByteArray data = fromUnicode(filedata); 0113 file.write(data.data(), data.length()); 0114 file.close(); 0115 0116 index += 8; 0117 } 0118 } 0119 }