FXSettings.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                           S e t t i n g s   C l a s s                         *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1998,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXSettings.h,v 1.33 2006/01/22 17:58:09 fox Exp $                        *
00023 ********************************************************************************/
00024 #ifndef FXSETTINGS_H
00025 #define FXSETTINGS_H
00026 
00027 #ifndef FXDICT_H
00028 #include "FXDict.h"
00029 #endif
00030 
00031 namespace FX {
00032 
00033 
00034 class FXStringDict;
00035 
00036 
00044 class FXAPI FXSettings : public FXDict {
00045   FXDECLARE(FXSettings)
00046 protected:
00047   bool modified;
00048 protected:
00049   virtual void *createData(const void*);
00050   virtual void deleteData(void*);
00051   FXchar* dequote(FXchar* text) const;
00052   FXchar* enquote(FXchar* result,const FXchar* text);
00053   FXStringDict* insert(const FXchar* ky){ return (FXStringDict*)FXDict::insert(ky,NULL); }
00054   FXStringDict* replace(const FXchar* ky,FXStringDict* section){ return (FXStringDict*)FXDict::replace(ky,section,true); }
00055   FXStringDict* remove(const FXchar* ky){ return (FXStringDict*)FXDict::remove(ky); }
00056 public:
00057 
00059   FXSettings();
00060 
00062   FXSettings(const FXSettings& orig);
00063 
00065   FXSettings &operator=(const FXSettings& orig);
00066 
00068   bool parseFile(const FXString& filename,bool mark);
00069 
00071   bool unparseFile(const FXString& filename);
00072 
00074   FXStringDict* data(FXuint pos) const { return (FXStringDict*)FXDict::data(pos); }
00075 
00077   FXStringDict* find(const FXchar *section) const { return (FXStringDict*)FXDict::find(section); }
00078 
00080   FXint readFormatEntry(const FXchar *section,const FXchar *key,const FXchar *fmt,...) FX_SCANF(4,5) ;
00081 
00083   const FXchar *readStringEntry(const FXchar *section,const FXchar *key,const FXchar *def=NULL);
00084 
00086   FXint readIntEntry(const FXchar *section,const FXchar *key,FXint def=0);
00087 
00089   FXuint readUnsignedEntry(const FXchar *section,const FXchar *key,FXuint def=0);
00090 
00092   FXdouble readRealEntry(const FXchar *section,const FXchar *key,FXdouble def=0.0);
00093 
00095   FXColor readColorEntry(const FXchar *section,const FXchar *key,FXColor def=0);
00096 
00098   FXbool readBoolEntry(const FXchar *section,const FXchar *key,FXbool def=FALSE);
00099 
00101   FXint writeFormatEntry(const FXchar *section,const FXchar *key,const FXchar *fmt,...) FX_PRINTF(4,5) ;
00102 
00104   bool writeStringEntry(const FXchar *section,const FXchar *key,const FXchar *val);
00105 
00107   bool writeIntEntry(const FXchar *section,const FXchar *key,FXint val);
00108 
00110   bool writeUnsignedEntry(const FXchar *section,const FXchar *key,FXuint val);
00111 
00113   bool writeRealEntry(const FXchar *section,const FXchar *key,FXdouble val);
00114 
00116   bool writeColorEntry(const FXchar *section,const FXchar *key,FXColor val);
00117 
00119   bool writeBoolEntry(const FXchar *section,const FXchar *key,FXbool val);
00120 
00122   bool deleteEntry(const FXchar *section,const FXchar *key);
00123 
00125   bool existingEntry(const FXchar *section,const FXchar *key);
00126 
00128   bool deleteSection(const FXchar *section);
00129 
00131   bool existingSection(const FXchar *section);
00132 
00134   bool clear();
00135 
00137   void setModified(bool mdfy=true){ modified=mdfy; }
00138 
00140   bool isModified() const { return modified; }
00141 
00143   virtual ~FXSettings();
00144   };
00145 
00146 
00147 namespace FXSettingsHelpers
00148 {
00151     template<typename type> inline void rwSetting(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, type &val, const type &defval);
00152     template<> inline void rwSetting<bool>(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, bool &val, const bool &defval)
00153     {
00154         save ? settings.writeIntEntry(section, name, val)
00155             : (val=(0!=settings.readIntEntry(section, name, defval)));
00156     }
00157     template<> inline void rwSetting<FXint>(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, FXint &val, const FXint &defval)
00158     {
00159         save ? settings.writeIntEntry(section, name, val)
00160             : (val=settings.readIntEntry(section, name, defval));
00161     }
00162     template<> inline void rwSetting<FXuint>(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, FXuint &val, const FXuint &defval)
00163     {
00164         save ? settings.writeUnsignedEntry(section, name, val)
00165             : (val=settings.readUnsignedEntry(section, name, defval));
00166     }
00167     /* GCC won't accept that FXColor is different to a FXuint :(
00168     template<> inline void rwSetting<FXColor(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, FXColor &val, const FXColor &defval)
00169     {
00170         save ? settings.writeColorEntry(section, name, val)
00171             : (val=settings.readColorEntry(section, name, defval));
00172     }*/
00173     template<> inline void rwSetting<double>(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, double &val, const double &defval)
00174     {
00175         save ? settings.writeRealEntry(section, name, val)
00176             : (val=settings.readRealEntry(section, name, defval));
00177     }
00178     template<> inline void rwSetting<FXString>(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, FXString &val, const FXString &defval)
00179     {
00180         save ? settings.writeStringEntry(section, name, val.text())
00181             : (val=settings.readStringEntry(section, name, defval.text()), true);
00182     }
00183     // Ambiguity buster
00184     template<typename type> inline void rwSetting(bool save, FXSettings &settings, const FXchar *section, const FXchar *name, type &val, int defval)
00185     {
00186         rwSetting<type>(save, settings, section, name, val, (type) defval);
00187     }
00188 }
00189 
00190 }
00191 
00192 #endif
00193 

(C) 2002-2009 Niall Douglas. Some parts (C) to assorted authors.
Generated on Fri Nov 20 18:31:24 2009 for TnFOX by doxygen v1.4.7