Main Page   Class Hierarchy   Compound List   File List   Compound Members  

kconfigbase.h

00001 // vi:ts=4:shiftwidth=4:expandtab
00002 /*
00003    This file is part of the KDE libraries
00004    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00005    Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
00006    Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
00007    Copyright (C) 2002 Zhang Le <ejoy@users.sourceforge.net>
00008    Last Change:02-Sep-2002.
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License as published by the Free Software Foundation; either
00013    version 2 of the License, or (at your option) any later version.
00014 
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019 
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023    Boston, MA 02111-1307, USA.
00024 */
00025 
00026 // $Id: kconfigbase.h,v 1.1.1.1 2002/12/01 04:08:23 zl Exp $
00027 
00028 #ifndef _KCONFIGBASE_H
00029 #define _KCONFIGBASE_H
00030 
00031 #include <vector>
00032 #include "kconfigdata.h"
00033 using std::vector;
00034 
00035 namespace config {
00036 
00037 class KConfigBackEnd;
00038 class KConfigBasePrivate;
00039 class KConfigGroup;
00040 
00065 class KConfigBase
00066 {
00067 
00068   friend class KConfigBackEnd;
00069   friend class KConfigINIBackEnd;
00070   friend class KConfigGroup;
00071 
00072 public:
00076   KConfigBase();
00077 
00081   virtual ~KConfigBase();
00082 
00093   void setGroup( const string& group );
00094   void setGroup( const char* group );
00095 
00102   string group() const;
00103 
00110   bool hasGroup(const string &group) const;
00111 
00117   virtual vector<string> groupList() const = 0;
00118 
00124   string locale() const;
00125 
00133    string readEntry( const string& pKey,
00134                      const string& aDefault = "" ) const;
00135    string readEntry( const char *pKey,
00136                      const string& aDefault = "" ) const;
00137 
00138 
00149   int readListEntry( const string& pKey, vector<string> &list, char sep = ',' ) const;
00150   int readListEntry( const char *pKey, vector<string> &list, char sep = ',' ) const;
00151 
00159   vector<string> readListEntry( const string& pKey, char sep = ',' ) const;
00160   vector<string> readListEntry( const char *pKey, char sep = ',' ) const;
00161 
00168   vector<int> readIntListEntry( const string& pKey ) const;
00169   vector<int> readIntListEntry( const char *pKey ) const;
00170 
00182   string readPathEntry( const string& pKey, const string & aDefault = "" ) const;
00183   string readPathEntry( const char *pKey, const string & aDefault = "" ) const;
00184 
00196   int readNumEntry( const string& pKey, int nDefault = 0 ) const;
00197   int readNumEntry( const char *pKey, int nDefault = 0 ) const;
00198 
00210   unsigned int readUnsignedNumEntry( const string& pKey, unsigned int nDefault = 0 ) const;
00211   unsigned int readUnsignedNumEntry( const char *pKey, unsigned int nDefault = 0 ) const;
00212 
00213 
00225   long readLongNumEntry( const string& pKey, long nDefault = 0 ) const;
00226   long readLongNumEntry( const char *pKey, long nDefault = 0 ) const;
00227 
00239   unsigned long readUnsignedLongNumEntry( const string& pKey, unsigned long nDefault = 0 ) const;
00240   unsigned long readUnsignedLongNumEntry( const char *pKey, unsigned long nDefault = 0 ) const;
00241 
00253   double readDoubleNumEntry( const string& pKey, double nDefault = 0.0 ) const;
00254   double readDoubleNumEntry( const char *pKey, double nDefault = 0.0 ) const;
00255 
00267   bool readBoolEntry( const string& pKey, const bool bDefault = false ) const;
00268   bool readBoolEntry( const char *pKey, const bool bDefault = false ) const;
00269 
00278    string readEntryUntranslated( const string& pKey,
00279                      const string& aDefault = "" ) const;
00280    string readEntryUntranslated( const char *pKey,
00281                      const string& aDefault = "" ) const;
00282 
00300   void writeEntry( const string& pKey, const string& pValue,
00301                       bool bPersistent = true, bool bGlobal = false,
00302                       bool bNLS = false );
00303 
00304   void writeEntry( const char *pKey, const string& pValue,
00305                       bool bPersistent = true, bool bGlobal = false,
00306                       bool bNLS = false );
00307 
00327   void writeEntry( const string& pKey, const vector<string> &rValue,
00328                    char sep = ',', bool bPersistent = true, bool bGlobal = false, bool bNLS = false );
00329   void writeEntry( const char *pKey, const vector<string> &rValue,
00330                    char sep = ',', bool bPersistent = true, bool bGlobal = false, bool bNLS = false );
00331 
00332 
00352   void writeEntry( const string& pKey, const vector<int>& rValue,
00353                    bool bPersistent = true, bool bGlobal = false, bool bNLS = false );
00354   void writeEntry( const char *pKey, const vector<int>& rValue,
00355                    bool bPersistent = true, bool bGlobal = false, bool bNLS = false );
00356 
00374   void writeEntry( const string& pKey, const char *pValue,
00375                       bool bPersistent = true, bool bGlobal = false,
00376                       bool bNLS = false )
00377     { writeEntry(pKey, string(pValue), bPersistent, bGlobal, bNLS); }
00378   void writeEntry( const char *pKey, const char *pValue,
00379                       bool bPersistent = true, bool bGlobal = false,
00380                       bool bNLS = false )
00381     { writeEntry(pKey, string(pValue), bPersistent, bGlobal, bNLS); }
00382 
00398   void writeEntry( const string& pKey, int nValue,
00399                       bool bPersistent = true, bool bGlobal = false,
00400                       bool bNLS = false );
00401   void writeEntry( const char *pKey, int nValue,
00402                       bool bPersistent = true, bool bGlobal = false,
00403                       bool bNLS = false );
00404 
00420   void writeEntry( const string& pKey, unsigned int nValue,
00421                       bool bPersistent = true, bool bGlobal = false,
00422                       bool bNLS = false );
00423   void writeEntry( const char *pKey, unsigned int nValue,
00424                       bool bPersistent = true, bool bGlobal = false,
00425                       bool bNLS = false );
00426 
00441   void writeEntry( const string& pKey, long nValue,
00442                       bool bPersistent = true, bool bGlobal = false,
00443                       bool bNLS = false );
00444   void writeEntry( const char *pKey, long nValue,
00445                       bool bPersistent = true, bool bGlobal = false,
00446                       bool bNLS = false );
00447 
00462   void writeEntry( const string& pKey, unsigned long nValue,
00463                       bool bPersistent = true, bool bGlobal = false,
00464                       bool bNLS = false );
00465   void writeEntry( const char *pKey, unsigned long nValue,
00466                       bool bPersistent = true, bool bGlobal = false,
00467                       bool bNLS = false );
00468 
00486   void writeEntry( const string& pKey, double nValue,
00487                       bool bPersistent = true, bool bGlobal = false,
00488                       char format = 'g', int precision = 6,
00489                       bool bNLS = false );
00490   void writeEntry( const char *pKey, double nValue,
00491                       bool bPersistent = true, bool bGlobal = false,
00492                       char format = 'g', int precision = 6,
00493                       bool bNLS = false );
00494 
00509   void writeEntry( const string& pKey, bool bValue,
00510                       bool bPersistent = true, bool bGlobal = false,
00511                       bool bNLS = false );
00512   void writeEntry( const char *pKey, bool bValue,
00513                       bool bPersistent = true, bool bGlobal = false,
00514                       bool bNLS = false );
00515 
00533   void writePathEntry( const string& pKey, const string & path,
00534                        bool bPersistent = true, bool bGlobal = false,
00535                        bool bNLS = false );
00536   void writePathEntry( const char *pKey, const string & path,
00537                        bool bPersistent = true, bool bGlobal = false,
00538                        bool bNLS = false );
00539 
00548    void deleteEntry( const string& pKey,
00549                    bool bNLS = false, bool bGlobal = false);
00550    void deleteEntry( const char *pKey,
00551                    bool bNLS = false, bool bGlobal = false);
00552 
00570   bool deleteGroup( const string& group, bool bDeep = true, bool bGlobal = false );
00571 
00579   void setDollarExpansion( bool _bExpand = true ) { bExpand = _bExpand; }
00580 
00586   bool isDollarExpansion() const { return bExpand; }
00587 
00602   virtual void rollback( bool bDeep = true );
00603 
00617   virtual void sync();
00618 
00622   bool isDirty() const { return bDirty; }
00623 
00631   virtual void setReadOnly(bool _ro) { bReadOnly = _ro; }
00632 
00638   bool isReadOnly() const { return bReadOnly; }
00639 
00649   bool hasKey( const string& key ) const;
00650 
00661   virtual map<string, string> entryMap(const string &group) const = 0;
00662 
00675   virtual void reparseConfiguration() = 0;
00676 
00680   bool isImmutable() const;
00681 
00686   bool groupIsImmutable(const string &group) const;
00687 
00692   bool entryIsImmutable(const string &key) const;
00693 
00699   enum ConfigState { NoAccess, ReadOnly, ReadWrite };
00700 
00713   ConfigState getConfigState() const;
00714 
00715 protected:
00721   void setLocale();
00722 
00728   virtual void setDirty(bool _bDirty = true) { bDirty = _bDirty; }
00729 
00735   virtual void parseConfigFiles();
00736 
00752   virtual KEntryMap internalEntryMap( const string& pGroup ) const = 0;
00753 
00765   virtual KEntryMap internalEntryMap() const = 0;
00766 
00782   virtual void putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup = true) = 0;
00783 
00798   virtual KEntry lookupData(const KEntryKey &_key) const = 0;
00799 
00800   virtual bool internalHasGroup(const string &group) const = 0;
00801 
00805   KConfigBackEnd *backEnd;
00806 public:
00810 //  bool hasGroup(const string &_pGroup) const;
00811   bool hasGroup(const char *_pGroup) const;
00812   bool hasKey( const char *pKey ) const;
00813 
00814 protected:
00815   string readEntryUtf8( const char *pKey) const;
00816 
00819   string mGroup;
00820 
00823   string aLocaleString;
00824 
00828   bool bDirty;
00829 
00830   bool bLocaleInitialized;
00831   bool bReadOnly;           // currently only used by KSimpleConfig
00832   mutable bool bExpand;     // whether dollar expansion is used
00833 
00834 protected:
00835   virtual void virtual_hook( int id, void* data );
00836 private:
00837   KConfigBasePrivate *d;
00838 };
00839 
00840 class KConfigGroupSaverPrivate;
00841 
00875 class KConfigGroupSaver
00876 {
00877 public:
00886   KConfigGroupSaver( KConfigBase* config, string group )
00887       : _config(config), _oldgroup(config->group())
00888         { _config->setGroup( group ); }
00889 
00890   KConfigGroupSaver( KConfigBase* config, const char *group )
00891       : _config(config), _oldgroup(config->group())
00892         { _config->setGroup( group ); }
00893 
00894   KConfigGroupSaver( KConfigBase* config, const string &group )
00895       : _config(config), _oldgroup(config->group())
00896         { _config->setGroup( group ); }
00897 
00898   ~KConfigGroupSaver() { _config->setGroup( _oldgroup ); }
00899 
00900     KConfigBase* config() { return _config; };
00901 
00902 private:
00903   KConfigBase* _config;
00904   string _oldgroup;
00905 
00906   KConfigGroupSaver(const KConfigGroupSaver&);
00907   KConfigGroupSaver& operator=(const KConfigGroupSaver&);
00908 
00909   KConfigGroupSaverPrivate *d;
00910 };
00911 
00912 class KConfigGroupPrivate;
00913 
00914 class KConfigGroup: public KConfigBase
00915 {
00916 public:
00921    KConfigGroup(KConfigBase *master, const string &group);
00929    KConfigGroup(KConfigBase *master, const char * group);
00930 
00937    void deleteGroup(bool bGlobal = false);
00938 
00939    // The following functions are reimplemented:
00940    virtual void setDirty(bool b);
00941    virtual void putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup = true);
00942    virtual KEntry lookupData(const KEntryKey &_key) const;
00943    virtual void sync();
00944 
00945 private:
00946    // Hide the following members:
00947    void setGroup() { }
00948    void setDesktopGroup() { }
00949    void group() { }
00950    void hasGroup() { }
00951    void setReadOnly(bool) { }
00952    void isDirty() { }
00953 
00954    // The following members are not used.
00955    virtual vector<string> groupList() const { return vector<string>(); }
00956    virtual void rollback(bool) { }
00957    virtual void reparseConfiguration() { }
00958    virtual map<string, string> entryMap(const string &) const
00959     { return map<string,string>(); }
00960    virtual KEntryMap internalEntryMap( const string&) const
00961     { return KEntryMap(); }
00962    virtual KEntryMap internalEntryMap() const
00963     { return KEntryMap(); }
00964    virtual bool internalHasGroup(const string &) const
00965     { return false; }
00966 
00967    void getConfigState() { }
00968 
00969    KConfigBase *mMaster;
00970 protected:
00971    virtual void virtual_hook( int id, void* data );
00972 private:
00973    KConfigGroupPrivate* d;
00974 };
00975  }; //namespace config
00976 
00977 #endif

Generated on Sat Dec 14 20:05:52 2002 for libconfig by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002