scummvm/localize.cpp

97 lines
2.6 KiB
C++
Raw Normal View History

2003-08-15 18:00:22 +00:00
// Residual - Virtual machine to run LucasArts' 3D adventure games
2005-01-01 10:23:18 +00:00
// Copyright (C) 2003-2005 The ScummVM-Residual Team (www.scummvm.org)
2003-08-15 18:00:22 +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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "stdafx.h"
#include "localize.h"
#include "registry.h"
#include "debug.h"
2003-08-15 18:00:22 +00:00
#include <cstdio>
#include <cstring>
Localizer *g_localizer = NULL;
2003-08-15 18:00:22 +00:00
Localizer::Localizer() {
std::FILE *f;
const char *namesToTry[] = { "/GRIM.TAB", "/Grim.tab", "/grim.tab" };
2003-08-15 18:00:22 +00:00
for (unsigned i = 0; i < sizeof(namesToTry) / sizeof(namesToTry[0]); i++) {
2004-12-31 22:05:26 +00:00
const char *datadir = g_registry->get("DataDir");
std::string fname = (datadir != NULL ? datadir : ".");
fname += namesToTry[i];
f = std::fopen(fname.c_str(), "rb");
if (f != NULL)
break;
}
if (f == NULL)
return;
2003-08-15 18:00:22 +00:00
// Get the file size
std::fseek(f, 0, SEEK_END);
long filesize = std::ftell(f);
std::fseek(f, 0, SEEK_SET);
2003-08-15 18:00:22 +00:00
// Read in the data
char *data = new char[filesize + 1];
std::fread(data, 1, filesize, f);
data[filesize] = '\0';
std::fclose(f);
2003-08-15 18:00:22 +00:00
if (filesize < 4 || std::memcmp(data, "RCNE", 4) != 0)
error("Invalid magic reading grim.tab\n");
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 = std::strchr(line, '\n');
2004-12-09 23:55:43 +00:00
if (nextline != NULL) {
if (nextline[-1] == '\r')
nextline[-1] = '\0';
nextline++;
}
char *tab = std::strchr(line, '\t');
2004-12-09 23:55:43 +00:00
if (tab == NULL)
continue;
2004-12-09 23:55:43 +00:00
std::string key(line, tab - line);
std::string val = tab + 1;
2004-12-09 23:55:43 +00:00
_entries[key] = val;
}
2003-08-15 18:00:22 +00:00
delete[] data;
2003-08-15 18:00:22 +00:00
}
std::string Localizer::localize(const char *str) const {
if (str[0] != '/')
return str;
2004-12-09 23:55:43 +00:00
const char *slash2 = std::strchr(str + 1, '/');
if (slash2 == NULL)
return str;
2003-08-15 18:00:22 +00:00
std::string key(str + 1, slash2 - str - 1);
2004-12-10 07:26:03 +00:00
StringMap::const_iterator i = _entries.find(key);
2004-12-09 23:55:43 +00:00
if (i == _entries.end())
return str;
2003-08-15 18:00:22 +00:00
return "/" + key + '/' + i->second;
2003-08-15 18:00:22 +00:00
}