/* 
 * 
 * Confidential Information of Telekinesys Research Limited (t/a Havok).  Not for disclosure or distribution without Havok's
 * prior written consent.This software contains code, techniques and know-how which is confidential and proprietary to Havok.
 * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2008 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
 * 
 */
#include <Demos/demos.h>


#include <Demos/DemoCommon/Utilities/GameUtils/KeyValuesDatabase.h>
#include <Common/Base/System/Io/IStream/hkIStream.h>

/*
#ifdef _MSC_VER
#	pragma once
#	pragma warning( disable : 4786 )
#	pragma warning( disable : 4503 )
#endif
*/

KeyValuesDatabase::KeyValuesDatabase()
{
	m_lastSection = HK_NULL;
}

KeyValuesDatabase::~KeyValuesDatabase()
{
}

const hkString&	KeyValuesDatabase::getStringValue( const hkString& section, const hkString& key )
{
	const hkKeyValueEntry* e = HK_NULL;
	if ( m_lastSection )
	{
		if ( section == m_lastSectionString )
		{
			e = m_lastSection;
		}
	}

	if ( !e )
	{
		e = m_keyValues.find( section );
		m_lastSection = e;
		m_lastSectionString = section;
	}

	if( !e )
	{
		char str[120];
		hkString::snprintf( str, 120, "Key[%s][*] - not defined", section.cString() );
		hkcerr << str << hkendl;
		return m_emptyString;
	}

	const hkString* s = e->find( key );
	if( !s )
	{
		char str[120];
		hkString::snprintf( str, 120, "Key[%s][%s] - not defined", section.cString(), key.cString() );
		hkcerr << str << hkendl;
	}

	hkString& value = m_keyValues[section][key];
	return value;
}

hkReal	KeyValuesDatabase::getFloatValue( const hkString& section, const hkString& key )  
{
	const hkString& stringValue = getStringValue( section, key );
	return hkString::atof( stringValue.cString() );
}

int KeyValuesDatabase::getIntValue( const hkString& section, const hkString& key ) 
{
	const hkString& stringValue = getStringValue( section, key );
	return hkString::atoi( stringValue.cString() );
}

void KeyValuesDatabase::setStringValue( const hkString& section, const hkString& key, const hkString& valueStr )
{
	m_keyValues[section][key] = valueStr;
}
 
void KeyValuesDatabase::setFloatValue( const hkString& section, const hkString& key, hkReal value )
{
	char valueStr[50];
	hkString::snprintf( valueStr, 50, "%f", value );
	setStringValue( section, key, hkString( valueStr ) );
}

void KeyValuesDatabase::setIntValue( const hkString& section, const hkString& key, int value )
{
	char valueStr[50];
	hkString::snprintf( valueStr, 50, "%i", value );
	setStringValue( section, key, hkString( valueStr ) );
}

hkResult KeyValuesDatabase::load( hkIstream& stream )
{
  	const int	maxLineSize = 150;
	char		lineBuffer[maxLineSize];
	hkString	line;

	hkString	section;

	while( stream.isOk() )
	{
		stream.getline( lineBuffer, 150 );
		line = hkString( lineBuffer );
	
		//comment line
		if( line[0] == ';' ) 
		{
			continue; 
		}	

		// trim whitespace at end
		char trim = line[line.getLength() - 1];
		while( (line.getLength() > 0) && ((' ' == trim) || ('\r' == trim) || ('\n' == trim)) )
		{
			line = line.substr( 0, line.getLength() - 1 );
			trim = line[line.getLength() - 1];
		}

		if( 0 == line.getLength() )
		{
			continue;
		}

		//section heading
		if( '[' == line[0] )  
		{
			section = line.substr( 1, line.getLength() -2 );
			continue;
		}//if

		const char* pos = hkString::strStr( line.cString(), "=" );
		if( pos )
		{
			int iPos = static_cast<int>( (hkUlong)(pos - line.cString()) );
		
			//valid key value
			hkString key = line.substr( 0, iPos );
			hkString valueStr = line.substr( iPos + 1, line.getLength() - (iPos + 1) );

			m_keyValues[section][key] = valueStr;
		}

	}//while

	return HK_SUCCESS;
}

hkResult KeyValuesDatabase::save( hkOstream& stream )
{
	for( int i = 0; i < m_keyValues.m_storage.getSize(); i++ )
	{
		stream << "[" << m_keyValues.m_storage[i].first << "]\n";
		
		const hkKeyValueEntry& e = m_keyValues.m_storage[i].second;
		for( int j = 0; j < e.m_storage.getSize(); j++ )
		{
			stream << e.m_storage[j].first << "=" << e.m_storage[j].second << "\n";
		}
	}

	return HK_SUCCESS;
}

int	KeyValuesDatabase::getNumSections()
{
	return m_keyValues.m_storage.getSize();
}

void KeyValuesDatabase::getSectionByIndex( int index, hkString& section )
{
	HK_ASSERT2(0x4324287f,  (0 <= index) && (index < getNumSections()), "Section index out of range" );

	section = m_keyValues.m_storage[index].first;	
}

int KeyValuesDatabase::getNumKeysForSection( const hkString& section )
{
	hkKeyValueEntry& kv = m_keyValues[section];
	return kv.m_storage.getSize();
}

void KeyValuesDatabase::getKeyValueByIndex( const hkString& section, int index, hkString& key, hkString& value )
{
	HK_ASSERT2(0x63a609aa,  (0 <= index) && (index < getNumKeysForSection( section )), "Key index out of range for section" );

	hkKeyValueEntry& kv = m_keyValues[section];
	key = kv.m_storage[index].first;
	value = kv.m_storage[index].second;
}

/*
* Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20080529)
*
* Confidential Information of Havok.  (C) Copyright 1999-2008 
* Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
* Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
* rights, and intellectual property rights in the Havok software remain in
* Havok and/or its suppliers.
*
* Use of this software for evaluation purposes is subject to and indicates 
* acceptance of the End User licence Agreement for this product. A copy of 
* the license is included with this software and is also available at
* www.havok.com/tryhavok
*
*/
