diff src/win32/Reg.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/Reg.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,379 @@
     1.4 +#include "stdafx.h"
     1.5 +#include "Reg.h"
     1.6 +
     1.7 +static char    buffer[2048];
     1.8 +static HKEY    vbKey = NULL;
     1.9 +static CString regVbaPath;
    1.10 +
    1.11 +#define VBA_PREF "preferences"
    1.12 +
    1.13 +bool regEnabled = true;
    1.14 +
    1.15 +void regInit(const char *path)
    1.16 +{
    1.17 +	if (regEnabled)
    1.18 +	{
    1.19 +		DWORD disp = 0;
    1.20 +		LONG  res  = RegCreateKeyEx(HKEY_CURRENT_USER,
    1.21 +	                            "Software\\Emulators\\VisualBoyAdvance",
    1.22 +	                            0,
    1.23 +	                            "",
    1.24 +	                            REG_OPTION_NON_VOLATILE,
    1.25 +	                            KEY_ALL_ACCESS,
    1.26 +	                            NULL,
    1.27 +	                            &vbKey,
    1.28 +	                            &disp);
    1.29 +	}
    1.30 +	regVbaPath.Format("%s\\vba.ini", path);
    1.31 +}
    1.32 +
    1.33 +void regShutdown()
    1.34 +{
    1.35 +	LONG res = RegCloseKey(vbKey);
    1.36 +}
    1.37 +
    1.38 +const char *regGetINIPath()
    1.39 +{
    1.40 +	return regVbaPath;
    1.41 +}
    1.42 +
    1.43 +const char *regQueryStringValue(const char *key, const char *def)
    1.44 +{
    1.45 +	if (regEnabled)
    1.46 +	{
    1.47 +		DWORD type = 0;
    1.48 +		DWORD size = 2048;
    1.49 +
    1.50 +		LONG res = RegQueryValueEx(vbKey,
    1.51 +		                           key,
    1.52 +		                           NULL,
    1.53 +		                           &type,
    1.54 +		                           (UCHAR *)buffer,
    1.55 +		                           &size);
    1.56 +
    1.57 +		if (res == ERROR_SUCCESS && type == REG_SZ)
    1.58 +			return buffer;
    1.59 +
    1.60 +		return def;
    1.61 +	}
    1.62 +
    1.63 +	DWORD res = GetPrivateProfileString(VBA_PREF,
    1.64 +	                                    key,
    1.65 +	                                    def,
    1.66 +	                                    (LPTSTR)buffer,
    1.67 +	                                    2048,
    1.68 +	                                    regVbaPath);
    1.69 +
    1.70 +	if (res)
    1.71 +		return buffer;
    1.72 +
    1.73 +	return def;
    1.74 +}
    1.75 +
    1.76 +DWORD regQueryDwordValue(const char *key, DWORD def, bool force)
    1.77 +{
    1.78 +	if (regEnabled || force)
    1.79 +	{
    1.80 +		DWORD type   = 0;
    1.81 +		DWORD size   = sizeof(DWORD);
    1.82 +		DWORD result = 0;
    1.83 +
    1.84 +		LONG res = RegQueryValueEx(vbKey,
    1.85 +		                           key,
    1.86 +		                           NULL,
    1.87 +		                           &type,
    1.88 +		                           (UCHAR *)&result,
    1.89 +		                           &size);
    1.90 +
    1.91 +		if (res == ERROR_SUCCESS && type == REG_DWORD)
    1.92 +			return result;
    1.93 +
    1.94 +		return def;
    1.95 +	}
    1.96 +
    1.97 +	return GetPrivateProfileInt(VBA_PREF,
    1.98 +	                            key,
    1.99 +	                            def,
   1.100 +	                            regVbaPath);
   1.101 +}
   1.102 +
   1.103 +BOOL regQueryBinaryValue(const char *key, char *value, int count)
   1.104 +{
   1.105 +	if (regEnabled)
   1.106 +	{
   1.107 +		DWORD type   = 0;
   1.108 +		DWORD size   = count;
   1.109 +		DWORD result = 0;
   1.110 +
   1.111 +		LONG res = RegQueryValueEx(vbKey,
   1.112 +		                           key,
   1.113 +		                           NULL,
   1.114 +		                           &type,
   1.115 +		                           (UCHAR *)value,
   1.116 +		                           &size);
   1.117 +
   1.118 +		if (res == ERROR_SUCCESS && type == REG_BINARY)
   1.119 +			return TRUE;
   1.120 +
   1.121 +		return FALSE;
   1.122 +	}
   1.123 +	CString k = key;
   1.124 +	k += "Count";
   1.125 +	int size = GetPrivateProfileInt(VBA_PREF,
   1.126 +	                                k,
   1.127 +	                                -1,
   1.128 +	                                regVbaPath);
   1.129 +	if (size >= 0 && size < count)
   1.130 +		count = size;
   1.131 +	return GetPrivateProfileStruct(VBA_PREF,
   1.132 +	                               key,
   1.133 +	                               value,
   1.134 +	                               count,
   1.135 +	                               regVbaPath);
   1.136 +}
   1.137 +
   1.138 +void regSetStringValue(const char *key, const char *value)
   1.139 +{
   1.140 +	if (regEnabled)
   1.141 +	{
   1.142 +		LONG res = RegSetValueEx(vbKey,
   1.143 +		                         key,
   1.144 +		                         NULL,
   1.145 +		                         REG_SZ,
   1.146 +		                         (const UCHAR *)value,
   1.147 +		                         strlen(value)+1);
   1.148 +	}
   1.149 +	else
   1.150 +	{
   1.151 +		WritePrivateProfileString(VBA_PREF,
   1.152 +		                          key,
   1.153 +		                          value,
   1.154 +		                          regVbaPath);
   1.155 +	}
   1.156 +}
   1.157 +
   1.158 +void regSetDwordValue(const char *key, DWORD value, bool force)
   1.159 +{
   1.160 +	if (regEnabled || force)
   1.161 +	{
   1.162 +		LONG res = RegSetValueEx(vbKey,
   1.163 +		                         key,
   1.164 +		                         NULL,
   1.165 +		                         REG_DWORD,
   1.166 +		                         (const UCHAR *)&value,
   1.167 +		                         sizeof(DWORD));
   1.168 +	}
   1.169 +	else
   1.170 +	{
   1.171 +		wsprintf(buffer, "%u", value);
   1.172 +		WritePrivateProfileString(VBA_PREF,
   1.173 +		                          key,
   1.174 +		                          buffer,
   1.175 +		                          regVbaPath);
   1.176 +	}
   1.177 +}
   1.178 +
   1.179 +void regSetBinaryValue(const char *key, char *value, int count)
   1.180 +{
   1.181 +	if (regEnabled)
   1.182 +	{
   1.183 +		LONG res = RegSetValueEx(vbKey,
   1.184 +		                         key,
   1.185 +		                         NULL,
   1.186 +		                         REG_BINARY,
   1.187 +		                         (const UCHAR *)value,
   1.188 +		                         count);
   1.189 +	}
   1.190 +	else
   1.191 +	{
   1.192 +		CString k = key;
   1.193 +		k += "Count";
   1.194 +		wsprintf(buffer, "%u", count);
   1.195 +
   1.196 +		WritePrivateProfileString(VBA_PREF,
   1.197 +		                          k,
   1.198 +		                          buffer,
   1.199 +		                          regVbaPath);
   1.200 +
   1.201 +		WritePrivateProfileStruct(VBA_PREF,
   1.202 +		                          key,
   1.203 +		                          value,
   1.204 +		                          count,
   1.205 +		                          regVbaPath);
   1.206 +	}
   1.207 +}
   1.208 +
   1.209 +void regDeleteValue(const char *key)
   1.210 +{
   1.211 +	if (regEnabled)
   1.212 +	{
   1.213 +		LONG res = RegDeleteValue(vbKey,
   1.214 +		                          key);
   1.215 +	}
   1.216 +	else
   1.217 +	{
   1.218 +		WritePrivateProfileString(VBA_PREF,
   1.219 +		                          key,
   1.220 +		                          NULL,
   1.221 +		                          regVbaPath);
   1.222 +	}
   1.223 +}
   1.224 +
   1.225 +bool regCreateFileType(const char *ext, const char *type)
   1.226 +{
   1.227 +	DWORD disp = 0;
   1.228 +	HKEY  key;
   1.229 +	LONG  res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
   1.230 +	                           ext,
   1.231 +	                           0,
   1.232 +	                           "",
   1.233 +	                           REG_OPTION_NON_VOLATILE,
   1.234 +	                           KEY_ALL_ACCESS,
   1.235 +	                           NULL,
   1.236 +	                           &key,
   1.237 +	                           &disp);
   1.238 +	if (res == ERROR_SUCCESS)
   1.239 +	{
   1.240 +		res = RegSetValueEx(key,
   1.241 +		                    "",
   1.242 +		                    0,
   1.243 +		                    REG_SZ,
   1.244 +		                    (const UCHAR *)type,
   1.245 +		                    strlen(type)+1);
   1.246 +		RegCloseKey(key);
   1.247 +		return true;
   1.248 +	}
   1.249 +	return false;
   1.250 +}
   1.251 +
   1.252 +bool regAssociateType(const char *type, const char *desc, const char *application)
   1.253 +{
   1.254 +	DWORD disp = 0;
   1.255 +	HKEY  key;
   1.256 +	LONG  res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
   1.257 +	                           type,
   1.258 +	                           0,
   1.259 +	                           "",
   1.260 +	                           REG_OPTION_NON_VOLATILE,
   1.261 +	                           KEY_ALL_ACCESS,
   1.262 +	                           NULL,
   1.263 +	                           &key,
   1.264 +	                           &disp);
   1.265 +	if (res == ERROR_SUCCESS)
   1.266 +	{
   1.267 +		res = RegSetValueEx(key,
   1.268 +		                    "",
   1.269 +		                    0,
   1.270 +		                    REG_SZ,
   1.271 +		                    (const UCHAR *)desc,
   1.272 +		                    strlen(desc)+1);
   1.273 +		HKEY key2;
   1.274 +		res = RegCreateKeyEx(key,
   1.275 +		                     "Shell\\Open\\Command",
   1.276 +		                     0,
   1.277 +		                     "",
   1.278 +		                     REG_OPTION_NON_VOLATILE,
   1.279 +		                     KEY_ALL_ACCESS,
   1.280 +		                     NULL,
   1.281 +		                     &key2,
   1.282 +		                     &disp);
   1.283 +		if (res == ERROR_SUCCESS)
   1.284 +		{
   1.285 +			res = RegSetValueEx(key2,
   1.286 +			                    "",
   1.287 +			                    0,
   1.288 +			                    REG_SZ,
   1.289 +			                    (const UCHAR *)application,
   1.290 +			                    strlen(application)+1);
   1.291 +			RegCloseKey(key2);
   1.292 +			RegCloseKey(key);
   1.293 +			return true;
   1.294 +		}
   1.295 +
   1.296 +		RegCloseKey(key);
   1.297 +	}
   1.298 +	return false;
   1.299 +}
   1.300 +
   1.301 +static void regExportSettingsToINI(HKEY key, const char *section)
   1.302 +{
   1.303 +	char valueName[256];
   1.304 +	int  index = 0;
   1.305 +	while (1)
   1.306 +	{
   1.307 +		DWORD nameSize = 256;
   1.308 +		DWORD size     = 2048;
   1.309 +		DWORD type;
   1.310 +		LONG  res = RegEnumValue(key,
   1.311 +		                         index,
   1.312 +		                         valueName,
   1.313 +		                         &nameSize,
   1.314 +		                         NULL,
   1.315 +		                         &type,
   1.316 +		                         (LPBYTE)buffer,
   1.317 +		                         &size);
   1.318 +
   1.319 +		if (res == ERROR_SUCCESS)
   1.320 +		{
   1.321 +			switch (type)
   1.322 +			{
   1.323 +			case REG_DWORD:
   1.324 +			{
   1.325 +				char temp[256];
   1.326 +				wsprintf(temp, "%u", *((DWORD *)buffer));
   1.327 +				WritePrivateProfileString(section,
   1.328 +				                          valueName,
   1.329 +				                          temp,
   1.330 +				                          regVbaPath);
   1.331 +				break;
   1.332 +			}
   1.333 +			case REG_SZ:
   1.334 +				WritePrivateProfileString(section,
   1.335 +				                          valueName,
   1.336 +				                          buffer,
   1.337 +				                          regVbaPath);
   1.338 +				break;
   1.339 +			case REG_BINARY:
   1.340 +			{
   1.341 +				char temp[256];
   1.342 +
   1.343 +				wsprintf(temp, "%u", size);
   1.344 +				CString k = valueName;
   1.345 +				k += "Count";
   1.346 +				WritePrivateProfileString(section,
   1.347 +				                          k,
   1.348 +				                          temp,
   1.349 +				                          regVbaPath);
   1.350 +				WritePrivateProfileStruct(section,
   1.351 +				                          valueName,
   1.352 +				                          buffer,
   1.353 +				                          size,
   1.354 +				                          regVbaPath);
   1.355 +				break;
   1.356 +			}
   1.357 +			}
   1.358 +			index++;
   1.359 +		}
   1.360 +		else
   1.361 +			break;
   1.362 +	}
   1.363 +}
   1.364 +
   1.365 +void regExportSettingsToINI()
   1.366 +{
   1.367 +	if (vbKey != NULL)
   1.368 +	{
   1.369 +		regExportSettingsToINI(vbKey, VBA_PREF);
   1.370 +	}
   1.371 +
   1.372 +	HKEY key;
   1.373 +
   1.374 +	if (RegOpenKey(HKEY_CURRENT_USER,
   1.375 +	               "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) ==
   1.376 +	    ERROR_SUCCESS)
   1.377 +	{
   1.378 +		regExportSettingsToINI(key, "Viewer");
   1.379 +		RegCloseKey(key);
   1.380 +	}
   1.381 +}
   1.382 +