config.h

Go to the documentation of this file.
00001 #ifndef CONFIG_H
00002 #define CONFIG_H
00003 
00004 #include "qtbc.h"
00005 #include <qstrlist.h>
00006 #include <qfile.h>
00007 #include <qdict.h>
00008 #include <qlist.h>
00009 #include <qtextstream.h>
00010 
00014 class ConfigOption
00015 {
00016     friend class Config;
00017 
00018   public:
00019 
00021     enum OptionType 
00022     { 
00023       O_Info,      //<! A section header
00024       O_List,      //<! A list of items
00025       O_Enum,      //<! A fixed set of items
00026       O_String,    //<! A single item
00027       O_Int,       //<! An integer value
00028       O_Bool,      //<! A boolean value
00029       O_Obsolete   //<! An obsolete option
00030     };
00031     enum 
00032     { 
00036       MAX_OPTION_LENGTH = 23  
00037     };
00038     ConfigOption(OptionType t) : m_kind(t) 
00039     {
00040       m_spaces.fill(' ',40);
00041     }
00042     virtual ~ConfigOption()
00043     {
00044     }
00045 
00047     OptionType kind() const { return m_kind; }
00048     QCString name() const { return m_name; }
00049     QCString docs() const { return m_doc; }
00050 
00051     QCString dependsOn() const { return m_dependency; }
00052     void addDependency(const char *dep) { m_dependency = dep; }
00053     void setEncoding(const QCString &e) { m_encoding = e; }
00054 
00055   protected:
00056     virtual void writeTemplate(QTextStream &t,bool sl,bool upd) = 0;
00057     virtual void convertStrToVal() {}
00058     virtual void substEnvVars() = 0;
00059     virtual void init() {}
00060 
00061     QCString convertToComment(const QCString &s);
00062     void writeBoolValue(QTextStream &t,bool v);
00063     void writeIntValue(QTextStream &t,int i);
00064     void writeStringValue(QTextStream &t,QCString &s);
00065     void writeStringList(QTextStream &t,QStrList &l);
00066 
00067     QCString m_spaces;
00068     QCString m_name;
00069     QCString m_doc;
00070     QCString m_dependency;
00071     QCString m_encoding;
00072     OptionType m_kind;
00073 };
00074 
00078 class ConfigInfo : public ConfigOption
00079 {
00080   public:
00081     ConfigInfo(const char *name,const char *doc) 
00082       : ConfigOption(O_Info)
00083     {
00084       m_name = name;
00085       m_doc = doc;
00086     }
00087     void writeTemplate(QTextStream &t, bool sl,bool)
00088     {
00089       if (!sl)
00090       {
00091         t << "\n";
00092       }
00093       t << "#---------------------------------------------------------------------------\n";
00094       t << "# " << m_doc << endl;
00095       t << "#---------------------------------------------------------------------------\n";
00096     }
00097     void substEnvVars() {}
00098 };
00099 
00103 class ConfigList : public ConfigOption
00104 {
00105   public:
00106     enum WidgetType { String, File, Dir, FileAndDir };
00107     ConfigList(const char *name,const char *doc) 
00108       : ConfigOption(O_List)
00109     {
00110       m_name = name;
00111       m_doc = doc;
00112       m_widgetType = String;
00113     }
00114     void addValue(const char *v) { m_value.append(v); }
00115     void setWidgetType(WidgetType w) { m_widgetType = w; }
00116     WidgetType widgetType() const { return m_widgetType; }
00117     QStrList *valueRef() { return &m_value; }
00118     void writeTemplate(QTextStream &t,bool sl,bool)
00119     {
00120       if (!sl)
00121       {
00122         t << endl;
00123         t << convertToComment(m_doc);
00124         t << endl;
00125       }
00126       t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
00127       writeStringList(t,m_value);
00128       t << "\n";
00129     }
00130     void substEnvVars();
00131     void init() { m_value.clear(); }
00132   private:
00133     QStrList m_value;
00134     WidgetType m_widgetType;
00135 };
00136 
00140 class ConfigEnum : public ConfigOption
00141 {
00142   public:
00143     ConfigEnum(const char *name,const char *doc,const char *defVal) 
00144       : ConfigOption(O_Enum)
00145     {
00146       m_name = name;
00147       m_doc = doc;
00148       m_value = defVal;
00149       m_defValue = defVal;
00150     }
00151     void addValue(const char *v) { m_valueRange.append(v); }
00152     QStrListIterator iterator() 
00153     {
00154       return QStrListIterator(m_valueRange);
00155     }
00156     QCString *valueRef() { return &m_value; }
00157     void substEnvVars();
00158     void writeTemplate(QTextStream &t,bool sl,bool)
00159     {
00160       if (!sl)
00161       {
00162         t << endl;
00163         t << convertToComment(m_doc);
00164         t << endl;
00165       }
00166       t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
00167       writeStringValue(t,m_value);
00168       t << "\n";
00169     }
00170     void init() { m_value = m_defValue.copy(); }
00171 
00172   private:
00173     QStrList m_valueRange;
00174     QCString m_value;
00175     QCString m_defValue;
00176 };
00177 
00181 class ConfigString : public ConfigOption
00182 {
00183   public:
00184     enum WidgetType { String, File, Dir };
00185     ConfigString(const char *name,const char *doc) 
00186       : ConfigOption(O_String)
00187     {
00188       m_name = name;
00189       m_doc = doc;
00190       m_widgetType = String;
00191     }
00192    ~ConfigString()
00193     {
00194     }
00195     void setWidgetType(WidgetType w) { m_widgetType = w; }
00196     WidgetType widgetType() const { return m_widgetType; }
00197     void setDefaultValue(const char *v) { m_defValue = v; }
00198     QCString *valueRef() { return &m_value; }
00199     void writeTemplate(QTextStream &t,bool sl,bool)
00200     {
00201       if (!sl)
00202       {
00203         t << endl;
00204         t << convertToComment(m_doc);
00205         t << endl;
00206       }
00207       t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
00208       writeStringValue(t,m_value);
00209       t << "\n";
00210     }
00211     void substEnvVars();
00212     void init() { m_value = m_defValue.copy(); }
00213   
00214   private:
00215     QCString m_value;
00216     QCString m_defValue;
00217     WidgetType m_widgetType;
00218 };
00219 
00223 class ConfigInt : public ConfigOption
00224 {
00225   public:
00226     ConfigInt(const char *name,const char *doc,int minVal,int maxVal,int defVal) 
00227       : ConfigOption(O_Int)
00228     {
00229       m_name = name;
00230       m_doc = doc;
00231       m_value = defVal;
00232       m_defValue = defVal;
00233       m_minVal = minVal;
00234       m_maxVal = maxVal;
00235     }
00236     QCString *valueStringRef() { return &m_valueString; }
00237     int *valueRef() { return &m_value; }
00238     int minVal() const { return m_minVal; }
00239     int maxVal() const { return m_maxVal; }
00240     void convertStrToVal();
00241     void substEnvVars();
00242     void writeTemplate(QTextStream &t,bool sl,bool upd)
00243     {
00244       if (!sl)
00245       {
00246         t << endl;
00247         t << convertToComment(m_doc);
00248         t << endl;
00249       }
00250       t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
00251       if (upd && !m_valueString.isEmpty())
00252       {
00253         writeStringValue(t,m_valueString);
00254       }
00255       else
00256       {
00257         writeIntValue(t,m_value);
00258       }
00259       t << "\n";
00260     }
00261     void init() { m_value = m_defValue; }
00262   private:
00263     int m_value;
00264     int m_defValue;
00265     int m_minVal;
00266     int m_maxVal;
00267     QCString m_valueString;
00268 };
00269 
00273 class ConfigBool : public ConfigOption
00274 {
00275   public:
00276     ConfigBool(const char *name,const char *doc,bool defVal) 
00277       : ConfigOption(O_Bool)
00278     {
00279       m_name = name;
00280       m_doc = doc;
00281       m_value = defVal;
00282       m_defValue = defVal;
00283     }
00284     QCString *valueStringRef() { return &m_valueString; }
00285     bool *valueRef() { return &m_value; }
00286     void convertStrToVal();
00287     void substEnvVars();
00288     void setValueString(const QCString &v) { m_valueString = v; }
00289     void writeTemplate(QTextStream &t,bool sl,bool upd)
00290     {
00291       if (!sl)
00292       {
00293         t << endl;
00294         t << convertToComment(m_doc);
00295         t << endl;
00296       }
00297       t << m_name << m_spaces.left(MAX_OPTION_LENGTH-m_name.length()) << "= ";
00298       if (upd && !m_valueString.isEmpty())
00299       {
00300         writeStringValue(t,m_valueString);
00301       }
00302       else
00303       {
00304         writeBoolValue(t,m_value);
00305       }
00306       t << "\n";
00307     }
00308     void init() { m_value = m_defValue; }
00309   private:
00310     bool m_value;
00311     bool m_defValue;
00312     QCString m_valueString;
00313 };
00314 
00318 class ConfigObsolete : public ConfigOption
00319 {
00320   public:
00321     ConfigObsolete(OptionType t) : ConfigOption(t)  {}
00322     void writeTemplate(QTextStream &,bool,bool) {}
00323     void substEnvVars() {}
00324 };
00325 
00326 
00327 // some convenience macros
00328 #define Config_getString(val)  Config::instance()->getString(__FILE__,__LINE__,val)
00329 #define Config_getInt(val)     Config::instance()->getInt(__FILE__,__LINE__,val)
00330 #define Config_getList(val)    Config::instance()->getList(__FILE__,__LINE__,val)
00331 #define Config_getEnum(val)    Config::instance()->getEnum(__FILE__,__LINE__,val)
00332 #define Config_getBool(val)    Config::instance()->getBool(__FILE__,__LINE__,val)
00333 
00345 class Config
00346 {
00347   public:
00349     // public API
00351 
00353     static Config *instance()
00354     {
00355       if (m_instance==0) m_instance = new Config;
00356       return m_instance;
00357     }
00359     static void deleteInstance()
00360     {
00361       delete m_instance;
00362       m_instance=0;
00363     }
00364     
00368     QListIterator<ConfigOption> iterator()
00369     {
00370       return QListIterator<ConfigOption>(*m_options);
00371     }
00372 
00382     QCString &getString(const char *fileName,int num,const char *name) const;
00383 
00388     QStrList &getList(const char *fileName,int num,const char *name) const;
00389 
00394     QCString &getEnum(const char *fileName,int num,const char *name) const;
00395 
00400     int      &getInt(const char *fileName,int num,const char *name) const;
00401 
00406     bool     &getBool(const char *fileName,int num,const char *name) const;
00407 
00411     ConfigOption *get(const char *name) const
00412     {
00413       return m_dict->find(name); 
00414     }
00415     /* @} */
00416 
00425     ConfigInfo   *addInfo(const char *name,const char *doc)
00426     {
00427       ConfigInfo *result = new ConfigInfo(name,doc);
00428       m_options->append(result);
00429       return result;
00430     }
00431 
00435     ConfigString *addString(const char *name,
00436                             const char *doc)
00437     {
00438       ConfigString *result = new ConfigString(name,doc);
00439       m_options->append(result);
00440       m_dict->insert(name,result);
00441       return result;
00442     }
00443 
00448     ConfigEnum   *addEnum(const char *name,
00449                           const char *doc,
00450                           const char *defVal)
00451     {
00452       ConfigEnum *result = new ConfigEnum(name,doc,defVal);
00453       m_options->append(result);
00454       m_dict->insert(name,result);
00455       return result;
00456     }
00457 
00461     ConfigList   *addList(const char *name,
00462                           const char *doc)
00463     {
00464       ConfigList *result = new ConfigList(name,doc);
00465       m_options->append(result);
00466       m_dict->insert(name,result);
00467       return result;
00468     }
00469 
00475     ConfigInt    *addInt(const char *name,
00476                          const char *doc,
00477                          int minVal,int maxVal,int defVal)
00478     {
00479       ConfigInt *result = new ConfigInt(name,doc,minVal,maxVal,defVal);
00480       m_options->append(result);
00481       m_dict->insert(name,result);
00482       return result;
00483     }
00484 
00489     ConfigBool   *addBool(const char *name,
00490                           const char *doc,
00491                           bool defVal)
00492     {
00493       ConfigBool *result = new ConfigBool(name,doc,defVal);
00494       m_options->append(result);
00495       m_dict->insert(name,result);
00496       return result;
00497     }
00499     ConfigOption *addObsolete(const char *name)
00500     {
00501       ConfigObsolete *option = new ConfigObsolete(ConfigOption::O_Obsolete);
00502       m_dict->insert(name,option);
00503       m_obsolete->append(option);
00504       return option;
00505     }
00512     void writeTemplate(QTextStream &t,bool shortIndex,bool updateOnly);
00513 
00515     // internal API
00517 
00521     void convertStrToVal();
00522 
00526     void substituteEnvironmentVars();
00527 
00531     void check();
00532 
00534     void init();
00535 
00540     bool parseString(const char *fn,const char *str);
00541 
00546     bool parse(const char *fn);
00547 
00551     void create();
00552 
00553   protected:
00554 
00555     Config()
00556     { 
00557       m_options  = new QList<ConfigOption>;
00558       m_obsolete = new QList<ConfigOption>;
00559       m_dict     = new QDict<ConfigOption>(257);
00560       m_options->setAutoDelete(TRUE);
00561       m_obsolete->setAutoDelete(TRUE);
00562       m_initialized = FALSE;
00563       create();
00564     }
00565    ~Config()
00566     {
00567       delete m_options;
00568       delete m_obsolete;
00569       delete m_dict;
00570     }
00571 
00572   private:
00573     QList<ConfigOption> *m_options;
00574     QList<ConfigOption> *m_obsolete;
00575     QDict<ConfigOption> *m_dict;
00576     static Config *m_instance;
00577     bool m_initialized;
00578 };
00579 
00580 #endif



Generated on Mon Mar 31 10:58:34 2008 by  doxygen 1.5.1