config.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  *
4  *
5  *
6  * Copyright (C) 1997-2015 by Dimitri van Heesch.
7  *
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.
13  *
14  * Documents produced by Doxygen are derivative works derived from the
15  * input used in their production; they are not affected by this license.
16  *
17  */
18 
19 #ifndef CONFIG_H
20 #define CONFIG_H
21 
22 #include <qstrlist.h>
23 #include <qdict.h>
24 #include <qlist.h>
25 #include <qregexp.h>
26 #include "ftextstream.h"
27 
28 
29 /** Abstract base class for any configuration option.
30  */
32 {
33  friend class Config;
34 
35  public:
36 
37  /*! The type of option */
38  enum OptionType
39  {
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
48  };
49  enum
50  {
51  /*! Maximum length of an option in the config file. Used for
52  * alignment purposes.
53  */
55  };
57  {
58  m_spaces.fill(' ',40);
59  }
60  virtual ~ConfigOption()
61  {
62  }
63 
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; }
68 
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; }
73 
74  protected:
75  virtual void writeTemplate(FTextStream &t,bool sl,bool upd) = 0;
76  virtual void convertStrToVal() {}
77  virtual void substEnvVars() = 0;
78  virtual void init() {}
79 
80  void writeBoolValue(FTextStream &t,bool v);
81  void writeIntValue(FTextStream &t,int i);
84 
92 };
93 
94 /** Section marker for grouping the configuration options.
95  */
96 class ConfigInfo : public ConfigOption
97 {
98  public:
99  ConfigInfo(const char *name,const char *doc)
101  {
102  m_name = name;
103  m_doc = doc;
104  }
105  void writeTemplate(FTextStream &t, bool sl,bool);
106  void substEnvVars() {}
107 };
108 
109 /** Class respresenting a list type option.
110  */
111 class ConfigList : public ConfigOption
112 {
113  public:
114  enum WidgetType { String, File, Dir, FileAndDir };
115  ConfigList(const char *name,const char *doc)
117  {
118  m_name = name;
119  m_doc = doc;
120  m_widgetType = String;
121  }
122  void addValue(const char *v) { m_value.append(v); }
123  void setWidgetType(WidgetType w) { m_widgetType = w; }
124  WidgetType widgetType() const { return m_widgetType; }
125  QStrList *valueRef() { return &m_value; }
126  void writeTemplate(FTextStream &t,bool sl,bool);
127  void substEnvVars();
128  void init() { m_value.clear(); }
129  private:
132 };
133 
134 /** Class representing an enum type option.
135  */
136 class ConfigEnum : public ConfigOption
137 {
138  public:
139  ConfigEnum(const char *name,const char *doc,const char *defVal)
141  {
142  m_name = name;
143  m_doc = doc;
144  m_value = defVal;
145  m_defValue = defVal;
146  }
147  void addValue(const char *v) { m_valueRange.append(v); }
149  {
150  return QStrListIterator(m_valueRange);
151  }
152  QCString *valueRef() { return &m_value; }
153  void substEnvVars();
154  void writeTemplate(FTextStream &t,bool sl,bool);
155  void init() { m_value = m_defValue.copy(); }
156 
157  private:
161 };
162 
163 /** Class representing a string type option.
164  */
166 {
167  public:
168  enum WidgetType { String, File, Dir, Image };
169  ConfigString(const char *name,const char *doc)
171  {
172  m_name = name;
173  m_doc = doc;
174  m_widgetType = String;
175  }
177  {
178  }
179  void setWidgetType(WidgetType w) { m_widgetType = w; }
180  WidgetType widgetType() const { return m_widgetType; }
181  void setDefaultValue(const char *v) { m_defValue = v; }
182  QCString *valueRef() { return &m_value; }
183  void writeTemplate(FTextStream &t,bool sl,bool);
184  void substEnvVars();
185  void init() { m_value = m_defValue.copy(); }
186 
187  private:
191 };
192 
193 /** Class representing an integer type option.
194  */
195 class ConfigInt : public ConfigOption
196 {
197  public:
198  ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal)
200  {
201  m_name = name;
202  m_doc = doc;
203  m_value = defVal;
204  m_defValue = defVal;
205  m_minVal = minVal;
206  m_maxVal = maxVal;
207  }
208  QCString *valueStringRef() { return &m_valueString; }
209  int *valueRef() { return &m_value; }
210  int minVal() const { return m_minVal; }
211  int maxVal() const { return m_maxVal; }
212  void convertStrToVal();
213  void substEnvVars();
214  void writeTemplate(FTextStream &t,bool sl,bool upd);
215  void init() { m_value = m_defValue; }
216  private:
217  int m_value;
219  int m_minVal;
220  int m_maxVal;
222 };
223 
224 /** Class representing a Boolean type option.
225  */
226 class ConfigBool : public ConfigOption
227 {
228  public:
229  ConfigBool(const char *name,const char *doc,bool defVal)
231  {
232  m_name = name;
233  m_doc = doc;
234  m_value = defVal;
235  m_defValue = defVal;
236  }
237  QCString *valueStringRef() { return &m_valueString; }
238  bool *valueRef() { return &m_value; }
239  void convertStrToVal();
240  void substEnvVars();
241  void setValueString(const QCString &v) { m_valueString = v; }
242  void writeTemplate(FTextStream &t,bool sl,bool upd);
243  void init() { m_value = m_defValue; }
244  private:
245  bool m_value;
248 };
249 
250 /** Section marker for obsolete options
251  */
253 {
254  public:
256  { m_name = name; }
257  void writeTemplate(FTextStream &,bool,bool);
258  void substEnvVars() {}
259 };
260 
261 /** Section marker for compile time optional options
262  */
264 {
265  public:
267  { m_name = name; }
268  void writeTemplate(FTextStream &,bool,bool);
269  void substEnvVars() {}
270 };
271 
272 
273 // some convenience macros for access the config options
274 #define Config_getString(val) Config::instance()->getString(__FILE__,__LINE__,val)
275 #define Config_getInt(val) Config::instance()->getInt(__FILE__,__LINE__,val)
276 #define Config_getList(val) Config::instance()->getList(__FILE__,__LINE__,val)
277 #define Config_getEnum(val) Config::instance()->getEnum(__FILE__,__LINE__,val)
278 #define Config_getBool(val) Config::instance()->getBool(__FILE__,__LINE__,val)
279 
280 /** Singleton for configuration variables.
281  *
282  * This object holds the global static variables
283  * read from a user-supplied configuration file.
284  * The static member instance() can be used to get
285  * a pointer to the one and only instance.
286  *
287  * Set all variables to their default values by
288  * calling Config::instance()->init()
289  *
290  */
291 class Config
292 {
293  public:
294  /////////////////////////////
295  // public API
296  /////////////////////////////
297 
298  /*! Returns the one and only instance of this class */
299  static Config *instance()
300  {
301  if (m_instance==0) m_instance = new Config;
302  return m_instance;
303  }
304  /*! Delete the instance */
305  static void deleteInstance()
306  {
307  delete m_instance;
308  m_instance=0;
309  }
310 
311  /*! Returns an iterator that can by used to iterate over the
312  * configuration options.
313  */
315  {
316  return QListIterator<ConfigOption>(*m_options);
317  }
318 
319  /*!
320  * @name Getting configuration values.
321  * @{
322  */
323 
324  /*! Returns the value of the string option with name \a fileName.
325  * The arguments \a num and \a name are for debugging purposes only.
326  * There is a convenience function Config_getString() for this.
327  */
328  QCString &getString(const char *fileName,int num,const char *name) const;
329 
330  /*! Returns the value of the list option with name \a fileName.
331  * The arguments \a num and \a name are for debugging purposes only.
332  * There is a convenience function Config_getList() for this.
333  */
334  QStrList &getList(const char *fileName,int num,const char *name) const;
335 
336  /*! Returns the value of the enum 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_getEnum() for this.
339  */
340  QCString &getEnum(const char *fileName,int num,const char *name) const;
341 
342  /*! Returns the value of the integer 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_getInt() for this.
345  */
346  int &getInt(const char *fileName,int num,const char *name) const;
347 
348  /*! Returns the value of the boolean 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_getBool() for this.
351  */
352  bool &getBool(const char *fileName,int num,const char *name) const;
353 
354  /*! Returns the ConfigOption corresponding with \a name or 0 if
355  * the option is not supported.
356  */
357  ConfigOption *get(const char *name) const
358  {
359  return m_dict->find(name);
360  }
361  /* @} */
362 
363  /*!
364  * @name Adding configuration options.
365  * @{
366  */
367 
368  /*! Starts a new configuration section with \a name and description \a doc.
369  * \returns An object representing the option.
370  */
371  ConfigInfo *addInfo(const char *name,const char *doc)
372  {
373  ConfigInfo *result = new ConfigInfo(name,doc);
374  m_options->append(result);
375  return result;
376  }
377 
378  /*! Adds a new string option with \a name and documentation \a doc.
379  * \returns An object representing the option.
380  */
381  ConfigString *addString(const char *name,
382  const char *doc)
383  {
384  ConfigString *result = new ConfigString(name,doc);
385  m_options->append(result);
386  m_dict->insert(name,result);
387  return result;
388  }
389 
390  /*! Adds a new enumeration option with \a name and documentation \a doc
391  * and initial value \a defVal.
392  * \returns An object representing the option.
393  */
394  ConfigEnum *addEnum(const char *name,
395  const char *doc,
396  const char *defVal)
397  {
398  ConfigEnum *result = new ConfigEnum(name,doc,defVal);
399  m_options->append(result);
400  m_dict->insert(name,result);
401  return result;
402  }
403 
404  /*! Adds a new string option with \a name and documentation \a doc.
405  * \returns An object representing the option.
406  */
407  ConfigList *addList(const char *name,
408  const char *doc)
409  {
410  ConfigList *result = new ConfigList(name,doc);
411  m_options->append(result);
412  m_dict->insert(name,result);
413  return result;
414  }
415 
416  /*! Adds a new integer option with \a name and documentation \a doc.
417  * The integer has a range between \a minVal and \a maxVal and a
418  * default value of \a defVal.
419  * \returns An object representing the option.
420  */
421  ConfigInt *addInt(const char *name,
422  const char *doc,
423  int minVal,int maxVal,int defVal)
424  {
425  ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
426  m_options->append(result);
427  m_dict->insert(name,result);
428  return result;
429  }
430 
431  /*! Adds a new boolean option with \a name and documentation \a doc.
432  * The boolean has a default value of \a defVal.
433  * \returns An object representing the option.
434  */
435  ConfigBool *addBool(const char *name,
436  const char *doc,
437  bool defVal)
438  {
439  ConfigBool *result = new ConfigBool(name,doc,defVal);
440  m_options->append(result);
441  m_dict->insert(name,result);
442  return result;
443  }
444  /*! Adds an option that has become obsolete. */
445  ConfigOption *addObsolete(const char *name)
446  {
447  ConfigObsolete *option = new ConfigObsolete(name);
448  m_dict->insert(name,option);
449  m_obsolete->append(option);
450  return option;
451  }
452  /*! Adds an option that has been disabled at compile time. */
453  ConfigOption *addDisabled(const char *name)
454  {
455  ConfigDisabled *option = new ConfigDisabled(name);
456  m_dict->insert(name,option);
457  m_disabled->append(option);
458  return option;
459  }
460  /*! @} */
461 
462  /*! Writes a template configuration to stream \a t. If \a shortIndex
463  * is \c TRUE the description of each configuration option will
464  * be omitted.
465  */
466  void writeTemplate(FTextStream &t,bool shortIndex,bool updateOnly);
467 
468  void setHeader(const char *header) { m_header = header; }
469 
470  /////////////////////////////
471  // internal API
472  /////////////////////////////
473 
474  /*! Converts the string values read from the configuration file
475  * to real values for non-string type options (like int, and bools)
476  */
477  void convertStrToVal();
478 
479  /*! Replaces references to environment variable by the actual value
480  * of the environment variable.
481  */
482  void substituteEnvironmentVars();
483 
484  /*! Checks if the values of the variable are correct, adjusts them
485  * if needed, and report any errors.
486  */
487  void check();
488 
489  /*! Initialize config variables to their default value */
490  void init();
491 
492  /*! Parse a configuration data in string \a str.
493  * \returns TRUE if successful, or FALSE if the string could not be
494  * parsed.
495  */
496  //bool parseString(const char *fn,const char *str);
497  bool parseString(const char *fn,const char *str,bool upd = FALSE);
498 
499  /*! Parse a configuration file with name \a fn.
500  * \returns TRUE if successful, FALSE if the file could not be
501  * opened or read.
502  */
503  bool parse(const char *fn,bool upd = FALSE);
504 
505  /*! Called from the constructor, will add doxygen's default options
506  * to the configuration object
507  */
508  void create();
509 
510  /*! Append user start comment
511  */
513  {
514  m_startComment += u;
515  }
516  /*! Append user comment
517  */
519  {
520  m_userComment += u;
521  }
522  /*! Take the user start comment and reset it internally
523  * \returns user start comment
524  */
526  {
527  QCString result=m_startComment;
528  m_startComment.resize(0);
529  return result.replace(QRegExp("\r"),"");
530  }
531  /*! Take the user comment and reset it internally
532  * \returns user comment
533  */
535  {
538  return result.replace(QRegExp("\r"),"");
539  }
540 
541  protected:
542 
544  {
545  m_options = new QList<ConfigOption>;
546  m_obsolete = new QList<ConfigOption>;
547  m_disabled = new QList<ConfigOption>;
548  m_dict = new QDict<ConfigOption>(257);
549  m_options->setAutoDelete(TRUE);
550  m_obsolete->setAutoDelete(TRUE);
551  m_disabled->setAutoDelete(TRUE);
552  m_initialized = FALSE;
553  create();
554  }
556  {
557  delete m_options;
558  delete m_obsolete;
559  delete m_disabled;
560  delete m_dict;
561  }
562 
563  private:
564  void checkFileName(const char *);
568  QDict<ConfigOption> *m_dict;
574 };
575 
576 #endif
virtual ~ConfigOption()
Definition: config.h:60
QCString * valueRef()
Definition: config.h:182
bool resize(uint newlen)
Definition: qcstring.h:225
QList< ConfigOption > * m_disabled
Definition: config.h:567
static Config * m_instance
Definition: config.h:569
void setEncoding(const QCString &e)
Definition: config.h:71
QCString m_valueString
Definition: config.h:247
QCString m_spaces
Definition: config.h:85
void writeBoolValue(FTextStream &t, bool v)
Definition: config.cpp:726
QStrList m_valueRange
Definition: config.h:158
ConfigList * addList(const char *name, const char *doc)
Definition: config.h:407
OptionType m_kind
Definition: config.h:91
QCString m_name
Definition: config.h:86
static QCString result
The QRegExp class provides pattern matching using regular expressions or wildcards.
Definition: qregexp.h:46
QCString m_userComment
Definition: config.h:90
bool m_defValue
Definition: config.h:246
bool m_initialized
Definition: config.h:572
ConfigInfo(const char *name, const char *doc)
Definition: config.h:99
void substEnvVars()
Definition: config.h:106
ConfigInfo * addInfo(const char *name, const char *doc)
Definition: config.h:371
WidgetType m_widgetType
Definition: config.h:131
void setHeader(const char *header)
Definition: config.h:468
QCString m_dependency
Definition: config.h:88
QCString * valueStringRef()
Definition: config.h:208
void writeStringValue(FTextStream &t, QCString &s)
Definition: config.cpp:737
void setWidgetType(WidgetType w)
Definition: config.h:123
const bool FALSE
Definition: qglobal.h:370
void writeStringList(FTextStream &t, QStrList &l)
Definition: config.cpp:768
QDict< ConfigOption > * m_dict
Definition: config.h:568
void addValue(const char *v)
Definition: config.h:122
ConfigBool * addBool(const char *name, const char *doc, bool defVal)
Definition: config.h:435
QCString docs() const
Definition: config.h:67
ConfigOption(OptionType t)
Definition: config.h:56
Simplified and optimized version of QTextStream.
Definition: ftextstream.h:11
QStrListIterator iterator()
Definition: config.h:148
OptionType kind() const
Definition: config.h:65
ConfigString * addString(const char *name, const char *doc)
Definition: config.h:381
void init()
Definition: config.h:155
QCString takeStartComment()
Definition: config.h:525
QList< ConfigOption > * m_options
Definition: config.h:565
static QStrList * l
Definition: config.cpp:1044
QList< ConfigOption > * m_obsolete
Definition: config.h:566
bool check(const std::vector< std::vector< float > > &outputs)
int minVal() const
Definition: config.h:210
QCString * valueRef()
Definition: config.h:152
ConfigDisabled(const char *name)
Definition: config.h:266
~ConfigString()
Definition: config.h:176
virtual void convertStrToVal()
Definition: config.h:76
QAsciiDict< Entry > fn
int m_value
Definition: config.h:217
ConfigEnum * addEnum(const char *name, const char *doc, const char *defVal)
Definition: config.h:394
void writeIntValue(FTextStream &t, int i)
Definition: config.cpp:732
QCString m_header
Definition: config.h:573
QCString m_defValue
Definition: config.h:160
QStrList m_value
Definition: config.h:130
const double e
fileName
Definition: dumpTree.py:9
ConfigObsolete(const char *name)
Definition: config.h:255
void substEnvVars()
Definition: config.h:269
bool * valueRef()
Definition: config.h:238
int * valueRef()
Definition: config.h:209
int maxVal() const
Definition: config.h:211
QCString m_value
Definition: config.h:188
void setWidgetType(WidgetType w)
Definition: config.h:179
bool m_value
Definition: config.h:245
JAVACC_STRING_TYPE String
Definition: JavaCC.h:22
QCString m_userComment
Definition: config.h:571
WidgetType widgetType() const
Definition: config.h:124
int m_defValue
Definition: config.h:218
static void deleteInstance()
Definition: config.h:305
QCString m_doc
Definition: config.h:87
virtual void substEnvVars()=0
QCString m_value
Definition: config.h:159
void setValueString(const QCString &v)
Definition: config.h:241
~Config()
Definition: config.h:555
friend class Config
Definition: config.h:33
ConfigList(const char *name, const char *doc)
Definition: config.h:115
static Config * instance()
Definition: config.h:299
void init()
Definition: config.h:215
void appendUserComment(const QCString &u)
Definition: config.h:518
void init()
Definition: config.h:243
ConfigOption * addDisabled(const char *name)
Definition: config.h:453
WidgetType m_widgetType
Definition: config.h:190
QCString m_startComment
Definition: config.h:570
ConfigInt(const char *name, const char *doc, int minVal, int maxVal, int defVal)
Definition: config.h:198
QCString m_encoding
Definition: config.h:89
QCString name() const
Definition: config.h:66
void addDependency(const char *dep)
Definition: config.h:70
ConfigString(const char *name, const char *doc)
Definition: config.h:169
int m_minVal
Definition: config.h:219
Definition: image.h:24
int m_maxVal
Definition: config.h:220
bool fill(char c, int len=-1)
Definition: qcstring.h:243
QCString doc
void init()
Definition: config.h:185
void setDefaultValue(const char *v)
Definition: config.h:181
QInternalListIterator< char > QStrListIterator
Definition: qstrlist.h:54
QCString dependsOn() const
Definition: config.h:69
QCString m_defValue
Definition: config.h:189
ConfigOption * addObsolete(const char *name)
Definition: config.h:445
QListIterator< ConfigOption > iterator()
Definition: config.h:314
void init()
Definition: config.h:128
virtual void init()
Definition: config.h:78
void substEnvVars()
Definition: config.h:258
WidgetType widgetType() const
Definition: config.h:180
ConfigBool(const char *name, const char *doc, bool defVal)
Definition: config.h:229
void setAutoDelete(bool enable)
Definition: qlist.h:99
QStrList * valueRef()
Definition: config.h:125
QCString takeUserComment()
Definition: config.h:534
ConfigInt * addInt(const char *name, const char *doc, int minVal, int maxVal, int defVal)
Definition: config.h:421
QCString & replace(uint index, uint len, const char *s)
Definition: qcstring.cpp:411
static QCString * s
Definition: config.cpp:1042
const bool TRUE
Definition: qglobal.h:371
static QCString str
void addValue(const char *v)
Definition: config.h:147
void appendStartComment(const QCString &u)
Definition: config.h:512
virtual void writeTemplate(FTextStream &t, bool sl, bool upd)=0
ConfigEnum(const char *name, const char *doc, const char *defVal)
Definition: config.h:139
void setUserComment(const QCString &u)
Definition: config.h:72
QCString m_valueString
Definition: config.h:221
Config()
Definition: config.h:543
QCString * valueStringRef()
Definition: config.h:237