scummvm/engines/grim/localize.cpp

124 lines
3.2 KiB
C++
Raw Normal View History

2009-05-26 14:13:08 +00:00
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
2011-04-16 14:12:44 +02:00
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
2003-08-15 18:00:22 +00:00
#include "common/file.h"
#include "common/str.h"
#include "common/endian.h"
#include "engines/grim/localize.h"
#include "engines/grim/grim.h"
2011-04-10 11:24:03 +02:00
#include "engines/grim/colormap.h"
2005-01-01 12:27:57 +00:00
2009-05-25 06:49:57 +00:00
namespace Grim {
Localizer *g_localizer = NULL;
2003-08-15 18:00:22 +00:00
static int sortCallback(const void *entry1, const void *entry2) {
2011-05-01 18:39:28 +02:00
return scumm_stricmp(((Localizer::LocaleEntry *)entry1)->text, ((Localizer::LocaleEntry *)entry2)->text);
}
2003-08-15 18:00:22 +00:00
Localizer::Localizer() {
2008-08-01 18:27:11 +00:00
Common::File f;
const char *namesToTry[] = { "GRIM.TAB", "Grim.tab", "grim.tab" };
2003-08-15 18:00:22 +00:00
_data = 0;
if (g_grim->getGameFlags() & ADGF_DEMO || g_grim->getGameType() == GType_MONKEY4)
2005-08-27 16:08:44 +00:00
return;
for (unsigned i = 0; i < sizeof(namesToTry) / sizeof(namesToTry[0]); i++) {
f.open(namesToTry[i]);
2008-08-01 18:27:11 +00:00
if (f.isOpen())
break;
}
2008-08-01 18:27:11 +00:00
if (!f.isOpen()) {
error("Localizer::Localizer: Unable to find localization information (grim.tab)");
return;
}
2003-08-15 18:00:22 +00:00
2008-08-01 18:27:11 +00:00
long filesize = f.size();
2003-08-15 18:00:22 +00:00
// Read in the data
_data = new char[filesize + 1];
f.read(_data, filesize);
_data[filesize] = '\0';
2008-08-01 18:27:11 +00:00
f.close();
2003-08-15 18:00:22 +00:00
if (filesize < 4 || READ_BE_UINT32(_data) != MKTAG('R','C','N','E'))
error("Invalid magic reading grim.tab");
2003-08-15 18:00:22 +00:00
// Decode the data
for (int i = 4; i < filesize; i++)
_data[i] ^= '\xdd';
2003-08-15 18:00:22 +00:00
char *nextline;
for (char *line = _data + 4; line != NULL && *line != '\0'; line = nextline) {
nextline = strchr(line, '\n');
2004-12-09 23:55:43 +00:00
2008-07-30 07:04:32 +00:00
if (nextline) {
if (nextline[-1] == '\r')
nextline[-1] = '\0';
nextline++;
}
char *tab = strchr(line, '\t');
2004-12-09 23:55:43 +00:00
2008-07-30 07:04:32 +00:00
if (!tab)
continue;
2004-12-09 23:55:43 +00:00
LocaleEntry entry;
entry.text = line;
entry.text[tab - line] = '\0';
entry.translation = tab + 1;
_entries.push_back(entry);
}
2003-08-15 18:00:22 +00:00
qsort(_entries.begin(), _entries.size(), sizeof(LocaleEntry), sortCallback);
2003-08-15 18:00:22 +00:00
}
Common::String Localizer::localize(const char *str) const {
2005-01-02 13:34:50 +00:00
assert(str);
2008-08-01 18:27:11 +00:00
if (str[0] != '/' || str[0] == 0)
return str;
2004-12-09 23:55:43 +00:00
const char *slash2 = strchr(str + 1, '/');
2008-07-30 07:04:32 +00:00
if (!slash2)
return str;
2003-08-15 18:00:22 +00:00
LocaleEntry key, *result;
Common::String s(str + 1, slash2 - str - 1);
2011-03-25 07:29:35 +08:00
key.text = const_cast<char *>(s.c_str());
result = (Localizer::LocaleEntry *)bsearch(&key, _entries.begin(), _entries.size(), sizeof(LocaleEntry), sortCallback);
if (!result)
2005-01-02 13:34:50 +00:00
return slash2 + 1;
2003-08-15 18:00:22 +00:00
return result->translation;
}
Localizer::~Localizer() {
delete[] _data;
2003-08-15 18:00:22 +00:00
}
2009-05-25 06:49:57 +00:00
} // end of namespace Grim