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 
00196 
00197 
00198 #ifndef _MBOINIDATA_H_
00199 #define _MBOINIDATA_H_
00200 
00201 #include "Mbo.h"
00202 #include "MboSynch.h"
00203 #include "MboStrList.h"
00204 #include "MboStrMap.h"
00205 #include "MboStrHelper.h"
00206 #include "triplet.h"
00207 #include "quaternion.h"
00208 #include <vector>
00209 #include <float.h>
00210 #pragma push_macro("new")
00211 #undef new
00212 #include <fstream>
00213 #pragma pop_macro("new")
00214 #include <sstream>
00215 #include <sys/types.h>
00216 #include <sys/stat.h>
00217 
00218 #if defined(_MSC_VER)
00219 # pragma warning(push)
00220 # pragma warning(disable: 4251) // disable warning C4251: '...' : class '...' needs to have dll-interface to be used by clients of class '...'
00221 # pragma warning(disable: 4275) // non dll-interface class '...' used as base for dll-interface class '...'
00222 # pragma warning(disable: 4290) // C++-exception-specification ignored
00223 # ifndef _CPPRTTI
00224 #  error You must compile using RTTI
00225 # endif // _CPPRTTI
00226 #endif // _MSC_VER
00227 
00228 namespace mbo
00229 {
00230 
00231 template<class _E, class _Tr, class _A>
00232 COLORREF StrToColor(
00233         const std::basic_string<_E, _Tr, _A >& str,
00234         _IN COLORREF                           colDefault
00235     )
00236 {
00237     CStrMapStr msstrColor(str);
00238     return RGB( (msstrColor.contains_key("R") ? StrToInt<_E, _Tr, _A>(msstrColor.find("R")->second) : GetRValue(colDefault))
00239               , (msstrColor.contains_key("G") ? StrToInt<_E, _Tr, _A>(msstrColor.find("G")->second) : GetGValue(colDefault))
00240               , (msstrColor.contains_key("B") ? StrToInt<_E, _Tr, _A>(msstrColor.find("B")->second) : GetBValue(colDefault))
00241               );
00242 }
00243 
00244 template<class _E, class _Tr, class _A>
00245 std::basic_string<_E, _Tr, _A > & StrFromColor(
00246         std::basic_string<_E, _Tr, _A > & str,
00247         _IN COLORREF                      col
00248     )
00249 {
00250     str = "R="  + StrFrom<_E>(max(0, min(GetRValue(col), 255)))
00251         + ",G=" + StrFrom<_E>(max(0, min(GetGValue(col), 255)))
00252         + ",B=" + StrFrom<_E>(max(0, min(GetBValue(col), 255)));
00253     return str;
00254 }
00255 
00256 static std::string StrFromColor(_IN COLORREF col)
00257 {
00258     std::string str;
00259     return StrFromColor(str, col);
00260 }
00261 
00262 typedef std::list<std::pair<MboString, MboString> > CIniSectionList;
00263 
00274 class MBO_API CIniData 
00275 {
00276 public:
00296     explicit CIniData(
00297             _IN const MboString & strFilename,
00298             _IN const MboString & strSectionPrefix = ""
00299         );
00300 
00304     virtual ~CIniData();
00305 
00310     virtual const MboString & GetFilename() const;
00311 
00314     virtual MboString  GetIniDirectory() const;
00315 
00320     virtual const MboString & GetSectionPrefix() const;
00321 
00331     bool GetSectionNames(
00332             _IN  const MboString &  strSectionPrefix,
00333             _OUT CStrList &         vstrSectionNames
00334         ) const;
00335 
00343     bool GetSectionNames(
00344             _OUT CStrList &         vstrSectionNames
00345         ) const;
00346 
00352     bool SectionExists(
00353             _IN const MboString &   strSection
00354         ) const;
00355 
00362     virtual bool GetKeyNames(
00363             _IN  const MboString &  strSection,
00364             _OUT CStrList &         vstrKeyNames
00365         ) const;
00366 
00373     virtual bool KeyNameExists(
00374             _IN  const MboString &  strSection,
00375             _IN  const MboString &  strKeyname
00376         ) const;
00377 
00398     virtual bool ReadString(
00399             _IN  const MboString &  strSection,
00400             _IN  const MboString &  strKeyname,
00401             _OUT MboString &        strValue,
00402             _IN  const MboString &  strDefault    = "",
00403             _IN  bool               bWriteDefault = false,
00404             _IN  bool               bParseEnvVars = false 
00405         ) const;
00406 
00419     virtual MboString GetString(
00420             _IN  const MboString &  strSection,
00421             _IN  const MboString &  strKeyname,
00422             _IN  const MboString &  strDefault    = "",
00423             _IN  bool               bWriteDefault = false,
00424             _IN  bool               bParseEnvVars = false
00425         ) const;
00426 
00442     static MboString GetIniString(
00443             _IN  const MboString &  strFilename,
00444             _IN  const MboString &  strSection,
00445             _IN  const MboString &  strKeyname,
00446             _IN  const MboString &  strDefault    = "",
00447             _IN  bool               bWriteDefault = false,
00448             _IN  bool               bParseEnvVars = false
00449         );
00450 
00470     virtual bool ReadInt(
00471             _IN  const MboString &  strSection,
00472             _IN  const MboString &  strKeyname,
00473             _OUT int &              nValue,
00474             _IN  int                nDefault      = 0,
00475             _IN  bool               bWriteDefault = false,
00476             _IN  int                nMinimum      = INT_MIN,
00477             _IN  int                nMaximum      = INT_MAX
00478         ) const;
00479 
00491     virtual int GetInt(
00492             _IN  const MboString &  strSection,
00493             _IN  const MboString &  strKeyname,
00494             _IN  int                nDefault      = 0,
00495             _IN  bool               bWriteDefault = false,
00496             _IN  int                nMinimum      = INT_MIN,
00497             _IN  int                nMaximum      = INT_MAX
00498         ) const;
00499 
00514     static int GetIniInt(
00515             _IN  const MboString &  strFilename,
00516             _IN  const MboString &  strSection,
00517             _IN  const MboString &  strKeyname,
00518             _IN  int                nDefault      = 0,
00519             _IN  bool               bWriteDefault = false,
00520             _IN  int                nMinimum      = INT_MIN,
00521             _IN  int                nMaximum      = INT_MAX
00522         );
00523 
00543     virtual bool ReadLong(
00544             _IN  const MboString &  strSection,
00545             _IN  const MboString &  strKeyname,
00546             _OUT long &             lValue,
00547             _IN  long               lDefault      = 0,
00548             _IN  bool               bWriteDefault = false,
00549             _IN  long               lMinimum      = LONG_MIN,
00550             _IN  long               lMaximum      = LONG_MAX
00551         ) const;
00552 
00564     virtual long GetLong(
00565             _IN  const MboString &  strSection,
00566             _IN  const MboString &  strKeyname,
00567             _IN  long               lDefault      = 0L,
00568             _IN  bool               bWriteDefault = false,
00569             _IN  long               lMinimum      = LONG_MIN,
00570             _IN  long               lMaximum      = LONG_MAX
00571         ) const;
00572 
00587     static long GetIniLong(
00588             _IN  const MboString &  strFilename,
00589             _IN  const MboString &  strSection,
00590             _IN  const MboString &  strKeyname,
00591             _IN  long               lDefault      = 0L,
00592             _IN  bool               bWriteDefault = false,
00593             _IN  long               lMinimum      = LONG_MIN,
00594             _IN  long               lMaximum      = LONG_MAX
00595         );
00596 
00616     virtual bool ReadFloat(
00617             _IN  const MboString &  strSection,
00618             _IN  const MboString &  strKeyname,
00619             _OUT float &            fValue,
00620             _IN  float              fDefault      = 0.0f,
00621             _IN  bool               bWriteDefault = false,
00622             _IN  float              fMinimum      = -FLT_MAX,
00623             _IN  float              fMaximum      = FLT_MAX
00624         ) const;
00625 
00637     virtual float GetFloat(
00638             _IN  const MboString &  strSection,
00639             _IN  const MboString &  strKeyname,
00640             _IN  float              fDefault      = 0.0f,
00641             _IN  bool               bWriteDefault = false,
00642             _IN  float              fMinimum      = -FLT_MAX,
00643             _IN  float              fMaximum      = FLT_MAX
00644         ) const;
00645 
00660     static float GetIniFloat(
00661             _IN  const MboString &  strFilename,
00662             _IN  const MboString &  strSection,
00663             _IN  const MboString &  strKeyname,
00664             _IN  float              fDefault      = 0.0f,
00665             _IN  bool               bWriteDefault = false,
00666             _IN  float              fMinimum      = -FLT_MAX,
00667             _IN  float              fMaximum      = FLT_MAX
00668         );
00669 
00689     virtual bool ReadDouble(
00690             _IN  const MboString &  strSection,
00691             _IN  const MboString &  strKeyname,
00692             _OUT double &           fValue,
00693             _IN  double             fDefault      = 0.0,
00694             _IN  bool               bWriteDefault = false,
00695             _IN  double             fMinimum      = -DBL_MAX,
00696             _IN  double             fMaximum      = DBL_MAX
00697         ) const;
00698 
00710     virtual double GetDouble(
00711             _IN  const MboString &  strSection,
00712             _IN  const MboString &  strKeyname,
00713             _IN  double             fDefault      = 0.0,
00714             _IN  bool               bWriteDefault = false,
00715             _IN  double             fMinimum      = -DBL_MAX,
00716             _IN  double             fMaximum      = DBL_MAX
00717         ) const;
00718 
00733     static double GetIniDouble(
00734             _IN  const MboString &  strFilename,
00735             _IN  const MboString &  strSection,
00736             _IN  const MboString &  strKeyname,
00737             _IN  double             fDefault      = 0.0,
00738             _IN  bool               bWriteDefault = false,
00739             _IN  double             fMinimum      = -DBL_MAX,
00740             _IN  double             fMaximum      = DBL_MAX
00741         );
00742 
00760     virtual bool ReadBool(
00761             _IN  const MboString &  strSection,
00762             _IN  const MboString &  strKeyname,
00763             _OUT bool &             bValue,
00764             _IN  bool               bDefault      = false,
00765             _IN  bool               bWriteDefault = false
00766         ) const;
00767 
00777     virtual bool GetBool(
00778             _IN  const MboString &  strSection,
00779             _IN  const MboString &  strKeyname,
00780             _IN  bool               bDefault      = false,
00781             _IN  bool               bWriteDefault = false
00782         ) const;
00783 
00796     static bool GetIniBool(
00797             _IN  const MboString &  strFilename,
00798             _IN  const MboString &  strSection,
00799             _IN  const MboString &  strKeyname,
00800             _IN  bool               bDefault      = false,
00801             _IN  bool               bWriteDefault = false
00802         );
00803 
00821     virtual bool ReadColor(
00822             _IN  const MboString &  strSection,
00823             _IN  const MboString &  strKeyname,
00824             _OUT COLORREF &         colValue,
00825             _IN  COLORREF           colDefault    = RGB(0, 0, 0),
00826             _IN  bool               bWriteDefault = false
00827         ) const;
00828 
00838     virtual COLORREF GetColor(
00839             _IN  const MboString &  strSection,
00840             _IN  const MboString &  strKeyname,
00841             _IN  COLORREF           colDefault    = RGB(0, 0, 0),
00842             _IN  bool               bWriteDefault = false
00843         ) const;
00844 
00857     static COLORREF GetIniColor(
00858             _IN  const MboString &  strFilename,
00859             _IN  const MboString &  strSection,
00860             _IN  const MboString &  strKeyname,
00861             _IN  COLORREF           colDefault    = RGB(0, 0, 0),
00862             _IN  bool               bWriteDefault = false
00863         );
00864 
00890     virtual bool ReadMap(
00891             _IN  const MboString &  strSection,
00892             _OUT CStrMapStr &       msstrValue,
00893             _IN  const CStrMapStr & msstrDefault  = CStrMapStr(),
00894             _IN  bool               bWriteDefault = false,
00895             _IN  bool               bWriteEmpty   = false,
00896             _IN  bool               bRetrieveAll  = false
00897         ) const;
00898 
00916     virtual DEPRECATED CStrMapStr GetMap(
00917             _IN  const MboString &  strSection,
00918             _IN  const CStrMapStr & msstrDefault  = CStrMapStr(),
00919             _IN  bool               bWriteDefault = false,
00920             _IN  bool               bWriteEmpty   = false,
00921             _IN  bool               bRetrieveAll  = false
00922         ) const;
00923 
00943     static DEPRECATED CStrMapStr GetIniMap(
00944             _IN  const MboString &  strFilename,
00945             _IN  const MboString &  strSection,
00946             _IN  const CStrMapStr & msstrDefault  = CStrMapStr(),
00947             _IN  bool               bWriteDefault = false,
00948             _IN  bool               bWriteEmpty   = false
00949         );
00950 
00976     virtual bool ReadSectionList(
00977             _IN  const MboString&       strSection,
00978             _OUT CIniSectionList&       vsstrValue,
00979             _IN  const CIniSectionList& vsstrDefault  = CIniSectionList(),
00980             _IN  bool                   bWriteDefault = false,
00981             _IN  bool                   bWriteEmpty   = false,
00982             _IN  bool                   bRetrieveAll  = false
00983         ) const;
00984 
00995     virtual bool EmptySection(
00996             _IN  const MboString &  strSection,
00997             _IN  bool               bKeepComments = true
00998         );
00999 
01008     virtual bool ClearSection(
01009             _IN  const MboString& strSection
01010         );
01011 
01021     virtual bool DeleteKey(
01022             _IN  const MboString &  strSection,
01023             _IN  const MboString &  strKeyname
01024         );
01025 
01026 
01036     virtual bool RenameKey(
01037             _IN  const MboString&  strSection,
01038             _IN  const MboString&  strKeynameOld,
01039             _IN  const MboString&  strKeynameNew
01040         );
01041 
01049     virtual bool WriteString(
01050             _IN  const MboString &  strSection,
01051             _IN  const MboString &  strKeyname,
01052             _IN  const MboString &  strValue
01053         );
01054 
01062     virtual bool WriteInt(
01063             _IN  const MboString &  strSection,
01064             _IN  const MboString &  strKeyname,
01065             _IN  int                nValue
01066         );
01067 
01075     virtual bool WriteLong(
01076             _IN  const MboString &  strSection,
01077             _IN  const MboString &  strKeyname,
01078             _IN  long               lValue
01079         );
01080 
01088     virtual bool WriteFloat(
01089             _IN  const MboString &  strSection,
01090             _IN  const MboString &  strKeyname,
01091             _IN  float              fValue
01092         );
01093 
01101     virtual bool WriteDouble(
01102             _IN  const MboString &  strSection,
01103             _IN  const MboString &  strKeyname,
01104             _IN  double             fValue
01105         );
01106 
01114     virtual bool WriteBool(
01115             _IN  const MboString &  strSection,
01116             _IN  const MboString &  strKeyname,
01117             _IN  bool               bValue
01118         );
01119 
01127     virtual bool WriteColor(
01128             _IN  const MboString &  strSection,
01129             _IN  const MboString &  strKeyname,
01130             _IN  COLORREF           colValue
01131         );
01132 
01144     virtual bool WriteMap(
01145             _IN  const MboString &  strSection,
01146             _IN  CStrMapStr         msstrValue,
01147             _IN  bool               bWriteEmpty   = false,
01148             _IN  bool               bKeepComments = true
01149         );
01150 
01164     virtual bool WriteSectionList(
01165             _IN  const MboString&  strSection,
01166             _IN  CIniSectionList   vsstrValue,
01167             _IN  bool              bWriteEmpty     = false,
01168             _IN  bool              bKeepComments   = true
01169         );
01170 
01185     virtual bool ReplaceSectionList(
01186             _IN  const MboString&        strSection,
01187             _IN  const CIniSectionList&  vsstrValue,
01188             _IN  bool                    bWriteEmpty = false
01189         );
01190 
01198     bool IsValidKey(
01199             _IN const MboString&  strKeyName, 
01200             _IN bool              bAcceptComment = false
01201         ) const;
01202 
01205     mbo::CLock IniDataGetLock() const;
01206 
01207 protected:
01208 
01211     virtual bool ReadFile();
01212 
01217     virtual bool IniDataFlush(_IN bool bModified = false) const;
01218 
01219     
01220     typedef mbo::quaternion<bool, MboString, MboString, MboString>   TIniValue;
01221     typedef std::list<TIniValue>                       TIniData;
01222     typedef std::pair<MboString, TIniData>             TIniSection;
01223     typedef std::list<TIniSection>                     TIniCache;
01224 
01227     bool IsEmptyIniValue(const TIniValue& val) const;
01228 
01232     TIniCache::iterator GetSection(
01233             _IN const MboString&  strSection
01234         );
01235 
01239     TIniCache::const_iterator GetSection(
01240             _IN const MboString&  strSection
01241         ) const;
01242 
01247     TIniCache::iterator GetSectionOrCreate(
01248             _IN const MboString&  strSection
01249         );
01250 
01255     TIniValue*  GetValuePtr(
01256             _IN const TIniCache::iterator& itSection,
01257             _IN const MboString&  strKeyName
01258         );
01259 
01264     const TIniValue* GetValuePtr(
01265             _IN const TIniCache::const_iterator&  itSection,
01266             _IN const MboString&                  strKeyName
01267         ) const;
01268 
01273     TIniValue* GetValuePtr(
01274             _IN const MboString&  strSection,
01275             _IN const MboString&  strKeyName
01276         );
01277 
01282     const TIniValue* GetValuePtr(
01283             _IN const MboString&  strSection,
01284             _IN const MboString&  strKeyName
01285         ) const;
01286 
01290     TIniData::iterator FindFirstEmpty(
01291             _IN _OUT TIniCache::iterator&  itSection
01292         );
01293 
01300     TIniData::iterator AddValue(
01301             _IN _OUT TIniCache::iterator&  itSection,
01302             _IN TIniData::iterator         itValue,
01303             _IN const MboString&           strKeyName,
01304             _IN const MboString&           strValue
01305         );
01306 
01312     TIniData::iterator AddValue(
01313             _IN _OUT TIniCache::iterator&  itSection,
01314             _IN const MboString&           strKeyName,
01315             _IN const MboString&           strValue
01316         );
01317 
01323     TIniData::iterator AddValue(
01324             _IN const MboString&  strSection,
01325             _IN const MboString&  strKeyName,
01326             _IN const MboString&  strValue
01327         );
01328 
01329 private:
01330 
01334     bool GetMTime() const;
01335 
01338     class CIniMutex: public mbo::MutexModel
01339     {
01340     public:
01341 
01344         CIniMutex(CIniData& cIniData)
01345             : m_cIniData(cIniData)
01346         {
01347         }
01348 
01349         void Enter() const
01350         {
01351             mbo::MutexModel::Enter();
01352 
01353             if (GetLevel() <= 1 && m_cIniData.GetMTime())
01354             {
01355                 m_cIniData.ReadFile();
01356             }
01357         }
01358 
01361         void Leave() const
01362         {
01363             if (GetLevel() <= 1)
01364             {
01365                 m_cIniData.IniDataFlush();
01366             }
01367             mbo::MutexModel::Leave();
01368         }
01369 
01370     protected:
01371 
01372         CIniData& m_cIniData; 
01373     };
01374 
01375     const MboString         m_strFilename;      
01376     const MboString         m_strSectionPrefix; 
01377     CIniMutex               m_mxIniData;        
01378     mutable std::fstream    m_stmIniFile;       
01379     mutable TIniCache       m_tCache;           
01380     mutable bool            m_bModified;        
01381     mutable time_t          m_tMtime;           
01383 private:
01384 
01387     CIniData& operator = (_IN const CIniData&)
01388     {
01389         abort();
01390         return *this;
01391     }
01392 };
01393 
01394 }; 
01395 
01396 #ifdef _MSC_VER
01397 # pragma warning(pop)
01398 #endif
01399 
01400 #endif // _CINIDATA_H_