home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / Reg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  10.1 KB  |  370 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // 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. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20.  
  21. static char buffer[2048];
  22. static HKEY vbKey = NULL;
  23. static CString regVbaPath;
  24.  
  25. #define VBA_PREF "preferences"
  26.  
  27. bool regEnabled = true;
  28.  
  29. void regInit(const char *path)
  30. {
  31.   DWORD disp = 0;
  32.   LONG res = RegCreateKeyEx(HKEY_CURRENT_USER,
  33.                             "Software\\Emulators\\VisualBoyAdvance",
  34.                             0,
  35.                             "",
  36.                             REG_OPTION_NON_VOLATILE,
  37.                             KEY_ALL_ACCESS,
  38.                             NULL,
  39.                             &vbKey,
  40.                             &disp);
  41.   regVbaPath.Format("%s\\vba.ini", path);
  42. }
  43.  
  44. void regShutdown()
  45. {
  46.   LONG res = RegCloseKey(vbKey);
  47. }
  48.  
  49. const char *regGetINIPath()
  50. {
  51.   return regVbaPath;
  52. }
  53.  
  54. char *regQueryStringValue(const char * key, char *def)
  55. {
  56.   if(regEnabled) {
  57.     DWORD type = 0;
  58.     DWORD size = 2048;
  59.     
  60.     LONG res = RegQueryValueEx(vbKey,
  61.                                key,
  62.                                NULL,
  63.                                &type,
  64.                                (UCHAR *)buffer,
  65.                                &size);
  66.     
  67.     if(res == ERROR_SUCCESS && type == REG_SZ)
  68.       return buffer;
  69.  
  70.     return def;
  71.   }
  72.  
  73.   DWORD res = GetPrivateProfileString(VBA_PREF,
  74.                                       key,
  75.                                       def,
  76.                                       (LPTSTR)buffer,
  77.                                       2048,
  78.                                       regVbaPath);
  79.  
  80.   if(res)
  81.     return buffer;
  82.  
  83.   return def;
  84. }
  85.  
  86. DWORD regQueryDwordValue(const char * key, DWORD def, bool force)
  87. {
  88.   if(regEnabled || force) {
  89.     DWORD type = 0;
  90.     DWORD size = sizeof(DWORD);
  91.     DWORD result = 0;
  92.     
  93.     LONG res = RegQueryValueEx(vbKey,
  94.                                key,
  95.                                NULL,
  96.                                &type,
  97.                                (UCHAR *)&result,
  98.                                &size);
  99.     
  100.     if(res == ERROR_SUCCESS && type == REG_DWORD)
  101.       return result;
  102.  
  103.     return def;
  104.   }
  105.  
  106.   return GetPrivateProfileInt(VBA_PREF,
  107.                               key,
  108.                               def,
  109.                               regVbaPath);
  110. }
  111.  
  112. BOOL regQueryBinaryValue(const char * key, char *value, int count)
  113. {
  114.   if(regEnabled) {
  115.     DWORD type = 0;
  116.     DWORD size = count;
  117.     DWORD result = 0;
  118.     
  119.     
  120.     LONG res = RegQueryValueEx(vbKey,
  121.                                key,
  122.                                NULL,
  123.                                &type,
  124.                                (UCHAR *)value,
  125.                                &size);
  126.     
  127.     if(res == ERROR_SUCCESS && type == REG_BINARY)
  128.       return TRUE;
  129.  
  130.     return FALSE;
  131.   }
  132.   CString k = key;
  133.   k += "Count";
  134.   int size = GetPrivateProfileInt(VBA_PREF,
  135.                                   k,
  136.                                   -1,
  137.                                   regVbaPath);
  138.   if(size >= 0 && size < count)
  139.     count = size;
  140.   return GetPrivateProfileStruct(VBA_PREF,
  141.                                  key,
  142.                                  value,
  143.                                  count,
  144.                                  regVbaPath);
  145. }
  146.  
  147. void regSetStringValue(const char * key, const char * value)
  148. {
  149.   if(regEnabled) {
  150.     LONG res = RegSetValueEx(vbKey,
  151.                              key,
  152.                              NULL,
  153.                              REG_SZ,
  154.                              (const UCHAR *)value,
  155.                              strlen(value)+1);
  156.   } else {
  157.     WritePrivateProfileString(VBA_PREF,
  158.                               key,
  159.                               value,
  160.                               regVbaPath);
  161.   }
  162. }
  163.  
  164. void regSetDwordValue(const char * key, DWORD value, bool force)
  165. {
  166.   if(regEnabled || force) {
  167.     LONG res = RegSetValueEx(vbKey,
  168.                              key,
  169.                              NULL,
  170.                              REG_DWORD,
  171.                              (const UCHAR *)&value,
  172.                              sizeof(DWORD));
  173.   } else {
  174.     wsprintf(buffer, "%u", value);
  175.     WritePrivateProfileString(VBA_PREF,
  176.                               key,
  177.                               buffer,
  178.                               regVbaPath);
  179.   }
  180. }
  181.  
  182. void regSetBinaryValue(const char *key, char *value, int count)
  183. {
  184.   if(regEnabled) {
  185.     LONG res = RegSetValueEx(vbKey,
  186.                              key,
  187.                              NULL,
  188.                              REG_BINARY,
  189.                              (const UCHAR *)value,
  190.                              count);
  191.   } else {
  192.     CString k = key;
  193.     k += "Count";
  194.     wsprintf(buffer, "%u", count);
  195.     
  196.     WritePrivateProfileString(VBA_PREF,
  197.                               k,
  198.                               buffer,
  199.                               regVbaPath);
  200.                            
  201.     WritePrivateProfileStruct(VBA_PREF,
  202.                               key,
  203.                               value,
  204.                               count,
  205.                               regVbaPath);
  206.   }
  207. }
  208.  
  209. void regDeleteValue(char *key)
  210. {
  211.   if(regEnabled) {
  212.     LONG res = RegDeleteValue(vbKey,
  213.                               key);
  214.   } else {
  215.     WritePrivateProfileString(VBA_PREF,
  216.                               key,
  217.                               NULL,
  218.                               regVbaPath);
  219.   }
  220. }
  221.  
  222. bool regCreateFileType(const char *ext, const char *type)
  223. {
  224.   DWORD disp = 0;
  225.   HKEY key;
  226.   LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  227.                             ext,
  228.                             0,
  229.                             "",
  230.                             REG_OPTION_NON_VOLATILE,
  231.                             KEY_ALL_ACCESS,
  232.                             NULL,
  233.                             &key,
  234.                             &disp);
  235.   if(res == ERROR_SUCCESS) {
  236.     res = RegSetValueEx(key,
  237.                         "",
  238.                         0,
  239.                         REG_SZ,
  240.                         (const UCHAR *)type,
  241.                         strlen(type)+1);
  242.     RegCloseKey(key);
  243.     return true;
  244.   }
  245.   return false;
  246. }
  247.  
  248. bool regAssociateType(const char *type, const char *desc, const char *application)
  249. {
  250.   DWORD disp = 0;
  251.   HKEY key;
  252.   LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
  253.                             type,
  254.                             0,
  255.                             "",
  256.                             REG_OPTION_NON_VOLATILE,
  257.                             KEY_ALL_ACCESS,
  258.                             NULL,
  259.                             &key,
  260.                             &disp);
  261.   if(res == ERROR_SUCCESS) {
  262.     res = RegSetValueEx(key,
  263.                         "",
  264.                         0,
  265.                         REG_SZ,
  266.                         (const UCHAR *)desc,
  267.                         strlen(desc)+1);
  268.     HKEY key2;
  269.     res = RegCreateKeyEx(key,
  270.                          "Shell\\Open\\Command",
  271.                          0,
  272.                          "",
  273.                          REG_OPTION_NON_VOLATILE,
  274.                          KEY_ALL_ACCESS,
  275.                          NULL,
  276.                          &key2,
  277.                          &disp);
  278.     if(res == ERROR_SUCCESS) {
  279.       res = RegSetValueEx(key2,
  280.                           "",
  281.                           0,
  282.                           REG_SZ,
  283.                           (const UCHAR *)application,
  284.                           strlen(application)+1);
  285.       RegCloseKey(key2);
  286.       RegCloseKey(key);
  287.       return true;
  288.     }
  289.     
  290.     RegCloseKey(key);
  291.   }
  292.   return false;
  293. }
  294.  
  295. static void regExportSettingsToINI(HKEY key, const char *section)
  296. {
  297.   char valueName[256];
  298.   int index = 0;
  299.   while(1) {
  300.     DWORD nameSize = 256;
  301.     DWORD size = 2048;
  302.     DWORD type;
  303.     LONG res = RegEnumValue(key,
  304.                             index,
  305.                             valueName,
  306.                             &nameSize,
  307.                             NULL,
  308.                             &type,
  309.                             (LPBYTE)buffer,
  310.                             &size);
  311.       
  312.     if(res == ERROR_SUCCESS) {
  313.       switch(type) {
  314.       case REG_DWORD:
  315.         {
  316.           char temp[256];
  317.           wsprintf(temp, "%u", *((DWORD *)buffer));
  318.           WritePrivateProfileString(section,
  319.                                     valueName,
  320.                                     temp,
  321.                                     regVbaPath);
  322.         }
  323.         break;
  324.       case REG_SZ:
  325.         WritePrivateProfileString(section,
  326.                                   valueName,
  327.                                   buffer,
  328.                                   regVbaPath);
  329.         break;
  330.       case REG_BINARY:
  331.         {
  332.           char temp[256];
  333.           
  334.           wsprintf(temp, "%u", size);
  335.           CString k = valueName;
  336.           k += "Count";
  337.           WritePrivateProfileString(section,
  338.                                     k,
  339.                                     temp,
  340.                                     regVbaPath);
  341.           WritePrivateProfileStruct(section,
  342.                                     valueName,
  343.                                     buffer,
  344.                                     size,
  345.                                     regVbaPath);
  346.         }
  347.         break;
  348.       }
  349.       index++;
  350.     } else
  351.       break;
  352.   }
  353. }
  354.  
  355. void regExportSettingsToINI()
  356.   if(vbKey != NULL) {
  357.     regExportSettingsToINI(vbKey, VBA_PREF);
  358.   }
  359.  
  360.   HKEY key;
  361.  
  362.   if(RegOpenKey(HKEY_CURRENT_USER, 
  363.                 "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) ==
  364.      ERROR_SUCCESS) {
  365.     regExportSettingsToINI(key, "Viewer");
  366.     RegCloseKey(key);
  367.   }
  368. }
  369.