Subversion Repositories spk

Rev

Rev 94 | 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
 
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
 
21
void CTextDB::parseTextFile(int iFromPage, int iToPage, const Utils::String &sFile, int iLang)
22
{
23
	// open and read the text file
24
	CFileIO File(sFile);
82 cycrow 25
	if ( File.startRead() ) {
26
		while ( !File.atEnd() ) {
52 cycrow 27
			this->_parseFileLine(iFromPage, iToPage, iLang, File.readEndOfLine());
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
}
50
Utils::String CTextDB::get(int iPage, int iID) const
51
{
52
	return this->get(m_iLang, iPage, iID);
53
}
54
Utils::String CTextDB::get(int iLang, int iPage, int iID) const
55
{
56
	TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
57
	if ( itr != m_pTexts->end() ) return _parseText(iLang, itr->second);
58
	return "";	
59
}
60
 
101 cycrow 61
bool CTextDB::anyTextLoaded() const
62
{
63
	return !m_pTexts->empty();
64
}
65
 
35 cycrow 66
Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText, const Utils::String &sReplace) const
67
{
84 cycrow 68
	Utils::String::size_type pos = sReplace.find_first_of(",", 0);
69
	if ( pos != Utils::String::npos ) {
35 cycrow 70
		int iPage = Utils::String(sReplace.substr(1, pos));
71
		int iID = Utils::String(sReplace.substr(pos + 1, sReplace.length() - pos - 2));
72
		return sText.findReplace(sReplace, this->get(iLang, iPage, iID));
73
	}
74
 
75
	return sText;
76
}
77
 
78
Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText) const
79
{
80
	Utils::String sNewText = sText;
81
 
84 cycrow 82
	Utils::String::size_type pos = sNewText.find_first_of("{", 0);
83
	while ( pos != Utils::String::npos ) {
84
		Utils::String::size_type endPos = sNewText.find_first_of("}", pos);
85
		if ( endPos != Utils::String::npos ) {
35 cycrow 86
			sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));
87
			pos = sNewText.find_first_of("{", 0);
88
		}
89
		else
90
			pos = sNewText.find_first_of("{", pos);
91
	}
92
	return sNewText;
93
}
94
 
95
void CTextDB::_parseFileLine(int iFromPage, int iToPage, int iLang, const Utils::String &sLine)
96
{
97
	if ( m_iInPage ) {
98
		// check for end of page
99
		size_t pos = sLine.find("</page>");
84 cycrow 100
		if ( pos != Utils::String::npos ) {
35 cycrow 101
			m_iInPage = 0;
102
		}
103
		else {
104
			this->_parsePage(iLang, sLine);
105
		}
106
	}
107
	else {
108
		size_t pos = sLine.find("<page id=\"");
84 cycrow 109
		if ( pos != Utils::String::npos ) {
35 cycrow 110
			pos += 10; // move to the end of the search string (after ")
111
			size_t endPos = sLine.find("\"", pos);
112
			int iPageID = Utils::String(sLine.substr(pos, endPos - pos));
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
 
125
void CTextDB::_parsePage(int iLang, const Utils::String &sLine)
126
{
127
	// move to the id number
128
	size_t pos = sLine.find("<t id=\"");
84 cycrow 129
	if ( pos == Utils::String::npos ) return;
35 cycrow 130
 
131
	pos += 7; // move past to the "
132
	size_t endPos = sLine.find("\"", pos);
84 cycrow 133
	if ( endPos == Utils::String::npos ) return;
35 cycrow 134
 
135
	// get the number to add
136
	int iID = Utils::String(sLine.substr(pos, endPos - pos));
137
 
138
	// now find the text
139
	pos = sLine.find(">", endPos);
84 cycrow 140
	if ( pos == Utils::String::npos ) return;
35 cycrow 141
	++pos;
142
	endPos = sLine.find("</t>", pos);
84 cycrow 143
	if ( endPos == Utils::String::npos ) return;
35 cycrow 144
 
145
	this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));
146
}
147
 
148
void CTextDB::_addText(int iLang, int iID, const Utils::String &sText)
149
{
94 cycrow 150
	// split the comment out
151
	Utils::String fullComment;
152
	Utils::String text = sText;
153
 
154
	while ( text.isin("(") && text.isin(")") ) {
155
		int iStart = text.findPos("(");
156
		if ( iStart != -1 ) {
157
			int iEnd = text.findPos(")", iStart);
158
			if ( iEnd != -1 ) {
159
				Utils::String comment = text.mid(iStart, ++iEnd);
160
				text.erase(iStart, iEnd - iStart);
161
				if ( !fullComment.empty() ) fullComment += ", ";
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
 
174
Utils::String CTextDB::_mapID(int iLang, int iPage, int iID) const
175
{
176
	Utils::String sID;
177
	sID += (long)iLang;
178
	sID += ":";
179
	sID += (long)iPage;
180
	sID += ":";
181
	sID += (long)iID;
182
 
183
	return sID;
184
}
185
 
186
}//NAMESPACE