Re: [gtkmm] saving settings in win32



Today i finally found the time to "finish" my registry class. It is
still very limited, but it suits my needs for the moment. It allows you
to store and retrieve transparantly strings,doubles,integers and
booleans to and from either gconf or the win32 registry. It not very
well tested, but afaik it works ok on win95/98/XP and of course all
GNU/linux boxes with gconf.

Goodies like adding subdirs etc. will be added when i (or someone else)
need them.

example:
-----------------------------
#include <iostream>
#include "Settings.hh"

int main()
{
	Settings s("SETTINGSTEST");
	s.set("Name", "Bart");
	s.set("Married", false);
	s.set("Height", 1.84);
	s.set("Year", 2004);
	
	std::cout << s.get_string( "Name" ) << std::endl;
	std::cout << s.get_bool( "Married" ) << std::endl;
	std::cout << s.get_double( "Height" ) << std::endl;
	std::cout << s.get_int( "Year" ) << std::endl;
}
-----------------------------------------------------------


Have fun :^)

Bart
 



On Tue, 2004-06-01 at 14:06, B.Hakvoort wrote:
> On Tue, 2004-06-01 at 09:06, Cedric Gustin wrote:
> > It might be interesting to write your own (simple) 
> > registry class that wraps a small subset of both gconf and the win32 
> > registry API.
> > 
> > Cedric 
> > 
> 
> I like the idea and i'll give it a try. You mentioned "standard win32
> registry access functions", do you have some documentation or
> examplecode on this?
> As soon as i have something decent i'll post my code for feedback.
> 
> Bart
> 
> 
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
#include "Settings.hh"

Settings::Settings()
{
}

Settings::Settings(const std::string & application_name)
{
	#ifdef WIN32
		settings_maindir = "Software\\" + application_name + "\\";//means   HKEY_LOCAL_MACHINE\Software\<your application>
	#endif
	#ifndef WIN32
		Gnome::Conf::init();
		refptr_Gconf_Client = Gnome::Conf::Client::get_default_client();
		settings_maindir = "/apps/" + application_name + "/";//means   apps/<your application>
	#endif
}

void Settings::set( const std::string & key, const std::string & value)
{
	#ifdef WIN32
		RegCreateKeyEx(HKEY_LOCAL_MACHINE,settings_maindir.c_str() , 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &Hkey, NULL);
		RegSetValueEx(Hkey, key.c_str(), 0, REG_SZ, (const unsigned char *)value.c_str(), value.length());
		RegCloseKey( Hkey );
	#endif
	#ifndef WIN32
		refptr_Gconf_Client->set( settings_maindir + key , value );
	#endif
}

void Settings::set( const std::string & key, double value)
{
	#ifdef WIN32
		RegCreateKeyEx(HKEY_LOCAL_MACHINE,settings_maindir.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &Hkey, NULL);
		RegSetValueEx(Hkey, key.c_str(), 0, REG_QWORD, (BYTE *)&value, sizeof(value));
		RegCloseKey( Hkey );
	#endif
	#ifndef WIN32
		refptr_Gconf_Client->set( settings_maindir + key , value );
	#endif
}

std::string Settings::get_string(const std::string & key)
{
	#ifdef WIN32
		read_size = sizeof(str_value);
		RegCreateKeyEx(HKEY_LOCAL_MACHINE,settings_maindir.c_str(), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE, NULL, &Hkey, NULL);
			
		RegQueryValueEx(Hkey, key.c_str(), 0,NULL,(BYTE*)str_value,&read_size);
		RegCloseKey( Hkey );
		return str_value;
	#endif
	#ifndef WIN32
		return refptr_Gconf_Client->get_string( settings_maindir + key );
	#endif
}

double Settings::get_double(const std::string &key )
{
	#ifdef WIN32
		read_size=sizeof(read_value);
		RegCreateKeyEx(HKEY_LOCAL_MACHINE,settings_maindir.c_str() , 0, NULL, REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE, NULL, &Hkey, NULL);
		RegQueryValueEx(Hkey, key.c_str(), 0,NULL,(BYTE*)&read_value,&read_size);
		RegCloseKey( Hkey );
		return read_value;
	#endif
	#ifndef WIN32
		return refptr_Gconf_Client->get_float( settings_maindir + key );
	#endif
}
	
int Settings::get_int(const std::string & key)
{
	return (int) get_double( key );
}

bool Settings::get_bool(const std::string &key )
{
	return (bool) get_double( key );
}
#ifdef WIN32
	#include <windows.h>
#endif
#ifndef WIN32
	#include <gconfmm.h>
#endif

#include <string>

class Settings
{
public:
	Settings();
	Settings(const std::string & application_name);//ONLY the name....
	void set( const std::string & key, const std::string & value);//set strings
	void set( const std::string & key, double value);//set integers,doubles,booleans
	std::string get_string(const std::string & key);
	double get_double(const std::string & key);
	int get_int(const std::string & key);
	bool get_bool(const std::string & key);
	
private:
	#ifdef WIN32
		HKEY Hkey;
		unsigned long read_size;
		double read_value;
		char str_value[500];//change as needed
	#endif
	#ifndef WIN32
			Glib::RefPtr<Gnome::Conf::Client> refptr_Gconf_Client;
	#endif
	
	std::string settings_maindir;

};


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]