1 /******************************************************************************
6 * Copyright (C) 1997-2015 by Dimitri van Heesch.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
26 #include "ftextstream.h"
29 /** Abstract base class for any configuration option.
33 friend class ConfigImpl;
37 /*! The type of option */
40 O_Info, //<! A section header
41 O_List, //<! A list of items
42 O_Enum, //<! A fixed set of items
43 O_String, //<! A single item
44 O_Int, //<! An integer value
45 O_Bool, //<! A boolean value
46 O_Obsolete, //<! An obsolete option
47 O_Disabled //<! Disabled compile time option
51 /*! Maximum length of an option in the config file. Used for
54 MAX_OPTION_LENGTH = 23
56 ConfigOption(OptionType t) : m_kind(t)
58 m_spaces.fill(' ',40);
60 virtual ~ConfigOption()
64 /*! returns the kind of option this is. */
65 OptionType kind() const { return m_kind; }
66 QCString name() const { return m_name; }
67 QCString docs() const { return m_doc; }
69 QCString dependsOn() const { return m_dependency; }
70 void addDependency(const char *dep) { m_dependency = dep; }
71 void setEncoding(const QCString &e) { m_encoding = e; }
72 void setUserComment(const QCString &u) { m_userComment += u; }
75 virtual void writeTemplate(FTextStream &t,bool sl,bool upd) = 0;
76 virtual void compareDoxyfile(FTextStream &t) = 0;
77 virtual void convertStrToVal() {}
78 virtual void emptyValueToDefault() {}
79 virtual void substEnvVars() = 0;
80 virtual void init() {}
82 void writeBoolValue(FTextStream &t,bool v);
83 void writeIntValue(FTextStream &t,int i);
84 void writeStringValue(FTextStream &t,QCString &s);
85 void writeStringList(FTextStream &t,QStrList &l);
90 QCString m_dependency;
92 QCString m_userComment;
96 /** Section marker for grouping the configuration options.
98 class ConfigInfo : public ConfigOption
101 ConfigInfo(const char *name,const char *doc)
102 : ConfigOption(O_Info)
107 void writeTemplate(FTextStream &t, bool sl,bool);
108 void compareDoxyfile(FTextStream &){};
109 void substEnvVars() {}
112 /** Class respresenting a list type option.
114 class ConfigList : public ConfigOption
117 enum WidgetType { String, File, Dir, FileAndDir };
118 ConfigList(const char *name,const char *doc)
119 : ConfigOption(O_List)
123 m_widgetType = String;
125 void addValue(const char *v) { m_defaultValue.append(v); }
126 void setWidgetType(WidgetType w) { m_widgetType = w; }
127 WidgetType widgetType() const { return m_widgetType; }
128 QStrList *valueRef() { return &m_value; }
129 void writeTemplate(FTextStream &t,bool sl,bool);
130 void compareDoxyfile(FTextStream &t);
132 void init() { m_value = m_defaultValue; }
135 QStrList m_defaultValue;
136 WidgetType m_widgetType;
139 /** Class representing an enum type option.
141 class ConfigEnum : public ConfigOption
144 ConfigEnum(const char *name,const char *doc,const char *defVal)
145 : ConfigOption(O_Enum)
152 void addValue(const char *v) { m_valueRange.append(v); }
153 QStrListIterator iterator()
155 return QStrListIterator(m_valueRange);
157 QCString *valueRef() { return &m_value; }
159 void writeTemplate(FTextStream &t,bool sl,bool);
160 void compareDoxyfile(FTextStream &t);
161 void init() { m_value = m_defValue.copy(); }
164 QStrList m_valueRange;
169 /** Class representing a string type option.
171 class ConfigString : public ConfigOption
174 enum WidgetType { String, File, Dir, Image };
175 ConfigString(const char *name,const char *doc)
176 : ConfigOption(O_String)
180 m_widgetType = String;
185 void setWidgetType(WidgetType w) { m_widgetType = w; }
186 WidgetType widgetType() const { return m_widgetType; }
187 void setDefaultValue(const char *v) { m_defValue = v; }
188 QCString *valueRef() { return &m_value; }
189 void writeTemplate(FTextStream &t,bool sl,bool);
190 void compareDoxyfile(FTextStream &t);
192 void init() { m_value = m_defValue.copy(); }
193 void emptyValueToDefault() { if(m_value.isEmpty()) m_value=m_defValue; };
198 WidgetType m_widgetType;
201 /** Class representing an integer type option.
203 class ConfigInt : public ConfigOption
206 ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal)
207 : ConfigOption(O_Int)
216 QCString *valueStringRef() { return &m_valueString; }
217 int *valueRef() { return &m_value; }
218 int minVal() const { return m_minVal; }
219 int maxVal() const { return m_maxVal; }
220 void convertStrToVal();
222 void writeTemplate(FTextStream &t,bool sl,bool upd);
223 void compareDoxyfile(FTextStream &t);
224 void init() { m_value = m_defValue; }
230 QCString m_valueString;
233 /** Class representing a Boolean type option.
235 class ConfigBool : public ConfigOption
238 ConfigBool(const char *name,const char *doc,bool defVal)
239 : ConfigOption(O_Bool)
246 QCString *valueStringRef() { return &m_valueString; }
247 bool *valueRef() { return &m_value; }
248 void convertStrToVal();
250 void setValueString(const QCString &v) { m_valueString = v; }
251 void writeTemplate(FTextStream &t,bool sl,bool upd);
252 void compareDoxyfile(FTextStream &t);
253 void init() { m_value = m_defValue; }
257 QCString m_valueString;
260 /** Section marker for obsolete options
262 class ConfigObsolete : public ConfigOption
265 ConfigObsolete(const char *name) : ConfigOption(O_Obsolete)
267 void writeTemplate(FTextStream &,bool,bool);
268 void compareDoxyfile(FTextStream &) {}
269 void substEnvVars() {}
272 /** Section marker for compile time optional options
274 class ConfigDisabled : public ConfigOption
277 ConfigDisabled(const char *name) : ConfigOption(O_Disabled)
279 void writeTemplate(FTextStream &,bool,bool);
280 void compareDoxyfile(FTextStream &) {}
281 void substEnvVars() {}
284 // some convenience macros for access the config options
285 #define ConfigImpl_getString(val) ConfigImpl::instance()->getString(__FILE__,__LINE__,val)
286 #define ConfigImpl_getInt(val) ConfigImpl::instance()->getInt(__FILE__,__LINE__,val)
287 #define ConfigImpl_getList(val) ConfigImpl::instance()->getList(__FILE__,__LINE__,val)
288 #define ConfigImpl_getEnum(val) ConfigImpl::instance()->getEnum(__FILE__,__LINE__,val)
289 #define ConfigImpl_getBool(val) ConfigImpl::instance()->getBool(__FILE__,__LINE__,val)
292 /** Singleton for configuration variables.
294 * This object holds the global static variables
295 * read from a user-supplied configuration file.
296 * The static member instance() can be used to get
297 * a pointer to the one and only instance.
299 * Set all variables to their default values by
300 * calling Config::instance()->init()
306 /////////////////////////////
308 /////////////////////////////
310 /*! Returns the one and only instance of this class */
311 static ConfigImpl *instance()
313 if (m_instance==0) m_instance = new ConfigImpl;
316 /*! Delete the instance */
317 static void deleteInstance()
323 /*! Returns an iterator that can by used to iterate over the
324 * configuration options.
326 QListIterator<ConfigOption> iterator()
328 return QListIterator<ConfigOption>(*m_options);
332 * @name Getting configuration values.
336 /*! Returns the value of the string option with name \a fileName.
337 * The arguments \a num and \a name are for debugging purposes only.
338 * There is a convenience function Config_getString() for this.
340 QCString &getString(const char *fileName,int num,const char *name) const;
342 /*! Returns the value of the list option with name \a fileName.
343 * The arguments \a num and \a name are for debugging purposes only.
344 * There is a convenience function Config_getList() for this.
346 QStrList &getList(const char *fileName,int num,const char *name) const;
348 /*! Returns the value of the enum option with name \a fileName.
349 * The arguments \a num and \a name are for debugging purposes only.
350 * There is a convenience function Config_getEnum() for this.
352 QCString &getEnum(const char *fileName,int num,const char *name) const;
354 /*! Returns the value of the integer option with name \a fileName.
355 * The arguments \a num and \a name are for debugging purposes only.
356 * There is a convenience function Config_getInt() for this.
358 int &getInt(const char *fileName,int num,const char *name) const;
360 /*! Returns the value of the boolean option with name \a fileName.
361 * The arguments \a num and \a name are for debugging purposes only.
362 * There is a convenience function Config_getBool() for this.
364 bool &getBool(const char *fileName,int num,const char *name) const;
366 /*! Returns the ConfigOption corresponding with \a name or 0 if
367 * the option is not supported.
369 ConfigOption *get(const char *name) const
371 return m_dict->find(name);
376 * @name Adding configuration options.
380 /*! Starts a new configuration section with \a name and description \a doc.
381 * \returns An object representing the option.
383 ConfigInfo *addInfo(const char *name,const char *doc)
385 ConfigInfo *result = new ConfigInfo(name,doc);
386 m_options->append(result);
390 /*! Adds a new string option with \a name and documentation \a doc.
391 * \returns An object representing the option.
393 ConfigString *addString(const char *name,
396 ConfigString *result = new ConfigString(name,doc);
397 m_options->append(result);
398 m_dict->insert(name,result);
402 /*! Adds a new enumeration option with \a name and documentation \a doc
403 * and initial value \a defVal.
404 * \returns An object representing the option.
406 ConfigEnum *addEnum(const char *name,
410 ConfigEnum *result = new ConfigEnum(name,doc,defVal);
411 m_options->append(result);
412 m_dict->insert(name,result);
416 /*! Adds a new string option with \a name and documentation \a doc.
417 * \returns An object representing the option.
419 ConfigList *addList(const char *name,
422 ConfigList *result = new ConfigList(name,doc);
423 m_options->append(result);
424 m_dict->insert(name,result);
428 /*! Adds a new integer option with \a name and documentation \a doc.
429 * The integer has a range between \a minVal and \a maxVal and a
430 * default value of \a defVal.
431 * \returns An object representing the option.
433 ConfigInt *addInt(const char *name,
435 int minVal,int maxVal,int defVal)
437 ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
438 m_options->append(result);
439 m_dict->insert(name,result);
443 /*! Adds a new boolean option with \a name and documentation \a doc.
444 * The boolean has a default value of \a defVal.
445 * \returns An object representing the option.
447 ConfigBool *addBool(const char *name,
451 ConfigBool *result = new ConfigBool(name,doc,defVal);
452 m_options->append(result);
453 m_dict->insert(name,result);
456 /*! Adds an option that has become obsolete. */
457 ConfigOption *addObsolete(const char *name)
459 ConfigObsolete *option = new ConfigObsolete(name);
460 m_dict->insert(name,option);
461 m_obsolete->append(option);
464 /*! Adds an option that has been disabled at compile time. */
465 ConfigOption *addDisabled(const char *name)
467 ConfigDisabled *option = new ConfigDisabled(name);
468 m_dict->insert(name,option);
469 m_disabled->append(option);
474 /*! Writes a template configuration to stream \a t. If \a shortIndex
475 * is \c TRUE the description of each configuration option will
478 void writeTemplate(FTextStream &t,bool shortIndex,bool updateOnly);
480 /*! Writes a the differences between the current configuration and the
481 * template configuration to stream \a t.
483 void compareDoxyfile(FTextStream &t);
485 void setHeader(const char *header) { m_header = header; }
487 /////////////////////////////
489 /////////////////////////////
491 /*! Converts the string values read from the configuration file
492 * to real values for non-string type options (like int, and bools)
494 void convertStrToVal();
496 /*! Sets default value in case value is empty
498 void emptyValueToDefault();
500 /*! Replaces references to environment variable by the actual value
501 * of the environment variable.
503 void substituteEnvironmentVars();
505 /*! Initialize config variables to their default value */
508 /*! Parse a configuration data in string \a str.
509 * \returns TRUE if successful, or FALSE if the string could not be
512 //bool parseString(const char *fn,const char *str);
513 bool parseString(const char *fn,const char *str,bool upd = FALSE);
515 /*! Parse a configuration file with name \a fn.
516 * \returns TRUE if successful, FALSE if the file could not be
519 bool parse(const char *fn,bool upd = FALSE);
521 /*! Called from the constructor, will add doxygen's default options
522 * to the configuration object
526 /*! Append user start comment
528 void appendStartComment(const QCString &u)
532 /*! Append user comment
534 void appendUserComment(const QCString &u)
538 /*! Take the user start comment and reset it internally
539 * \returns user start comment
541 QCString takeStartComment()
543 QCString result=m_startComment;
544 m_startComment.resize(0);
545 return result.replace(QRegExp("\r"),"");
547 /*! Take the user comment and reset it internally
548 * \returns user comment
550 QCString takeUserComment()
552 QCString result=m_userComment;
553 m_userComment.resize(0);
554 return result.replace(QRegExp("\r"),"");
561 m_options = new QList<ConfigOption>;
562 m_obsolete = new QList<ConfigOption>;
563 m_disabled = new QList<ConfigOption>;
564 m_dict = new QDict<ConfigOption>(257);
565 m_options->setAutoDelete(TRUE);
566 m_obsolete->setAutoDelete(TRUE);
567 m_disabled->setAutoDelete(TRUE);
568 m_initialized = FALSE;
580 QList<ConfigOption> *m_options;
581 QList<ConfigOption> *m_obsolete;
582 QList<ConfigOption> *m_disabled;
583 QDict<ConfigOption> *m_dict;
584 static ConfigImpl *m_instance;
585 QCString m_startComment;
586 QCString m_userComment;