Subversion Repositories spk

Rev

Rev 35 | 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;
11
	m_iInPage = 0;
12
}
13
 
14
CTextDB::~CTextDB(void)
15
{
16
	if ( m_pTexts ) delete m_pTexts;
17
}
18
 
19
void CTextDB::parseTextFile(int iFromPage, int iToPage, const Utils::String &sFile, int iLang)
20
{
21
	// open and read the text file
22
	CFileIO File(sFile);
23
	CyStringList *lines = File.ReadLinesStr();
24
	if ( File.StartRead() ) {
25
		while ( !File.AtEnd() ) {
26
			this->_parseFileLine(iFromPage, iToPage, iLang, File.ReadToEndLine(false).ToString());
27
		}
28
		File.StopRead();
29
 
30
	}
31
	m_iInPage = 0;
32
}
33
 
34
void CTextDB::setLanguage(int iLang)
35
{
36
	m_iLang = iLang;
37
}
38
 
39
bool CTextDB::exists(int iPage, int iID) const
40
{
41
	return this->exists(m_iLang, iPage, iID);
42
}
43
bool CTextDB::exists(int iLang, int iPage, int iID) const
44
{
45
	TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
46
	if ( itr != m_pTexts->end() ) return true;
47
	return false;
48
}
49
Utils::String CTextDB::get(int iPage, int iID) const
50
{
51
	return this->get(m_iLang, iPage, iID);
52
}
53
Utils::String CTextDB::get(int iLang, int iPage, int iID) const
54
{
55
	TextListItr itr = m_pTexts->find(_mapID(iLang, iPage, iID));
56
	if ( itr != m_pTexts->end() ) return _parseText(iLang, itr->second);
57
	return "";	
58
}
59
 
60
Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText, const Utils::String &sReplace) const
61
{
62
	std::string::size_type pos = sReplace.find_first_of(",", 0);
63
	if ( pos != std::string::npos ) {
64
		int iPage = Utils::String(sReplace.substr(1, pos));
65
		int iID = Utils::String(sReplace.substr(pos + 1, sReplace.length() - pos - 2));
66
		return sText.findReplace(sReplace, this->get(iLang, iPage, iID));
67
	}
68
 
69
	return sText;
70
}
71
 
72
Utils::String CTextDB::_parseText(int iLang, const Utils::String &sText) const
73
{
74
	Utils::String sNewText = sText;
75
 
76
	std::string::size_type pos = sNewText.find_first_of("{", 0);
77
	while ( pos != std::string::npos ) {
78
		std::string::size_type endPos = sNewText.find_first_of("}", pos);
79
		if ( endPos != std::string::npos ) {
80
			sNewText = this->_parseText(iLang, sNewText, sNewText.substr(pos, (endPos - pos) + 1));
81
			pos = sNewText.find_first_of("{", 0);
82
		}
83
		else
84
			pos = sNewText.find_first_of("{", pos);
85
	}
86
	return sNewText;
87
}
88
 
89
void CTextDB::_parseFileLine(int iFromPage, int iToPage, int iLang, const Utils::String &sLine)
90
{
91
	if ( m_iInPage ) {
92
		// check for end of page
93
		size_t pos = sLine.find("</page>");
94
		if ( pos != std::string::npos ) {
95
			m_iInPage = 0;
96
		}
97
		else {
98
			this->_parsePage(iLang, sLine);
99
		}
100
	}
101
	else {
102
		size_t pos = sLine.find("<page id=\"");
103
		if ( pos != std::string::npos ) {
104
			pos += 10; // move to the end of the search string (after ")
105
			size_t endPos = sLine.find("\"", pos);
106
			int iPageID = Utils::String(sLine.substr(pos, endPos - pos));
107
			iPageID %= 10000; // make sure we remove the game id
108
			//TODO: mark the game id so we can overright if the game id is the same or higher
109
			if ( iFromPage > 0 && iPageID < iFromPage ) return; // out of range
110
			if ( iToPage > 0 && iPageID > iToPage) return; // out of range
111
 
112
			m_iInPage = iPageID;
113
		}
114
	}
115
}
116
 
117
 
118
 
119
void CTextDB::_parsePage(int iLang, const Utils::String &sLine)
120
{
121
	// move to the id number
122
	size_t pos = sLine.find("<t id=\"");
123
	if ( pos == std::string::npos ) return;
124
 
125
	pos += 7; // move past to the "
126
	size_t endPos = sLine.find("\"", pos);
127
	if ( endPos == std::string::npos ) return;
128
 
129
	// get the number to add
130
	int iID = Utils::String(sLine.substr(pos, endPos - pos));
131
 
132
	// now find the text
133
	pos = sLine.find(">", endPos);
134
	if ( pos == std::string::npos ) return;
135
	++pos;
136
	endPos = sLine.find("</t>", pos);
137
	if ( endPos == std::string::npos ) return;
138
 
139
	this->_addText(iLang, iID, sLine.substr(pos, endPos - pos));
140
}
141
 
142
void CTextDB::_addText(int iLang, int iID, const Utils::String &sText)
143
{
144
	(*m_pTexts)[_mapID(iLang, m_iInPage, iID)] = sText;
145
}
146
 
147
Utils::String CTextDB::_mapID(int iLang, int iPage, int iID) const
148
{
149
	Utils::String sID;
150
	sID += (long)iLang;
151
	sID += ":";
152
	sID += (long)iPage;
153
	sID += ":";
154
	sID += (long)iID;
155
 
156
	return sID;
157
}
158
 
159
}//NAMESPACE