8
#include <qtextstream.h>
10
/*! \brief Abstract base class for any configuration option.
19
/*! The type of option */
22
O_Info, //<! A section header
23
O_List, //<! A list of items
24
O_Enum, //<! A fixed set of items
25
O_String, //<! A single item
26
O_Int, //<! An integer value
27
O_Bool, //<! A boolean value
28
O_Obsolete //<! An obsolete option
32
/*! Maximum length of an option in the config file. Used for
35
MAX_OPTION_LENGTH = 23
37
ConfigOption(OptionType t) : m_kind(t)
39
m_spaces.fill(' ',40);
41
virtual ~ConfigOption()
45
/*! returns the kind of option this is. */
46
OptionType kind() const { return m_kind; }
47
QCString name() const { return m_name; }
48
QCString docs() const { return m_doc; }
50
QCString dependsOn() const { return m_dependency; }
51
void addDependency(const char *dep) { m_dependency = dep; }
54
virtual void writeTemplate(QTextStream &t,bool sl,bool upd) = 0;
55
virtual void convertStrToVal() {}
56
virtual void substEnvVars() = 0;
57
virtual void init() {}
59
QCString convertToComment(const QCString &s);
60
void writeBoolValue(QTextStream &t,bool v);
61
void writeIntValue(QTextStream &t,int i);
62
void writeStringValue(QTextStream &t,QCString &s);
63
void writeStringList(QTextStream &t,QStrList &l);
68
QCString m_dependency;
72
/*! \brief Section marker for grouping the configuration options
75
class ConfigInfo : public ConfigOption
78
ConfigInfo(const char *name,const char *doc)
79
: ConfigOption(O_Info)
84
void writeTemplate(QTextStream &t, bool sl,bool)
90
t << "#---------------------------------------------------------------------------\n";
91
t << "# " << m_doc << endl;
92
t << "#---------------------------------------------------------------------------\n";
94
void substEnvVars() {}
97
/*! \brief Option of the list type.
100
class ConfigList : public ConfigOption
103
enum WidgetType { String, File, Dir, FileAndDir };
104
ConfigList(const char *name,const char *doc)
105
: ConfigOption(O_List)
109
m_widgetType = String;
111
void addValue(const char *v) { m_value.append(v); }
112
void setWidgetType(WidgetType w) { m_widgetType = w; }
113
WidgetType widgetType() const { return m_widgetType; }
114
QStrList *valueRef() { return &m_value; }
115
void writeTemplate(QTextStream &t,bool sl,bool)
120
t << convertToComment(m_doc);
123
t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
124
writeStringList(t,m_value);
128
void init() { m_value.clear(); }
131
WidgetType m_widgetType;
134
/*! \brief Option of the enum type.
137
class ConfigEnum : public ConfigOption
140
ConfigEnum(const char *name,const char *doc,const char *defVal)
141
: ConfigOption(O_Enum)
148
void addValue(const char *v) { m_valueRange.append(v); }
149
QStrListIterator iterator()
151
return QStrListIterator(m_valueRange);
153
QCString *valueRef() { return &m_value; }
155
void writeTemplate(QTextStream &t,bool sl,bool)
160
t << convertToComment(m_doc);
163
t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
164
writeStringValue(t,m_value);
167
void init() { m_value = m_defValue.copy(); }
170
QStrList m_valueRange;
175
/*! \brief Option of the string type.
178
class ConfigString : public ConfigOption
181
enum WidgetType { String, File, Dir };
182
ConfigString(const char *name,const char *doc)
183
: ConfigOption(O_String)
187
m_widgetType = String;
192
void setWidgetType(WidgetType w) { m_widgetType = w; }
193
WidgetType widgetType() const { return m_widgetType; }
194
void setDefaultValue(const char *v) { m_defValue = v; }
195
QCString *valueRef() { return &m_value; }
196
void writeTemplate(QTextStream &t,bool sl,bool)
201
t << convertToComment(m_doc);
204
t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
205
writeStringValue(t,m_value);
209
void init() { m_value = m_defValue.copy(); }
214
WidgetType m_widgetType;
217
/*! \brief Option of the integer type.
220
class ConfigInt : public ConfigOption
223
ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal)
224
: ConfigOption(O_Int)
233
QCString *valueStringRef() { return &m_valueString; }
234
int *valueRef() { return &m_value; }
235
int minVal() const { return m_minVal; }
236
int maxVal() const { return m_maxVal; }
237
void convertStrToVal();
239
void writeTemplate(QTextStream &t,bool sl,bool upd)
244
t << convertToComment(m_doc);
247
t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
248
if (upd && !m_valueString.isEmpty())
250
writeStringValue(t,m_valueString);
254
writeIntValue(t,m_value);
258
void init() { m_value = m_defValue; }
264
QCString m_valueString;
267
/*! \brief Option of the boolean type.
270
class ConfigBool : public ConfigOption
273
ConfigBool(const char *name,const char *doc,bool defVal)
274
: ConfigOption(O_Bool)
281
QCString *valueStringRef() { return &m_valueString; }
282
bool *valueRef() { return &m_value; }
283
void convertStrToVal();
285
void setValueString(const QCString &v) { m_valueString = v; }
286
void writeTemplate(QTextStream &t,bool sl,bool upd)
291
t << convertToComment(m_doc);
294
t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
295
if (upd && !m_valueString.isEmpty())
297
writeStringValue(t,m_valueString);
301
writeBoolValue(t,m_value);
305
void init() { m_value = m_defValue; }
309
QCString m_valueString;
312
/*! \brief Section marker for obsolete options
315
class ConfigObsolete : public ConfigOption
318
ConfigObsolete(OptionType t) : ConfigOption(t) {}
319
void writeTemplate(QTextStream &,bool,bool) {}
320
void substEnvVars() {}
324
// some convenience macros
325
#define Config_getString(val) Config::instance()->getString(__FILE__,__LINE__,val)
326
#define Config_getInt(val) Config::instance()->getInt(__FILE__,__LINE__,val)
327
#define Config_getList(val) Config::instance()->getList(__FILE__,__LINE__,val)
328
#define Config_getEnum(val) Config::instance()->getEnum(__FILE__,__LINE__,val)
329
#define Config_getBool(val) Config::instance()->getBool(__FILE__,__LINE__,val)
331
/*! \brief Singleton for configuration variables.
333
* This object holds the global static variables
334
* read from a user-supplied configuration file.
335
* The static member instance() can be used to get
336
* a pointer to the one and only instance.
338
* Set all variables to their default values by
339
* calling Config::instance()->init()
345
/////////////////////////////
347
/////////////////////////////
349
/*! Returns the one and only instance of this class */
350
static Config *instance()
352
if (m_instance==0) m_instance = new Config;
355
/*! Delete the instance */
356
static void deleteInstance()
361
/*! Returns an iterator that can by used to iterate over the
362
* configuration options.
364
QPtrListIterator<ConfigOption> iterator()
366
return QPtrListIterator<ConfigOption>(*m_options);
370
* @name Getting configuration values.
374
/*! Returns the value of the string option with name \a fileName.
375
* The arguments \a num and \a name are for debugging purposes only.
376
* There is a convenience function Config_getString() for this.
378
QCString &getString(const char *fileName,int num,const char *name) const;
380
/*! Returns the value of the list option with name \a fileName.
381
* The arguments \a num and \a name are for debugging purposes only.
382
* There is a convenience function Config_getList() for this.
384
QStrList &getList(const char *fileName,int num,const char *name) const;
386
/*! Returns the value of the enum option with name \a fileName.
387
* The arguments \a num and \a name are for debugging purposes only.
388
* There is a convenience function Config_getEnum() for this.
390
QCString &getEnum(const char *fileName,int num,const char *name) const;
392
/*! Returns the value of the integer option with name \a fileName.
393
* The arguments \a num and \a name are for debugging purposes only.
394
* There is a convenience function Config_getInt() for this.
396
int &getInt(const char *fileName,int num,const char *name) const;
398
/*! Returns the value of the boolean option with name \a fileName.
399
* The arguments \a num and \a name are for debugging purposes only.
400
* There is a convenience function Config_getBool() for this.
402
bool &getBool(const char *fileName,int num,const char *name) const;
404
/*! Returns the ConfigOption corresponding with \a name or 0 if
405
* the option is not supported.
407
ConfigOption *get(const char *name) const
409
return m_dict->find(name);
414
* @name Adding configuration options.
418
/*! Starts a new configuration section with \a name and description \a doc.
419
* \returns An object representing the option.
421
ConfigInfo *addInfo(const char *name,const char *doc)
423
ConfigInfo *result = new ConfigInfo(name,doc);
424
m_options->append(result);
428
/*! Adds a new string option with \a name and documentation \a doc.
429
* \returns An object representing the option.
431
ConfigString *addString(const char *name,
434
ConfigString *result = new ConfigString(name,doc);
435
m_options->append(result);
436
m_dict->insert(name,result);
440
/*! Adds a new enumeration option with \a name and documentation \a doc
441
* and initial value \a defVal.
442
* \returns An object representing the option.
444
ConfigEnum *addEnum(const char *name,
448
ConfigEnum *result = new ConfigEnum(name,doc,defVal);
449
m_options->append(result);
450
m_dict->insert(name,result);
454
/*! Adds a new string option with \a name and documentation \a doc.
455
* \returns An object representing the option.
457
ConfigList *addList(const char *name,
460
ConfigList *result = new ConfigList(name,doc);
461
m_options->append(result);
462
m_dict->insert(name,result);
466
/*! Adds a new integer option with \a name and documentation \a doc.
467
* The integer has a range between \a minVal and \a maxVal and a
468
* default value of \a defVal.
469
* \returns An object representing the option.
471
ConfigInt *addInt(const char *name,
473
int minVal,int maxVal,int defVal)
475
ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
476
m_options->append(result);
477
m_dict->insert(name,result);
481
/*! Adds a new boolean option with \a name and documentation \a doc.
482
* The boolean has a default value of \a defVal.
483
* \returns An object representing the option.
485
ConfigBool *addBool(const char *name,
489
ConfigBool *result = new ConfigBool(name,doc,defVal);
490
m_options->append(result);
491
m_dict->insert(name,result);
494
/*! Adds an option that has become obsolete. */
495
ConfigOption *addObsolete(const char *name)
497
ConfigObsolete *option = new ConfigObsolete(ConfigOption::O_Obsolete);
498
m_dict->insert(name,option);
499
m_obsolete->append(option);
504
/*! Writes a template configuration file to \a f. If \a shortIndex
505
* is \c TRUE the description of each configuration option will
508
void writeTemplate(QFile *f,bool shortIndex,bool updateOnly);
510
/////////////////////////////
512
/////////////////////////////
514
/*! Converts the string values read from the configuration file
515
* to real values for non-string type options (like int, and bools)
517
void convertStrToVal();
519
/*! Replaces references to environment variable by the actual value
520
* of the environment variable.
522
void substituteEnvironmentVars();
524
/*! Checks if the values of the variable are correct, adjusts them
525
* if needed, and report any errors.
529
/*! Initialize config variables to their default value */
532
/*! Parse a configuration file with name \a fn.
533
* \returns TRUE if successful, FALSE if the file could not be
536
bool parse(const char *fn);
538
/*! Called from the constructor, will add doxygen's default options
539
* to the configuration object
547
m_options = new QPtrList<ConfigOption>;
548
m_obsolete = new QPtrList<ConfigOption>;
549
m_dict = new QDict<ConfigOption>(257);
550
m_options->setAutoDelete(TRUE);
551
m_obsolete->setAutoDelete(TRUE);
552
m_initialized = FALSE;
563
QPtrList<ConfigOption> *m_options;
564
QPtrList<ConfigOption> *m_obsolete;
565
QDict<ConfigOption> *m_dict;
566
static Config *m_instance;