aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/databases.cpp
blob: d8d18753711e683702b6e84e9d6e2099406324f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/file.h"
#include "common/textconsole.h"

#include "gob/databases.h"

namespace Gob {

Databases::Databases() {
}

Databases::~Databases() {
}

void Databases::setLanguage(Common::Language language) {
	Common::String lang;

	if      (language == Common::UNK_LANG)
		lang = "";
	else if (language == Common::EN_ANY)
		lang = "E";
	else if (language == Common::EN_GRB)
		lang = "E";
	else if (language == Common::EN_USA)
		lang = "E";
	else if (language == Common::DE_DEU)
		lang = "G";
	else if (language == Common::FR_FRA)
		lang = "F";
	else
		warning("Databases::setLanguage(): Language \"%s\" not supported",
				Common::getLanguageDescription(language));

	if (!_databases.empty() && (lang != _language))
		warning("Databases::setLanguage(): \"%s\" != \"%s\" and there's still databases open!",
				_language.c_str(), lang.c_str());

	_language = lang;
}

bool Databases::open(const Common::String &id, const Common::String &file) {
	if (_databases.contains(id)) {
		warning("Databases::open(): A database with the ID \"%s\" already exists", id.c_str());
		return false;
	}

	Common::File dbFile;
	if (!dbFile.open(file)) {
		warning("Databases::open(): No such file \"%s\"", file.c_str());
		return false;
	}

	dBase db;
	if (!db.load(dbFile)) {
		warning("Databases::open(): Failed loading database file \"%s\"", file.c_str());
		return false;
	}

	_databases.setVal(id, Common::StringMap());
	DBMap::iterator map = _databases.find(id);
	assert(map != _databases.end());

	if (!buildMap(db, map->_value)) {
		warning("Databases::open(): Failed building a map for database \"%s\"", file.c_str());
		_databases.erase(map);
		return false;
	}

	return true;
}

bool Databases::close(const Common::String &id) {
	DBMap::iterator db = _databases.find(id);
	if (db == _databases.end()) {
		warning("Databases::open(): A database with the ID \"%s\" does not exist", id.c_str());
		return false;
	}

	_databases.erase(db);
	return true;
}

bool Databases::getString(const Common::String &id, Common::String group,
		Common::String section, Common::String keyword, Common::String &result) const {

	DBMap::iterator db = _databases.find(id);
	if (db == _databases.end()) {
		warning("Databases::getString(): A database with the ID \"%s\" does not exist", id.c_str());
		return false;
	}

	if (_language.empty()) {
		warning("Databases::getString(): No language set");
		return false;
	}

	Common::String key = _language + ":" + group + ":" + section + ":" + keyword;

	Common::StringMap::const_iterator entry = db->_value.find(key);
	if (entry == db->_value.end())
		return false;

	result = entry->_value;
	return true;
}

int Databases::findField(const dBase &db, const Common::String &field,
		dBase::Type type) const {

	const Common::Array<dBase::Field> &fields = db.getFields();

	for (uint i = 0; i < fields.size(); i++) {
		if (!fields[i].name.equalsIgnoreCase(field))
			continue;

		if (fields[i].type != type)
			return -1;

		return i;
	}

	return -1;
}

bool Databases::buildMap(const dBase &db, Common::StringMap &map) const {
	int fLanguage = findField(db, "Langage", dBase::kTypeString);
	int fGroup    = findField(db, "Nom"    , dBase::kTypeString);
	int fSection  = findField(db, "Section", dBase::kTypeString);
	int fKeyword  = findField(db, "Motcle" , dBase::kTypeString);
	int fText     = findField(db, "Texte"  , dBase::kTypeString);

	if ((fLanguage < 0) || (fGroup < 0) || (fSection < 0) || (fKeyword < 0) || (fText < 0))
		return false;

	const Common::Array<dBase::Record> &records = db.getRecords();

	Common::Array<dBase::Record>::const_iterator record;
	for (record = records.begin(); record != records.end(); ++record) {
		Common::String key;

		key += db.getString(*record, fLanguage) + ":";
		key += db.getString(*record, fGroup   ) + ":";
		key += db.getString(*record, fSection ) + ":";
		key += db.getString(*record, fKeyword );

		map.setVal(key, db.getString(*record, fText));
	}

	return true;
}

} // End of namespace Gob