Rev 94 | Rev 254 | 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_pTextComment = new TextList;m_iInPage = 0;}CTextDB::~CTextDB(void){if ( m_pTexts ) delete m_pTexts;if ( m_pTextComment ) delete m_pTextComment;}void CTextDB::parseTextFile(int iFromPage, int iToPage, const Utils::String &sFile, int iLang){// open and read the text fileCFileIO File(sFile);if ( File.startRead() ) {while ( !File.atEnd() ) {this->_parseFileLine(iFromPage, iToPage, iLang, File.readEndOfLine());}File.close();}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 "";}bool CTextDB::anyTextLoaded() const{return !m_pTexts->empty();}Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText, const Utils::String &sReplace) const{Utils::String::size_type pos = sReplace.find_first_of(",", 0);if ( pos != Utils::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;Utils::String::size_type pos = sNewText.find_first_of("{", 0);while ( pos != Utils::String::npos ) {Utils::String::size_type endPos = sNewText.find_first_of("}", pos);if ( endPos != Utils::String::npos ) {sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));pos = sNewText.find_first_of("{", 0);}elsepos = 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 pagesize_t pos = sLine.find("</page>");if ( pos != Utils::String::npos ) {m_iInPage = 0;}else {this->_parsePage(iLang, sLine);}}else {size_t pos = sLine.find("<page id=\"");if ( pos != Utils::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 higherif ( iFromPage > 0 && iPageID < iFromPage ) return; // out of rangeif ( iToPage > 0 && iPageID > iToPage) return; // out of rangem_iInPage = iPageID;}}}void CTextDB::_parsePage(int iLang, const Utils::String &sLine){// move to the id numbersize_t pos = sLine.find("<t id=\"");if ( pos == Utils::String::npos ) return;pos += 7; // move past to the "size_t endPos = sLine.find("\"", pos);if ( endPos == Utils::String::npos ) return;// get the number to addint iID = Utils::String(sLine.substr(pos, endPos - pos));// now find the textpos = sLine.find(">", endPos);if ( pos == Utils::String::npos ) return;++pos;endPos = sLine.find("</t>", pos);if ( endPos == Utils::String::npos ) return;this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));}void CTextDB::_addText(int iLang, int iID, const Utils::String &sText){// split the comment outUtils::String fullComment;Utils::String text = sText;while ( text.isin("(") && text.isin(")") ) {int iStart = text.findPos("(");if ( iStart != -1 ) {int iEnd = text.findPos(")", iStart);if ( iEnd != -1 ) {Utils::String comment = text.mid(iStart, ++iEnd);text.erase(iStart, iEnd - iStart);if ( !fullComment.empty() ) fullComment += ", ";fullComment += comment;}}}text.removeEndSpace();(*m_pTexts)[_mapID(iLang, m_iInPage, iID)] = text;if ( !fullComment.empty() )(*m_pTextComment)[_mapID(iLang, m_iInPage, iID)] = fullComment;}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