summaryrefslogtreecommitdiff
path: root/src/registry.cpp
blob: e16d1094f15785b38ed8d2248e96b974c4973b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//  w32dlib - Win32 Dialog Helpers
//
//  Copyright (C) 2005  Ian Cowburn (ianc@noddybox.demon.co.uk)
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  -------------------------------------------------------------------------
//
#include <cstring>
#include "w32dlib/registry.h"

namespace W32DLib
{

// ------------------------------------------------------------
//
Registry::Registry(HKEY root, const char *path, bool permanent)
{
    m_openres=RegCreateKeyEx(root,
			     path,
			     0,
			     0,
			     permanent ? REG_OPTION_NON_VOLATILE:
					 REG_OPTION_VOLATILE,
			     KEY_WRITE|KEY_READ,
			     0,
			     &m_key,
			     0);
}


// ------------------------------------------------------------
//
Registry::Registry(HKEY root, const char *path)
{
    m_openres=RegOpenKeyEx(root,path,0,KEY_READ,&m_key);
}


// ------------------------------------------------------------
//
Registry::~Registry()
{
    if (m_openres==ERROR_SUCCESS)
    {
    	RegCloseKey(m_key);
    }
}


// ------------------------------------------------------------
//
bool Registry::IsOpen()
{
    return m_openres==ERROR_SUCCESS;
}


// ------------------------------------------------------------
//
bool Registry::Read(const char *name, bool& val)
{
    unsigned i=0;
    bool res;

    res=Read(name,i);

    if (res)
    {
    	val=(i!=0);
    }

    return res;
}


// ------------------------------------------------------------
//
bool Registry::Read(const char *name, unsigned& val)
{
    DWORD size=sizeof(val);
    DWORD type;
    unsigned i;

    if (RegQueryValueEx(m_key,name,0,&type,(LPBYTE)&i,&size)==ERROR_SUCCESS)
    {
    	if (type==REG_DWORD)
	{
	    val=i;
	    return true;
	}
    }

    return false;
}


// ------------------------------------------------------------
//
bool Registry::Read(const char *name, std::string& val)
{
    bool res=false;
    DWORD size;
    DWORD type;

    if (RegQueryValueEx(m_key,name,0,&type,0,&size)==ERROR_SUCCESS)
    {
	if (type==REG_SZ || type==REG_EXPAND_SZ)
	{
	    char *buff=new char[size];

	    if (RegQueryValueEx(m_key,name,0,0,(LPBYTE)buff,&size)
	    						==ERROR_SUCCESS)
	    {
		res=true;
		val=buff;
	    }

	    delete[] buff;
	}
    }

    return res;
}


// ------------------------------------------------------------
//
bool Registry::Write(const char *name, bool val)
{
    unsigned i=val?1:0;

    return Write(name,i);
}


// ------------------------------------------------------------
//
bool Registry::Write(const char *name, unsigned val)
{
    return RegSetValueEx(m_key,
			 name,
			 0,
			 REG_DWORD,
			 (LPBYTE)&val,
			 sizeof(val))==ERROR_SUCCESS;
}


// ------------------------------------------------------------
//
bool Registry::Write(const char *name, const char *val)
{
    return RegSetValueEx(m_key,
			 name,
			 0,
			 REG_SZ,
			 (LPBYTE)val,
			 std::strlen(val)+1)==ERROR_SUCCESS;
}



// ------------------------------------------------------------
//
bool Registry::Write(const char *name, const std::string& val)
{
    return RegSetValueEx(m_key,
			 name,
			 0,
			 REG_SZ,
			 (LPBYTE)val.c_str(),
			 val.length()+1)==ERROR_SUCCESS;
}



};	// namespace W32DLib

// END OF FILE