1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 /*
30   config.h
31 */
32 
33 #ifndef CONFIG_H
34 #define CONFIG_H
35 
36 #include "location.h"
37 #include "qdoccommandlineparser.h"
38 
39 #include <QtCore/qmap.h>
40 #include <QtCore/qpair.h>
41 #include <QtCore/qset.h>
42 #include <QtCore/qstack.h>
43 #include <QtCore/qstringlist.h>
44 
45 QT_BEGIN_NAMESPACE
46 
47 template<typename T>
48 class Singleton
49 {
50 public:
51     Singleton(const Singleton &) = delete;
52     Singleton &operator=(const Singleton &) = delete;
instance()53     static T &instance()
54     {
55         static T instance;
56         return instance;
57     }
58 
59 protected:
60     Singleton() = default;
61 };
62 
63 /*
64   This struct contains all the information for
65   one config variable found in a qdocconf file.
66  */
67 struct ConfigVar
68 {
69     bool m_plus {};
70     QString m_name {};
71     QStringList m_values {};
72     QString m_currentPath {};
73     Location m_location {};
74 
ConfigVarConfigVar75     ConfigVar() : m_plus(false) {}
76 
ConfigVarConfigVar77     ConfigVar(const QString &name, const QStringList &values, const QString &dir)
78         : m_plus(true), m_name(name), m_values(values), m_currentPath(dir)
79     {
80     }
81 
ConfigVarConfigVar82     ConfigVar(const QString &name, const QStringList &values, const QString &dir,
83               const Location &loc)
84         : m_plus(false), m_name(name), m_values(values), m_currentPath(dir), m_location(loc)
85     {
86     }
87 };
88 
89 /*
90   In this multimap, the key is a config variable name.
91  */
92 typedef QMultiMap<QString, ConfigVar> ConfigVarMultimap;
93 
94 class Config : public Singleton<Config>
95 {
96     Q_DECLARE_TR_FUNCTIONS(QDoc::Config)
97 
98 public:
99     ~Config();
100 
101     enum QDocPass { Neither, Prepare, Generate };
102 
103     void init(const QString &programName, const QStringList &args);
getDebug()104     bool getDebug() const { return m_debug; }
105 
106     void clear();
107     void reset();
108     void load(const QString &fileName);
109     void setStringList(const QString &var, const QStringList &values);
110     void insertStringList(const QString &var, const QStringList &values);
111 
112     void showHelp(int exitCode = 0) { m_parser.showHelp(exitCode); }
qdocFiles()113     QStringList qdocFiles() const { return m_parser.positionalArguments(); }
programName()114     const QString &programName() const { return m_prog; }
location()115     const Location &location() const { return m_location; }
lastLocation()116     const Location &lastLocation() const { return m_lastLocation; }
117     bool getBool(const QString &var) const;
118     int getInt(const QString &var) const;
119 
120     QString getOutputDir(const QString &format = QString("HTML")) const;
121     QSet<QString> getOutputFormats() const;
122     QString getString(const QString &var, const QString &defaultString = QString()) const;
123     QSet<QString> getStringSet(const QString &var) const;
124     QStringList getStringList(const QString &var) const;
125     QStringList getCanonicalPathList(const QString &var, bool validate = false) const;
126     QRegExp getRegExp(const QString &var) const;
127     QVector<QRegExp> getRegExpList(const QString &var) const;
128     QSet<QString> subVars(const QString &var) const;
129     void subVarsAndValues(const QString &var, ConfigVarMultimap &t) const;
130     QStringList getAllFiles(const QString &filesVar, const QString &dirsVar,
131                             const QSet<QString> &excludedDirs = QSet<QString>(),
132                             const QSet<QString> &excludedFiles = QSet<QString>());
133     QString getIncludeFilePath(const QString &fileName) const;
134     QStringList getExampleQdocFiles(const QSet<QString> &excludedDirs,
135                                     const QSet<QString> &excludedFiles);
136     QStringList getExampleImageFiles(const QSet<QString> &excludedDirs,
137                                      const QSet<QString> &excludedFiles);
138     QString getExampleProjectFile(const QString &examplePath);
139 
140     static QStringList loadMaster(const QString &fileName);
141     static bool isFileExcluded(const QString &fileName, const QSet<QString> &excludedFiles);
142     static QStringList getFilesHere(const QString &dir, const QString &nameFilter,
143                                     const Location &location = Location(),
144                                     const QSet<QString> &excludedDirs = QSet<QString>(),
145                                     const QSet<QString> &excludedFiles = QSet<QString>());
146     static QString findFile(const Location &location, const QStringList &files,
147                             const QStringList &dirs, const QString &fileName,
148                             QString *userFriendlyFilePath = nullptr);
149     static QString findFile(const Location &location, const QStringList &files,
150                             const QStringList &dirs, const QString &fileBase,
151                             const QStringList &fileExtensions,
152                             QString *userFriendlyFilePath = nullptr);
153     static QString copyFile(const Location &location, const QString &sourceFilePath,
154                             const QString &userFriendlySourceFilePath,
155                             const QString &targetDirPath);
156     static int numParams(const QString &value);
157     static bool removeDirContents(const QString &dir);
158     static void pushWorkingDir(const QString &dir);
159     static QString popWorkingDir();
160 
161     static const QString dot;
162 
163     static bool generateExamples;
164     static QString installDir;
165     static QString overrideOutputDir;
166     static QSet<QString> overrideOutputFormats;
167 
168     inline bool singleExec() const;
169     inline bool dualExec() const;
defines()170     QStringList &defines() { return m_defines; }
dependModules()171     QStringList &dependModules() { return m_dependModules; }
includePaths()172     QStringList &includePaths() { return m_includePaths; }
indexDirs()173     QStringList &indexDirs() { return m_indexDirs; }
currentDir()174     QString currentDir() const { return m_currentDir; }
setCurrentDir(const QString & path)175     void setCurrentDir(const QString &path) { m_currentDir = path; }
previousCurrentDir()176     QString previousCurrentDir() const { return m_previousCurrentDir; }
setPreviousCurrentDir(const QString & path)177     void setPreviousCurrentDir(const QString &path) { m_previousCurrentDir = path; }
178 
qdocPass()179     QDocPass qdocPass() const { return m_qdocPass; }
setQDocPass(const QDocPass & pass)180     void setQDocPass(const QDocPass &pass) { m_qdocPass = pass; };
preparing()181     bool preparing() const { return (m_qdocPass == Prepare); }
generating()182     bool generating() const { return (m_qdocPass == Generate); }
183 
184 private:
185     void processCommandLineOptions(const QStringList &args);
186     void setIncludePaths();
187     void setIndexDirs();
188 
189     QStringList m_dependModules {};
190     QStringList m_defines {};
191     QStringList m_includePaths {};
192     QStringList m_indexDirs {};
193     QStringList m_exampleFiles {};
194     QStringList m_exampleDirs {};
195     QString m_currentDir {};
196     QString m_previousCurrentDir {};
197 
198     static bool m_debug;
199     static bool isMetaKeyChar(QChar ch);
200     void load(Location location, const QString &fileName);
201 
202     QString m_prog {};
203     Location m_location {};
204     Location m_lastLocation {};
205     ConfigVarMultimap m_configVars {};
206 
207     static QMap<QString, QString> m_uncompressedFiles;
208     static QMap<QString, QString> m_extractedDirs;
209     static QStack<QString> m_workingDirs;
210     static QMap<QString, QStringList> m_includeFilesMap;
211     QDocCommandLineParser m_parser {};
212 
213     QDocPass m_qdocPass { Neither };
214 };
215 
216 struct ConfigStrings
217 {
218     static QString ALIAS;
219     static QString AUTOLINKERRORS;
220     static QString BUILDVERSION;
221     static QString CLANGDEFINES;
222     static QString CODEINDENT;
223     static QString CODEPREFIX;
224     static QString CODESUFFIX;
225     static QString CPPCLASSESPAGE;
226     static QString CPPCLASSESTITLE;
227     static QString DEFINES;
228     static QString DEPENDS;
229     static QString DESCRIPTION;
230     static QString DOCBOOKEXTENSIONS;
231     static QString ENDHEADER;
232     static QString EXAMPLEDIRS;
233     static QString EXAMPLES;
234     static QString EXAMPLESINSTALLPATH;
235     static QString EXCLUDEDIRS;
236     static QString EXCLUDEFILES;
237     static QString EXTRAIMAGES;
238     static QString FALSEHOODS;
239     static QString FORMATTING;
240     static QString HEADERDIRS;
241     static QString HEADERS;
242     static QString HEADERSCRIPTS;
243     static QString HEADERSTYLES;
244     static QString HOMEPAGE;
245     static QString HOMETITLE;
246     static QString IGNOREDIRECTIVES;
247     static QString IGNORETOKENS;
248     static QString IGNORESINCE;
249     static QString IGNOREWORDS;
250     static QString IMAGEDIRS;
251     static QString IMAGES;
252     static QString INCLUDEPATHS;
253     static QString INDEXES;
254     static QString LANDINGPAGE;
255     static QString LANDINGTITLE;
256     static QString LANGUAGE;
257     static QString LOCATIONINFO;
258     static QString LOGPROGRESS;
259     static QString MACRO;
260     static QString MANIFESTMETA;
261     static QString MODULEHEADER;
262     static QString NATURALLANGUAGE;
263     static QString NAVIGATION;
264     static QString NOLINKERRORS;
265     static QString OBSOLETELINKS;
266     static QString OUTPUTDIR;
267     static QString OUTPUTENCODING;
268     static QString OUTPUTFORMATS;
269     static QString OUTPUTPREFIXES;
270     static QString OUTPUTSUFFIXES;
271     static QString PROJECT;
272     static QString REDIRECTDOCUMENTATIONTODEVNULL;
273     static QString QHP;
274     static QString QUOTINGINFORMATION;
275     static QString SCRIPTDIRS;
276     static QString SCRIPTS;
277     static QString SHOWINTERNAL;
278     static QString SINGLEEXEC;
279     static QString SOURCEDIRS;
280     static QString SOURCEENCODING;
281     static QString SOURCES;
282     static QString SPURIOUS;
283     static QString STYLEDIRS;
284     static QString STYLE;
285     static QString STYLES;
286     static QString STYLESHEETS;
287     static QString SYNTAXHIGHLIGHTING;
288     static QString TABSIZE;
289     static QString TAGFILE;
290     static QString TIMESTAMPS;
291     static QString TRANSLATORS;
292     static QString URL;
293     static QString VERSION;
294     static QString VERSIONSYM;
295     static QString FILEEXTENSIONS;
296     static QString IMAGEEXTENSIONS;
297     static QString QMLONLY;
298     static QString QMLTYPESPAGE;
299     static QString QMLTYPESTITLE;
300     static QString WARNINGLIMIT;
301     static QString WRITEQAPAGES;
302 };
303 
304 #define CONFIG_ALIAS ConfigStrings::ALIAS
305 #define CONFIG_AUTOLINKERRORS ConfigStrings::AUTOLINKERRORS
306 #define CONFIG_BUILDVERSION ConfigStrings::BUILDVERSION
307 #define CONFIG_CLANGDEFINES ConfigStrings::CLANGDEFINES
308 #define CONFIG_CODEINDENT ConfigStrings::CODEINDENT
309 #define CONFIG_CODEPREFIX ConfigStrings::CODEPREFIX
310 #define CONFIG_CODESUFFIX ConfigStrings::CODESUFFIX
311 #define CONFIG_CPPCLASSESPAGE ConfigStrings::CPPCLASSESPAGE
312 #define CONFIG_CPPCLASSESTITLE ConfigStrings::CPPCLASSESTITLE
313 #define CONFIG_DEFINES ConfigStrings::DEFINES
314 #define CONFIG_DEPENDS ConfigStrings::DEPENDS
315 #define CONFIG_DESCRIPTION ConfigStrings::DESCRIPTION
316 #define CONFIG_DOCBOOKEXTENSIONS ConfigStrings::DOCBOOKEXTENSIONS
317 #define CONFIG_ENDHEADER ConfigStrings::ENDHEADER
318 #define CONFIG_EXAMPLEDIRS ConfigStrings::EXAMPLEDIRS
319 #define CONFIG_EXAMPLES ConfigStrings::EXAMPLES
320 #define CONFIG_EXAMPLESINSTALLPATH ConfigStrings::EXAMPLESINSTALLPATH
321 #define CONFIG_EXCLUDEDIRS ConfigStrings::EXCLUDEDIRS
322 #define CONFIG_EXCLUDEFILES ConfigStrings::EXCLUDEFILES
323 #define CONFIG_EXTRAIMAGES ConfigStrings::EXTRAIMAGES
324 #define CONFIG_FALSEHOODS ConfigStrings::FALSEHOODS
325 #define CONFIG_FORMATTING ConfigStrings::FORMATTING
326 #define CONFIG_HEADERDIRS ConfigStrings::HEADERDIRS
327 #define CONFIG_HEADERS ConfigStrings::HEADERS
328 #define CONFIG_HEADERSCRIPTS ConfigStrings::HEADERSCRIPTS
329 #define CONFIG_HEADERSTYLES ConfigStrings::HEADERSTYLES
330 #define CONFIG_HOMEPAGE ConfigStrings::HOMEPAGE
331 #define CONFIG_HOMETITLE ConfigStrings::HOMETITLE
332 #define CONFIG_IGNOREDIRECTIVES ConfigStrings::IGNOREDIRECTIVES
333 #define CONFIG_IGNORESINCE ConfigStrings::IGNORESINCE
334 #define CONFIG_IGNORETOKENS ConfigStrings::IGNORETOKENS
335 #define CONFIG_IGNOREWORDS ConfigStrings::IGNOREWORDS
336 #define CONFIG_IMAGEDIRS ConfigStrings::IMAGEDIRS
337 #define CONFIG_IMAGES ConfigStrings::IMAGES
338 #define CONFIG_INCLUDEPATHS ConfigStrings::INCLUDEPATHS
339 #define CONFIG_INDEXES ConfigStrings::INDEXES
340 #define CONFIG_LANDINGPAGE ConfigStrings::LANDINGPAGE
341 #define CONFIG_LANDINGTITLE ConfigStrings::LANDINGTITLE
342 #define CONFIG_LANGUAGE ConfigStrings::LANGUAGE
343 #define CONFIG_LOCATIONINFO ConfigStrings::LOCATIONINFO
344 #define CONFIG_LOGPROGRESS ConfigStrings::LOGPROGRESS
345 #define CONFIG_MACRO ConfigStrings::MACRO
346 #define CONFIG_MANIFESTMETA ConfigStrings::MANIFESTMETA
347 #define CONFIG_MODULEHEADER ConfigStrings::MODULEHEADER
348 #define CONFIG_NATURALLANGUAGE ConfigStrings::NATURALLANGUAGE
349 #define CONFIG_NAVIGATION ConfigStrings::NAVIGATION
350 #define CONFIG_NOLINKERRORS ConfigStrings::NOLINKERRORS
351 #define CONFIG_OBSOLETELINKS ConfigStrings::OBSOLETELINKS
352 #define CONFIG_OUTPUTDIR ConfigStrings::OUTPUTDIR
353 #define CONFIG_OUTPUTENCODING ConfigStrings::OUTPUTENCODING
354 #define CONFIG_OUTPUTFORMATS ConfigStrings::OUTPUTFORMATS
355 #define CONFIG_OUTPUTPREFIXES ConfigStrings::OUTPUTPREFIXES
356 #define CONFIG_OUTPUTSUFFIXES ConfigStrings::OUTPUTSUFFIXES
357 #define CONFIG_PROJECT ConfigStrings::PROJECT
358 #define CONFIG_REDIRECTDOCUMENTATIONTODEVNULL ConfigStrings::REDIRECTDOCUMENTATIONTODEVNULL
359 #define CONFIG_QHP ConfigStrings::QHP
360 #define CONFIG_QUOTINGINFORMATION ConfigStrings::QUOTINGINFORMATION
361 #define CONFIG_SCRIPTDIRS ConfigStrings::SCRIPTDIRS
362 #define CONFIG_SCRIPTS ConfigStrings::SCRIPTS
363 #define CONFIG_SHOWINTERNAL ConfigStrings::SHOWINTERNAL
364 #define CONFIG_SINGLEEXEC ConfigStrings::SINGLEEXEC
365 #define CONFIG_SOURCEDIRS ConfigStrings::SOURCEDIRS
366 #define CONFIG_SOURCEENCODING ConfigStrings::SOURCEENCODING
367 #define CONFIG_SOURCES ConfigStrings::SOURCES
368 #define CONFIG_SPURIOUS ConfigStrings::SPURIOUS
369 #define CONFIG_STYLEDIRS ConfigStrings::STYLEDIRS
370 #define CONFIG_STYLE ConfigStrings::STYLE
371 #define CONFIG_STYLES ConfigStrings::STYLES
372 #define CONFIG_STYLESHEETS ConfigStrings::STYLESHEETS
373 #define CONFIG_SYNTAXHIGHLIGHTING ConfigStrings::SYNTAXHIGHLIGHTING
374 #define CONFIG_TABSIZE ConfigStrings::TABSIZE
375 #define CONFIG_TAGFILE ConfigStrings::TAGFILE
376 #define CONFIG_TIMESTAMPS ConfigStrings::TIMESTAMPS
377 #define CONFIG_TRANSLATORS ConfigStrings::TRANSLATORS
378 #define CONFIG_URL ConfigStrings::URL
379 #define CONFIG_VERSION ConfigStrings::VERSION
380 #define CONFIG_VERSIONSYM ConfigStrings::VERSIONSYM
381 #define CONFIG_FILEEXTENSIONS ConfigStrings::FILEEXTENSIONS
382 #define CONFIG_IMAGEEXTENSIONS ConfigStrings::IMAGEEXTENSIONS
383 #define CONFIG_QMLONLY ConfigStrings::QMLONLY
384 #define CONFIG_QMLTYPESPAGE ConfigStrings::QMLTYPESPAGE
385 #define CONFIG_QMLTYPESTITLE ConfigStrings::QMLTYPESTITLE
386 #define CONFIG_WARNINGLIMIT ConfigStrings::WARNINGLIMIT
387 #define CONFIG_WRITEQAPAGES ConfigStrings::WRITEQAPAGES
388 
singleExec()389 inline bool Config::singleExec() const
390 {
391     return getBool(CONFIG_SINGLEEXEC);
392 }
393 
dualExec()394 inline bool Config::dualExec() const
395 {
396     return !getBool(CONFIG_SINGLEEXEC);
397 }
398 
399 QT_END_NAMESPACE
400 
401 #endif
402