2009-05-26 14:13:08 +00:00
|
|
|
/* Residual - A 3D game interpreter
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-06-13 14:57:47 +00:00
|
|
|
* 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
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
#include "common/file.h"
|
2009-05-10 09:27:35 +00:00
|
|
|
#include "common/str.h"
|
|
|
|
#include "common/endian.h"
|
2009-05-10 07:31:02 +00:00
|
|
|
|
2009-05-24 19:13:58 +00:00
|
|
|
#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 {
|
|
|
|
|
2004-12-31 21:35:04 +00:00
|
|
|
Localizer *g_localizer = NULL;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-10 09:27:35 +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);
|
2009-05-10 09:27:35 +00:00
|
|
|
}
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
Localizer::Localizer() {
|
2008-08-01 18:27:11 +00:00
|
|
|
Common::File f;
|
2009-04-05 14:48:54 +00:00
|
|
|
const char *namesToTry[] = { "GRIM.TAB", "Grim.tab", "grim.tab" };
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2011-08-14 19:48:25 +02:00
|
|
|
_data = 0;
|
|
|
|
|
2011-05-14 12:11:53 +02:00
|
|
|
if (g_grim->getGameFlags() & ADGF_DEMO || g_grim->getGameType() == GType_MONKEY4)
|
2005-08-27 16:08:44 +00:00
|
|
|
return;
|
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
for (unsigned i = 0; i < sizeof(namesToTry) / sizeof(namesToTry[0]); i++) {
|
2009-04-05 14:48:54 +00:00
|
|
|
f.open(namesToTry[i]);
|
2008-08-01 18:27:11 +00:00
|
|
|
if (f.isOpen())
|
2004-02-24 21:09:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-08-01 18:27:11 +00:00
|
|
|
if (!f.isOpen()) {
|
2009-05-31 07:33:18 +00:00
|
|
|
error("Localizer::Localizer: Unable to find localization information (grim.tab)");
|
2004-02-24 21:09:53 +00:00
|
|
|
return;
|
2005-04-03 11:33:28 +00:00
|
|
|
}
|
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
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
// Read in the data
|
2011-06-09 19:13:02 -07:00
|
|
|
_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
|
|
|
|
2011-06-09 19:13:02 -07:00
|
|
|
if (filesize < 4 || READ_BE_UINT32(_data) != MKTAG('R','C','N','E'))
|
2008-09-28 15:23:15 +00:00
|
|
|
error("Invalid magic reading grim.tab");
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
// Decode the data
|
|
|
|
for (int i = 4; i < filesize; i++)
|
2011-06-09 19:13:02 -07:00
|
|
|
_data[i] ^= '\xdd';
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
char *nextline;
|
2011-06-09 19:13:02 -07:00
|
|
|
for (char *line = _data + 4; line != NULL && *line != '\0'; line = nextline) {
|
2009-05-10 09:27:35 +00:00
|
|
|
nextline = strchr(line, '\n');
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (nextline) {
|
2004-02-24 21:09:53 +00:00
|
|
|
if (nextline[-1] == '\r')
|
|
|
|
nextline[-1] = '\0';
|
|
|
|
nextline++;
|
|
|
|
}
|
2009-05-10 09:27:35 +00:00
|
|
|
char *tab = strchr(line, '\t');
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!tab)
|
2004-02-24 21:09:53 +00:00
|
|
|
continue;
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
LocaleEntry entry;
|
2011-06-09 19:13:02 -07:00
|
|
|
entry.text = line;
|
2009-05-10 09:27:35 +00:00
|
|
|
entry.text[tab - line] = '\0';
|
2011-06-09 19:13:02 -07:00
|
|
|
entry.translation = tab + 1;
|
2009-05-10 09:27:35 +00:00
|
|
|
_entries.push_back(entry);
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
qsort(_entries.begin(), _entries.size(), sizeof(LocaleEntry), sortCallback);
|
|
|
|
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 09:27:35 +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)
|
2004-02-24 21:09:53 +00:00
|
|
|
return str;
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
const char *slash2 = strchr(str + 1, '/');
|
2008-07-30 07:04:32 +00:00
|
|
|
if (!slash2)
|
2004-02-24 21:09:53 +00:00
|
|
|
return str;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-10 09:27:35 +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());
|
2009-05-10 09:27:35 +00:00
|
|
|
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
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
return result->translation;
|
|
|
|
}
|
|
|
|
|
|
|
|
Localizer::~Localizer() {
|
2011-08-15 22:05:02 +02:00
|
|
|
delete[] _data;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
2009-05-25 06:49:57 +00:00
|
|
|
|
|
|
|
} // end of namespace Grim
|