Subversion Repositories spk

Rev

Rev 101 | Rev 254 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 cycrow 1
 
2
#include "TextDB.h"
3
#include "File_IO.h"
4
 
5
namespace SPK {
6
 
7
CTextDB::CTextDB(void)
8
{
9
	m_iLang = 44;
10
	m_pTexts = new TextList;
94 cycrow 11
	m_pTextComment = new TextList;
35 cycrow 12
	m_iInPage = 0;
13
}
14
 
15
CTextDB::~CTextDB(void)
16
{
17
	if ( m_pTexts ) delete m_pTexts;
94 cycrow 18
	if ( m_pTextComment ) delete m_pTextComment;
35 cycrow 19
}
20
 
197 cycrow 21
void CTextDB::parseTextFile(int iFromPage, int iToPage, const Utils::WString &sFile, int iLang)
35 cycrow 22
{
23
	// open and read the text file
24
	CFileIO File(sFile);
82 cycrow 25
	if ( File.startRead() ) {
26
		while ( !File.atEnd() ) {
197 cycrow 27
			this->_parseFileLine(iFromPage, iToPage, iLang, File.readEndOfLineStr());
35 cycrow 28
		}
82 cycrow 29
		File.close();
35 cycrow 30
 
31
	}
32
	m_iInPage = 0;
33
}
34
 
35
void CTextDB::setLanguage(int iLang)
36
{
37
	m_iLang = iLang;
38
}
39
 
40
bool CTextDB::exists(int iPage, int iID) const
41
{
42
	return this->exists(m_iLang, iPage, iID);
43
}
44
bool CTextDB::exists(int iLang, int iPage, int iID) const
45
{
46
	TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
47
	if ( itr != m_pTexts->end() ) return true;
48
	return false;
49
}
197 cycrow 50
Utils::WString CTextDB::get(int iPage, int iID) const
35 cycrow 51
{
52
	return this->get(m_iLang, iPage, iID);
53
}
197 cycrow 54
Utils::WString CTextDB::get(int iLang, int iPage, int iID) const
35 cycrow 55
{
56
	TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
57
	if ( itr != m_pTexts->end() ) return _parseText(iLang, itr->second);
197 cycrow 58
	return L"";	
35 cycrow 59
}
60
 
101 cycrow 61
bool CTextDB::anyTextLoaded() const
62
{
63
	return !m_pTexts->empty();
64
}
65
 
197 cycrow 66
Utils::WString CTextDB::_parseText(int iLang, const Utils::WString &sText, const Utils::WString &sReplace) const
35 cycrow 67
{
197 cycrow 68
	Utils::WString::size_type pos = sReplace.find_first_of(L",", 0);
69
	if ( pos != Utils::WString::npos ) {
70
		int iPage = Utils::WString(sReplace.substr(1, pos));
71
		int iID = Utils::WString(sReplace.substr(pos + 1, sReplace.length() - pos - 2));
35 cycrow 72
		return sText.findReplace(sReplace, this->get(iLang, iPage, iID));
73
	}
74
 
75
	return sText;
76
}
77
 
197 cycrow 78
Utils::WString CTextDB::_parseText(int iLang, const Utils::WString &sText) const
35 cycrow 79
{
197 cycrow 80
	Utils::WString sNewText = sText;
35 cycrow 81
 
197 cycrow 82
	Utils::WString::size_type pos = sNewText.find_first_of(L"{", 0);
83
	while ( pos != Utils::WString::npos ) {
84
		Utils::WString::size_type endPos = sNewText.find_first_of(L"}", pos);
85
		if ( endPos != Utils::WString::npos ) {
35 cycrow 86
			sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));
197 cycrow 87
			pos = sNewText.find_first_of(L"{", 0);
35 cycrow 88
		}
89
		else
197 cycrow 90
			pos = sNewText.find_first_of(L"{", pos);
35 cycrow 91
	}
92
	return sNewText;
93
}
94
 
197 cycrow 95
void CTextDB::_parseFileLine(int iFromPage, int iToPage, int iLang, const Utils::WString &sLine)
35 cycrow 96
{
97
	if ( m_iInPage ) {
98
		// check for end of page
197 cycrow 99
		size_t pos = sLine.find(L"</page>");
100
		if ( pos != Utils::WString::npos ) {
35 cycrow 101
			m_iInPage = 0;
102
		}
103
		else {
104
			this->_parsePage(iLang, sLine);
105
		}
106
	}
107
	else {
197 cycrow 108
		size_t pos = sLine.find(L"<page id=\"");
109
		if ( pos != Utils::WString::npos ) {
35 cycrow 110
			pos += 10; // move to the end of the search string (after ")
197 cycrow 111
			size_t endPos = sLine.find(L"\"", pos);
112
			int iPageID = Utils::WString(sLine.substr(pos, endPos - pos));
35 cycrow 113
			iPageID %= 10000; // make sure we remove the game id
114
			//TODO: mark the game id so we can overright if the game id is the same or higher
115
			if ( iFromPage > 0 && iPageID < iFromPage ) return; // out of range
116
			if ( iToPage > 0 && iPageID > iToPage) return; // out of range
117
 
118
			m_iInPage = iPageID;
119
		}
120
	}
121
}
122
 
123
 
124
 
197 cycrow 125
void CTextDB::_parsePage(int iLang, const Utils::WString &sLine)
35 cycrow 126
{
127
	// move to the id number
197 cycrow 128
	size_t pos = sLine.find(L"<t id=\"");
129
	if ( pos == Utils::WString::npos ) return;
35 cycrow 130
 
131
	pos += 7; // move past to the "
197 cycrow 132
	size_t endPos = sLine.find(L"\"", pos);
133
	if ( endPos == Utils::WString::npos ) return;
35 cycrow 134
 
135
	// get the number to add
197 cycrow 136
	int iID = Utils::WString(sLine.substr(pos, endPos - pos));
35 cycrow 137
 
138
	// now find the text
197 cycrow 139
	pos = sLine.find(L">", endPos);
140
	if ( pos == Utils::WString::npos ) return;
35 cycrow 141
	++pos;
197 cycrow 142
	endPos = sLine.find(L"</t>", pos);
143
	if ( endPos == Utils::WString::npos ) return;
35 cycrow 144
 
145
	this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));
146
}
147
 
197 cycrow 148
void CTextDB::_addText(int iLang, int iID, const Utils::WString &sText)
35 cycrow 149
{
94 cycrow 150
	// split the comment out
197 cycrow 151
	Utils::WString fullComment;
152
	Utils::WString text = sText;
94 cycrow 153
 
197 cycrow 154
	while ( text.contains(L"(") && text.contains(L")") ) {
155
		int iStart = text.findPos(L"(");
94 cycrow 156
		if ( iStart != -1 ) {
197 cycrow 157
			int iEnd = text.findPos(L")", iStart);
94 cycrow 158
			if ( iEnd != -1 ) {
197 cycrow 159
				Utils::WString comment = text.mid(iStart, ++iEnd);
94 cycrow 160
				text.erase(iStart, iEnd - iStart);
197 cycrow 161
				if ( !fullComment.empty() ) fullComment += L", ";
94 cycrow 162
				fullComment += comment;
163
			}
164
		}
165
	}
166
 
167
	text.removeEndSpace();
168
 
169
	(*m_pTexts)[_mapID(iLang, m_iInPage, iID)] = text;
170
	if ( !fullComment.empty() )
171
		(*m_pTextComment)[_mapID(iLang, m_iInPage, iID)] = fullComment;
35 cycrow 172
}
173
 
197 cycrow 174
Utils::WString CTextDB::_mapID(int iLang, int iPage, int iID) const
35 cycrow 175
{
197 cycrow 176
	Utils::WString sID;
35 cycrow 177
	sID += (long)iLang;
197 cycrow 178
	sID += L":";
35 cycrow 179
	sID += (long)iPage;
197 cycrow 180
	sID += L":";
35 cycrow 181
	sID += (long)iID;
182
 
183
	return sID;
184
}
185
 
186
}//NAMESPACE