1 //
2 // Cross-platform free Puyo-Puyo clone.
3 // Copyright (C) 2006, 2007 Emma's Software
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 of the License, or
8 // (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
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 #if defined (HAVE_CONFIG_H)
20 #include <config.h>
21 #endif // HAVE_CONFIG_H
22 #include <string>
23 #include <windows.h>
24 #include "Win32Options.h"
25 
26 using namespace Amoebax;
27 
28 //
29 // Constants.
30 //
31 static const char *k_RegistrySubkey = "Software\\Emma's Software\\Amoebax\\";
32 
33 ///
34 /// \brief Win32Options default constructor.
35 ///
Win32Options(void)36 Win32Options::Win32Options (void):
37     Options ()
38 {
39 }
40 
41 bool
getBooleanValue(const std::string & section,const std::string & name,bool defaultValue)42 Win32Options::getBooleanValue (const std::string &section,
43                              const std::string &name,
44                              bool defaultValue)
45 {
46     bool value = defaultValue;
47     unsigned int integerValue = getIntegerValue (section, name, 0xdeadbeef);
48     if ( 0xdeadbeef != integerValue )
49     {
50         value = 0 == integerValue ? false : true;
51     }
52     return value;
53 }
54 
55 int
getIntegerValue(const std::string & section,const std::string & name,int defaultValue)56 Win32Options::getIntegerValue (const std::string &section,
57                                const std::string &name,
58                                int defaultValue)
59 {
60     int value = defaultValue;
61 
62     std::string regPath (k_RegistrySubkey);
63     regPath += section;
64 
65     HKEY regKey;
66     if ( ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, regPath.c_str (),
67                                         0, KEY_READ, &regKey) )
68     {
69         DWORD valueSize = sizeof (value);
70         RegQueryValueEx (regKey, name.c_str (), NULL, NULL, (BYTE *)(&value),
71                          &valueSize);
72         RegCloseKey (regKey);
73     }
74 
75     return value;
76 }
77 
78 std::string
getStringValue(const std::string & section,const std::string & name,const std::string & defaultValue)79 Win32Options::getStringValue (const std::string &section,
80                               const std::string &name,
81                               const std::string &defaultValue)
82 {
83     std::string value = defaultValue;
84 
85     std::string regPath (k_RegistrySubkey);
86     regPath += section;
87 
88     HKEY regKey;
89     if ( ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, regPath.c_str (),
90                                         0, KEY_READ, &regKey) )
91     {
92         char registryValue[512];
93         DWORD valueSize = sizeof (registryValue);
94         if ( ERROR_SUCCESS == RegQueryValueEx (regKey, name.c_str (), NULL,
95                                                NULL, (BYTE *)(&registryValue),
96                                                &valueSize) )
97         {
98             registryValue[valueSize - 1] = '\0';
99             value = std::string (registryValue);
100         }
101         RegCloseKey (regKey);
102     }
103 
104     return value;
105 }
106 
107 void
setBooleanValue(const std::string & section,const std::string & name,bool value)108 Win32Options::setBooleanValue (const std::string &section,
109                                const std::string &name,
110                                bool value)
111 {
112     setIntegerValue (section, name, value ? 1 : 0);
113 }
114 
115 void
setIntegerValue(const std::string & section,const std::string & name,int value)116 Win32Options::setIntegerValue (const std::string &section,
117                                const std::string &name,
118                                int value)
119 {
120     std::string regPath (k_RegistrySubkey);
121     regPath += section;
122 
123     HKEY regKey;
124     if ( ERROR_SUCCESS == RegCreateKeyEx (HKEY_CURRENT_USER, regPath.c_str (),
125                                           0, NULL, REG_OPTION_NON_VOLATILE,
126                                           KEY_WRITE, NULL, &regKey, NULL) )
127     {
128         RegSetValueEx (regKey, name.c_str (), 0, REG_DWORD,
129                        (BYTE *)(&value), sizeof (value));
130         RegCloseKey (regKey);
131     }
132 }
133 
134 void
setStringValue(const std::string & section,const std::string & name,const std::string & value)135 Win32Options::setStringValue (const std::string &section,
136                               const std::string &name,
137                               const std::string &value)
138 {
139     std::string regPath (k_RegistrySubkey);
140     regPath += section;
141 
142     HKEY regKey;
143     if ( ERROR_SUCCESS == RegCreateKeyEx (HKEY_CURRENT_USER, regPath.c_str (),
144                                           0, NULL, REG_OPTION_NON_VOLATILE,
145                                           KEY_WRITE, NULL, &regKey, NULL) )
146     {
147         RegSetValueEx (regKey, name.c_str (), 0, REG_SZ,
148                        (BYTE *)(value.c_str ()), value.size ());
149         RegCloseKey (regKey);
150     }
151 }
152