Improve readability of version string
[TortoiseGit.git] / src / Utils / Registry.cpp
blobabe96784b6d9d6050f21426d458ada0f246b7b69
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013, 2016 - 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, nullptr, &type, nullptr, (LPDWORD)&size);
76 if (LastError == ERROR_SUCCESS)
78 auto buffer = std::make_unique<char[]>(size);
79 if ((LastError = RegQueryValueEx(hKey, m_key, nullptr, &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, nullptr, &type, nullptr, (LPDWORD)&size);
114 if (LastError == ERROR_SUCCESS)
116 auto buffer = std::make_unique<char[]>(size);
117 if ((LastError = RegQueryValueEx(hKey, m_key, nullptr, &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 = nullptr;
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, nullptr, &m_hKey, &disp);
153 if (rc != ERROR_SUCCESS)
154 return rc;
155 return RegCloseKey(m_hKey);
158 DWORD CRegistryKey::removeKey()
160 RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE|m_sam, &m_hKey);
161 return SHDeleteKey(m_base, (LPCTSTR)m_path);
164 bool CRegistryKey::getValues(CStringList& values)
166 values.RemoveAll();
168 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
170 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; ++i)
172 TCHAR value[255] = { 0 };
173 DWORD size = _countof(value);
174 rc = RegEnumValue(m_hKey, i, value, &size, nullptr, nullptr, nullptr, nullptr);
175 if (rc == ERROR_SUCCESS)
176 values.AddTail(value);
180 return !values.IsEmpty();
183 bool CRegistryKey::getSubKeys(CStringList& subkeys)
185 subkeys.RemoveAll();
187 if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
189 for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; ++i)
191 TCHAR value[1024] = { 0 };
192 DWORD size = _countof(value);
193 FILETIME last_write_time;
194 rc = RegEnumKeyEx(m_hKey, i, value, &size, nullptr, nullptr, nullptr, &last_write_time);
195 if (rc == ERROR_SUCCESS)
196 subkeys.AddTail(value);
200 return !subkeys.IsEmpty();
202 #endif