Textual changes
[TortoiseGit.git] / src / Utils / Registry.cpp
blob391453e62a5afdbc5d1e4916f08385ab0d58440a
1 // TortoiseSVN - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006,2008 - 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)
30 : CRegBaseCommon<CString> (key, force, base)
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 stdstring& key, bool force, HKEY base)
48 : CRegBaseCommon<stdstring> (key, force, base)
50 stdstring::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)
64 : CRegTypedBase<CRect, CRegBase> (key, def, force, base)
66 read();
69 void CRegRect::InternalRead (HKEY hKey, CRect& value)
71 DWORD size = 0;
72 DWORD type = 0;
73 RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
75 std::auto_ptr<char> buffer (new char[size]);
76 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
78 ASSERT(type==REG_BINARY);
79 value = CRect((LPRECT)buffer.get());
83 void CRegRect::InternalWrite (HKEY hKey, const CRect& value)
85 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)(LPCRECT)value, sizeof(value));
88 #endif
90 //////////////////////////////////////////////////////////////////////////////////////////////
92 #ifdef __ATLTYPES_H__ // defines CPoint
93 CRegPoint::CRegPoint(void)
94 : CRegTypedBase<CPoint, CRegBase>(CPoint(0,0))
98 CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base)
99 : CRegTypedBase<CPoint, CRegBase> (key, def, force, base)
101 read();
104 void CRegPoint::InternalRead (HKEY hKey, CPoint& value)
106 DWORD size = 0;
107 DWORD type = 0;
108 RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
110 std::auto_ptr<char> buffer(new char[size]);
111 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
113 ASSERT(type==REG_BINARY);
114 value = CPoint(*(POINT*)buffer.get());
118 void CRegPoint::InternalWrite (HKEY hKey, const CPoint& value)
120 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)&value, sizeof(value));
122 #endif
124 /////////////////////////////////////////////////////////////////////
126 #ifdef __AFXCOLL_H__ // defines CStringList
127 CRegistryKey::CRegistryKey(const CString& key, HKEY base)
129 m_base = base;
130 m_hKey = NULL;
131 m_path = key;
132 m_path.TrimLeft(_T("\\"));
135 CRegistryKey::~CRegistryKey()
137 if (m_hKey)
138 RegCloseKey(m_hKey);
141 DWORD CRegistryKey::createKey()
143 DWORD disp;
144 DWORD rc = RegCreateKeyEx(m_base, m_path, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &m_hKey, &disp);
145 if (rc != ERROR_SUCCESS)
147 return rc;
149 return RegCloseKey(m_hKey);
152 DWORD CRegistryKey::removeKey()
154 RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE, &m_hKey);
155 return SHDeleteKey(m_base, (LPCTSTR)m_path);
158 bool CRegistryKey::getValues(CStringList& values)
160 values.RemoveAll();
162 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
164 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
166 TCHAR value[255];
167 DWORD size = sizeof value / sizeof TCHAR;
168 rc = RegEnumValue(m_hKey, i, value, &size, NULL, NULL, NULL, NULL);
169 if (rc == ERROR_SUCCESS)
171 values.AddTail(value);
176 return values.GetCount() > 0;
179 bool CRegistryKey::getSubKeys(CStringList& subkeys)
181 subkeys.RemoveAll();
183 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
185 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
187 TCHAR value[1024];
188 DWORD size = sizeof value / sizeof TCHAR;
189 FILETIME last_write_time;
190 rc = RegEnumKeyEx(m_hKey, i, value, &size, NULL, NULL, NULL, &last_write_time);
191 if (rc == ERROR_SUCCESS)
193 subkeys.AddTail(value);
198 return subkeys.GetCount() > 0;
200 #endif