/** 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/xml/xmlnode.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/core/xml/xmlbase.hpp"

sgxXMLNode::~sgxXMLNode()
{
	sgxVector<sgxXMLNode *>::iterator it = m_ChildNodes.begin();

	for(;it!=m_ChildNodes.end();++it) {
		delete (*it);
	}
}

/**
Retrieve the first child of the current node, called @name, or
NULL if this doesn't exist.

  This is a const-safe version
*/
const sgxXMLNode *sgxXMLNode::getChild(const sgxString &name) const
{
	sgxVector<sgxXMLNode *>::const_iterator cit = m_ChildNodes.begin();

	for(;cit!=m_ChildNodes.end();++cit) {
		if ((*cit) && (*cit)->m_Element.m_Name == name) {
			return (*cit);
		}
	}

	return NULL;
}

/**
Retrieve the first child of the current node, called @name, or
NULL if this doesn't exist.

  This is NOT const-safe.
*/
sgxXMLNode *sgxXMLNode::getChild(const sgxString &name)
{
	sgxVector<sgxXMLNode *>::iterator it = m_ChildNodes.begin();

	for(;it!=m_ChildNodes.end();++it) {
		if ((*it) && (*it)->m_Element.m_Name == name) {
			return (*it);
		}
	}

	return NULL;
}

/**
Determines whether a specific attribute exists at this node. The value
is ignored.
*/
tBOOL	sgxXMLNode::doesAttributeExist(const sgxString &name) const
{
	sgxString v;
	return getAttributeImpl(name, v);
}

/**
Retrieves the attribute value into @value, matching @name. So a tag like,
<a href="blah"> has a attribute name of "href", and a resultant value of "blah"
*/
tBOOL	sgxXMLNode::getAttributeImpl(const sgxString &name, sgxString &value) const
{
	sgxVector<sgxXMLAttribute>::const_iterator cit = m_Attribute.begin();

	for(;cit!=m_Attribute.end();++cit) {
		if ((*cit).m_Name == name) {
			value = (*cit).m_Value;
			return TRUE;
		}
	}

	return FALSE;
}

/**
Thse will change the existing attribute, or add a new one, if none
existed previously.

@returns TRUE if a new attribute was added
*/
tBOOL	sgxXMLNode::setAttributeImpl(const sgxString &name, sgxString &value)
{
	sgxVector<sgxXMLAttribute>::iterator cit = m_Attribute.begin();

	for(;cit!=m_Attribute.end();++cit) {
		if ((*cit).m_Name == name) {
			(*cit).m_Value = value;
			return FALSE;
		}
	}

	// Not there - so add it
	addAttribute(name, value);

	return TRUE;
}

tBOOL	
sgxXMLNode::setAttribute(const sgxString &name, tINT32 &value) {
	sgxString v;
	sgxStringFormat(v, "%d", value);
	return setAttributeImpl(name, v);
}


//
// GetAttribute: These versions will apply a default value, if no tag is found.
//
tBOOL	sgxXMLNode::getAttribute(const sgxString &name, sgxString &value, const sgxString &default_value) const
{
	if (!getAttributeImpl(name, value)) {
		value = default_value;
		return FALSE;
	}

	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tUINT32 &value, const tUINT32 default_value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		value = default_value;
		return FALSE;
	}

	value = sgxAtoi(str.c_str());
	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tINT32 &value, const tINT32 default_value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		value = default_value;
		return FALSE;
	}

	value = sgxAtoi(str.c_str());
	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tREAL32 &value, const tREAL32 default_value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		value = default_value;
		return FALSE;
	}

	value = sgxAtof(str.c_str());
	return TRUE;
}

//
// GetAttribute: These versions leave the data unchanged
//
tBOOL	sgxXMLNode::getAttribute(const sgxString &name, sgxString &value) const
{
	if (!getAttributeImpl(name, value)) {
		return FALSE;
	}

	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tUINT32 &value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		return FALSE;
	}

	value = sgxAtoi(str.c_str());
	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tINT32 &value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		return FALSE;
	}

	value = sgxAtoi(str.c_str());
	return TRUE;
}

tBOOL	sgxXMLNode::getAttribute(const sgxString &name, tREAL32 &value) const
{
	sgxString str;

	if (!getAttributeImpl(name, str)) {
		return FALSE;
	}

	value = sgxAtof(str.c_str());
	return TRUE;
}
