home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / system / source / registry.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  6.6 KB  |  244 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #include "stdafx.h"
  27. #include <windows.h>
  28.  
  29. #include <vd2/system/VDString.h>
  30. #include <vd2/system/registry.h>
  31.  
  32. VDRegistryKey::VDRegistryKey(const char *keyName, bool global, bool write) {
  33.     const HKEY rootKey = global ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
  34.  
  35.     if (write) {
  36.         if (RegCreateKeyEx(rootKey, keyName, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, (PHKEY)&pHandle, NULL))
  37.             pHandle = NULL;
  38.     } else {
  39.         if (RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, (PHKEY)&pHandle))
  40.             pHandle = NULL;
  41.     }
  42. }
  43.  
  44. VDRegistryKey::~VDRegistryKey() {
  45.     if (pHandle)
  46.         RegCloseKey((HKEY)pHandle);
  47. }
  48.  
  49. bool VDRegistryKey::setBool(const char *pszName, bool v) const {
  50.     if (pHandle) {
  51.         DWORD dw = v;
  52.  
  53.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_DWORD, (const BYTE *)&dw, sizeof dw))
  54.             return true;
  55.     }
  56.  
  57.     return false;
  58. }
  59.  
  60. bool VDRegistryKey::setInt(const char *pszName, int i) const {
  61.     if (pHandle) {
  62.         DWORD dw = i;
  63.  
  64.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_DWORD, (const BYTE *)&dw, sizeof dw))
  65.             return true;
  66.     }
  67.  
  68.     return false;
  69. }
  70.  
  71. bool VDRegistryKey::setString(const char *pszName, const char *pszString) const {
  72.     if (pHandle) {
  73.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_SZ, (const BYTE *)pszString, strlen(pszString)))
  74.             return true;
  75.     }
  76.  
  77.     return false;
  78. }
  79.  
  80. bool VDRegistryKey::setString(const char *pszName, const wchar_t *pszString) const {
  81.     if (pHandle) {
  82.         if (GetVersion() & 0x80000000) {
  83.             VDStringA s(VDTextWToA(pszString));
  84.  
  85.             if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_SZ, (const BYTE *)s.data(), s.size()))
  86.                 return true;
  87.         } else {
  88.             if (RegSetValueExW((HKEY)pHandle, VDTextAToW(pszName).c_str(), 0, REG_SZ, (const BYTE *)pszString, sizeof(wchar_t) * wcslen(pszString)))
  89.                 return true;
  90.         }
  91.     }
  92.  
  93.     return false;
  94. }
  95.  
  96. bool VDRegistryKey::setBinary(const char *pszName, const char *data, int len) const {
  97.     if (pHandle) {
  98.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_BINARY, (const BYTE *)data, len))
  99.             return true;
  100.     }
  101.  
  102.     return false;
  103. }
  104.  
  105. bool VDRegistryKey::getBool(const char *pszName, bool def) const {
  106.     DWORD type, v, s=sizeof(DWORD);
  107.  
  108.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)&v, &s)
  109.         || type != REG_DWORD)
  110.         return def;
  111.  
  112.     return v != 0;
  113. }
  114.  
  115. int VDRegistryKey::getInt(const char *pszName, int def) const {
  116.     DWORD type, v, s=sizeof(DWORD);
  117.  
  118.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)&v, &s)
  119.         || type != REG_DWORD)
  120.         return def;
  121.  
  122.     return (int)v;
  123. }
  124.  
  125. int VDRegistryKey::getEnumInt(const char *pszName, int maxVal, int def) const {
  126.     int v = getInt(pszName, def);
  127.  
  128.     if (v<0 || v>=maxVal)
  129.         v = def;
  130.  
  131.     return v;
  132. }
  133.  
  134. bool VDRegistryKey::getString(const char *pszName, VDStringA& str) const {
  135.     DWORD type, s = sizeof(DWORD);
  136.  
  137.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, NULL, &s) || type != REG_SZ)
  138.         return false;
  139.  
  140.     str.resize(s);
  141.     if (RegQueryValueEx((HKEY)pHandle, pszName, 0, NULL, (BYTE *)str.data(), &s))
  142.         return false;
  143.  
  144.     if (!s)
  145.         str.clear();
  146.     else
  147.         str.resize(strlen(str.c_str()));        // Trim off pesky terminating NULLs.
  148.  
  149.     return true;
  150. }
  151.  
  152. bool VDRegistryKey::getString(const char *pszName, VDStringW& str) const {
  153.     if (!pHandle)
  154.         return false;
  155.  
  156.     if (GetVersion() & 0x80000000) {
  157.         VDStringA v;
  158.         if (!getString(pszName, v))
  159.             return false;
  160.         str = VDTextAToW(v);
  161.         return true;
  162.     }
  163.  
  164.     const VDStringW wsName(VDTextAToW(pszName));
  165.     DWORD type, s = sizeof(DWORD);
  166.  
  167.     if (!pHandle || RegQueryValueExW((HKEY)pHandle, wsName.c_str(), 0, &type, NULL, &s) || type != REG_SZ)
  168.         return false;
  169.  
  170.     if (s <= 0)
  171.         str.clear();
  172.     else {
  173.         str.resize((s + sizeof(wchar_t) - 1) / sizeof(wchar_t));
  174.  
  175.         if (RegQueryValueExW((HKEY)pHandle, wsName.c_str(), 0, NULL, (BYTE *)&str[0], &s))
  176.             return false;
  177.  
  178.         str.resize(wcslen(str.c_str()));        // Trim off pesky terminating NULLs.
  179.     }
  180.  
  181.     return true;
  182. }
  183.  
  184. int VDRegistryKey::getBinaryLength(const char *pszName) const {
  185.     DWORD type, s = sizeof(DWORD);
  186.  
  187.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, NULL, &s)
  188.         || type != REG_BINARY)
  189.         return -1;
  190.  
  191.     return s;
  192. }
  193.  
  194. bool VDRegistryKey::getBinary(const char *pszName, char *buf, int maxlen) const {
  195.     DWORD type, s = maxlen;
  196.  
  197.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)buf, &s) || maxlen < (int)s || type != REG_BINARY)
  198.         return false;
  199.  
  200.     return true;
  201. }
  202.  
  203. bool VDRegistryKey::removeValue(const char *name) {
  204.     if (!pHandle || RegDeleteValue((HKEY)pHandle, name))
  205.         return false;
  206.  
  207.     return true;
  208. }
  209.  
  210. ///////////////////////////////////////////////////////////////////////////////
  211.  
  212. VDRegistryValueIterator::VDRegistryValueIterator(const VDRegistryKey& key)
  213.     : mpHandle(key.getRawHandle())
  214.     , mIndex(0)
  215. {
  216. }
  217.  
  218. const char *VDRegistryValueIterator::Next() {
  219.     DWORD len = sizeof(mName)/sizeof(mName[0]);
  220.     LONG error = RegEnumValueA((HKEY)mpHandle, mIndex, mName, &len, NULL, NULL, NULL, NULL);
  221.  
  222.     if (error)
  223.         return NULL;
  224.  
  225.     ++mIndex;
  226.     return mName;
  227. }
  228.  
  229. ///////////////////////////////////////////////////////////////////////////////
  230.  
  231. VDString VDRegistryAppKey::s_appbase;
  232.  
  233. VDRegistryAppKey::VDRegistryAppKey() : VDRegistryKey(s_appbase.c_str()) {
  234. }
  235.  
  236. VDRegistryAppKey::VDRegistryAppKey(const char *pszKey, bool write)
  237.     : VDRegistryKey((s_appbase + pszKey).c_str(), false, write)
  238. {
  239. }
  240.  
  241. void VDRegistryAppKey::setDefaultKey(const char *pszAppName) {
  242.     s_appbase = pszAppName;
  243. }
  244.