Subversion Repositories spk

Rev

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

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