/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/core/settings/configuration.cpp

*/

#include "sgx/core/core.hpp"

CConfiguration	*CConfiguration::m_pSingleton;

CConfiguration *CConfiguration::get()
{
	if (!m_pSingleton) {
		m_pSingleton = new CConfiguration();
	}
	return m_pSingleton;
}

CConfiguration::CConfiguration() 
{
	m_bcomplete = FALSE;
}

/**
add a mapping between @opt and @value in the configuration. Any changes
to 'complete' configurations (i.e. those issued after @complete is called)
are ignored, and existing options are replaced.
*/ 
void 
CConfiguration::setOption(const sgxString &opt, tINT32 value)
{
    if (m_bcomplete) {
        sgxTrace("Option '%s' ignored!", opt.c_str());
        return;
    }

	sgxString str;
	sgxStringFormat(str, "%d", value);
    m_Options[opt] = str;
}

void 
CConfiguration::setOption(const sgxString &opt, const sgxString &value)
{
    if (m_bcomplete) {
        sgxTrace("Option '%s' ignored!", opt.c_str());
        return;
    }

    m_Options[opt] = value;
}


/**
Returns the current value of the specified option, @opt. If the option
doesn't exist, then the default @opt_default is returned.
*/
tINT32	CConfiguration::getOption(const sgxString &opt, tINT32 opt_default/*=0*/)
{
	tINT32 r;
	getOption(r, opt, opt_default);
	return r;
}

tBOOL	CConfiguration::getOption(tINT32 &result, const sgxString &opt, const tINT32 opt_default/*=0*/) {
	sgxOptionMap::iterator it = m_Options.find(opt);

	if (it == m_Options.end()) {
		result = opt_default;
		return FALSE;
	}

	result = sgxAtoi((*it).second);
	return TRUE;
}

tBOOL	CConfiguration::getOption(sgxString &result, const sgxString &opt, const sgxString &opt_default/*=0*/)
{
	sgxOptionMap::iterator it = m_Options.find(opt);

	if (it == m_Options.end()) {
		result = opt_default;
		return FALSE;
	}

	result = (*it).second;
	return TRUE;
}

/**
Marks this configuration as 'complete'. This means that no other options can be
added. This helps stops mispellings (e.g. GODMODE and GOODMODE) from both 
being used.
*/
void 
CConfiguration::complete(void)
{
	m_bcomplete = TRUE;
}
