/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     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: src/drivers/common/expat/expat.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/filesystem/filesystem.hpp"

#include "libraries/expat/xmlparse/xmlparse.h"
#include "libraries/expat/expat.hpp"

struct expat_state {
	sgxXMLNode				*pCurrentRoot;
	sgxVector<sgxXMLNode *>	NodeStack;
};


CXMLParser *sgxXMLExpatParser::create()
{
	if (!ms_pSingleton) {
		ms_pSingleton = new sgxXMLExpatParser();
	}
	return ms_pSingleton;
}

sgxXMLExpatParser::sgxXMLExpatParser()
{
}

sgxXMLExpatParser::~sgxXMLExpatParser()
{
}

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

void startElement(void *userData, const char *name, const char **atts)
{
	expat_state *pState = (expat_state *)userData;

	sgxXMLNode	*pTag = new sgxXMLNode();

	pTag->setElement(sgxXMLElement(name));

	const char **p = atts;
	while(*p) {
		pTag->addAttribute(sgxString(*p), sgxString(*(p+1)));
		p+=2;
	}

	pState->pCurrentRoot = pState->pCurrentRoot->addNode(pTag);
	pState->NodeStack.push_back(pState->pCurrentRoot);
}

void endElement(void *userData, const char *name)
{
	SGX_UNUSED(name);
	
	expat_state *pState = (expat_state *)userData;

	pState->NodeStack.pop_back();
	pState->pCurrentRoot = pState->NodeStack.back();
}

void dataHandler(void *userData, const XML_Char *s, int len)
{
	expat_state *pState = (expat_state *)userData;

	pState->pCurrentRoot->appendData(sgxString(s, len));
}


tBOOL sgxXMLExpatParser::parseFile(sgxXMLTree &tree, CSGXFile &file)
{
char buf[1024];	// this is arbitrary
int done;
int depth = 0;
expat_state state;

	state.pCurrentRoot = tree.getRootNode();
	state.NodeStack.push_back(state.pCurrentRoot);

	XML_Parser parser = XML_ParserCreate(NULL);
	XML_SetUserData(parser, &state);
	XML_SetElementHandler(parser, startElement, endElement);
	XML_SetCharacterDataHandler(parser, dataHandler);

	do {
		int len = file.read((tBYTE *)buf, sizeof(buf));

		done = len < sizeof(buf);

		if (!XML_Parse(parser, buf, len, done)) {
			sgxTrace(
			"%s at line %d\n",
			XML_ErrorString(XML_GetErrorCode(parser)),
			XML_GetCurrentLineNumber(parser));
			return FALSE;
		}
	} while (!done);

	XML_ParserFree(parser);
	return TRUE;
}
