GProfile &GetErrorProfile()
{
	if (g_strErrorFile.IsEmpty())
	{
		if (g_pzStaticErrMap)
		{
			g_strErrorFile = "Static Load";
			g_pstrErrorFileContents = new GString(g_pzStaticErrMap,strlen(g_pzStaticErrMap));
		}
		else
		{
			const char *pzErrFile = GetProfile().GetString("System", "Errors", 0);
			if (pzErrFile && pzErrFile[0])
			{
				g_pstrErrorFileContents = new GString();
				if (!g_pstrErrorFileContents->FromFile(pzErrFile,0))
				{
					pzErrFile = 0;
				}
				g_strErrorFile = pzErrFile;
			}
			// if the error file could not be found, default to a minimal error file
			if (!pzErrFile)
			{
				g_pstrErrorFileContents = new GString();
				(*g_pstrErrorFileContents) = "[Exception]\nSubSystem=0\n[Profile]\nSubSystem=1\n0=[%s.%s]Error Description File Not loaded.\n1=[%s]Error Description File Not loaded.  Call SetErrorDescriptions().\n";
				g_strErrorFile = "Static";
			}
		}
	}
	static GProfile ErrorProfile(g_pstrErrorFileContents->StrVal(), (int)g_pstrErrorFileContents->Length(), 0);
	return ErrorProfile;
}
void GProfile::ProfileParse(const char *szBuffer, __int64 dwSize)
{
	GProfileSection *pSection = 0;

	// parse the file
	__int64 nIdx = 0;

	GString strLine;
	while (nIdx < dwSize)
	{
		GetLine(strLine, szBuffer, &nIdx, dwSize);

		strLine.TrimRightWS();
		strLine.TrimLeftWS();

		if ((strLine.IsEmpty()) || (strLine.GetAt(0) == ';'))
			continue;


		strLine.Replace("\\n", '\n');

		if (strLine.GetAt(0) == '[')
		{
			__int64 nIdx = strLine.Find(']');
			if (nIdx == -1)
				nIdx = strLine.Length();

			// new section
			pSection = new GProfileSection;
			pSection->m_strName = strLine.Mid(1, nIdx - 1);
			pSection->m_strName.TrimLeftWS();
			pSection->m_strName.TrimRightWS();

			m_lstSections.AddLast(pSection);
		}
		else if (pSection)
		{
			__int64 nIdx = strLine.Find('=');
			if (nIdx == -1)
				continue;
			GProfileEntry *pNVP = new GProfileEntry;
			pSection->m_lstNVP.AddLast(pNVP);
			pNVP->m_strName = strLine.Left(nIdx);
			pNVP->m_strName.TrimLeftWS();
			pNVP->m_strName.TrimRightWS();
			
			pNVP->m_strValue = strLine.Mid(nIdx + 1);
			pNVP->m_strValue.TrimLeftWS();
			pNVP->m_strValue.TrimRightWS();
		}
	}
	m_bCached = true;
}
void	GXmlWriter::WriteData(const GString &Content)
{
    if (this->_rest.Size() != 0)
    {
        if (this->_container.ExistKey(this->_rest[0]))
        {
            for (unsigned int i = 0; i < this->_space; ++i)
                this->_xml += "\t";
            this->_xml += "<" + this->_rest[0] + ">\n";
            ++this->_space;
            GStringList l = this->_container[this->_rest[0]];
            this->_current.PushFront(this->_rest[0]);
            this->_rest.Erase(0);
            for (int i = l.Size() - 1; i >= 0; --i)
                this->_rest.PushFront(l[i]);
            this->WriteData(Content);
        }
        else
        {
            for (unsigned int i = 0; i < this->_space; ++i)
                this->_xml += "\t";
            if (Content.IsEmpty())
                this->_xml += "</" + this->_rest[0] + ">\n";
            else
                this->_xml += "<" + this->_rest[0] + ">" + Content + "</" + this->_rest[0] + ">\n";
            this->_rest.Erase(0);
        }
    }
    else
    {
        if (this->_current.Size() >= 1)
        {
            --this->_space;
            for (unsigned int i = 0; i < this->_space; ++i)
                this->_xml += "\t";
            this->_xml += "</" + this->_current[0] + ">\n";
            this->_current.Erase(0);
            this->WriteData(Content);
        }
        else
        {
            this->_rest.PushBack(this->_main);
            this->WriteData(Content);
        }
    }

}