#include "Registry.h"

Registry::Registry() {

}

Registry::~Registry() {

}

unsigned long Registry::ReadLong(HKEY hKey, string sPath, string sValue) {
	unsigned long lData = 0;
	HKEY hMyKey;
	unsigned long lType = 0;
	unsigned char pcData[1025];
	unsigned long lSize = 1024;

	// Open registry key
	if(RegOpenKeyEx(hKey, sPath.c_str(), 0, KEY_ALL_ACCESS, &hMyKey) != ERROR_SUCCESS)
		return 0;

	// Set the value
	if(RegQueryValueEx(hMyKey, sValue.c_str(), 0, &lType, pcData, &lSize) != ERROR_SUCCESS)
		return 0;

	if(lType != REG_DWORD)
		return 0;

	// Close the key again
	RegCloseKey(hMyKey);

	// First problem...what is in pcData?
	lData = pcData[0] + (pcData[1] << 8) + (pcData[2] << 16) + (pcData[3] << 24);
	//theLog->Message(string("Loaded dword: ") + mjpTools::Long2String(lData), 0);
	return lData;
}

bool Registry::SaveLong(HKEY hKey, string sPath, string sValue, long lData) {
	HKEY hMyKey;
	unsigned long lOpenType;
	unsigned char pcTemp[4];

	// Open or create registry key
	if(RegCreateKeyEx(hKey, sPath.c_str(), 0, "NTRegKey32", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMyKey, &lOpenType) != ERROR_SUCCESS)
		return false;

	// Set the value
	// Endian-ness problems abound...
	pcTemp[0] = lData && 0xFF;
	pcTemp[1] = (lData >> 8) && 0xFF;
	pcTemp[2] = (lData >> 16) && 0xFF;
	pcTemp[3] = (lData >> 24) && 0xFF;
	if(RegSetValueEx(hMyKey, sValue.c_str(), 0, REG_DWORD, pcTemp, 4) != ERROR_SUCCESS)
		return false;

	// Close the key again
	if(RegCloseKey(hMyKey) != ERROR_SUCCESS)
		return false;

	//theLog->Message(string("Saved dword: ") + sValue + string(" = ") + mjpTools::Long2String((ulong)lData), 0);
	return true;
}

string Registry::ReadString(HKEY hKey, string sPath, string sValue) {
	string sData;
	HKEY hMyKey;
	unsigned long lType = 0;
	unsigned char pcData[1025];
	unsigned long lSize = 1024;

	// Open registry key
	if(RegOpenKeyEx(hKey, sPath.c_str(), 0, KEY_ALL_ACCESS, &hMyKey) != ERROR_SUCCESS)
		return string("");

	// Set the value
	if(RegQueryValueEx(hMyKey, sValue.c_str(), 0, &lType, pcData, &lSize) != ERROR_SUCCESS)
		return string("");

	if(lType != REG_SZ)
		return string("");

	// Close the key again
	RegCloseKey(hMyKey);
	sData = (char*)pcData;
	//theLog->Message(string("Loaded string: ") + string(sData), 0);
	return sData;
}

bool Registry::SaveString(HKEY hKey, string sPath, string sValue, string sData) {
	HKEY hMyKey;
	unsigned long lOpenType;
	// Open or create registry key
	if(RegCreateKeyEx(hKey, sPath.c_str(), 0, "NTRegKey32", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hMyKey, &lOpenType) != ERROR_SUCCESS)
		return false;

	// Set the value
	if(RegSetValueEx(hMyKey, sValue.c_str(), 0, REG_SZ, (const unsigned char*)sData.c_str(), sData.length()+1) != ERROR_SUCCESS)
		return false;

	// Close the key again
	if(RegCloseKey(hMyKey) != ERROR_SUCCESS)
		return false;

	//theLog->Message(string("Saved String: ") + sValue + string(" = ") + sData, 0);
	return true;
}
