From 2ff45ffd1a7ef36369c135090b39528860b25684 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 24 Aug 2012 23:37:37 +0000 Subject: Ini driver is close, loading isn't working perfectly yet... --- src/unstable/settings.cpp | 5 +- src/unstable/settingsdriverini.cpp | 155 ++++++++++++++++++++++++++++++++ src/unstable/settingsdriverini.h | 29 ++++++ src/unstable/settingsdriverregistry.cpp | 3 + 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/unstable/settingsdriverini.cpp create mode 100644 src/unstable/settingsdriverini.h (limited to 'src/unstable') diff --git a/src/unstable/settings.cpp b/src/unstable/settings.cpp index 792aff9..b812b60 100644 --- a/src/unstable/settings.cpp +++ b/src/unstable/settings.cpp @@ -2,6 +2,7 @@ #include "bu/settingsdrivertaf.h" #include "bu/settingsdriverregistry.h" +#include "bu/settingsdriverini.h" Bu::Settings::Settings( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct, Bu::Settings::Driver eDriver ) : @@ -15,7 +16,7 @@ Bu::Settings::Settings( const Bu::UtfString &sCompany, #if defined( WIN32 ) pDriver = new Bu::SettingsDriverRegistry(); #else - pDriver = new Bu::SettingsDriverTaf(); + pDriver = new Bu::SettingsDriverIni(); #endif break; @@ -24,7 +25,7 @@ Bu::Settings::Settings( const Bu::UtfString &sCompany, break; case DriverIni: - throw Bu::ExceptionBase("Not supported"); + pDriver = new Bu::SettingsDriverIni(); break; } diff --git a/src/unstable/settingsdriverini.cpp b/src/unstable/settingsdriverini.cpp new file mode 100644 index 0000000..46a40c6 --- /dev/null +++ b/src/unstable/settingsdriverini.cpp @@ -0,0 +1,155 @@ +#include "bu/settingsdriverini.h" + +#include "bu/file.h" +#include "bu/buffer.h" + +#include "bu/sio.h" + +#include + +using namespace Bu; + +Bu::SettingsDriverIni::SettingsDriverIni() +{ +} + +Bu::SettingsDriverIni::~SettingsDriverIni() +{ + Bu::File fOut( sPath, Bu::File::WriteNew ); + Bu::Buffer bOut( fOut ); + Bu::Formatter f( bOut ); + + for( GroupHash::iterator i = hGroup.begin(); i; i++ ) + { + f << "[" << i.getKey().get() << "]" << f.nl; + for( StrHash::iterator k = (*i).begin(); k; k++ ) + { + f << k.getKey().get() << "=" << k.getValue().get() << f.nl; + } + f << f.nl; + } +} + +void Bu::SettingsDriverIni::init( const Bu::UtfString &sCompany, + const Bu::UtfString &sProduct ) +{ + Bu::UtfString us( getenv("HOME") ); + us += "/.config/"; + us += sCompany; + us += "/"; + us += sProduct; + sPath = us.get(); + try + { + Bu::File fIn( sPath, Bu::File::Read|Bu::File::Create ); + Bu::Buffer bIn( fIn ); + StrHash hKeys; + Bu::String sGroup; + while( !bIn.isEos() ) + { + Bu::String sIn = bIn.readLine(); + if( sIn[0] == '[' ) + { + if( !sGroup.isEmpty() ) + { + hGroup.insert( sGroup, hKeys ); + hKeys.clear(); + } + sGroup = Bu::String( sIn.begin()+1, sIn.find(']') ); + } + else + { + Bu::String::iterator i = sIn.find('='); + if( !i ) + continue; + + hKeys.insert( Bu::String( sIn.begin(), i ), + Bu::String( i+1, sIn.end() ) ); + } + } + } + catch(...) + { + } +} + +void Bu::SettingsDriverIni::set( const Bu::UtfString &sKey, const Bu::UtfString &sValue ) +{ + Bu::String suKey = sKey.get(); + Bu::String::iterator i = suKey.find('/'); + Bu::UtfString sGrp; + Bu::UtfString sId; + if( !i ) + { + sGrp = ""; + sId = sKey; + } + else + { + Bu::String::iterator in; + for(;;) + { + in = i; + i = (in + 1).find('/'); + if( !i ) + break; + } + + sGrp.set( Bu::String( suKey.begin(), in ) ); + sId.set( Bu::String( in+1, suKey.end() ) ); + } + +// sio << "Group: " << sGrp.get() << sio.nl +// << "Key: " << sId.get() << sio.nl; + + if( !hGroup.has( sGrp ) ) + { + StrHash hVal; + hVal.insert( sId, sValue ); + hGroup.insert( sGrp, hVal ); + } + else + { + hGroup.get( sGrp ).insert( sId, sValue ); + } +} + +Bu::UtfString Bu::SettingsDriverIni::get( const Bu::UtfString &sKey, const Bu::UtfString &sValue ) +{ + Bu::String suKey = sKey.get(); + Bu::String::iterator i = suKey.find('/'); + Bu::UtfString sGrp; + Bu::UtfString sId; + if( !i ) + { + sGrp = ""; + sId = sKey; + } + else + { + Bu::String::iterator in; + for(;;) + { + in = i; + i = (in + 1).find('/'); + if( !i ) + break; + } + + sGrp.set( Bu::String( suKey.begin(), in ) ); + sId.set( Bu::String( in+1, suKey.end() ) ); + } + + sio << "Group: " << sGrp.get() << sio.nl + << "Key: " << sId.get() << sio.nl; + + try + { + return hGroup.get( sGrp ).get( sId ); + } + catch(...) + { + return sValue; + } +} + diff --git a/src/unstable/settingsdriverini.h b/src/unstable/settingsdriverini.h new file mode 100644 index 0000000..c3942d6 --- /dev/null +++ b/src/unstable/settingsdriverini.h @@ -0,0 +1,29 @@ +#ifndef BU_SETTINGS_DRIVER_INI_H +#define BU_SETTINGS_DRIVER_INI_H + +#include "bu/settingsdriver.h" +#include "bu/string.h" +#include "bu/hash.h" + +namespace Bu +{ + class SettingsDriverIni : public SettingsDriver + { + public: + SettingsDriverIni(); + virtual ~SettingsDriverIni(); + + protected: + virtual void init( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct ); + virtual void set( const Bu::UtfString &sKey, const Bu::UtfString &sValue ); + virtual Bu::UtfString get( const Bu::UtfString &sKey, const Bu::UtfString &sValue ); + + private: + Bu::String sPath; + typedef Bu::Hash StrHash; + typedef Bu::Hash GroupHash; + GroupHash hGroup; + }; +}; + +#endif diff --git a/src/unstable/settingsdriverregistry.cpp b/src/unstable/settingsdriverregistry.cpp index 40a08ac..89eda7b 100644 --- a/src/unstable/settingsdriverregistry.cpp +++ b/src/unstable/settingsdriverregistry.cpp @@ -1,3 +1,5 @@ +#ifdef WIN32 + #include "bu/settingsdriverregistry.h" #include #include "bu/string.h" @@ -86,3 +88,4 @@ Bu::UtfString Bu::SettingsDriverRegistry::get( const Bu::UtfString &sKey, const return Bu::UtfString( Bu::String( buf, iRet ) ); } +#endif -- cgit v1.2.3