From 3c38c55a26df5b2d8827b88a99f5bb996c4d13c2 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 24 Aug 2012 22:28:24 +0000 Subject: The basic registry interface works now. --- src/unstable/settings.cpp | 2 + src/unstable/settingsdriverregistry.cpp | 88 +++++++++++++++++++++++++++++++++ src/unstable/settingsdriverregistry.h | 28 +++++++++++ 3 files changed, 118 insertions(+) create mode 100644 src/unstable/settingsdriverregistry.cpp create mode 100644 src/unstable/settingsdriverregistry.h (limited to 'src/unstable') diff --git a/src/unstable/settings.cpp b/src/unstable/settings.cpp index ac3975f..792aff9 100644 --- a/src/unstable/settings.cpp +++ b/src/unstable/settings.cpp @@ -1,6 +1,7 @@ #include "bu/settings.h" #include "bu/settingsdrivertaf.h" +#include "bu/settingsdriverregistry.h" Bu::Settings::Settings( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct, Bu::Settings::Driver eDriver ) : @@ -12,6 +13,7 @@ Bu::Settings::Settings( const Bu::UtfString &sCompany, { case DriverNative: #if defined( WIN32 ) + pDriver = new Bu::SettingsDriverRegistry(); #else pDriver = new Bu::SettingsDriverTaf(); #endif diff --git a/src/unstable/settingsdriverregistry.cpp b/src/unstable/settingsdriverregistry.cpp new file mode 100644 index 0000000..40a08ac --- /dev/null +++ b/src/unstable/settingsdriverregistry.cpp @@ -0,0 +1,88 @@ +#include "bu/settingsdriverregistry.h" +#include +#include "bu/string.h" + +#define phKey ((HKEY *)rphKey) + +Bu::SettingsDriverRegistry::SettingsDriverRegistry() +{ +} + +Bu::SettingsDriverRegistry::~SettingsDriverRegistry() +{ + RegCloseKey( *phKey ); +} + +void Bu::SettingsDriverRegistry::init( const Bu::UtfString &sCompany, const Bu::UtfString &sProduct ) +{ + Bu::UtfString us("Software\\"); + us += sCompany; + us += "\\"; + us += sProduct; + rphKey = new HKEY; + RegCreateKeyExA( HKEY_CURRENT_USER, us.get().getStr(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, phKey, NULL ); +} + +void Bu::SettingsDriverRegistry::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::String sPKey; + for(; i;) + { + in = i; + in++; + if( in ) + { + if( !sPKey.isEmpty() ) + sPKey += "\\"; + sPKey += *i; + } + i = in; + } + + HKEY key; + RegCreateKeyExA( *phKey, sPKey.getStr(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL ); + Bu::String sTmp = sValue.get(); + RegSetValueExA( key, lPath.last().getStr(), 0, REG_SZ, (BYTE *)sTmp.getStr(), sTmp.getSize()+1 ); + RegCloseKey( key ); +} + +Bu::UtfString Bu::SettingsDriverRegistry::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::String sPKey; + for(; i;) + { + in = i; + in++; + if( in ) + { + if( !sPKey.isEmpty() ) + sPKey += "\\"; + sPKey += *i; + } + i = in; + } + + HKEY key; + if( RegOpenKeyExA( *phKey, sPKey.getStr(), 0, KEY_ALL_ACCESS, &key ) + != ERROR_SUCCESS ) + return sValue; + char buf[4096]; + DWORD iRet = 4096; + if( RegQueryValueEx( key, lPath.last().getStr(), NULL, NULL, (BYTE *)buf, &iRet ) != ERROR_SUCCESS ) + { + RegCloseKey( key ); + return sValue; + } + RegCloseKey( key ); + + return Bu::UtfString( Bu::String( buf, iRet ) ); +} + diff --git a/src/unstable/settingsdriverregistry.h b/src/unstable/settingsdriverregistry.h new file mode 100644 index 0000000..2d718d2 --- /dev/null +++ b/src/unstable/settingsdriverregistry.h @@ -0,0 +1,28 @@ +#ifndef BU_SETTINGS_DRIVER_REGISTRY_H +#define BU_SETTINGS_DRIVER_REGISTRY_H + +#ifdef WIN32 + +#include "bu/settingsdriver.h" + +namespace Bu +{ + class SettingsDriverRegistry : public SettingsDriver + { + public: + SettingsDriverRegistry(); + virtual ~SettingsDriverRegistry(); + + 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: + void *rphKey; + }; +}; + +#endif + +#endif -- cgit v1.2.3