PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/win32/shellext/Registry.cpp

https://bitbucket.org/tortoisehg/hgtk/
C++ | 205 lines | 173 code | 17 blank | 15 comment | 13 complexity | 2b83ba812774f7423f3049aaa70cbea0 MD5 | raw file
  1// TortoiseSVN - a Windows shell extension for easy version control
  2
  3// Copyright (C) 2003-2006,2008-2009 - TortoiseSVN
  4
  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.
  9
 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.
 14
 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.
 18//
 19#include "stdafx.h"
 20#include "registry.h"
 21
 22//////////////////////////////////////////////////////////////////////////////////////////////
 23
 24#ifdef __CSTRINGT_H__
 25CRegBase::CRegBase()
 26{
 27}
 28
 29CRegBase::CRegBase (const CString& key, bool force, HKEY base, REGSAM sam)
 30    : CRegBaseCommon<CString> (key, force, base, sam)
 31{
 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("\\"));
 38}
 39#endif
 40
 41//////////////////////////////////////////////////////////////////////////////////////////////
 42
 43CRegStdBase::CRegStdBase()
 44{
 45}
 46
 47CRegStdBase::CRegStdBase (const tstring& key, bool force, HKEY base, REGSAM sam)
 48    : CRegBaseCommon<tstring> (key, force, base, sam)
 49{
 50    tstring::size_type pos = key.find_last_of(_T('\\'));
 51    if (pos != tstring::npos)
 52    {
 53        m_path = key.substr(0, pos);
 54        m_key = key.substr(pos + 1);
 55    }
 56}
 57
 58//////////////////////////////////////////////////////////////////////////////////////////////
 59
 60#ifdef __ATLTYPES_H__   // defines CRect 
 61CRegRect::CRegRect(void)
 62    : CRegTypedBase<CRect, CRegBase>(CRect(0,0,0,0))
 63{
 64}
 65
 66CRegRect::CRegRect(const CString& key, const CRect& def, bool force, HKEY base, REGSAM sam)
 67    : CRegTypedBase<CRect, CRegBase> (key, def, force, base, sam)
 68{
 69    read();
 70}
 71
 72void CRegRect::InternalRead (HKEY hKey, CRect& value)
 73{
 74    DWORD size = 0;
 75    DWORD type = 0;
 76    RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
 77
 78    auto_buffer<char> buffer (size);
 79    if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
 80    {
 81        ASSERT(type==REG_BINARY);
 82        value = CRect((LPRECT)buffer.get());
 83    }
 84}
 85
 86void CRegRect::InternalWrite (HKEY hKey, const CRect& value)
 87{
 88    LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)(LPCRECT)value, sizeof(value));
 89}
 90
 91#endif
 92
 93//////////////////////////////////////////////////////////////////////////////////////////////
 94
 95#ifdef __ATLTYPES_H__   // defines CPoint 
 96CRegPoint::CRegPoint(void)
 97    : CRegTypedBase<CPoint, CRegBase>(CPoint(0,0))
 98{
 99}
100
101CRegPoint::CRegPoint(const CString& key, const CPoint& def, bool force, HKEY base, REGSAM sam)
102    : CRegTypedBase<CPoint, CRegBase> (key, def, force, base, sam)
103{
104    read();
105}
106
107void CRegPoint::InternalRead (HKEY hKey, CPoint& value)
108{
109    DWORD size = 0;
110    DWORD type = 0;
111    RegQueryValueEx(hKey, m_key, NULL, &type, NULL, (LPDWORD) &size);
112
113    auto_buffer<char> buffer(size);
114    if ((LastError = RegQueryValueEx(hKey, m_key, NULL, &type, (BYTE*) buffer.get(), &size))==ERROR_SUCCESS)
115    {
116        ASSERT(type==REG_BINARY);
117        value = CPoint(*(POINT*)buffer.get());
118    }
119}
120
121void CRegPoint::InternalWrite (HKEY hKey, const CPoint& value)
122{
123    LastError = RegSetValueEx(hKey, m_key, 0, REG_BINARY, (BYTE *)&value, sizeof(value));
124}
125#endif
126
127/////////////////////////////////////////////////////////////////////
128
129#ifdef __AFXCOLL_H__   // defines CStringList 
130CRegistryKey::CRegistryKey(const CString& key, HKEY base, REGSAM sam)
131{
132    m_base = base;
133    m_hKey = NULL;
134    m_sam = sam;
135    m_path = key;
136    m_path.TrimLeft(_T("\\"));
137}
138
139CRegistryKey::~CRegistryKey()
140{
141    if (m_hKey)
142        RegCloseKey(m_hKey);
143}
144
145DWORD CRegistryKey::createKey()
146{
147    DWORD disp;
148    DWORD rc = RegCreateKeyEx(m_base, m_path, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE|m_sam, NULL, &m_hKey, &disp);
149    if (rc != ERROR_SUCCESS)
150    {
151        return rc;
152    }
153    return RegCloseKey(m_hKey);
154}
155
156DWORD CRegistryKey::removeKey()
157{
158    RegOpenKeyEx(m_base, m_path, 0, KEY_WRITE|m_sam, &m_hKey);
159    return SHDeleteKey(m_base, (LPCTSTR)m_path);
160}
161
162bool CRegistryKey::getValues(CStringList& values)
163{
164    values.RemoveAll();
165
166    if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
167    {
168        for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
169        { 
170            TCHAR value[255];
171            DWORD size = sizeof value / sizeof TCHAR;
172            rc = RegEnumValue(m_hKey, i, value, &size, NULL, NULL, NULL, NULL);
173            if (rc == ERROR_SUCCESS) 
174            {
175                values.AddTail(value);
176            }
177        }
178    }
179
180    return values.GetCount() > 0;
181}
182
183bool CRegistryKey::getSubKeys(CStringList& subkeys)
184{
185    subkeys.RemoveAll();
186
187    if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE|m_sam, &m_hKey)==ERROR_SUCCESS)
188    {
189        for (int i = 0, rc = ERROR_SUCCESS; rc == ERROR_SUCCESS; i++)
190        { 
191            TCHAR value[1024];
192            DWORD size = sizeof value / sizeof TCHAR;
193            FILETIME last_write_time;
194            rc = RegEnumKeyEx(m_hKey, i, value, &size, NULL, NULL, NULL, &last_write_time);
195            if (rc == ERROR_SUCCESS) 
196            {
197                subkeys.AddTail(value);
198            }
199        }
200    }
201
202    return subkeys.GetCount() > 0;
203}
204#endif
205