1*32b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*32b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*32b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*32b1fd08SAndrew Rist  * distributed with this work for additional information
6*32b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*32b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*32b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
9*32b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*32b1fd08SAndrew Rist  *
11*32b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*32b1fd08SAndrew Rist  *
13*32b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*32b1fd08SAndrew Rist  * software distributed under the License is distributed on an
15*32b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*32b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
17*32b1fd08SAndrew Rist  * specific language governing permissions and limitations
18*32b1fd08SAndrew Rist  * under the License.
19*32b1fd08SAndrew Rist  *
20*32b1fd08SAndrew Rist  *************************************************************/
21*32b1fd08SAndrew Rist 
22*32b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "registry.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <Shlwapi.h>
27cdf0e10cSrcweir #include <assert.h>
28cdf0e10cSrcweir #include <algorithm>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #ifdef _MSC_VER
31cdf0e10cSrcweir #pragma warning(disable : 4786 4350)
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //-----------------------------------------------------
35cdf0e10cSrcweir /** Create instance and open the specified Registry key
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	@throws  RegistryWriteAccessDenyException
38cdf0e10cSrcweir 			 RegistryAccessDenyException
39cdf0e10cSrcweir 			 RegistryKeyNotFoundException
40cdf0e10cSrcweir */
RegistryKeyImpl(HKEY RootKey,const std::wstring & KeyName)41cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName) :
42cdf0e10cSrcweir 	m_hRootKey(RootKey),
43cdf0e10cSrcweir 	m_hSubKey(0),
44cdf0e10cSrcweir 	m_KeyName(KeyName),
45cdf0e10cSrcweir 	m_IsWriteable(false)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //-----------------------------------------------------
50cdf0e10cSrcweir /** Create instance and open the specified Registry key
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	@throws  RegistryWriteAccessDenyException
53cdf0e10cSrcweir 			 RegistryAccessDenyException
54cdf0e10cSrcweir 			 RegistryKeyNotFoundException
55cdf0e10cSrcweir */
RegistryKeyImpl(HKEY RootKey)56cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey) :
57cdf0e10cSrcweir 	m_hRootKey(RootKey),
58cdf0e10cSrcweir 	m_hSubKey(0),
59cdf0e10cSrcweir 	m_IsWriteable(false)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir //-----------------------------------------------------
64cdf0e10cSrcweir /** Create an instances of the specified Registry key,
65cdf0e10cSrcweir 	the key is assumed to be already opened.
66cdf0e10cSrcweir */
RegistryKeyImpl(HKEY RootKey,HKEY SubKey,const std::wstring & KeyName,bool Writeable)67cdf0e10cSrcweir RegistryKeyImpl::RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable) :
68cdf0e10cSrcweir 	m_hRootKey(RootKey),
69cdf0e10cSrcweir 	m_hSubKey(SubKey),
70cdf0e10cSrcweir 	m_KeyName(KeyName),
71cdf0e10cSrcweir 	m_IsWriteable(Writeable)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir //-----------------------------------------------------
76cdf0e10cSrcweir /**
77cdf0e10cSrcweir */
~RegistryKeyImpl()78cdf0e10cSrcweir RegistryKeyImpl::~RegistryKeyImpl()
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	if (IsOpen())
81cdf0e10cSrcweir 		Close();
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir //############################################
86cdf0e10cSrcweir // Queries
87cdf0e10cSrcweir //############################################
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
90cdf0e10cSrcweir //-----------------------------------------------------
91cdf0e10cSrcweir /** The name of the key at hand, maybe empty
92cdf0e10cSrcweir 	if this is any of the root keys
93cdf0e10cSrcweir */
GetName() const94cdf0e10cSrcweir std::wstring RegistryKeyImpl::GetName() const
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	return m_KeyName;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir //-----------------------------------------------------
100cdf0e10cSrcweir /**
101cdf0e10cSrcweir */
IsOpen() const102cdf0e10cSrcweir bool RegistryKeyImpl::IsOpen() const
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	return m_hSubKey != 0;
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir //-----------------------------------------------------
108cdf0e10cSrcweir /** Is this one of the root keys
109cdf0e10cSrcweir 	HKEY_CLASSES_ROOT
110cdf0e10cSrcweir 	HKEY_CURRENT_USER
111cdf0e10cSrcweir 	etc.
112cdf0e10cSrcweir */
IsRootKey() const113cdf0e10cSrcweir bool RegistryKeyImpl::IsRootKey() const
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	return (0 == m_KeyName.length());
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //-----------------------------------------------------
119cdf0e10cSrcweir /** Do we have write access on the key at hand
120cdf0e10cSrcweir */
IsWriteable() const121cdf0e10cSrcweir bool RegistryKeyImpl::IsWriteable() const
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	return m_IsWriteable;
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //-----------------------------------------------------
127cdf0e10cSrcweir /** Convenience function to determine if the
128cdf0e10cSrcweir 	Registry key at hand has the specified
129cdf0e10cSrcweir 	value
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	@precond IsOpen = true
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	throws RegistryAccessDenyException
134cdf0e10cSrcweir */
HasValue(const std::wstring & Name) const135cdf0e10cSrcweir bool RegistryKeyImpl::HasValue(const std::wstring& Name) const
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	StringListPtr names = GetSubValueNames();
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	StringList::iterator iter_end = names->end();
140cdf0e10cSrcweir 	StringList::iterator iter = std::find(names->begin(), iter_end, Name);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	return (iter != iter_end);
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir struct CompareNamesCaseInsensitive
146cdf0e10cSrcweir {
CompareNamesCaseInsensitiveCompareNamesCaseInsensitive147cdf0e10cSrcweir 	CompareNamesCaseInsensitive(const std::wstring& Name) :
148cdf0e10cSrcweir 		name_(Name)
149cdf0e10cSrcweir 	{}
150cdf0e10cSrcweir 
operator ()CompareNamesCaseInsensitive151cdf0e10cSrcweir 	bool operator() (const std::wstring& value)
152cdf0e10cSrcweir 	{
153cdf0e10cSrcweir 		return (0 == StrCmpI(value.c_str(), name_.c_str()));
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	std::wstring name_;
157cdf0e10cSrcweir };
158cdf0e10cSrcweir 
159cdf0e10cSrcweir //-----------------------------------------------------
160cdf0e10cSrcweir /** Convenience function to determine if the
161cdf0e10cSrcweir 	Registry key at hand has the specified
162cdf0e10cSrcweir 	sub-key
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	@precond IsOpen = true
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	throws RegistryAccessDenyException
167cdf0e10cSrcweir */
HasSubKey(const std::wstring & Name) const168cdf0e10cSrcweir bool RegistryKeyImpl::HasSubKey(const std::wstring& Name) const
169cdf0e10cSrcweir {
170cdf0e10cSrcweir 	StringListPtr names = GetSubKeyNames();
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 	StringList::iterator iter_end = names->end();
173cdf0e10cSrcweir 	StringList::iterator iter = std::find_if(names->begin(), iter_end, CompareNamesCaseInsensitive(Name));
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	return (iter != iter_end);
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir //-----------------------------------------------------
179cdf0e10cSrcweir /**
180cdf0e10cSrcweir */
Close()181cdf0e10cSrcweir void RegistryKeyImpl::Close()
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	if (RegCloseKey(m_hSubKey) != ERROR_SUCCESS) {
184cdf0e10cSrcweir         assert(false);
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	m_hSubKey = 0;
188cdf0e10cSrcweir 	m_IsWriteable = false;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir //-----------------------------------------------------
192cdf0e10cSrcweir /** Copies the specified value from RegistryKey to
193cdf0e10cSrcweir 	the registry key at hand, if a value with this
194cdf0e10cSrcweir 	name already exist under the registry key at hand
195cdf0e10cSrcweir 	it will be overwritten
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	@precond IsOpen = true
198cdf0e10cSrcweir 			 IsWriteable = true
199cdf0e10cSrcweir 			 RegistryKey.HasSubValue(Name) = true
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	@throws RegistryIOException
202cdf0e10cSrcweir 			RegistryWriteAccessDeniedException
203cdf0e10cSrcweir 			RegistryValueNotFoundException
204cdf0e10cSrcweir */
CopyValue(const RegistryKey & RegistryKey,const std::wstring & Name)205cdf0e10cSrcweir void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name)
206cdf0e10cSrcweir {
207cdf0e10cSrcweir 	assert(RegistryKey->HasValue(Name));
208cdf0e10cSrcweir #ifdef __MINGW32__
209cdf0e10cSrcweir 	SetValue((const RegistryValue&)(RegistryKey->GetValue(Name)));
210cdf0e10cSrcweir #else
211cdf0e10cSrcweir 	SetValue(RegistryKey->GetValue(Name));
212cdf0e10cSrcweir #endif
213cdf0e10cSrcweir     assert(HasValue(Name));
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir /** Copies the specified value from RegistryKey to
217cdf0e10cSrcweir 	the registry key at hand under a new name,
218cdf0e10cSrcweir 	if a value with this name already exist there
219cdf0e10cSrcweir 	it will be overwritten
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	@precond IsOpen = true
222cdf0e10cSrcweir 			 IsWriteable = true
223cdf0e10cSrcweir 			 RegistryKey.HasSubValue(Name) = true
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	@throws RegistryIOException
226cdf0e10cSrcweir 			RegistryWriteAccessDeniedException
227cdf0e10cSrcweir 			RegistryValueNotFoundException
228cdf0e10cSrcweir */
CopyValue(const RegistryKey & RegistryKey,const std::wstring & Name,const std::wstring & NewName)229cdf0e10cSrcweir void RegistryKeyImpl::CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName)
230cdf0e10cSrcweir {
231cdf0e10cSrcweir 	assert(RegistryKey->HasValue(Name));
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	RegistryValue RegVal = RegistryKey->GetValue(Name);
234cdf0e10cSrcweir 	RegVal->SetName(NewName);
235cdf0e10cSrcweir 	SetValue(RegVal);
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     assert(HasValue(NewName));
238cdf0e10cSrcweir }
239