00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef _APPCONF_H
00040 #define _APPCONF_H
00041
00042
00098
00099 #if APPCONF_USE_CONFIG_H
00100 # include <config.h>
00101 #endif
00102
00103 #include <iostream>
00104 #include <stdlib.h>
00105
00106
00107 typedef int Bool;
00108 #ifndef TRUE
00109 # define TRUE 1
00110 # define FALSE 0
00111 #endif
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00124
00126
00127 #ifndef APPCONF_USE_GETTEXT
00128 # define APPCONF_USE_GETTEXT 0
00129 #else
00130 # define APPCONF_DOMAIN "appconf"
00131 #endif
00132
00134 #ifndef APPCONF_CASE_SENSITIVE
00135 # define APPCONF_CASE_SENSITIVE 0
00136 #endif
00137
00139 #ifndef APPCONF_PATH_SEPARATOR
00140 # define APPCONF_PATH_SEPARATOR '/'
00141 #endif
00142
00144 #ifndef APPCONF_IMMUTABLE_PREFIX
00145 # define APPCONF_IMMUTABLE_PREFIX '!'
00146 #endif
00147
00149 #ifndef APPCONF_STRBUFLEN
00150 # define APPCONF_STRBUFLEN 1024
00151 #endif
00152
00154 #ifndef APPCONF_WIN32_NATIVE
00155 # define APPCONF_WIN32_NATIVE 1 // default: TRUE
00156 #endif
00157
00159
00162
00163
00164
00172 char *ExpandEnvVars(const char *psz);
00174
00177
00178
00180
00181 class BaseConfig
00182 {
00183 public:
00186
00187 BaseConfig();
00189 virtual ~BaseConfig();
00191
00198 virtual void setCurrentPath(const char *szPath = "");
00199
00205 virtual void changeCurrentPath(const char *szPath = "");
00206
00211 const char *getCurrentPath() const;
00218 static char *normalizePath(const char *szStartPath, const char *szPath);
00220
00224 void recordDefaults(Bool enable = TRUE);
00225
00233 class Enumerator
00234 {
00235 public:
00236
00237 Enumerator(size_t nCount, Bool bOwnsStrings);
00238 ~Enumerator();
00239
00240
00241
00242 void AddString(const char *sz);
00243
00244 void AddString(char *sz);
00245
00246
00247 void MakeUnique();
00248
00249
00251
00253
00254
00255 private:
00256 char **m_aszData;
00257 size_t m_nCount;
00258 Bool m_bOwnsStrings;
00259 };
00277 virtual Enumerator *enumSubgroups() const = 0;
00283 virtual Enumerator *enumEntries() const = 0;
00285
00294 virtual const char *readEntry(const char *szKey,
00295 const char *szDefault = NULL) const = 0;
00296
00304 long int readEntry(const char *szKey, long int Default) const;
00305
00313 double readEntry(const char *szKey, double Default) const;
00314
00321 virtual Bool writeEntry(const char *szKey, const char *szValue) = 0;
00322
00329 Bool writeEntry(const char *szKey, long int Value);
00330
00337 Bool writeEntry(const char *szKey, double Value);
00338
00348 virtual Bool deleteEntry(const char *szKey) = 0;
00350
00353
00354 virtual Bool flush(Bool = FALSE) { return TRUE; }
00356 Bool isInitialized() const { return m_bOk; }
00358
00368
00369 static char *filterOut(const char *szValue);
00371 static char *filterIn(const char *szValue);
00373 void expandVariables(Bool bExpand = TRUE) { m_bExpandVariables = bExpand; }
00375 Bool doesExpandVariables(void) const { return m_bExpandVariables; }
00377
00378 protected:
00380 Bool m_bOk;
00382 Bool m_bExpandVariables;
00384 Bool m_bRecordDefaults;
00385 private:
00386 char *m_szCurrentPath;
00387 };
00388
00389
00434
00435
00436 class FileConfig : public BaseConfig
00437 {
00438 public:
00455 FileConfig(const char *szFileName, Bool bLocalOnly = FALSE,
00456 Bool bUseSubDir = FALSE);
00462 FileConfig(std::istream *iStream);
00467 FileConfig(void);
00468
00474 ~FileConfig();
00476
00482 void readFile(const char *szFileName);
00483
00486
00487 const char *readEntry(const char *szKey, const char *szDefault = NULL) const;
00489 long int readEntry(const char *szKey, long int Default) const
00490 { return BaseConfig::readEntry(szKey, Default); }
00492 double readEntry(const char *szKey, double Default) const
00493 { return BaseConfig::readEntry(szKey, Default); }
00495 Bool writeEntry(const char *szKey, const char *szValue);
00497 Bool writeEntry(const char *szKey, long int Value)
00498 { return BaseConfig::writeEntry(szKey, Value);}
00500 Bool writeEntry(const char *szKey, double Value)
00501 { return BaseConfig::writeEntry(szKey, Value); }
00503 Bool deleteEntry(const char *szKey);
00505 Bool flush(Bool bCurrentOnly = FALSE);
00507 Bool flush(std::ostream *oStream, Bool = FALSE);
00508
00510 Bool parseLine(const char *psz);
00511
00512
00514 void changeCurrentPath(const char *szPath = "");
00516
00522
00523 Enumerator *enumSubgroups() const;
00525 Enumerator *enumEntries() const;
00527
00528
00529
00530 class ConfigGroup;
00531 class ConfigEntry
00532 {
00533 private:
00534 ConfigGroup *m_pParent;
00535 ConfigEntry *m_pNext;
00536 char *m_szName,
00537 *m_szValue,
00538 *m_szExpValue,
00539 *m_szComment;
00540 Bool m_bDirty,
00541 m_bLocal,
00542 m_bImmutable;
00543
00544 public:
00545 ConfigEntry(ConfigGroup *pParent, ConfigEntry *pNext, const char *szName);
00546 ~ConfigEntry();
00547
00548
00549 const char *Name() const { return m_szName; }
00550 const char *Value() const { return m_szValue; }
00551 const char *Comment() const { return m_szComment; }
00552 ConfigEntry *Next() const { return m_pNext; }
00553 Bool IsDirty() const { return m_bDirty; }
00554
00555
00556 const char *ExpandedValue();
00557
00558
00559 void SetValue(const char *szValue,
00560 Bool bLocal = TRUE,
00561 Bool bFromFile = FALSE);
00562 void SetComment(char *szComment);
00563 void SetDirty(Bool bDirty = TRUE);
00564 void SetNext(ConfigEntry *pNext) { m_pNext = pNext; }
00565 };
00566
00567 protected:
00568 class ConfigGroup
00569 {
00570 private:
00571 ConfigEntry *m_pEntries,
00572 *m_pLastEntry;
00573 ConfigGroup *m_pSubgroups,
00574 *m_pLastGroup,
00575 *m_pNext,
00576 *m_pParent;
00577 char *m_szName,
00578 *m_szComment;
00579 Bool m_bDirty;
00580
00581 public:
00582
00583 ConfigGroup(ConfigGroup *pParent, ConfigGroup *pNext, const char *szName);
00584
00585
00586 ~ConfigGroup();
00587
00588
00589 const char *Name() const { return m_szName; }
00590 const char *Comment() const { return m_szComment; }
00591 ConfigGroup *Next() const { return m_pNext; }
00592 ConfigGroup *Parent() const { return m_pParent; }
00593 ConfigGroup *Subgroup() const { return m_pSubgroups; }
00594 ConfigEntry *Entries() const { return m_pEntries; }
00595 Bool IsDirty() const { return m_bDirty; }
00596
00597
00598
00599 char *FullName() const;
00600
00601
00602 ConfigGroup *FindSubgroup(const char *szName) const;
00603 ConfigEntry *FindEntry (const char *szName) const;
00604
00605
00606 Bool DeleteSubgroup(const char *szName);
00607 Bool DeleteEntry (const char *szName);
00608
00609
00610 ConfigGroup *AddSubgroup(const char *szName);
00611 ConfigEntry *AddEntry (const char *szName);
00612
00613
00614 void SetDirty(Bool bDirty = TRUE);
00615
00616
00617 void SetComment(char *szComment);
00618
00619
00620 Bool flush(std::ostream *ostr);
00621 };
00622
00623
00624
00625 Bool DeleteIfEmpty();
00626
00627 ConfigGroup *m_pRootGroup,
00628 *m_pCurGroup;
00629
00630
00631
00632
00633 Bool readStream(std::istream *istr, ConfigGroup *pRootGroup = NULL);
00634
00635
00636
00637
00638 const char *GlobalConfigFile() const;
00639
00640 const char *LocalConfigFile() const;
00641
00643 void Init();
00644
00645 private:
00646 char *m_szFileName;
00647
00648 const char *m_szFullFileName;
00649 unsigned m_uiLine;
00650 Bool m_bParsingLocal;
00651 Bool m_bUseSubDir;
00652
00653
00654 void AppendCommentLine(const char *psz = NULL);
00655 char *m_szComment;
00656 };
00657
00658
00660
00661 #ifdef __WIN32__
00662
00663 class RegistryConfig : public BaseConfig
00664 {
00665 public:
00673 RegistryConfig(const char *szRootKey);
00675 ~RegistryConfig();
00677
00680
00681 const char *readEntry(const char *szKey, const char *szDefault = NULL) const;
00683 long int readEntry(const char *szKey, long int Default) const
00684 { return BaseConfig::readEntry(szKey, Default); }
00686 double readEntry(const char *szKey, double Default) const
00687 { return BaseConfig::readEntry(szKey, Default); }
00688
00690 Bool writeEntry(const char *szKey, const char *szValue);
00692 Bool writeEntry(const char *szKey, long int Value)
00693 { return BaseConfig::writeEntry(szKey, Default);}
00695 Bool writeEntry(const char *szKey, double Value)
00696 { return BaseConfig::writeEntry(szKey, Default); }
00697
00699 Bool deleteEntry(const char *szKey);
00701
00707
00708 Enumerator *enumSubgroups() const;
00710 Enumerator *enumEntries() const;
00712
00713
00714 virtual void changeCurrentPath(const char *szPath = "");
00715
00716 private:
00717 const char *ReadValue(void *hKey, const char *szValue) const;
00718 static Bool KeyIsEmpty(void *hKey);
00719
00720 void *m_hGlobalRootKey,
00721 *m_hLocalRootKey,
00722 *m_hGlobalCurKey,
00723 *m_hLocalCurKey;
00724
00725 char *m_pLastRead;
00726 };
00727
00728 #endif // WIN32
00729
00731 #if APPCONF_WIN32_NATIVE && defined(__WIN32__)
00732 typedef class RegistryConfig AppConfig;
00733 #else
00734 typedef class FileConfig AppConfig;
00735 #endif
00736
00738
00740 #endif //_APPCONF_H
00741