Rev 101 | 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::WString &sFile, int iLang)
{
// open and read the text file
CFileIO File(sFile);
if ( File.startRead() ) {
while ( !File.atEnd() ) {
this->_parseFileLine(iFromPage, iToPage, iLang, File.readEndOfLineStr());
}
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::WString CTextDB::get(int iPage, int iID) const
{
return this->get(m_iLang, iPage, iID);
}
Utils::WString 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 L"";
}
bool CTextDB::anyTextLoaded() const
{
return !m_pTexts->empty();
}
Utils::WString CTextDB::_parseText(int iLang, const Utils::WString &sText, const Utils::WString &sReplace) const
{
Utils::WString::size_type pos = sReplace.find_first_of(L",", 0);
if ( pos != Utils::WString::npos ) {
int iPage = Utils::WString(sReplace.substr(1, pos));
int iID = Utils::WString(sReplace.substr(pos + 1, sReplace.length() - pos - 2));
return sText.findReplace(sReplace, this->get(iLang, iPage, iID));
}
return sText;
}
Utils::WString CTextDB::_parseText(int iLang, const Utils::WString &sText) const
{
Utils::WString sNewText = sText;
Utils::WString::size_type pos = sNewText.find_first_of(L"{", 0);
while ( pos != Utils::WString::npos ) {
Utils::WString::size_type endPos = sNewText.find_first_of(L"}", pos);
if ( endPos != Utils::WString::npos ) {
sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));
pos = sNewText.find_first_of(L"{", 0);
}
else
pos = sNewText.find_first_of(L"{", pos);
}
return sNewText;
}
void CTextDB::_parseFileLine(int iFromPage, int iToPage, int iLang, const Utils::WString &sLine)
{
if ( m_iInPage ) {
// check for end of page
size_t pos = sLine.find(L"</page>");
if ( pos != Utils::WString::npos ) {
m_iInPage = 0;
}
else {
this->_parsePage(iLang, sLine);
}
}
else {
size_t pos = sLine.find(L"<page id=\"");
if ( pos != Utils::WString::npos ) {
pos += 10; // move to the end of the search string (after ")
size_t endPos = sLine.find(L"\"", pos);
int iPageID = Utils::WString(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::WString &sLine)
{
// move to the id number
size_t pos = sLine.find(L"<t id=\"");
if ( pos == Utils::WString::npos ) return;
pos += 7; // move past to the "
size_t endPos = sLine.find(L"\"", pos);
if ( endPos == Utils::WString::npos ) return;
// get the number to add
int iID = Utils::WString(sLine.substr(pos, endPos - pos));
// now find the text
pos = sLine.find(L">", endPos);
if ( pos == Utils::WString::npos ) return;
++pos;
endPos = sLine.find(L"</t>", pos);
if ( endPos == Utils::WString::npos ) return;
this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));
}
void CTextDB::_addText(int iLang, int iID, const Utils::WString &sText)
{
// split the comment out
Utils::WString fullComment;
Utils::WString text = sText;
while ( text.contains(L"(") && text.contains(L")") ) {
int iStart = text.findPos(L"(");
if ( iStart != -1 ) {
int iEnd = text.findPos(L")", iStart);
if ( iEnd != -1 ) {
Utils::WString comment = text.mid(iStart, ++iEnd);
text.erase(iStart, iEnd - iStart);
if ( !fullComment.empty() ) fullComment += L", ";
fullComment += comment;
}
}
}
text.removeEndSpace();
(*m_pTexts)[_mapID(iLang, m_iInPage, iID)] = text;
if ( !fullComment.empty() )
(*m_pTextComment)[_mapID(iLang, m_iInPage, iID)] = fullComment;
}
Utils::WString CTextDB::_mapID(int iLang, int iPage, int iID) const
{
Utils::WString sID;
sID += (long)iLang;
sID += L":";
sID += (long)iPage;
sID += L":";
sID += (long)iID;
return sID;
}
}//NAMESPACE