summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/unix/CPP/Windows/Registry.cpp
blob: 74e255df4abb755076c9d361eb5f00ac78fdc64b (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Windows/Registry.cpp

#include "StdAfx.h"

#ifndef _UNICODE
#include "Common/StringConvert.h"
#endif
#include "Windows/Registry.h"

#include <wx/config.h>

class HKEY_Impl
{
  public:
	wxString path;
	HKEY_Impl(wxString a) : path(a) {}
};

namespace NWindows {
namespace NRegistry {

#define ERROR_SET_VALUE (E_INVALIDARG) // FIXME
#define ERROR_GET_VALUE (E_INVALIDARG) // FIXME
#define PROGRAM_NAME L"p7zip"

static wxConfig * g_config = 0;
static int        g_config_ref = 0;

static void configAddRef() {
	if (g_config == 0) {
		g_config = new wxConfig(PROGRAM_NAME);
		g_config->Flush(true);
		wxConfigBase::Set(g_config);
	}
	g_config_ref++;
}

static void configSubRef() {
	if (g_config_ref >= 1)
	{
		g_config_ref--;
		if (g_config_ref == 0) {
			delete g_config;
			g_config = 0;
			wxConfigBase::Set(NULL);
		} else {
			g_config->Flush(true);
		}
	}
}

	LONG CKey::Close()
	{
		if (_object) 
		{
			configSubRef();
			delete _object;
		}
		_object = 0;
		return ERROR_SUCCESS;
	}

	LONG CKey::Create(HKEY parentKey, LPCTSTR keyName)
	{
		Close();

		configAddRef();

		wxString path;

		if (parentKey == HKEY_CURRENT_USER) {
			path=L"/" + wxString(keyName);
		} else {
			path = parentKey->path + L"/" + wxString(keyName);
		}
		_object = new HKEY_Impl(path);
		return ERROR_SUCCESS;
	}
	LONG CKey::Open(HKEY parentKey, LPCTSTR keyName, REGSAM accessMask)
	{
		Close();

		configAddRef();

		wxString path;

		if (parentKey == HKEY_CURRENT_USER) {
			path=L"/" + wxString(keyName);
		} else {
			path = parentKey->path + L"/" + wxString(keyName);
		}
		_object = new HKEY_Impl(path);
		return ERROR_SUCCESS;
	}

	LONG CKey::RecurseDeleteKey(LPCTSTR subKeyName)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->DeleteGroup(subKeyName);
		if (ret) return ERROR_SUCCESS;
		return ERROR_GET_VALUE;
	}

	LONG CKey::DeleteValue(LPCTSTR name)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->DeleteEntry(name);
		if (ret) return ERROR_SUCCESS;
		return ERROR_GET_VALUE;
	}

	LONG CKey::QueryValue(LPCTSTR name, UInt32 &value)
	{
		g_config->SetPath(_object->path);
		long val;
		bool ret = g_config->Read(name,&val);
		if (ret) {
			value = (UInt32)val;
			return ERROR_SUCCESS;
		}
		return ERROR_GET_VALUE;
	}

	LONG CKey::QueryValue(LPCTSTR name, bool &value)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->Read(name,&value);
		if (ret) return ERROR_SUCCESS;
		return ERROR_GET_VALUE;
	}

	LONG CKey::QueryValue(LPCTSTR name, CSysString &value)
	{
		g_config->SetPath(_object->path);
		wxString val;
		bool ret = g_config->Read(name,&val);
		if (ret) {
			value = val;
			return ERROR_SUCCESS;
		}
		return ERROR_GET_VALUE;
	}

LONG CKey::GetValue_IfOk(LPCTSTR name, UInt32 &value)
{
  UInt32 newVal;
  LONG res = QueryValue(name, newVal);
  if (res == ERROR_SUCCESS)
    value = newVal;
  return res;
}

LONG CKey::GetValue_IfOk(LPCTSTR name, bool &value)
{
  bool newVal;
  LONG res = QueryValue(name, newVal);
  if (res == ERROR_SUCCESS)
    value = newVal;
  return res;
}


	LONG CKey::SetValue(LPCTSTR valueName, UInt32 value)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->Write(valueName,(long)value);
		if (ret == true) return ERROR_SUCCESS;
		return ERROR_SET_VALUE;
	}
	LONG CKey::SetValue(LPCTSTR valueName, bool value)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->Write(valueName,value);
		if (ret == true) return ERROR_SUCCESS;
		return ERROR_SET_VALUE;
	}
	LONG CKey::SetValue(LPCTSTR valueName, LPCTSTR value)
	{
		g_config->SetPath(_object->path);
		bool ret = g_config->Write(valueName,value);
		if (ret == true) return ERROR_SUCCESS;
		return ERROR_SET_VALUE;
	}

	LONG CKey::SetValue(LPCTSTR name, const void *value, UInt32 size)
	{
		static char hexa[] = "0123456789ABCDEF";
		/* FIXME
		MYASSERT(value != NULL);
		MYASSERT(_object != NULL);
		return RegSetValueEx(_object, name, NULL, REG_BINARY, (const BYTE *)value, size);
		*/
		BYTE *buf = (BYTE *)value;
		wxString str;
		for(UInt32 i=0;i<size;i++)
		{
			str += 	hexa[ (buf[i]>>4) & 0x0f];
			str += 	hexa[ buf[i] & 0x0f];
		}
		return SetValue(name,(LPCTSTR)str);
	}

	LONG CKey::EnumKeys(CSysStringVector &keyNames)
	{
		g_config->SetPath(_object->path);
		keyNames.Clear();
		// enumeration variables
		wxString str;
		long dummy;
		bool bCont = g_config->GetFirstEntry(str, dummy);
		while ( bCont ) {
			keyNames.Add((const TCHAR *)str);
			bCont = g_config->GetNextEntry(str, dummy);
		}

		// now all groups...
		bCont = g_config->GetFirstGroup(str, dummy);
		while ( bCont ) {
			keyNames.Add((const TCHAR *)str);
			bCont = g_config->GetNextGroup(str, dummy);
  		}
		return ERROR_SUCCESS;
	}

	LONG CKey::QueryValue(LPCTSTR name, void *value, UInt32 &dataSize)
	{
		g_config->SetPath(_object->path);
		wxString str;
		bool ret = g_config->Read(name,&str);
		if (ret == false) return ERROR_GET_VALUE;

		size_t l =  str.Len() / 2;
		if (l > dataSize) l = dataSize;
		else              dataSize=l;

		BYTE *buf = (BYTE *)value;
		for(UInt32 i=0;i<dataSize;i++)
		{
			char cval[3];
			cval[0] = (char)str[2*i];
			cval[1] = (char)str[2*i+1];
			cval[2] = 0;
			unsigned uval = 0;
			sscanf(cval,"%x",&uval);
			buf[i]=(BYTE)uval;
		}

		return ERROR_SUCCESS;
	}


	LONG CKey::QueryValue(LPCTSTR name, CByteBuffer &value, UInt32 &dataSize)
	{
		g_config->SetPath(_object->path);
		wxString str;
		bool ret = g_config->Read(name,&str);
		if (ret == false) return ERROR_GET_VALUE;

		dataSize =  str.Len() / 2;
		value.SetCapacity(dataSize);
		return QueryValue(name, (BYTE *)value, dataSize);
	}


LONG CKey::SetValue_Strings(LPCTSTR valueName, const UStringVector &strings)
{
  UInt32 numChars = 0;
  int i;
  for (i = 0; i < strings.Size(); i++)
    numChars += strings[i].Length() + 1;
  CBuffer<wchar_t> buffer;
  buffer.SetCapacity(numChars);
  int pos = 0;
  for (i = 0; i < strings.Size(); i++)
  {
    const UString &s = strings[i];
    MyStringCopy((wchar_t *)buffer + pos, (const wchar_t *)s);
    pos += s.Length() + 1;
  }
  return SetValue(valueName, buffer, numChars * sizeof(wchar_t));
}

LONG CKey::GetValue_Strings(LPCTSTR valueName, UStringVector &strings)
{
  strings.Clear();
  CByteBuffer buffer;
  UInt32 dataSize;
  LONG res = QueryValue(valueName, buffer, dataSize);
  if (res != ERROR_SUCCESS)
    return res;
  if (dataSize % sizeof(wchar_t) != 0)
    return E_FAIL;
  const wchar_t *data = (const wchar_t *)(const Byte  *)buffer;
  int numChars = dataSize / sizeof(wchar_t);
  UString s;
  for (int i = 0; i < numChars; i++)
  {
    wchar_t c = data[i];
    if (c == 0)
    {
      strings.Add(s);
      s.Empty();
    }
    else
      s += c;
  }
  return res;
}


}
}