Changeset 70f2d7b in nscp for include/REGSettings.h


Ignore:
Timestamp:
03/29/06 22:19:32 (7 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
76aafc4
Parents:
aabbd97
Message:

Updated with reg settings (take 1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/REGSettings.h

    raabbd97 r70f2d7b  
    66#define BUFF_LEN 4096 
    77 
     8#include <iostream> 
    89class REGSettings : public TSettings 
    910{ 
     
    1920 
    2021  static bool hasSettings() { 
    21     // @todo 
    22     return false; 
     22    return getInt_(NS_HKEY_ROOT, NS_REG_ROOT, "use_reg", 0) == 1; 
     23  } 
     24 
     25  std::string getActiveType() { 
     26    return "registry"; 
    2327  } 
    2428 
    2529  sectionList getSections(unsigned int bufferLength = BUFF_LEN) { 
    26     sectionList ret; 
    27     return ret; 
     30    return getSubKeys_(NS_HKEY_ROOT, NS_REG_ROOT); 
    2831  } 
    2932 
     
    3437  */ 
    3538  sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) { 
    36     sectionList ret; 
    37     return ret; 
     39    return getValues_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str()); 
    3840  } 
    3941  /** 
     
    4547  */ 
    4648  std::string getString(std::string section, std::string key, std::string defaultValue = "") const { 
    47     std::string ret; 
    48     return ret; 
     49    return getString_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue); 
    4950  } 
    5051 
     
    6061  */ 
    6162  int getInt(std::string section, std::string key, int defaultValue = 0) { 
    62     return 0; 
     63    return getInt_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue); 
    6364  } 
    6465  void setInt(std::string section, std::string key, int value) { 
    6566  } 
     67 
     68 
     69  static std::string getString_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, std::string def) { 
     70    std::string ret = def; 
     71    HKEY hTemp; 
     72    if (RegOpenKeyEx(hKey, lpszPath, 0, KEY_QUERY_VALUE, &hTemp) != ERROR_SUCCESS) { 
     73      return def; 
     74    } 
     75    DWORD type; 
     76    DWORD cbData = 1024; 
     77    BYTE *bData = new BYTE[cbData]; 
     78    BOOL bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData); 
     79    if (type != REG_SZ) { 
     80      bRet = false; 
     81    } 
     82    RegCloseKey(hTemp); 
     83    if (bRet) { 
     84      ret = (LPCTSTR)bData; 
     85    } 
     86    delete [] bData; 
     87    return ret; 
     88  } 
     89  static DWORD getInt_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, DWORD def) { 
     90    DWORD ret = def; 
     91    LONG bRet; 
     92    HKEY hTemp; 
     93    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     94      return def; 
     95    } 
     96    DWORD type; 
     97    DWORD cbData = 1024; 
     98    BYTE *bData = new BYTE[sizeof(DWORD)]; 
     99    bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData); 
     100    if (type != REG_DWORD) { 
     101      bRet = -1; 
     102    } 
     103    RegCloseKey(hTemp); 
     104    if (bRet == ERROR_SUCCESS) { 
     105      ret = (DWORD)*bData; 
     106    } 
     107    delete [] bData; 
     108    return ret; 
     109  } 
     110  static sectionList getValues_(HKEY hKey, LPCTSTR lpszPath) { 
     111    sectionList ret; 
     112    LONG bRet; 
     113    HKEY hTemp; 
     114    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     115      return ret; 
     116    } 
     117    DWORD    cValues=0; 
     118    DWORD    cMaxValLen; 
     119    // Get the class name and the value count.  
     120    bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,NULL,NULL,NULL,&cValues,&cMaxValLen,NULL,NULL,NULL); 
     121    if ((bRet == ERROR_SUCCESS)&&(cValues>0)) { 
     122      TCHAR *lpValueName = new TCHAR[cMaxValLen+1]; 
     123      for (unsigned int i=0; i<cValues; i++) { 
     124        DWORD len = cMaxValLen; 
     125        bRet = RegEnumValue(hKey, i, lpValueName, &len, NULL, NULL, NULL, NULL); 
     126        if (bRet == ERROR_SUCCESS) { 
     127          ret.push_back(std::string(lpValueName)); 
     128        } 
     129      } 
     130      delete [] lpValueName; 
     131    } 
     132    return ret; 
     133  } 
     134  static sectionList getSubKeys_(HKEY hKey, LPCTSTR lpszPath) { 
     135    sectionList ret; 
     136    LONG bRet; 
     137    HKEY hTemp; 
     138    if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) { 
     139      return ret; 
     140    } 
     141    DWORD    cSubKeys=0; 
     142    DWORD    cMaxKeyLen; 
     143    // Get the class name and the value count.  
     144    bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,&cSubKeys,&cMaxKeyLen,NULL,NULL,NULL,NULL,NULL,NULL); 
     145    if ((bRet == ERROR_SUCCESS)&&(cSubKeys>0)) { 
     146      TCHAR *lpValueName = new TCHAR[cMaxKeyLen+1]; 
     147      for (unsigned int i=0; i<cSubKeys; i++) { 
     148        DWORD len = cMaxKeyLen; 
     149        bRet = RegEnumKey(hKey, i, lpValueName, len); 
     150        if (bRet == ERROR_SUCCESS) { 
     151          ret.push_back(std::string(lpValueName)); 
     152        } 
     153      } 
     154      delete [] lpValueName; 
     155    } 
     156    return ret; 
     157  } 
    66158}; 
Note: See TracChangeset for help on using the changeset viewer.