Subversion Repositories spk

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


#include "TextDB.h"
#include "File_IO.h"

namespace SPK {

CTextDB::CTextDB(void)
{
        m_iLang = 44;
        m_pTexts = new TextList;
        m_iInPage = 0;
}

CTextDB::~CTextDB(void)
{
        if ( m_pTexts ) delete m_pTexts;
}

void CTextDB::parseTextFile(int iFromPage, int iToPage, const Utils::String &sFile, int iLang)
{
        // open and read the text file
        CFileIO File(sFile);
        CyStringList *lines = File.ReadLinesStr();
        if ( File.StartRead() ) {
                while ( !File.AtEnd() ) {
                        this->_parseFileLine(iFromPage, iToPage, iLang, File.ReadToEndLine(false).ToString());
                }
                File.StopRead();

        }
        m_iInPage = 0;
}

void CTextDB::setLanguage(int iLang)
{
        m_iLang = iLang;
}

bool CTextDB::exists(int iPage, int iID) const
{
        return this->exists(m_iLang, iPage, iID);
}
bool CTextDB::exists(int iLang, int iPage, int iID) const
{
        TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
        if ( itr != m_pTexts->end() ) return true;
        return false;
}
Utils::String CTextDB::get(int iPage, int iID) const
{
        return this->get(m_iLang, iPage, iID);
}
Utils::String CTextDB::get(int iLang, int iPage, int iID) const
{
        TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
        if ( itr != m_pTexts->end() ) return _parseText(iLang, itr->second);
        return "";      
}

Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText, const Utils::String &sReplace) const
{
        std::string::size_type pos = sReplace.find_first_of(",", 0);
        if ( pos != std::string::npos ) {
                int iPage = Utils::String(sReplace.substr(1, pos));
                int iID = Utils::String(sReplace.substr(pos + 1, sReplace.length() - pos - 2));
                return sText.findReplace(sReplace, this->get(iLang, iPage, iID));
        }

        return sText;
}

Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText) const
{
        Utils::String sNewText = sText;

        std::string::size_type pos = sNewText.find_first_of("{", 0);
        while ( pos != std::string::npos ) {
                std::string::size_type endPos = sNewText.find_first_of("}", pos);
                if ( endPos != std::string::npos ) {
                        sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));
                        pos = sNewText.find_first_of("{", 0);
                }
                else
                        pos = sNewText.find_first_of("{", pos);
        }
        return sNewText;
}

void CTextDB::_parseFileLine(int iFromPage, int iToPage, int iLang, const Utils::String &sLine)
{
        if ( m_iInPage ) {
                // check for end of page
                size_t pos = sLine.find("</page>");
                if ( pos != std::string::npos ) {
                        m_iInPage = 0;
                }
                else {
                        this->_parsePage(iLang, sLine);
                }
        }
        else {
                size_t pos = sLine.find("<page id=\"");
                if ( pos != std::string::npos ) {
                        pos += 10; // move to the end of the search string (after ")
                        size_t endPos = sLine.find("\"", pos);
                        int iPageID = Utils::String(sLine.substr(pos, endPos - pos));
                        iPageID %= 10000; // make sure we remove the game id
                        //TODO: mark the game id so we can overright if the game id is the same or higher
                        if ( iFromPage > 0 && iPageID < iFromPage ) return; // out of range
                        if ( iToPage > 0 && iPageID > iToPage) return; // out of range

                        m_iInPage = iPageID;
                }
        }
}



void CTextDB::_parsePage(int iLang, const Utils::String &sLine)
{
        // move to the id number
        size_t pos = sLine.find("<t id=\"");
        if ( pos == std::string::npos ) return;
                
        pos += 7; // move past to the "
        size_t endPos = sLine.find("\"", pos);
        if ( endPos == std::string::npos ) return;

        // get the number to add
        int iID = Utils::String(sLine.substr(pos, endPos - pos));
        
        // now find the text
        pos = sLine.find(">", endPos);
        if ( pos == std::string::npos ) return;
        ++pos;
        endPos = sLine.find("</t>", pos);
        if ( endPos == std::string::npos ) return;

        this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));
}

void CTextDB::_addText(int iLang, int iID, const Utils::String &sText)
{
        (*m_pTexts)[_mapID(iLang, m_iInPage, iID)] = sText;
}

Utils::String CTextDB::_mapID(int iLang, int iPage, int iID) const
{
        Utils::String sID;
        sID += (long)iLang;
        sID += ":";
        sID += (long)iPage;
        sID += ":";
        sID += (long)iID;

        return sID;
}

}//NAMESPACE