Subversion Repositories spk

Rev

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