~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/doxygen/config.h

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef CONFIG_H
 
2
#define CONFIG_H
 
3
 
 
4
#include <qstrlist.h>
 
5
#include <qfile.h>
 
6
#include <qdict.h>
 
7
#include <qptrlist.h>
 
8
#include <qtextstream.h>
 
9
 
 
10
/*! \brief Abstract base class for any configuration option.
 
11
 *
 
12
 */
 
13
class ConfigOption
 
14
{
 
15
    friend class Config;
 
16
 
 
17
  public:
 
18
 
 
19
    /*! The type of option */
 
20
    enum OptionType 
 
21
    { 
 
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
 
29
    };
 
30
    enum 
 
31
    { 
 
32
     /*! Maximum length of an option in the config file. Used for 
 
33
      *  alignment purposes.
 
34
      */
 
35
      MAX_OPTION_LENGTH = 23  
 
36
    };
 
37
    ConfigOption(OptionType t) : m_kind(t) 
 
38
    {
 
39
      m_spaces.fill(' ',40);
 
40
    }
 
41
    virtual ~ConfigOption()
 
42
    {
 
43
    }
 
44
 
 
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; }
 
49
 
 
50
    QCString dependsOn() const { return m_dependency; }
 
51
    void addDependency(const char *dep) { m_dependency = dep; }
 
52
 
 
53
  protected:
 
54
    virtual void writeTemplate(QTextStream &t,bool sl,bool upd) = 0;
 
55
    virtual void convertStrToVal() {}
 
56
    virtual void substEnvVars() = 0;
 
57
    virtual void init() {}
 
58
 
 
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);
 
64
 
 
65
    QCString m_spaces;
 
66
    QCString m_name;
 
67
    QCString m_doc;
 
68
    QCString m_dependency;
 
69
    OptionType m_kind;
 
70
};
 
71
 
 
72
/*! \brief Section marker for grouping the configuration options
 
73
 *
 
74
 */
 
75
class ConfigInfo : public ConfigOption
 
76
{
 
77
  public:
 
78
    ConfigInfo(const char *name,const char *doc) 
 
79
      : ConfigOption(O_Info)
 
80
    {
 
81
      m_name = name;
 
82
      m_doc = doc;
 
83
    }
 
84
    void writeTemplate(QTextStream &t, bool sl,bool)
 
85
    {
 
86
      if (!sl)
 
87
      {
 
88
        t << "\n";
 
89
      }
 
90
      t << "#---------------------------------------------------------------------------\n";
 
91
      t << "# " << m_doc << endl;
 
92
      t << "#---------------------------------------------------------------------------\n";
 
93
    }
 
94
    void substEnvVars() {}
 
95
};
 
96
 
 
97
/*! \brief Option of the list type.
 
98
 *
 
99
 */
 
100
class ConfigList : public ConfigOption
 
101
{
 
102
  public:
 
103
    enum WidgetType { String, File, Dir, FileAndDir };
 
104
    ConfigList(const char *name,const char *doc) 
 
105
      : ConfigOption(O_List)
 
106
    {
 
107
      m_name = name;
 
108
      m_doc = doc;
 
109
      m_widgetType = String;
 
110
    }
 
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)
 
116
    {
 
117
      if (!sl)
 
118
      {
 
119
        t << endl;
 
120
        t << convertToComment(m_doc);
 
121
        t << endl;
 
122
      }
 
123
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
 
124
      writeStringList(t,m_value);
 
125
      t << "\n";
 
126
    }
 
127
    void substEnvVars();
 
128
    void init() { m_value.clear(); }
 
129
  private:
 
130
    QStrList m_value;
 
131
    WidgetType m_widgetType;
 
132
};
 
133
 
 
134
/*! \brief Option of the enum type.
 
135
 *
 
136
 */
 
137
class ConfigEnum : public ConfigOption
 
138
{
 
139
  public:
 
140
    ConfigEnum(const char *name,const char *doc,const char *defVal) 
 
141
      : ConfigOption(O_Enum)
 
142
    {
 
143
      m_name = name;
 
144
      m_doc = doc;
 
145
      m_value = defVal;
 
146
      m_defValue = defVal;
 
147
    }
 
148
    void addValue(const char *v) { m_valueRange.append(v); }
 
149
    QStrListIterator iterator() 
 
150
    {
 
151
      return QStrListIterator(m_valueRange);
 
152
    }
 
153
    QCString *valueRef() { return &m_value; }
 
154
    void substEnvVars();
 
155
    void writeTemplate(QTextStream &t,bool sl,bool)
 
156
    {
 
157
      if (!sl)
 
158
      {
 
159
        t << endl;
 
160
        t << convertToComment(m_doc);
 
161
        t << endl;
 
162
      }
 
163
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
 
164
      writeStringValue(t,m_value);
 
165
      t << "\n";
 
166
    }
 
167
    void init() { m_value = m_defValue.copy(); }
 
168
 
 
169
  private:
 
170
    QStrList m_valueRange;
 
171
    QCString m_value;
 
172
    QCString m_defValue;
 
173
};
 
174
 
 
175
/*! \brief Option of the string type.
 
176
 *
 
177
 */
 
178
class ConfigString : public ConfigOption
 
179
{
 
180
  public:
 
181
    enum WidgetType { String, File, Dir };
 
182
    ConfigString(const char *name,const char *doc) 
 
183
      : ConfigOption(O_String)
 
184
    {
 
185
      m_name = name;
 
186
      m_doc = doc;
 
187
      m_widgetType = String;
 
188
    }
 
189
   ~ConfigString()
 
190
    {
 
191
    }
 
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)
 
197
    {
 
198
      if (!sl)
 
199
      {
 
200
        t << endl;
 
201
        t << convertToComment(m_doc);
 
202
        t << endl;
 
203
      }
 
204
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
 
205
      writeStringValue(t,m_value);
 
206
      t << "\n";
 
207
    }
 
208
    void substEnvVars();
 
209
    void init() { m_value = m_defValue.copy(); }
 
210
  
 
211
  private:
 
212
    QCString m_value;
 
213
    QCString m_defValue;
 
214
    WidgetType m_widgetType;
 
215
};
 
216
 
 
217
/*! \brief Option of the integer type.
 
218
 *
 
219
 */
 
220
class ConfigInt : public ConfigOption
 
221
{
 
222
  public:
 
223
    ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal) 
 
224
      : ConfigOption(O_Int)
 
225
    {
 
226
      m_name = name;
 
227
      m_doc = doc;
 
228
      m_value = defVal;
 
229
      m_defValue = defVal;
 
230
      m_minVal = minVal;
 
231
      m_maxVal = maxVal;
 
232
    }
 
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();
 
238
    void substEnvVars();
 
239
    void writeTemplate(QTextStream &t,bool sl,bool upd)
 
240
    {
 
241
      if (!sl)
 
242
      {
 
243
        t << endl;
 
244
        t << convertToComment(m_doc);
 
245
        t << endl;
 
246
      }
 
247
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
 
248
      if (upd && !m_valueString.isEmpty())
 
249
      {
 
250
        writeStringValue(t,m_valueString);
 
251
      }
 
252
      else
 
253
      {
 
254
        writeIntValue(t,m_value);
 
255
      }
 
256
      t << "\n";
 
257
    }
 
258
    void init() { m_value = m_defValue; }
 
259
  private:
 
260
    int m_value;
 
261
    int m_defValue;
 
262
    int m_minVal;
 
263
    int m_maxVal;
 
264
    QCString m_valueString;
 
265
};
 
266
 
 
267
/*! \brief Option of the boolean type.
 
268
 *
 
269
 */
 
270
class ConfigBool : public ConfigOption
 
271
{
 
272
  public:
 
273
    ConfigBool(const char *name,const char *doc,bool defVal) 
 
274
      : ConfigOption(O_Bool)
 
275
    {
 
276
      m_name = name;
 
277
      m_doc = doc;
 
278
      m_value = defVal;
 
279
      m_defValue = defVal;
 
280
    }
 
281
    QCString *valueStringRef() { return &m_valueString; }
 
282
    bool *valueRef() { return &m_value; }
 
283
    void convertStrToVal();
 
284
    void substEnvVars();
 
285
    void setValueString(const QCString &v) { m_valueString = v; }
 
286
    void writeTemplate(QTextStream &t,bool sl,bool upd)
 
287
    {
 
288
      if (!sl)
 
289
      {
 
290
        t << endl;
 
291
        t << convertToComment(m_doc);
 
292
        t << endl;
 
293
      }
 
294
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
 
295
      if (upd && !m_valueString.isEmpty())
 
296
      {
 
297
        writeStringValue(t,m_valueString);
 
298
      }
 
299
      else
 
300
      {
 
301
        writeBoolValue(t,m_value);
 
302
      }
 
303
      t << "\n";
 
304
    }
 
305
    void init() { m_value = m_defValue; }
 
306
  private:
 
307
    bool m_value;
 
308
    bool m_defValue;
 
309
    QCString m_valueString;
 
310
};
 
311
 
 
312
/*! \brief Section marker for obsolete options
 
313
 *
 
314
 */
 
315
class ConfigObsolete : public ConfigOption
 
316
{
 
317
  public:
 
318
    ConfigObsolete(OptionType t) : ConfigOption(t)  {}
 
319
    void writeTemplate(QTextStream &,bool,bool) {}
 
320
    void substEnvVars() {}
 
321
};
 
322
 
 
323
 
 
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)
 
330
 
 
331
/*! \brief Singleton for configuration variables.
 
332
 *
 
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.
 
337
 *  
 
338
 *  Set all variables to their default values by
 
339
 *  calling Config::instance()->init()
 
340
 *
 
341
 */
 
342
class Config
 
343
{
 
344
  public:
 
345
    /////////////////////////////
 
346
    // public API
 
347
    /////////////////////////////
 
348
 
 
349
    /*! Returns the one and only instance of this class */
 
350
    static Config *instance()
 
351
    {
 
352
      if (m_instance==0) m_instance = new Config;
 
353
      return m_instance;
 
354
    }
 
355
    /*! Delete the instance */
 
356
    static void deleteInstance()
 
357
    {
 
358
      delete m_instance;
 
359
    }
 
360
    
 
361
    /*! Returns an iterator that can by used to iterate over the 
 
362
     *  configuration options.
 
363
     */
 
364
    QPtrListIterator<ConfigOption> iterator()
 
365
    {
 
366
      return QPtrListIterator<ConfigOption>(*m_options);
 
367
    }
 
368
 
 
369
    /*! 
 
370
     *  @name Getting configuration values.
 
371
     *  @{
 
372
     */
 
373
 
 
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.
 
377
     */
 
378
    QCString &getString(const char *fileName,int num,const char *name) const;
 
379
 
 
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.
 
383
     */
 
384
    QStrList &getList(const char *fileName,int num,const char *name) const;
 
385
 
 
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.
 
389
     */
 
390
    QCString &getEnum(const char *fileName,int num,const char *name) const;
 
391
 
 
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.
 
395
     */
 
396
    int      &getInt(const char *fileName,int num,const char *name) const;
 
397
 
 
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.
 
401
     */
 
402
    bool     &getBool(const char *fileName,int num,const char *name) const;
 
403
 
 
404
    /*! Returns the ConfigOption corresponding with \a name or 0 if
 
405
     *  the option is not supported.
 
406
     */
 
407
    ConfigOption *get(const char *name) const
 
408
    {
 
409
      return m_dict->find(name); 
 
410
    }
 
411
    /* @} */
 
412
 
 
413
    /*! 
 
414
     *  @name Adding configuration options. 
 
415
     *  @{
 
416
     */
 
417
 
 
418
    /*! Starts a new configuration section with \a name and description \a doc.
 
419
     *  \returns An object representing the option.
 
420
     */
 
421
    ConfigInfo   *addInfo(const char *name,const char *doc)
 
422
    {
 
423
      ConfigInfo *result = new ConfigInfo(name,doc);
 
424
      m_options->append(result);
 
425
      return result;
 
426
    }
 
427
 
 
428
    /*! Adds a new string option with \a name and documentation \a doc.
 
429
     *  \returns An object representing the option.
 
430
     */
 
431
    ConfigString *addString(const char *name,
 
432
                            const char *doc)
 
433
    {
 
434
      ConfigString *result = new ConfigString(name,doc);
 
435
      m_options->append(result);
 
436
      m_dict->insert(name,result);
 
437
      return result;
 
438
    }
 
439
 
 
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.
 
443
     */
 
444
    ConfigEnum   *addEnum(const char *name,
 
445
                          const char *doc,
 
446
                          const char *defVal)
 
447
    {
 
448
      ConfigEnum *result = new ConfigEnum(name,doc,defVal);
 
449
      m_options->append(result);
 
450
      m_dict->insert(name,result);
 
451
      return result;
 
452
    }
 
453
 
 
454
    /*! Adds a new string option with \a name and documentation \a doc.
 
455
     *  \returns An object representing the option.
 
456
     */
 
457
    ConfigList   *addList(const char *name,
 
458
                          const char *doc)
 
459
    {
 
460
      ConfigList *result = new ConfigList(name,doc);
 
461
      m_options->append(result);
 
462
      m_dict->insert(name,result);
 
463
      return result;
 
464
    }
 
465
 
 
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.
 
470
     */
 
471
    ConfigInt    *addInt(const char *name,
 
472
                         const char *doc,
 
473
                         int minVal,int maxVal,int defVal)
 
474
    {
 
475
      ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
 
476
      m_options->append(result);
 
477
      m_dict->insert(name,result);
 
478
      return result;
 
479
    }
 
480
 
 
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.
 
484
     */
 
485
    ConfigBool   *addBool(const char *name,
 
486
                          const char *doc,
 
487
                          bool defVal)
 
488
    {
 
489
      ConfigBool *result = new ConfigBool(name,doc,defVal);
 
490
      m_options->append(result);
 
491
      m_dict->insert(name,result);
 
492
      return result;
 
493
    }
 
494
    /*! Adds an option that has become obsolete. */
 
495
    ConfigOption *addObsolete(const char *name)
 
496
    {
 
497
      ConfigObsolete *option = new ConfigObsolete(ConfigOption::O_Obsolete);
 
498
      m_dict->insert(name,option);
 
499
      m_obsolete->append(option);
 
500
      return option;
 
501
    }
 
502
    /*! @} */
 
503
 
 
504
    /*! Writes a template configuration file to \a f. If \a shortIndex
 
505
     *  is \c TRUE the description of each configuration option will
 
506
     *  be omitted.
 
507
     */
 
508
    void writeTemplate(QFile *f,bool shortIndex,bool updateOnly);
 
509
 
 
510
    /////////////////////////////
 
511
    // internal API
 
512
    /////////////////////////////
 
513
 
 
514
    /*! Converts the string values read from the configuration file
 
515
     *  to real values for non-string type options (like int, and bools)
 
516
     */
 
517
    void convertStrToVal();
 
518
 
 
519
    /*! Replaces references to environment variable by the actual value
 
520
     *  of the environment variable.
 
521
     */
 
522
    void substituteEnvironmentVars();
 
523
 
 
524
    /*! Checks if the values of the variable are correct, adjusts them
 
525
     *  if needed, and report any errors.
 
526
     */
 
527
    void check();
 
528
 
 
529
    /*! Initialize config variables to their default value */
 
530
    void init();
 
531
 
 
532
    /*! Parse a configuration file with name \a fn.
 
533
     *  \returns TRUE if successful, FALSE if the file could not be 
 
534
     *  opened or read.
 
535
     */ 
 
536
    bool parse(const char *fn);
 
537
 
 
538
    /*! Called from the constructor, will add doxygen's default options
 
539
     *  to the configuration object 
 
540
     */
 
541
    void create();
 
542
 
 
543
  protected:
 
544
 
 
545
    Config()
 
546
    { 
 
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;
 
553
      create();
 
554
    }
 
555
   ~Config()
 
556
    {
 
557
      delete m_options;
 
558
      delete m_obsolete;
 
559
      delete m_dict;
 
560
    }
 
561
 
 
562
  private:
 
563
    QPtrList<ConfigOption> *m_options;
 
564
    QPtrList<ConfigOption> *m_obsolete;
 
565
    QDict<ConfigOption> *m_dict;
 
566
    static Config *m_instance;
 
567
    bool m_initialized;
 
568
};
 
569
 
 
570
#endif