scummvm/common/util.cpp

276 lines
6.2 KiB
C++
Raw Normal View History

/* ScummVM - Scumm Interpreter
2005-01-01 16:09:25 +00:00
* Copyright (C) 2002-2005 The ScummVM project
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*/
#include "common/stdafx.h"
#include "base/engine.h"
#include "common/util.h"
namespace Common {
//
// Print hexdump of the data passed in
//
void hexdump(const byte * data, int len, int bytesPerLine) {
assert(1 <= bytesPerLine && bytesPerLine <= 32);
int i;
byte c;
int offset = 0;
while (len >= bytesPerLine) {
printf("%06x: ", offset);
for (i = 0; i < bytesPerLine; i++) {
printf("%02x ", data[i]);
if (i % 4 == 3)
printf(" ");
}
printf(" |");
for (i = 0; i < bytesPerLine; i++) {
c = data[i];
2002-08-31 14:14:24 +00:00
if (c < 32 || c >= 127)
c = '.';
printf("%c", c);
}
printf("|\n");
data += bytesPerLine;
len -= bytesPerLine;
offset += bytesPerLine;
}
if (len <= 0)
return;
printf("%06x: ", offset);
2003-12-13 17:33:21 +00:00
for (i = 0; i < bytesPerLine; i++) {
if (i < len)
printf("%02x ", data[i]);
else
printf(" ");
if (i % 4 == 3)
printf(" ");
}
printf(" |");
for (i = 0; i < len; i++) {
c = data[i];
2002-08-31 14:14:24 +00:00
if (c < 32 || c >= 127)
c = '.';
printf("%c", c);
}
for (; i < bytesPerLine; i++)
printf(" ");
printf("|\n");
}
#pragma mark -
RandomSource::RandomSource() {
// Use system time as RNG seed. Normally not a good idea, if you are using
// a RNG for security purposes, but good enough for our purposes.
#if defined (__SYMBIAN32__) && defined (__WINS__)
uint32 seed = 0; // Symbian produces RT crash on time(0)
#else
uint32 seed = time(0);
#endif
setSeed(seed);
}
void RandomSource::setSeed(uint32 seed) {
_randSeed = seed;
}
uint RandomSource::getRandomNumber(uint max) {
_randSeed = 0xDEADBF03 * (_randSeed + 1);
_randSeed = (_randSeed >> 13) | (_randSeed << 19);
return _randSeed % (max + 1);
}
uint RandomSource::getRandomNumberRng(uint min, uint max) {
return getRandomNumber(max - min) + min;
}
#pragma mark -
const LanguageDescription g_languages[] = {
{"en", "English (US)", EN_USA},
{"de", "German", DE_DEU},
{"fr", "French", FR_FRA},
{"it", "Italian", IT_ITA},
{"pt", "Portuguese", PT_BRA},
{"es", "Spanish", ES_ESP},
{"jp", "Japanese", JA_JPN},
{"zh", "Chinese (Taiwan)", ZH_TWN},
{"kr", "Korean", KO_KOR},
{"gb", "English (GB)", EN_GRB},
{"se", "Swedish", SE_SWE},
2004-02-10 13:24:12 +00:00
{"hb", "Hebrew", HB_ISR},
2003-11-08 22:43:46 +00:00
{"ru", "Russian", RU_RUS},
{"cz", "Czech", CZ_CZE},
{"nl", "Dutch", NL_NLD},
{"nb", "Norwegian Bokm\xE5l", NB_NOR},
{0, 0, UNK_LANG}
};
Language parseLanguage(const String &str) {
if (str.isEmpty())
return UNK_LANG;
const char *s = str.c_str();
const LanguageDescription *l = g_languages;
for (; l->code; ++l) {
if (!scumm_stricmp(l->code, s))
return l->id;
}
return UNK_LANG;
}
const char *getLanguageCode(Language id) {
const LanguageDescription *l = g_languages;
for (; l->code; ++l) {
if (l->id == id)
return l->code;
}
return 0;
}
const char *getLanguageDescription(Language id) {
const LanguageDescription *l = g_languages;
for (; l->code; ++l) {
if (l->id == id)
return l->description;
}
return 0;
}
#pragma mark -
const PlatformDescription g_platforms[] = {
{"amiga", "ami", "Amiga", kPlatformAmiga},
{"atari", "atari-st", "Atari ST", kPlatformAtariST},
{"c64", "c64", "Commodore 64", kPlatformC64},
// The 'official' spelling seems to be "FM-TOWNS" (e.g. in the Indy4 demo).
// However, on the net many variations can be seen, like "FMTOWNS",
// "FM TOWNS", "FmTowns", etc.
{"fmtowns", "towns", "FM-TOWNS", kPlatformFMTowns},
{"linux", "linux", "Linux", kPlatformLinux},
{"macintosh", "mac", "Macintosh", kPlatformMacintosh},
2005-04-08 20:49:13 +00:00
{"nes", "nes", "NES", kPlatformNES},
{"pc", "dos", "DOS", kPlatformPC},
{"windows", "win", "Windows", kPlatformWindows},
{0, 0, "Default", kPlatformUnknown}
};
Platform parsePlatform(const String &str) {
if (str.isEmpty())
return kPlatformUnknown;
const char *s = str.c_str();
// Handle some special case separately, for compatibility with old config
// files.
if (!strcmp(s, "1"))
return kPlatformAmiga;
else if (!strcmp(s, "2"))
return kPlatformAtariST;
else if (!strcmp(s, "3"))
return kPlatformMacintosh;
const PlatformDescription *l = g_platforms;
for (; l->code; ++l) {
if (!scumm_stricmp(l->code, s) || !scumm_stricmp(l->code2, s))
return l->id;
}
return kPlatformUnknown;
}
const char *getPlatformCode(Platform id) {
const PlatformDescription *l = g_platforms;
for (; l->code; ++l) {
if (l->id == id)
return l->code;
}
return 0;
}
const char *getPlatformDescription(Platform id) {
const PlatformDescription *l = g_platforms;
for (; l->code; ++l) {
if (l->id == id)
return l->description;
}
return l->description;
}
#pragma mark -
const RenderModeDescription g_renderModes[] = {
{"hercGreen", "Hercules Green", kRenderHercG},
{"hercAmber", "Hercules Amber", kRenderHercA},
{"cga", "CGA", kRenderCGA},
{"ega", "EGA", kRenderEGA},
{"amiga", "Amiga", kRenderAmiga},
{0, 0, kRenderDefault}
};
RenderMode parseRenderMode(const String &str) {
if (str.isEmpty())
return kRenderDefault;
const char *s = str.c_str();
const RenderModeDescription *l = g_renderModes;
for (; l->code; ++l) {
if (!scumm_stricmp(l->code, s))
return l->id;
}
return kRenderDefault;
}
const char *getRenderModeCode(RenderMode id) {
const RenderModeDescription *l = g_renderModes;
for (; l->code; ++l) {
if (l->id == id)
return l->code;
}
return 0;
}
const char *getRenderModeDescription(RenderMode id) {
const RenderModeDescription *l = g_renderModes;
for (; l->code; ++l) {
if (l->id == id)
return l->description;
}
return 0;
}
} // End of namespace Common