/*	Storage.c - Handles persistent storage
*
*******************************************************************************
* Copyright (c) 1996-97 Digi International Inc., All Rights Reserved          *
*                                                                             *
* This  software  contains  proprietary  and  confidential   information   of *
* Digi International  Inc.   By  accepting  transfer  of this copy, Recipient *
* agrees to retain this software in  confidence,  to  prevent  disclosure  to *
* others,  and  to make no  use of this software other than that for which it *
* was delivered.  This is an unpublished copyright work  Digi   International *
* Inc.    Except   as   permitted   by  federal  law,  17 USC 117, copying is *
* strictly prohibited.                                                        *
*                                                                             *
* Digi  International  Inc.  CONFIDENTIAL  -   (Digi    International    Inc. *
* Confidential  when   combined   with   the   aggregated  modules  for  this *
* product) OBJECT CODE ONLY SOURCE MATERIALS                                  *
*                                                                             *
* (C) COPYRIGHT Digi International Inc. 1996-97                               *
*                                                                             *
* All Rights Reserved                                                         *
*                                                                             *
* US   Government   Users   Restricted   Rights   -   Use,   duplication   or *
* disclosure  restricted by GSA ADP Schedule Contract with Digi International *
* Inc.                                                                        *
*                                                                             *
*******************************************************************************
*
*/

#include "DS_RAS.h"
#include "Storage.h"

HKEY hkRoot = NULL;

BOOL StorageInitialize(char const * const pszModule)
{
	BOOL fSuccess = FALSE;
	HKEY hkAgents = NULL;
	DWORD eType;
	char szRoot[MAX_PATH];
	DWORD cbRoot = sizeof szRoot;

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ExtensionAgents", 0, KEY_QUERY_VALUE, &hkAgents) != ERROR_SUCCESS)
		goto Error;

	if (RegQueryValueEx(hkAgents, pszModule, NULL, &eType, szRoot, &cbRoot) != ERROR_SUCCESS)
		goto Error;

	if (eType != REG_SZ)
		goto Error;

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szRoot, 0, KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY, &hkRoot) != ERROR_SUCCESS)
		goto Error;

	fSuccess = TRUE;

Error:

	if (hkAgents != NULL)
		RegCloseKey(hkAgents);

	return fSuccess;
}

void StorageTearDown(void)
{
	if (hkRoot != NULL)
	{
		RegCloseKey(hkRoot);
		hkRoot = NULL;
	}
}

static BOOL NameToKeyAndValue(char const * const pszName, DWORD const dwAccess, HKEY * const phk, char const * * const ppsz)
{
	BOOL fSuccess = FALSE;
	char const * const pszLastSlash = strrchr(pszName, '/');
	char * pszKey = NULL;

	if (hkRoot == NULL) goto Error;

	if (pszLastSlash == NULL)
	{
		*phk = hkRoot;
		*ppsz = pszName;
	}
	else if (pszLastSlash == pszName)
	{
		*phk = hkRoot;
		*ppsz = pszName + 1;
	}
	else
	{
		size_t cb = pszLastSlash - pszName;
		size_t i;

		pszKey = malloc(cb + 1);
		if (pszKey == NULL) goto Error;

		for (i = 0; i < cb; i++)
			pszKey[i] = (pszName[i] == '/') ? '\\' : pszName[i];
		pszKey[i] = '\0';

		if (RegOpenKeyEx(hkRoot, pszKey, 0, dwAccess, phk) != ERROR_SUCCESS)
			goto Error;

		*ppsz = pszLastSlash + 1;
	}
	
	fSuccess = TRUE;

Error:
	
	if (pszKey != NULL)
		free(pszKey);

	if (!fSuccess)
	{
		*phk = NULL;
		*ppsz = NULL;
	}

	return fSuccess;
}

static void ReleaseKeyAndValue(HKEY * const phk, char const * * const ppsz)
{
	if ((*phk != NULL) && (*phk != hkRoot))
		RegCloseKey(*phk);
}


BOOL GetStoredString(char const * const pszName, char * const pszString, size_t * const pcbSize, char const * const pszDefault)
{
	BOOL fSuccess = FALSE;
	HKEY hkValue;
	char const * pszValue;
	DWORD eType;
		
	if (!NameToKeyAndValue(pszName, KEY_QUERY_VALUE, &hkValue, &pszValue)) goto Error;

	if ((RegQueryValueEx(hkValue, pszValue, NULL, &eType, pszString, pcbSize) != ERROR_SUCCESS) ||
		(eType != REG_SZ))

	{
		if (pszDefault == NULL) goto Error;

		{
			size_t cbSize = *pcbSize;
			size_t cbDefault = strlen(pszDefault) + 1;

			*pcbSize = cbDefault;
			if (cbDefault > *pcbSize) goto Done;

			strcpy(pszString, pszDefault);
		}
	}

	fSuccess = TRUE;
	goto Done;

Error:

	*pcbSize = 0;

Done:

	ReleaseKeyAndValue(&hkValue, &pszValue);

	return fSuccess;
}

BOOL GetStoredNumeric(char const * const pszName, void * const pvNumber, size_t const cbSize, void * const pvDefault)
{
	return FALSE;
}

BOOL SetStoredString(char const * const pszName, char const * const pszString)
{
	return FALSE;
}

BOOL SetStoredNumeric(char const * const pszName, void * const pvNumber, size_t const cbSize)
{
	return FALSE;
}

void DeleteStoredValue(char const * const pszName)
{
}

BOOL StoredValueExists(char const * const pszName)
{
	return FALSE;
}
