summaryrefslogtreecommitdiffstats
path: root/src/config.h
blob: 9d5bbf3795adbcef49f03f866540e11fe6879dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#ifndef CONFIG_H
#define CONFIG_H

#include "qtbc.h"
#include <qstrlist.h>
#include <qfile.h>
#include <qdict.h>
#include <qlist.h>
#include <qtextstream.h>


/*! \brief Abstract base class for any configuration option.
 *
 */
class ConfigOption
{
  public:

    enum OptionType { O_Info, O_List, O_Enum, O_String, O_Int, O_Bool };
    enum { MAX_OPTION_LENGTH = 23 };
    ConfigOption(OptionType t) : m_kind(t) 
    {
      m_spaces.fill(' ',40);
    }
    virtual ~ConfigOption()
    {
    }
    void addDependency(const char *dep)
    {
      m_dependency = dep;
    }
    OptionType kind() const { return m_kind; }
    virtual void writeTemplate(QTextStream &t,bool sl) = 0;
    virtual void convertStrToVal() {}
    virtual void substEnvVars() {}
    virtual void init() {}
    QCString name() const { return m_name; }
    QCString docs() const { return m_doc; }
    QCString dependsOn() const { return m_dependency; }

  protected:

    QCString convertToComment(const QCString &s)
    {
      QCString result;
      if (s.isEmpty()) return result;
      else
      {
        result+="# ";
        QCString tmp=s.stripWhiteSpace();
        char *p=tmp.data();
        char c;
        while ((c=*p++))
        {
          if (c=='\n') result+="\n# ";
          else result+=c;
        }
        result+='\n';
      }
      return result;
    }

    void writeBoolValue(QTextStream &t,bool v)
    {
      if (v) t << "YES"; else t << "NO";
    }

    void writeIntValue(QTextStream &t,int i)
    {
      t << i;
    }

    void writeStringValue(QTextStream &t,QCString &s)
    {
      const char *p=s.data();
      char c;
      bool hasBlanks=FALSE;
      if (p)
      {
        while ((c=*p++)!=0 && !hasBlanks) hasBlanks = (c==' ' || c=='\n' || c=='\t');
        if (hasBlanks) 
          t << "\"" << s << "\"";
        else
          t << s;
      }
    }

    void writeStringList(QTextStream &t,QStrList &l)
    {
      const char *p = l.first();
      bool first=TRUE;
      while (p)
      {
        char c;
        const char *s=p;
        bool hasBlanks=FALSE;
        while ((c=*p++)!=0 && !hasBlanks) hasBlanks = (c==' ' || c=='\n' || c=='\t');
        if (!first) t << "                         ";
        first=FALSE;
        if (hasBlanks) t << "\"" << s << "\""; else t << s;
        p = l.next();
        if (p) t << " \\" << endl;
      }
    }

    QCString m_spaces;
    QCString m_name;
    QCString m_doc;
    QCString m_dependency;
    OptionType m_kind;
};

/*! \brief Section marker for grouping the configuration options
 *
 */
class ConfigInfo : public ConfigOption
{
  public:
    ConfigInfo(const char *name,const char *doc) 
      : ConfigOption(O_Info)
    {
      m_name = name;
      m_doc = doc;
    }
    void writeTemplate(QTextStream &t, bool sl)
    {
      if (!sl)
      {
        t << "\n";
      }
      t << "#---------------------------------------------------------------------------\n";
      t << "# " << m_doc << endl;
      t << "#---------------------------------------------------------------------------\n";
    }
};

/*! \brief Option of the list type.
 *
 */
class ConfigList : public ConfigOption
{
  public:
    enum WidgetType { String, File, Dir, FileAndDir };
    ConfigList(const char *name,const char *doc) 
      : ConfigOption(O_List)
    {
      m_name = name;
      m_doc = doc;
      m_widgetType = String;
    }
    void addValue(const char *v) { m_value.append(v); }
    void setWidgetType(WidgetType w) { m_widgetType = w; }
    WidgetType widgetType() const { return m_widgetType; }
    QStrList *valueRef() { return &m_value; }
    void writeTemplate(QTextStream &t,bool sl)
    {
      if (!sl)
      {
        t << endl;
        t << convertToComment(m_doc);
        t << endl;
      }
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
      writeStringList(t,m_value);
      t << "\n";
    }
    void substEnvVars();
  private:
    QStrList m_value;
    WidgetType m_widgetType;
};

/*! \brief Option of the enum type.
 *
 */
class ConfigEnum : public ConfigOption
{
  public:
    ConfigEnum(const char *name,const char *doc,const char *defVal) 
      : ConfigOption(O_Enum)
    {
      m_name = name;
      m_doc = doc;
      m_value = defVal;
      m_defValue = defVal;
    }
    void addValue(const char *v) { m_valueRange.append(v); }
    QStrListIterator iterator() 
    {
      return QStrListIterator(m_valueRange);
    }
    QCString *valueRef() { return &m_value; }
    void writeTemplate(QTextStream &t,bool sl)
    {
      if (!sl)
      {
        t << endl;
        t << convertToComment(m_doc);
        t << endl;
      }
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
      writeStringValue(t,m_value);
      t << "\n";
    }
    void init() { m_value = m_defValue.copy(); }

  private:
    QStrList m_valueRange;
    QCString m_value;
    QCString m_defValue;
};

/*! \brief Option of the string type.
 *
 */
class ConfigString : public ConfigOption
{
  public:
    enum WidgetType { String, File, Dir };
    ConfigString(const char *name,const char *doc) 
      : ConfigOption(O_String)
    {
      m_name = name;
      m_doc = doc;
      m_widgetType = String;
    }
   ~ConfigString()
    {
    }
    void setWidgetType(WidgetType w) { m_widgetType = w; }
    WidgetType widgetType() const { return m_widgetType; }
    void setDefaultValue(const char *v) { m_defValue = v; }
    QCString *valueRef() { return &m_value; }
    void writeTemplate(QTextStream &t,bool sl)
    {
      if (!sl)
      {
        t << endl;
        t << convertToComment(m_doc);
        t << endl;
      }
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
      writeStringValue(t,m_value);
      t << "\n";
    }
    void substEnvVars();
    void init() { m_value = m_defValue.copy(); }
  
  private:
    QCString m_value;
    QCString m_defValue;
    WidgetType m_widgetType;
};

/*! \brief Option of the integer type.
 *
 */
class ConfigInt : public ConfigOption
{
  public:
    ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal) 
      : ConfigOption(O_Int)
    {
      m_name = name;
      m_doc = doc;
      m_value = defVal;
      m_defValue = defVal;
      m_minVal = minVal;
      m_maxVal = maxVal;
    }
    QCString *valueStringRef() { return &m_valueString; }
    int *valueRef() { return &m_value; }
    int minVal() const { return m_minVal; }
    int maxVal() const { return m_maxVal; }
    void convertStrToVal();
    void writeTemplate(QTextStream &t,bool sl)
    {
      if (!sl)
      {
        t << endl;
        t << convertToComment(m_doc);
        t << endl;
      }
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
      writeIntValue(t,m_value);
      t << "\n";
    }
    void init() { m_value = m_defValue; }
  private:
    int m_value;
    int m_defValue;
    int m_minVal;
    int m_maxVal;
    QCString m_valueString;
};

/*! \brief Option of the boolean type.
 *
 */
class ConfigBool : public ConfigOption
{
  public:
    ConfigBool(const char *name,const char *doc,bool defVal) 
      : ConfigOption(O_Bool)
    {
      m_name = name;
      m_doc = doc;
      m_value = defVal;
      m_defValue = defVal;
    }
    bool *valueRef() { return &m_value; }
    void writeTemplate(QTextStream &t,bool sl)
    {
      if (!sl)
      {
        t << endl;
        t << convertToComment(m_doc);
        t << endl;
      }
      t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
      writeBoolValue(t,m_value);
      t << "\n";
    }
    void init() { m_value = m_defValue; }
  private:
    bool m_value;
    bool m_defValue;
};

#define Config_getString(val)  Config::instance()->getString(__FILE__,__LINE__,val)
#define Config_getInt(val)     Config::instance()->getInt(__FILE__,__LINE__,val)
#define Config_getList(val)    Config::instance()->getList(__FILE__,__LINE__,val)
#define Config_getEnum(val)    Config::instance()->getEnum(__FILE__,__LINE__,val)
#define Config_getBool(val)    Config::instance()->getBool(__FILE__,__LINE__,val)

/*! \brief Singleton for configuration variables.
 *
 *  This object holds the global static variables
 *  read from a user-supplied configuration file.
 *  The static member instance() can be used to get
 *  a pointer to the one and only instance.
 */
struct Config
{
  public:
    static Config *instance()
    {
      if (m_instance==0) m_instance = new Config;
      return m_instance;
    }
    
    void writeTemplate(QFile *f,bool shortIndex);
    void convertStrToVal();
    void substituteEnvironmentVars();
    void check();
    void init();
    void parse(const QCString &config,const char *fn);
    QListIterator<ConfigOption> iterator()
    {
      return QListIterator<ConfigOption>(*m_options);
    }

    ////////////////////////
    // get functions
    ////////////////////////
    QCString &getString(const char *fileName,int num,const char *name) const;
    QStrList &getList(const char *fileName,int num,const char *name) const;
    QCString &getEnum(const char *fileName,int num,const char *name) const;
    int      &getInt(const char *fileName,int num,const char *name) const;
    bool     &getBool(const char *fileName,int num,const char *name) const;
    ConfigOption *get(const char *name) const
    {
      return m_dict->find(name); 
    }

    ////////////////////////
    // add functions
    ////////////////////////
    ConfigInfo   *addInfo(const char *name,const char *doc)
    {
      ConfigInfo *result = new ConfigInfo(name,doc);
      m_options->append(result);
      return result;
    }
    ConfigString *addString(const char *name,
                            const char *doc)
    {
      ConfigString *result = new ConfigString(name,doc);
      m_options->append(result);
      m_dict->insert(name,result);
      return result;
    }
    ConfigEnum   *addEnum(const char *name,
                          const char *doc,
                          const char *defVal)
    {
      ConfigEnum *result = new ConfigEnum(name,doc,defVal);
      m_options->append(result);
      m_dict->insert(name,result);
      return result;
    }
    ConfigList   *addList(const char *name,
                          const char *doc)
    {
      ConfigList *result = new ConfigList(name,doc);
      m_options->append(result);
      m_dict->insert(name,result);
      return result;
    }
    ConfigInt    *addInt(const char *name,
                         const char *doc,
                         int minVal,int maxVal,int defVal)
    {
      ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
      m_options->append(result);
      m_dict->insert(name,result);
      return result;
    }
    ConfigBool   *addBool(const char *name,
                          const char *doc,
                          bool defVal)
    {
      ConfigBool *result = new ConfigBool(name,doc,defVal);
      m_options->append(result);
      m_dict->insert(name,result);
      return result;
    }
  protected:
    Config()
    { 
      m_options = new QList<ConfigOption>;
      m_dict = new QDict<ConfigOption>(257);
      m_options->setAutoDelete(TRUE);
      m_initialized = FALSE;
      create();
    }
   ~Config()
    {
      delete m_options;
      delete m_dict;
    }
    void create();

  private:
    QList<ConfigOption> *m_options;
    QDict<ConfigOption> *m_dict;
    static Config *m_instance;
    bool m_initialized;
};

#endif