Update rebase documentation
[TortoiseGit.git] / src / Utils / Registry.cpp
blob797c37f75cd020d44d1cc38caa5b5d34a3bb2095
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013 - TortoiseGit
4 // Copyright (C) 2003-2006,2008-2010, 2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "registry.h"
23 //////////////////////////////////////////////////////////////////////////////////////////////
25 #if defined(__CSTRINGT_H__) || defined(__ATLSTR_H__)
26 CRegBase::CRegBase()
30 CRegBase::CRegBase (const CString& key, bool force, HKEY base, REGSAM sam)
31 : CRegBaseCommon<CString> (key, force, base, sam)
33 m_key.TrimLeft(_T("\\"));
34 int backslashpos = m_key.ReverseFind('\\');
35 m_path = m_key.Left(backslashpos);
36 m_path.TrimRight(_T("\\"));
37 m_key = m_key.Mid(backslashpos);
38 m_key.Trim(_T("\\"));
40 #endif
42 //////////////////////////////////////////////////////////////////////////////////////////////
44 CRegStdBase::CRegStdBase()
48 CRegStdBase::CRegStdBase (const tstring& key, bool force, HKEY base, REGSAM sam)
49 : CRegBaseCommon<tstring> (key, force, base, sam)
51 tstring::size_type pos = key.find_last_of(_T('\\'));
52 m_path = key.substr(0, pos);
53 m_key = key.substr(pos + 1);
56 //////////////////////////////////////////////////////////////////////////////////////////////
58 #ifdef __ATLTYPES_H__ // defines CRect
59 CRegRect::CRegRect(void)
60 : CRegTypedBase<CRect, CRegBase>(CRect(0,0,0,0))
64 CRegRect::CRegRect(const CString& key, const CRect& def, bool force, HKEY base, REGSAM sam)
65 : CRegTypedBase<CRect, CRegBase> (key, def, force, base, sam)
67 read();
70 void CRegRect::InternalRead (HKEY hKey, CRect& value)
72 DWORD size = 0;
73 DWORD type = 0;
74 LastError = RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
76 if (LastError == ERROR_SUCCESS)
78 std::unique_ptr<char[]> buffer (new char[size]);
79 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
81 ASSERT(type==REG_BINARY);
82 value = CRect((LPRECT)buffer.get());
87 void CRegRect::InternalWrite (HKEY hKey, const CRect& value)
89 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)(LPCRECT)value, sizeof(value));
92 #endif
94 //////////////////////////////////////////////////////////////////////////////////////////////
96 #ifdef __ATLTYPES_H__ // defines CPoint
97 CRegPoint::CRegPoint(void)
98 : CRegTypedBase<CPoint, CRegBase>(CPoint(0,0))
102 CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base, REGSAM sam)
103 : CRegTypedBase<CPoint, CRegBase> (key, def, force, base, sam)
105 read();
108 void CRegPoint::InternalRead (HKEY hKey, CPoint& value)
110 DWORD size = 0;
111 DWORD type = 0;
112 LastError = RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
114 if (LastError == ERROR_SUCCESS)
116 std::unique_ptr<char[]> buffer(new char[size]);
117 if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
119 ASSERT(type==REG_BINARY);
120 value = CPoint(*(POINT*)buffer.get());
125 void CRegPoint::InternalWrite (HKEY hKey, const CPoint& value)
127 LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)&value, sizeof(value));
129 #endif
131 /////////////////////////////////////////////////////////////////////
133 #ifdef __AFXCOLL_H__ // defines CStringList
134 CRegistryKey::CRegistryKey(const CString& key, HKEY base, REGSAM sam)
136 m_base = base;
137 m_hKey = NULL;
138 m_sam = sam;
139 m_path = key;
140 m_path.TrimLeft(_T("\\"));
143 CRegistryKey::~CRegistryKey()
145 if (m_hKey)
146 RegCloseKey(m_hKey);
149 DWORD CRegistryKey::createKey()
151 DWORD disp;
152 DWORD rc = RegCreateKeyEx(m_base, m_path, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE|m_sam, NULL, &m_hKey, &disp);
153 if (rc != ERROR_SUCCESS)
155 return rc;
157 return RegCloseKey(m_hKey);
160 DWORD CRegistryKey::removeKey()
162 RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE|m_sam, &m_hKey);
163 return SHDeleteKey(m_base, (LPCTSTR)m_path);
166 bool CRegistryKey::getValues(CStringList& values)
168 values.RemoveAll();
170 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
172 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; ++i)
174 TCHAR value[255] = { 0 };
175 DWORD size = _countof(value);
176 rc = RegEnumValue(m_hKey, i, value, &size, NULL, NULL, NULL, NULL);
177 if (rc == ERROR_SUCCESS)
179 values.AddTail(value);
184 return !values.IsEmpty();
187 bool CRegistryKey::getSubKeys(CStringList& subkeys)
189 subkeys.RemoveAll();
191 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
193 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; ++i)
195 TCHAR value[1024] = { 0 };
196 DWORD size = _countof(value);
197 FILETIME last_write_time;
198 rc = RegEnumKeyEx(m_hKey, i, value, &size, NULL, NULL, NULL, &last_write_time);
199 if (rc == ERROR_SUCCESS)
201 subkeys.AddTail(value);
206 return !subkeys.IsEmpty();
208 #endif