Update Registry.cpp to SVN 20830
[TortoiseGit.git] / src / Utils / Registry.cpp
blobfa28ac379bbb3457daeff359c93dfaf2ef62259f
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006,2008-2010 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "registry.h"
22 //////////////////////////////////////////////////////////////////////////////////////////////
24 #ifdef __CSTRINGT_H__
25 CRegBase::CRegBase()
29 CRegBase::CRegBase (const CString& key, bool force, HKEY base, REGSAM sam)
30 : CRegBaseCommon<CString> (key, force, base, sam)
32 m_key.TrimLeft(_T("\\"));
33 int backslashpos = m_key.ReverseFind('\\');
34 m_path = m_key.Left(backslashpos);
35 m_path.TrimRight(_T("\\"));
36 m_key = m_key.Mid(backslashpos);
37 m_key.Trim(_T("\\"));
39 #endif
41 //////////////////////////////////////////////////////////////////////////////////////////////
43 CRegStdBase::CRegStdBase()
47 CRegStdBase::CRegStdBase (const tstring& key, bool force, HKEY base, REGSAM sam)
48 : CRegBaseCommon<tstring> (key, force, base, sam)
50 tstring::size_type pos = key.find_last_of(_T('\\'));
51 m_path = key.substr(0, pos);
52 m_key = key.substr(pos + 1);
55 //////////////////////////////////////////////////////////////////////////////////////////////
57 #ifdef __ATLTYPES_H__ // defines CRect
58 CRegRect::CRegRect(void)
59 : CRegTypedBase<CRect, CRegBase>(CRect(0,0,0,0))
63 CRegRect::CRegRect(const CString& key, const CRect& def, bool force, HKEY base, REGSAM sam)
64 : CRegTypedBase<CRect, CRegBase> (key, def, force, base, sam)
66 read();
69 void CRegRect::InternalRead (HKEY hKey, CRect& value)
71 DWORD size = 0;
72 DWORD type = 0;
73 LastError = RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
75 if (LastError == ERROR_SUCCESS)
77 auto_buffer<char> buffer (size);
78 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
80 ASSERT(type==REG_BINARY);
81 value = CRect((LPRECT)buffer.get());
86 void CRegRect::InternalWrite (HKEY hKey, const CRect& value)
88 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)(LPCRECT)value, sizeof(value));
91 #endif
93 //////////////////////////////////////////////////////////////////////////////////////////////
95 #ifdef __ATLTYPES_H__ // defines CPoint
96 CRegPoint::CRegPoint(void)
97 : CRegTypedBase<CPoint, CRegBase>(CPoint(0,0))
101 CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base, REGSAM sam)
102 : CRegTypedBase<CPoint, CRegBase> (key, def, force, base, sam)
104 read();
107 void CRegPoint::InternalRead (HKEY hKey, CPoint& value)
109 DWORD size = 0;
110 DWORD type = 0;
111 LastError = RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
113 if (LastError == ERROR_SUCCESS)
115 auto_buffer<char> buffer(size);
116 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
118 ASSERT(type==REG_BINARY);
119 value = CPoint(*(POINT*)buffer.get());
124 void CRegPoint::InternalWrite (HKEY hKey, const CPoint& value)
126 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)&value, sizeof(value));
128 #endif
130 /////////////////////////////////////////////////////////////////////
132 #ifdef __AFXCOLL_H__ // defines CStringList
133 CRegistryKey::CRegistryKey(const CString& key, HKEY base, REGSAM sam)
135 m_base = base;
136 m_hKey = NULL;
137 m_sam = sam;
138 m_path = key;
139 m_path.TrimLeft(_T("\\"));
142 CRegistryKey::~CRegistryKey()
144 if (m_hKey)
145 RegCloseKey(m_hKey);
148 DWORD CRegistryKey::createKey()
150 DWORD disp;
151 DWORD rc = RegCreateKeyEx(m_base, m_path, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE|m_sam, NULL, &m_hKey, &disp);
152 if (rc != ERROR_SUCCESS)
154 return rc;
156 return RegCloseKey(m_hKey);
159 DWORD CRegistryKey::removeKey()
161 RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE|m_sam, &m_hKey);
162 return SHDeleteKey(m_base, (LPCTSTR)m_path);
165 bool CRegistryKey::getValues(CStringList& values)
167 values.RemoveAll();
169 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
171 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
173 TCHAR value[255];
174 DWORD size = _countof(value);
175 rc = RegEnumValue(m_hKey, i, value, &size, NULL, NULL, NULL, NULL);
176 if (rc == ERROR_SUCCESS)
178 values.AddTail(value);
183 return values.GetCount() > 0;
186 bool CRegistryKey::getSubKeys(CStringList& subkeys)
188 subkeys.RemoveAll();
190 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
192 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
194 TCHAR value[1024];
195 DWORD size = _countof(value);
196 FILETIME last_write_time;
197 rc = RegEnumKeyEx(m_hKey, i, value, &size, NULL, NULL, NULL, &last_write_time);
198 if (rc == ERROR_SUCCESS)
200 subkeys.AddTail(value);
205 return subkeys.GetCount() > 0;
207 #endif