#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "DynVector.h"
#include "ConfiguratorNode.h"

ConfiguratorNode::ConfiguratorNode()
{
	m_Next = new DynVector<ConfiguratorNode *>;
	m_Keys = new DynVector<Key *>;
}

ConfiguratorNode::~ConfiguratorNode()
{
	for(int i = 0;;i++)
	{
		if(!m_Next->Get(i)) break;
		delete *m_Next->Get(i);
	}
	delete m_Next;
	m_Next = 0;

	for(int i = 0;;i++)
	{
		if(!m_Keys->Get(i)) break;
		delete *m_Keys->Get(i);
	}
	delete m_Keys;
	m_Keys = 0;
}

ConfiguratorNode *ConfiguratorNode::Next_Level(const char *Item)
{
	for(int i = 0;;i++)
	{
		if(!m_Next->Get(i)) break;

		if(strcmp((*m_Next->Get(i))->Name, Item) == 0)
		{
			return *m_Next->Get(i);
		}
	}
	return this;
}

const char *ConfiguratorNode::Keys(const char *Item)
{
	for(int i = 0;;i++)
	{
		if(!m_Keys->Get(i)) break;

		if(strcmp((*m_Keys->Get(i))->Name, Item) == 0)
		{
			return (*m_Keys->Get(i))->Data;
		}
	}
	return 0;
}


void ConfiguratorNode::Add_Key(const char *Item, const char *Data)
{
	Key *k = new Key;
	strcpy_s(k->Name, 32, Item);
	strcpy_s(k->Data, 256, Data);
	m_Keys->Push(&k);
}

void ConfiguratorNode::Add_Level(const char *Item)
{
	ConfiguratorNode *c = new ConfiguratorNode;
	strcpy_s(c->Name, 32, Item);
	m_Next->Push(&c);
}