///////////////////////////////////////////////////////////////////////////////
// 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

#include "wnvreg32.h"

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

void registryConstructor (PVOID pRegistryParm)
{
    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;
    pRegistry->m_pLocalMachine = NULL;
    pRegistry->m_hKeyPrivate = NULL;
    pRegistry->m_hKeyBoard = NULL;
}

void registryDestructor (PVOID pRegistryParm)
{
    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;
    pRegistry->m_pLocalMachine = NULL;
    pRegistry->m_hKeyPrivate = NULL;
    pRegistry->m_hKeyBoard = NULL;
}

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

void registryWrite (PVOID pRegistryParm, HKEY hKey, REGISTRY_NAME tzName, DWORD dwVal)
{
    DWORD dwResult;

    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;

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

    if (!pRegistry->m_pLocalMachine)
    {
	TraceString ("\n**********registryWrite: Ignored.");
	return;
    }

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

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

DWORD registryRead (PVOID pRegistryParm, HKEY hKey, REGISTRY_NAME tzName, DWORD dwDefault)
{
    DWORD dwResult;
    DWORD dwVal = dwDefault;
    DWORD dwType;
    DWORD dwSize = sizeof(DWORD);

    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;
    dwResult = ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_GetValue (
	hKey,
	tzName,
	&dwType,
	(PBYTE)&dwVal,
	&dwSize);
    if (dwResult)
    {
	TraceString ("\nregisteryRead: ");
	TraceWString (tzName);
	TraceString (" failed, returning default, error = ");
	TraceLong (dwResult);
	return dwDefault;
    }
    else
    {
	TraceString ("\nregistryRead: ");
	TraceWString (tzName);
	TraceString (" = ");
	TraceLong (dwVal);
	return dwVal;
    }
}

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

HKEY registryOpenKey (PVOID pRegistryParm, REGISTRY_NAME tzName)
{
    HKEY hKey;
    DWORD dwResult;

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

    return hKey;
}

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

void registryCloseKey (PVOID pRegistryParm, HKEY hKey)
{
    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;
    ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_CloseAnyKey (hKey);
}

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

DWORD registryOpen (PVOID pRegistryParm, UINT nBoard)
{
    DWORD dwResult;
    TCHAR tzBoard [128];

    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;
    //
    // instantiate the registry access class
    //
    pRegistry->m_pLocalMachine = (PVOID)new CWnvRegLocalMachine;

    //
    // get the key to the private area
    //
    pRegistry->m_hKeyPrivate = ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_OpenPrivateSystemKey (WNVREG_PRODUCT_VIDEUM_KEY);
    if (!pRegistry->m_hKeyPrivate)
    {
	dwResult = GetLastError ();
	TraceString ("\n********** registryOpen: 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 = ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_OpenSubKey (
	pRegistry->m_hKeyPrivate, 
	tzBoard,
	REG_OPTION_NON_VOLATILE, 
	&pRegistry->m_hKeyBoard);
    if (dwResult || !pRegistry->m_hKeyBoard)
    {
	dwResult = GetLastError ();
	TraceString ("\n********** registryOpen: WnvReg_OpenSubKey BoardX failed.");
	return dwResult;
    }

    return 0;	// pass
}

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

DWORD registryClose (PVOID pRegistryParm)
{
    PREGISTRY pRegistry = (PREGISTRY)pRegistryParm;

    TraceString ("\nregistryClose: ");
    if (((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine))
    {
	if (pRegistry->m_hKeyPrivate)
	{
	    ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_CommitChanges (pRegistry->m_hKeyPrivate);
	    if (pRegistry->m_hKeyBoard)
	    {
		((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_CloseAnyKey (pRegistry->m_hKeyBoard);
		pRegistry->m_hKeyBoard = NULL;
	    }
	    ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine)->WnvReg_CloseAnyKey (pRegistry->m_hKeyPrivate);
	    pRegistry->m_hKeyPrivate = NULL;
	}

	delete ((CWnvRegLocalMachine *)pRegistry->m_pLocalMachine);
	pRegistry->m_pLocalMachine = NULL;
    }

    return 0;	// pass
}

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