forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinRegistryConfiguration.cpp
More file actions
144 lines (123 loc) · 3.36 KB
/
WinRegistryConfiguration.cpp
File metadata and controls
144 lines (123 loc) · 3.36 KB
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
//
// WinRegistryConfiguration.cpp
//
// Library: Util
// Package: Windows
// Module: WinRegistryConfiguration
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/WinRegistryConfiguration.h"
#include "Poco/Util/WinRegistryKey.h"
#include "Poco/NumberFormatter.h"
#include "Poco/NumberParser.h"
#include "Poco/Exception.h"
namespace Poco {
namespace Util {
WinRegistryConfiguration::WinRegistryConfiguration(const std::string& rootPath, REGSAM extraSam): _rootPath(rootPath), _extraSam(extraSam)
{
// rootPath must end with backslash
if (!_rootPath.empty())
{
if (_rootPath[_rootPath.length() - 1] != '\\')
_rootPath += '\\';
}
}
WinRegistryConfiguration::~WinRegistryConfiguration()
{
}
bool WinRegistryConfiguration::getRaw(const std::string& key, std::string& value) const
{
std::string keyName;
std::string fullPath = _rootPath + convertToRegFormat(key, keyName);
WinRegistryKey aKey(fullPath, true, _extraSam);
bool exists = aKey.exists(keyName);
if (exists)
{
WinRegistryKey::Type type = aKey.type(keyName);
switch (type)
{
case WinRegistryKey::REGT_STRING:
value = aKey.getString(keyName);
break;
case WinRegistryKey::REGT_STRING_EXPAND:
value = aKey.getStringExpand(keyName);
break;
case WinRegistryKey::REGT_BINARY:
{
std::vector<char> tmp = aKey.getBinary(keyName);
value.assign(tmp.begin(), tmp.end());
}
break;
case WinRegistryKey::REGT_DWORD:
value = Poco::NumberFormatter::format(aKey.getInt(keyName));
break;
#if defined(POCO_HAVE_INT64)
case WinRegistryKey::REGT_QWORD:
value = Poco::NumberFormatter::format(aKey.getInt64(keyName));
break;
#endif
default:
exists = false;
}
}
return exists;
}
void WinRegistryConfiguration::setRaw(const std::string& key, const std::string& value)
{
std::string keyName;
std::string fullPath = _rootPath + convertToRegFormat(key, keyName);
WinRegistryKey aKey(fullPath, false, _extraSam);
aKey.setString(keyName, value);
}
void WinRegistryConfiguration::enumerate(const std::string& key, Keys& range) const
{
std::string keyName;
std::string fullPath = _rootPath + convertToRegFormat(key, keyName);
if (fullPath.empty())
{
// return all root level keys
#if defined(_WIN32_WCE)
range.push_back("HKEY_CLASSES_ROOT");
range.push_back("HKEY_CURRENT_USER");
range.push_back("HKEY_LOCAL_MACHINE");
range.push_back("HKEY_USERS");
#else
range.push_back("HKEY_CLASSES_ROOT");
range.push_back("HKEY_CURRENT_CONFIG");
range.push_back("HKEY_CURRENT_USER");
range.push_back("HKEY_LOCAL_MACHINE");
range.push_back("HKEY_PERFORMANCE_DATA");
range.push_back("HKEY_USERS");
#endif
}
else
{
fullPath += '\\';
fullPath += keyName;
WinRegistryKey aKey(fullPath, true, _extraSam);
aKey.values(range);
aKey.subKeys(range);
}
}
void WinRegistryConfiguration::removeRaw(const std::string& key)
{
throw Poco::NotImplementedException("Removing a key in a WinRegistryConfiguration");
}
std::string WinRegistryConfiguration::convertToRegFormat(const std::string& key, std::string& value) const
{
std::size_t pos = key.rfind('.');
if (pos == std::string::npos)
{
value = key;
return std::string();
}
std::string prefix(key.substr(0,pos));
value = key.substr(pos + 1);
Poco::translateInPlace(prefix, ".", "\\");
return prefix;
}
} } // namespace Poco::Util