///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Winnov L.P., 1996.  All rights reserved
// registry.cpp: Registry Class implementation
///////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <tchar.h>
#include "wnvreg32.h"
#include "debug.h"
#include "registry.h"
#include "nlstring.h"

#define LOCAL_RADIX	10

CRegistry *gpRegistry = NULL;	// instanced by process

///////////////////////////////////////////////////////////////////////////////

CRegistry::CRegistry ()
{
    m_pLocalMachine = NULL;
    m_hKeyPrivate = NULL;
    m_hKeyBoard = NULL;
}

CRegistry::~CRegistry ()
{
}

///////////////////////////////////////////////////////////////////////////////

void CRegistry::Write (HKEY hKey, REGISTRY_NAME tzName, DWORD dwVal)
{
    DWORD dwResult;

    TraceString ("\nCRegistry::Write: ");
    TraceWString (tzName);
    TraceString (" = ");
    TraceLong (dwVal);

    dwResult = m_pLocalMachine->WnvReg_SetValue (
	hKey,
	tzName,
	REG_DWORD,
	(PBYTE)&dwVal,
	sizeof(dwVal));
    if (dwResult)
    {
	TraceString ("\n********** CRegistry::Write: WnvReg_SetValue failed.");
    }
}

///////////////////////////////////////////////////////////////////////////////

DWORD CRegistry::Read (HKEY hKey, REGISTRY_NAME tzName, DWORD dwDefault)
{
    DWORD dwResult;
    DWORD dwVal = dwDefault;
    DWORD dwType;
    DWORD dwSize = sizeof(DWORD);

    dwResult = m_pLocalMachine->WnvReg_GetValue (
	hKey,
	tzName,
	&dwType,
	(PBYTE)&dwVal,
	&dwSize);
    if (dwResult)
    {
	TraceString ("\nCRegistery::Read: ");
	TraceWString (tzName);
	TraceString (" failed, returning default, error = ");
	TraceLong (dwResult);
	return dwDefault;
    }
    else
    {
	TraceString ("\nCRegistery::Read: ");
	TraceWString (tzName);
	TraceString (" = ");
	TraceLong (dwVal);
	return dwVal;
    }
}

///////////////////////////////////////////////////////////////////////////////

HKEY CRegistry::OpenKey (REGISTRY_NAME tzName)
{
    HKEY hKey;
    DWORD dwResult;

    //
    // get the key to the tzName area under the private area
    //
    dwResult = m_pLocalMachine->WnvReg_OpenSubKey (
	m_hKeyBoard,
	tzName,
	REG_OPTION_NON_VOLATILE, 
	&hKey);
    if (dwResult || !hKey)
    {
	dwResult = GetLastError ();
	TraceString ("\n********** CRegistry::OpenKey: WnvReg_OpenSubKey failed.");
	SetLastError (dwResult);
	return NULL;
    }

    return hKey;
}

///////////////////////////////////////////////////////////////////////////////

void CRegistry::CloseKey (HKEY hKey)
{
    m_pLocalMachine->WnvReg_CloseAnyKey (hKey);
}

///////////////////////////////////////////////////////////////////////////////

DWORD CRegistry::Open (UINT nBoard)
{
    DWORD dwResult;
    TCHAR tzBoard [128];

    //
    // instantiate the registry access class
    //
    m_pLocalMachine = new CWnvRegLocalMachine;

    //
    // get the key to the private area
    //
    m_hKeyPrivate = m_pLocalMachine->WnvReg_OpenPrivateSystemKey (WNVREG_PRODUCT_VIDEUM_KEY);
    if (!m_hKeyPrivate)
    {
	dwResult = GetLastError ();
	TraceString ("\n********** CRegistry::Open: WnvReg_OpenPrivateSystemKey failed.");
	return dwResult;
    }

    //
    // concatenate the board number to create the board key
    //
    _tcscpy (tzBoard, NLS_BOARD);
    _itot (nBoard, &tzBoard [_tcslen (tzBoard)], LOCAL_RADIX);

    //
    // get the key to the BOARD area under the private area
    //
    dwResult = m_pLocalMachine->WnvReg_OpenSubKey (
	m_hKeyPrivate, 
	tzBoard,
	REG_OPTION_NON_VOLATILE, 
	&m_hKeyBoard);
    if (dwResult || !m_hKeyBoard)
    {
	dwResult = GetLastError ();
	TraceString ("\n********** CRegistry::Open: WnvReg_OpenSubKey BoardX failed.");
	return dwResult;
    }

    return 0;	// pass
}

///////////////////////////////////////////////////////////////////////////////

DWORD CRegistry::Close (void)
{
    TraceString ("\nCRegistry::Close: ");
    if (m_pLocalMachine)
    {
	if (m_hKeyBoard)
	{
	    m_pLocalMachine->WnvReg_CloseAnyKey (m_hKeyBoard);
	    m_hKeyBoard = NULL;
	}
	if (m_hKeyPrivate)
	{
	    m_pLocalMachine->WnvReg_CommitChanges (m_hKeyPrivate);
	    m_pLocalMachine->WnvReg_CloseAnyKey (m_hKeyPrivate);
	    TraceString ("OK...");
	    m_hKeyPrivate = NULL;
	}

	delete m_pLocalMachine;
	m_pLocalMachine = NULL;
    }

    return 0;	// pass
}

///////////////////////////////////////////////////////////////////////////////
//				    End of File
///////////////////////////////////////////////////////////////////////////////
