// ###########################################################################
// #
// # utils - Implementation of miscellaneous utility functions
// # Copyright (C) 2009-2011 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#pragma hdrstop
#include <stdlib.h>
#include <registry.hpp>
#include "utils.h"

//---------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:	HToI
//
//  PURPOSE:	Convert a String representation of a hex value to
//					its numeric value
//
//  INPUTS:		AnsiString			String containing hex representation
//
//  OUTPUT:		int					Numeric value of hex string
//											or -1 if string is empty or contains
//											invalid hex characters
//
///////////////////////////////////////////////////////////////////////
int __fastcall HToI(AnsiString s)
{
	int 	count 	= 0;
	char 	*ch_ptr 	= s.c_str();

	// Check that string is not empty
	if (*ch_ptr)
	{
		// Loop through each non-NULL character in the string
		while (*ch_ptr)
		{
			// Check that character is a valid hex digit
			if (isxdigit(*ch_ptr))
			{
				// Shift existing result and add new character value
				count = (count * 16) +
					(*ch_ptr >= '0' && *ch_ptr <= '9' ?
						*ch_ptr - '0' : toupper(*ch_ptr) - 'A' + 10);
			}
			else
			{
				// Error due to invalid hex digit
				return -1;
			}

			ch_ptr++;
		}

		return count;
	}
	else
	{
		// Error due to empty string
		return -1;
	}
}

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:	GetRegistryValue
//
//  PURPOSE:   Obtain a previously stored value from the registry. If
//					the branch doesn't exist it will be created. If the key
//					doesn't exist, a default value supplied will be returned.
//					This will look in HKCU\SOFTWARE\DialTone\OSCA Emulator
//
//  INPUTS:		AnsiString			Name of key to be retrieved
//					AnsiString			Default value to return if key not found
//
//  OUTPUT:		AnsiString			Value requested from the registry, or
//											the supplied default value if the
//											key cannot be located
//
///////////////////////////////////////////////////////////////////////
AnsiString __fastcall GetRegistryValue(AnsiString ValName, AnsiString DefVal)
{
	AnsiString S;
	TRegistry *Registry = new TRegistry;

	try
	{
		Registry->RootKey = HKEY_CURRENT_USER;
		Registry->OpenKey("\\SOFTWARE\\DialTone\\OSCA Emulator", true);

		if (Registry->ValueExists(ValName))
		{
			S = Registry->ReadString(ValName);
		}
		else
		{
			S = DefVal;
			Registry->WriteString(ValName, DefVal);
		}
		Registry->CloseKey();
	}
	__finally
	{
		delete Registry;
	}

	return S;
}

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:	SetRegistryValue
//
//  PURPOSE:   Store a value into the registry. If the branch doesn't
//					exist it will be created. If the key doesn't exist, it
//					will be created.
//					This will look in HKCU\SOFTWARE\DialTone\OSCA Emulator
//
//  INPUTS:		AnsiString			Name of key to be stored
//					AnsiString			Value to be stored against the key
//
//  OUTPUT:		void
//
///////////////////////////////////////////////////////////////////////
void __fastcall SetRegistryValue(AnsiString ValName, AnsiString Value)
{
	TRegistry *Registry = new TRegistry;

	try
	{
		Registry->RootKey = HKEY_LOCAL_MACHINE;
		Registry->OpenKey("\\SOFTWARE\\DialTone\\OSCA Emulator", true);
		Registry->WriteString(ValName, Value);
		Registry->CloseKey();
	}
	__finally
	{
		delete Registry;
	}
}

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:	TrashMemory
//
//  PURPOSE:   Randomize the contents of a block of memory
//
//  INPUTS:		unsigned char *	Pointer to start of memory block
//					unsigned int		Number of bytes to be randomized
//
//  OUTPUT:		void
//
///////////////////////////////////////////////////////////////////////
void __fastcall TrashMemory(unsigned char *addr, unsigned int count)
{
	do
	{
		*(addr++) = random(256);
	}
	while (--count);
}

//---------------------------------------------------------------------------

#pragma package(smart_init)

