1 /******************************************************************************
3 * $Id: config.h,v 1.39 2001/03/19 19:27:40 root Exp $
6 * Copyright (C) 1997-2012 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.
27 #include "ftextstream.h"
30 /** Abstract base class for any configuration option.
38 /*! The type of option */
41 O_Info, //<! A section header
42 O_List, //<! A list of items
43 O_Enum, //<! A fixed set of items
44 O_String, //<! A single item
45 O_Int, //<! An integer value
46 O_Bool, //<! A boolean value
47 O_Obsolete //<! An obsolete 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; }
74 virtual void writeTemplate(FTextStream &t,bool sl,bool upd) = 0;
75 virtual void convertStrToVal() {}
76 virtual void substEnvVars() = 0;
77 virtual void writeXML(FTextStream&) {}
78 virtual void init() {}
80 QCString convertToComment(const QCString &s);
81 void writeBoolValue(FTextStream &t,bool v);
82 void writeIntValue(FTextStream &t,int i);
83 void writeStringValue(FTextStream &t,QCString &s);
84 void writeStringList(FTextStream &t,QStrList &l);
89 QCString m_dependency;
94 /** Section marker for grouping the configuration options.
96 class ConfigInfo : public ConfigOption
99 ConfigInfo(const char *name,const char *doc)
100 : ConfigOption(O_Info)
105 void writeTemplate(FTextStream &t, bool sl,bool)
111 t << "#---------------------------------------------------------------------------\n";
112 t << "# " << m_doc << endl;
113 t << "#---------------------------------------------------------------------------\n";
115 void substEnvVars() {}
118 /** Class respresenting a list type option.
120 class ConfigList : public ConfigOption
123 enum WidgetType { String, File, Dir, FileAndDir };
124 ConfigList(const char *name,const char *doc)
125 : ConfigOption(O_List)
129 m_widgetType = String;
131 void addValue(const char *v) { m_value.append(v); }
132 void setWidgetType(WidgetType w) { m_widgetType = w; }
133 WidgetType widgetType() const { return m_widgetType; }
134 QStrList *valueRef() { return &m_value; }
135 void writeTemplate(FTextStream &t,bool sl,bool)
140 t << convertToComment(m_doc);
143 t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "=";
144 writeStringList(t,m_value);
148 void writeXML(FTextStream&);
149 void init() { m_value.clear(); }
152 WidgetType m_widgetType;
155 /** Class representing an enum type option.
157 class ConfigEnum : public ConfigOption
160 ConfigEnum(const char *name,const char *doc,const char *defVal)
161 : ConfigOption(O_Enum)
168 void addValue(const char *v) { m_valueRange.append(v); }
169 QStrListIterator iterator()
171 return QStrListIterator(m_valueRange);
173 QCString *valueRef() { return &m_value; }
175 void writeTemplate(FTextStream &t,bool sl,bool)
180 t << convertToComment(m_doc);
183 t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "=";
184 writeStringValue(t,m_value);
187 void writeXML(FTextStream&);
188 void init() { m_value = m_defValue.copy(); }
191 QStrList m_valueRange;
196 /** Class representing a string type option.
198 class ConfigString : public ConfigOption
201 enum WidgetType { String, File, Dir };
202 ConfigString(const char *name,const char *doc)
203 : ConfigOption(O_String)
207 m_widgetType = String;
212 void setWidgetType(WidgetType w) { m_widgetType = w; }
213 WidgetType widgetType() const { return m_widgetType; }
214 void setDefaultValue(const char *v) { m_defValue = v; }
215 QCString *valueRef() { return &m_value; }
216 void writeTemplate(FTextStream &t,bool sl,bool)
221 t << convertToComment(m_doc);
224 t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "=";
225 writeStringValue(t,m_value);
229 void writeXML(FTextStream&);
230 void init() { m_value = m_defValue.copy(); }
235 WidgetType m_widgetType;
238 /** Class representing an integer type option.
240 class ConfigInt : public ConfigOption
243 ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal)
244 : ConfigOption(O_Int)
253 QCString *valueStringRef() { return &m_valueString; }
254 int *valueRef() { return &m_value; }
255 int minVal() const { return m_minVal; }
256 int maxVal() const { return m_maxVal; }
257 void convertStrToVal();
259 void writeTemplate(FTextStream &t,bool sl,bool upd)
264 t << convertToComment(m_doc);
267 t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "=";
268 if (upd && !m_valueString.isEmpty())
270 writeStringValue(t,m_valueString);
274 writeIntValue(t,m_value);
278 void writeXML(FTextStream&);
279 void init() { m_value = m_defValue; }
285 QCString m_valueString;
288 /** Class representing a Boolean type option.
290 class ConfigBool : public ConfigOption
293 ConfigBool(const char *name,const char *doc,bool defVal)
294 : ConfigOption(O_Bool)
301 QCString *valueStringRef() { return &m_valueString; }
302 bool *valueRef() { return &m_value; }
303 void convertStrToVal();
305 void setValueString(const QCString &v) { m_valueString = v; }
306 void writeTemplate(FTextStream &t,bool sl,bool upd)
311 t << convertToComment(m_doc);
314 t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "=";
315 if (upd && !m_valueString.isEmpty())
317 writeStringValue(t,m_valueString);
321 writeBoolValue(t,m_value);
325 void writeXML(FTextStream&);
326 void init() { m_value = m_defValue; }
330 QCString m_valueString;
333 /** Section marker for obsolete options
335 class ConfigObsolete : public ConfigOption
338 ConfigObsolete(const char *name,OptionType t) : ConfigOption(t)
340 void writeTemplate(FTextStream &,bool,bool) {}
341 void substEnvVars() {}
342 void writeXML(FTextStream&);
346 // some convenience macros
347 #define Config_getString(val) Config::instance()->getString(__FILE__,__LINE__,val)
348 #define Config_getInt(val) Config::instance()->getInt(__FILE__,__LINE__,val)
349 #define Config_getList(val) Config::instance()->getList(__FILE__,__LINE__,val)
350 #define Config_getEnum(val) Config::instance()->getEnum(__FILE__,__LINE__,val)
351 #define Config_getBool(val) Config::instance()->getBool(__FILE__,__LINE__,val)
353 /** Singleton for configuration variables.
355 * This object holds the global static variables
356 * read from a user-supplied configuration file.
357 * The static member instance() can be used to get
358 * a pointer to the one and only instance.
360 * Set all variables to their default values by
361 * calling Config::instance()->init()
367 /////////////////////////////
369 /////////////////////////////
371 /*! Returns the one and only instance of this class */
372 static Config *instance()
374 if (m_instance==0) m_instance = new Config;
377 /*! Delete the instance */
378 static void deleteInstance()
384 /*! Returns an iterator that can by used to iterate over the
385 * configuration options.
387 QListIterator<ConfigOption> iterator()
389 return QListIterator<ConfigOption>(*m_options);
393 * @name Getting configuration values.
397 /*! Returns the value of the string option with name \a fileName.
398 * The arguments \a num and \a name are for debugging purposes only.
399 * There is a convenience function Config_getString() for this.
401 QCString &getString(const char *fileName,int num,const char *name) const;
403 /*! Returns the value of the list option with name \a fileName.
404 * The arguments \a num and \a name are for debugging purposes only.
405 * There is a convenience function Config_getList() for this.
407 QStrList &getList(const char *fileName,int num,const char *name) const;
409 /*! Returns the value of the enum option with name \a fileName.
410 * The arguments \a num and \a name are for debugging purposes only.
411 * There is a convenience function Config_getEnum() for this.
413 QCString &getEnum(const char *fileName,int num,const char *name) const;
415 /*! Returns the value of the integer option with name \a fileName.
416 * The arguments \a num and \a name are for debugging purposes only.
417 * There is a convenience function Config_getInt() for this.
419 int &getInt(const char *fileName,int num,const char *name) const;
421 /*! Returns the value of the boolean option with name \a fileName.
422 * The arguments \a num and \a name are for debugging purposes only.
423 * There is a convenience function Config_getBool() for this.
425 bool &getBool(const char *fileName,int num,const char *name) const;
427 /*! Returns the ConfigOption corresponding with \a name or 0 if
428 * the option is not supported.
430 ConfigOption *get(const char *name) const
432 return m_dict->find(name);
437 * @name Adding configuration options.
441 /*! Starts a new configuration section with \a name and description \a doc.
442 * \returns An object representing the option.
444 ConfigInfo *addInfo(const char *name,const char *doc)
446 ConfigInfo *result = new ConfigInfo(name,doc);
447 m_options->append(result);
451 /*! Adds a new string option with \a name and documentation \a doc.
452 * \returns An object representing the option.
454 ConfigString *addString(const char *name,
457 ConfigString *result = new ConfigString(name,doc);
458 m_options->append(result);
459 m_dict->insert(name,result);
463 /*! Adds a new enumeration option with \a name and documentation \a doc
464 * and initial value \a defVal.
465 * \returns An object representing the option.
467 ConfigEnum *addEnum(const char *name,
471 ConfigEnum *result = new ConfigEnum(name,doc,defVal);
472 m_options->append(result);
473 m_dict->insert(name,result);
477 /*! Adds a new string option with \a name and documentation \a doc.
478 * \returns An object representing the option.
480 ConfigList *addList(const char *name,
483 ConfigList *result = new ConfigList(name,doc);
484 m_options->append(result);
485 m_dict->insert(name,result);
489 /*! Adds a new integer option with \a name and documentation \a doc.
490 * The integer has a range between \a minVal and \a maxVal and a
491 * default value of \a defVal.
492 * \returns An object representing the option.
494 ConfigInt *addInt(const char *name,
496 int minVal,int maxVal,int defVal)
498 ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
499 m_options->append(result);
500 m_dict->insert(name,result);
504 /*! Adds a new boolean option with \a name and documentation \a doc.
505 * The boolean has a default value of \a defVal.
506 * \returns An object representing the option.
508 ConfigBool *addBool(const char *name,
512 ConfigBool *result = new ConfigBool(name,doc,defVal);
513 m_options->append(result);
514 m_dict->insert(name,result);
517 /*! Adds an option that has become obsolete. */
518 ConfigOption *addObsolete(const char *name)
520 ConfigObsolete *option = new ConfigObsolete(name,ConfigOption::O_Obsolete);
521 m_dict->insert(name,option);
522 m_obsolete->append(option);
527 /*! Writes a template configuration to stream \a t. If \a shortIndex
528 * is \c TRUE the description of each configuration option will
531 void writeTemplate(FTextStream &t,bool shortIndex,bool updateOnly);
533 /** Write XML representation of the config file */
534 void writeXML(FTextStream &t);
536 /////////////////////////////
538 /////////////////////////////
540 /*! Converts the string values read from the configuration file
541 * to real values for non-string type options (like int, and bools)
543 void convertStrToVal();
545 /*! Replaces references to environment variable by the actual value
546 * of the environment variable.
548 void substituteEnvironmentVars();
550 /*! Checks if the values of the variable are correct, adjusts them
551 * if needed, and report any errors.
555 /*! Initialize config variables to their default value */
558 /*! Parse a configuration data in string \a str.
559 * \returns TRUE if successful, or FALSE if the string could not be
562 bool parseString(const char *fn,const char *str);
564 /*! Parse a configuration file with name \a fn.
565 * \returns TRUE if successful, FALSE if the file could not be
568 bool parse(const char *fn);
570 /*! Called from the constructor, will add doxygen's default options
571 * to the configuration object
579 m_options = new QList<ConfigOption>;
580 m_obsolete = new QList<ConfigOption>;
581 m_dict = new QDict<ConfigOption>(257);
582 m_options->setAutoDelete(TRUE);
583 m_obsolete->setAutoDelete(TRUE);
584 m_initialized = FALSE;
595 QList<ConfigOption> *m_options;
596 QList<ConfigOption> *m_obsolete;
597 QDict<ConfigOption> *m_dict;
598 static Config *m_instance;