From d7d2c0929a5d7871e913451fd542e03ae4561183 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 24 Aug 2012 20:11:04 +0000 Subject: Taf isn't really...the best backend for this now, I need some more fixes there first, so next up we need the windows registry backend. --- src/tests/settings.cpp | 9 +++++ src/unstable/settings.cpp | 7 ++++ src/unstable/settings.h | 1 + src/unstable/settingsdriver.h | 2 + src/unstable/settingsdrivertaf.cpp | 81 ++++++++++++++++++++++++++++++++++++-- src/unstable/settingsdrivertaf.h | 7 ++++ 6 files changed, 103 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/tests/settings.cpp b/src/tests/settings.cpp index 0738cb7..2caa015 100644 --- a/src/tests/settings.cpp +++ b/src/tests/settings.cpp @@ -1,9 +1,18 @@ #include "bu/settings.h" +#include "bu/sio.h" + +using namespace Bu; int main() { Bu::Settings s("Xagasoft", "Settings"); + sio << s.get("Something", "BAD").get() << sio.nl; + sio << s.get("general/path", "BAD").get() << sio.nl; + sio << s.get("general/magic", "BAD").get() << sio.nl; + + s.set("Something", "bob"); + s.set("general/path", "E:\\Place"); } diff --git a/src/unstable/settings.cpp b/src/unstable/settings.cpp index e993250..ac3975f 100644 --- a/src/unstable/settings.cpp +++ b/src/unstable/settings.cpp @@ -36,5 +36,12 @@ Bu::Settings::~Settings() void Bu::Settings::set( const Bu::UtfString &sKey, const Bu::UtfString &sValue ) { + pDriver->set( sKey, sValue ); +} + +Bu::UtfString Bu::Settings::get( const Bu::UtfString &sKey, + const Bu::UtfString &sValue ) +{ + return pDriver->get( sKey, sValue ); } diff --git a/src/unstable/settings.h b/src/unstable/settings.h index 0736ee5..c1d0e1c 100644 --- a/src/unstable/settings.h +++ b/src/unstable/settings.h @@ -23,6 +23,7 @@ namespace Bu virtual ~Settings(); void set( const Bu::UtfString &sKey, const Bu::UtfString &sValue ); + Bu::UtfString get( const Bu::UtfString &sKey, const Bu::UtfString &sValue=Bu::UtfString() ); private: Bu::UtfString sCompany; diff --git a/src/unstable/settingsdriver.h b/src/unstable/settingsdriver.h index 0a63106..47b7d9d 100644 --- a/src/unstable/settingsdriver.h +++ b/src/unstable/settingsdriver.h @@ -20,6 +20,8 @@ namespace Bu protected: virtual void init( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct )=0; + virtual void set( const Bu::UtfString &sKey, const Bu::UtfString &sValue )=0; + virtual Bu::UtfString get( const Bu::UtfString &sKey, const Bu::UtfString &sValue )=0; }; }; diff --git a/src/unstable/settingsdrivertaf.cpp b/src/unstable/settingsdrivertaf.cpp index 164bf0b..8cbcbc5 100644 --- a/src/unstable/settingsdrivertaf.cpp +++ b/src/unstable/settingsdrivertaf.cpp @@ -12,18 +12,91 @@ Bu::SettingsDriverTaf::SettingsDriverTaf() : Bu::SettingsDriverTaf::~SettingsDriverTaf() { + if( !pRoot ) + return; + + Bu::File fOut( sPath, Bu::File::WriteNew ); + Bu::TafWriter tw( fOut ); + tw.writeGroup( pRoot ); + delete pRoot; } void Bu::SettingsDriverTaf::init( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct ) { Bu::UtfString us( getenv("HOME") ); - us += "/"; + us += "/.config/"; us += sCompany; us += "/"; us += sProduct; - Bu::File fIn( us.get(), Bu::File::Read|Bu::File::Create ); - Bu::TafReader tr( fIn ); - pRoot = tr.readGroup(); + sPath = us.get(); + try + { + Bu::File fIn( sPath, Bu::File::Read|Bu::File::Create ); + Bu::TafReader tr( fIn ); + pRoot = tr.readGroup(); + } + catch(...) + { + } + if( !pRoot ) + { + pRoot = new Bu::TafGroup( sProduct.get() ); + } +} + +void Bu::SettingsDriverTaf::set( const Bu::UtfString &sKey, + const Bu::UtfString &sValue ) +{ + Bu::StringList lPath = sKey.get().split('/'); + Bu::StringList::iterator i = lPath.begin(); + Bu::StringList::iterator in; + Bu::TafGroup *pGrp = pRoot; + for(; i;) + { + in = i; + in++; + if( in ) + { + if( pGrp->hasChild( *i ) ) + pGrp = (Bu::TafGroup *)pGrp->getChild( *i ); + else + pGrp = pGrp->addGroup( *i ); + } + else + { + pGrp->addProperty( *i, sValue.get() ); + } + i = in; + } +} + +Bu::UtfString Bu::SettingsDriverTaf::get( const Bu::UtfString &sKey, + const Bu::UtfString &sValue ) +{ + Bu::StringList lPath = sKey.get().split('/'); + Bu::StringList::iterator i = lPath.begin(); + Bu::StringList::iterator in; + Bu::TafGroup *pGrp = pRoot; + for(; i;) + { + in = i; + in++; + if( in ) + { + if( pGrp->hasChild( *i ) ) + pGrp = (Bu::TafGroup *)pGrp->getChild( *i ); + else + return sValue; + } + else + { + if( pGrp->hasProperty( *i ) ) + return pGrp->getProperty( *i ); + else + return sValue; + } + i = in; + } } diff --git a/src/unstable/settingsdrivertaf.h b/src/unstable/settingsdrivertaf.h index d7d751b..b5f8b07 100644 --- a/src/unstable/settingsdrivertaf.h +++ b/src/unstable/settingsdrivertaf.h @@ -2,11 +2,15 @@ #define BU_SETTINGS_DRIVER_TAF_H #include "bu/settingsdriver.h" +#include "bu/string.h" namespace Bu { class TafGroup; + /** + * The taf driver is flawed until I fix taf editing, I've been meaning to... + */ class SettingsDriverTaf : public SettingsDriver { public: @@ -15,8 +19,11 @@ namespace Bu 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; class Bu::TafGroup *pRoot; }; }; -- cgit v1.2.3