// Registry.cpp: implementation of the Registry class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NateNMEAInstaller.h"
#include "Registry.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

WCHAR StorageCard[256] = L"저장소 카드";
WCHAR InstallPath[256];

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Registry::Registry()
{

}

Registry::~Registry()
{

}

int
Registry::RemoveRegKey(HKEY HKey, WCHAR *wszPath, WCHAR *wszKey)
{
	HKEY	hKey;
	LONG	nRtn;

	// 특정 위치의 레지스트리를 연다.
	nRtn = RegOpenKeyEx(HKey, wszPath, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
		return -1;

	// Key를 삭제한다.
	nRtn = RegDeleteKey(hKey, wszKey);
	if(nRtn != ERROR_SUCCESS)
		return -2;

	// 레지스트리를 닫는다.
	RegCloseKey(hKey);

	return 0;
}

int
Registry::RemoveRegValue(HKEY HKey, WCHAR *wszPath, WCHAR *wszValue)
{
	HKEY	hKey;
	LONG	nRtn;

	// 특정 위치의 레지스트리를 연다.
	nRtn = RegOpenKeyEx(HKey, wszPath, 0, KEY_ALL_ACCESS, &hKey);
	if(nRtn != ERROR_SUCCESS)
		return -1;

	// Value 를 삭제한다.
	nRtn = RegDeleteValue(hKey, wszValue);
	if(nRtn != ERROR_SUCCESS)
		return -2;

	// 레지스트리를 닫는다.
	RegCloseKey(hKey);

	return 0;
}

int
Registry::CreateRegValue(HKEY HKey, WCHAR *wszPath, WCHAR *wszValueName, DWORD type, void *pValue, DWORD nLen)
{
	HKEY hkSub = NULL;
	LONG	nlResult;
	DWORD	tResult;

	// 키 위치를 열거나 생성한다.
	nlResult = RegCreateKeyEx(HKey, wszPath, 0, NULL, 0, 0, NULL, &hkSub, &tResult);
	if(nlResult != ERROR_SUCCESS)
		return -1;

	// 해당 키의 위치에 정보를 설정한다.
	if(wszValueName != NULL) {
		switch(type) {
		case REG_DWORD:
			if(nLen != 0)
				nlResult = RegSetValueEx(hkSub, wszValueName, 0, type, 
					(const BYTE *) pValue, nLen);
			else
				nlResult = RegSetValueEx(hkSub, wszValueName, 0, type, 
					(const BYTE *) NULL, 0);
			break;
		default:
			if(nLen != 0)
				nlResult = RegSetValueEx(hkSub, wszValueName, 0, type, 
					(const BYTE *) pValue, nLen);
			else
				nlResult = RegSetValueEx(hkSub, wszValueName, 0, type, 
					(const BYTE *) NULL, 0);
			break;
		}
		if(nlResult != ERROR_SUCCESS)
			return -2;
	}

	// 레지스트리를 닫는다.
	RegCloseKey(hkSub);

	return 0;
}

int
Registry::CreateRegValueSZ(HKEY HKey, WCHAR *wszPath, WCHAR *wszValueName, WCHAR *wszValue)
{
	if(wszValueName == NULL)
		return CreateRegValue(HKey, wszPath, NULL, REG_SZ, NULL, (DWORD) 0);
	else if(wszValue == NULL)
		return CreateRegValue(HKey, wszPath, wszValueName, REG_SZ, NULL, 
			(DWORD) 0);
	else
		return CreateRegValue(HKey, wszPath, wszValueName, REG_SZ, (void *)wszValue, 
			(DWORD) (wcslen((WCHAR*)wszValue) * sizeof(WCHAR)) + sizeof(WCHAR));
}

int
Registry::CreateRegValueBIN(HKEY HKey, WCHAR *wszPath, WCHAR *wszValueName, char *szValue, int nLen)
{
	if(wszValueName == NULL)
		return CreateRegValue(HKey, wszPath, NULL, REG_BINARY, NULL, (DWORD) 0);
	else
		return CreateRegValue(HKey, wszPath, wszValueName, REG_BINARY, (void *)szValue, 
			(DWORD) nLen);
}

int
Registry::CreateRegValueINT(HKEY HKey, WCHAR *wszPath, WCHAR *wszValueName, DWORD nValue)
{
	if(wszValueName == NULL)
		return CreateRegValue(HKey, wszPath, NULL, REG_DWORD, NULL, (DWORD) 0);
	else
		return CreateRegValue(HKey, wszPath, wszValueName, REG_DWORD, (void *)&nValue, 
			(DWORD) sizeof(DWORD));
}

int
Registry::RegGetValue(HKEY HKey, WCHAR *wszPath, WCHAR *wszName, WCHAR *wszValue)
{
	HKEY	hKey = NULL;
	DWORD	VNLen = 256, DLen = 256, Type;
	WCHAR	wszData[256];
	WCHAR	wszVName[256];
	LONG	nRet;
	int		i;

	nRet = RegOpenKeyEx(HKey, wszPath, 0, 0, &hKey);
	if(nRet != ERROR_SUCCESS)
		return -1;

	memset(wszData, 0, sizeof(WCHAR)*256);	
	for(i = 0; i < 100; i++) {
		memset(wszVName, 0, sizeof(WCHAR)*256);
		VNLen = 256;
		DLen = 256;
		nRet = RegEnumValue(hKey, i, wszVName, &VNLen, NULL, &Type, (BYTE *)wszData, &DLen);
		if(nRet != ERROR_SUCCESS)
			return -2;
		if(_wcsicmp(wszVName, wszName) == 0)
			break;
	}
	wcscpy(wszValue, wszData);

	RegCloseKey(hKey);

	return 0;
}

int
Registry::RegGetIntValue(HKEY HKey, WCHAR *wszPath, WCHAR *wszName, DWORD *dnValue)
{
	HKEY	hKey = NULL;
	DWORD	VNLen = 256, DLen = 256, Type;
	WCHAR	wszData[256];
	WCHAR	wszVName[256];
	LONG	nRet;
	int		i;

	nRet = RegOpenKeyEx(HKey, wszPath, 0, 0, &hKey);
	if(nRet != ERROR_SUCCESS)
		return -1;

	memset(wszData, 0, sizeof(WCHAR)*256);	
	for(i = 0; i < 100; i++) {
		memset(wszVName, 0, sizeof(WCHAR)*256);
		VNLen = 256;
		DLen = 256;
		nRet = RegEnumValue(hKey, i, wszVName, &VNLen, NULL, &Type, (BYTE *)wszData, &DLen);
		if(nRet != ERROR_SUCCESS)
			return -2;
		if(_wcsicmp(wszVName, wszName) == 0)
			break;
	}
	memset(wszVName, 0, sizeof(WCHAR)*256);
	VNLen = 256;
	DLen = 256;
	RegEnumValue(hKey, i, wszVName, &VNLen, NULL, &Type, (BYTE *)dnValue, &DLen);

	RegCloseKey(hKey);

	return 0;
}
