2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "../stdafx.h"
|
2006-02-12 10:44:52 +00:00
|
|
|
#include "../macros.h"
|
2006-08-24 12:08:25 +00:00
|
|
|
#include "../string.h"
|
2006-11-16 22:05:33 +00:00
|
|
|
#include "../table/control_codes.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2005-10-02 22:39:56 +00:00
|
|
|
#if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
|
2004-08-09 17:04:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
#if defined WIN32 || defined __WATCOMC__
|
|
|
|
#include <direct.h>
|
|
|
|
#endif /* WIN32 || __WATCOMC__ */
|
|
|
|
|
2004-09-08 15:01:12 +00:00
|
|
|
#ifdef __MORPHOS__
|
|
|
|
#ifdef stderr
|
|
|
|
#undef stderr
|
|
|
|
#endif
|
|
|
|
#define stderr stdout
|
2006-08-24 12:08:25 +00:00
|
|
|
#endif /* __MORPHOS__ */
|
2004-09-08 15:01:12 +00:00
|
|
|
|
2006-03-31 16:01:59 +00:00
|
|
|
#ifdef __WATCOMC__
|
|
|
|
uint _map_log_x; // an unpleasant hack required because Watcom is insisting on
|
|
|
|
uint _map_size_x; // these variables being valid references in map.h
|
|
|
|
uint _map_size_y;
|
|
|
|
uint _map_tile_mask;
|
|
|
|
uint _map_size;
|
2006-08-24 12:08:25 +00:00
|
|
|
#endif /* __WATCOMC__ */
|
2006-03-31 16:01:59 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Compiles a list of strings into a compiled string list */
|
|
|
|
|
|
|
|
typedef void (*ParseCmdProc)(char *buf, int value);
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
typedef struct LanguagePackHeader {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 ident;
|
2006-08-22 14:38:37 +00:00
|
|
|
uint32 version; // 32-bits of auto generated version info which is basically a hash of strings.h
|
|
|
|
char name[32]; // the international name of this language
|
2006-08-28 18:53:03 +00:00
|
|
|
char own_name[32]; // the localized name of this language
|
2006-08-22 14:38:37 +00:00
|
|
|
char isocode[16]; // the ISO code for the language (not country code)
|
|
|
|
uint16 offsets[32]; // the offsets
|
|
|
|
byte plural_form; // plural form index
|
|
|
|
byte pad[3]; // pad header to be a multiple of 4
|
2004-08-09 17:04:08 +00:00
|
|
|
} LanguagePackHeader;
|
|
|
|
|
|
|
|
typedef struct CmdStruct {
|
|
|
|
const char *cmd;
|
|
|
|
ParseCmdProc proc;
|
|
|
|
long value;
|
2005-07-15 14:53:44 +00:00
|
|
|
int8 consumes;
|
2005-07-17 10:18:23 +00:00
|
|
|
byte flags;
|
2004-08-09 17:04:08 +00:00
|
|
|
} CmdStruct;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
enum {
|
|
|
|
C_DONTCOUNT = 1,
|
2006-08-22 14:38:37 +00:00
|
|
|
C_CASE = 2,
|
2005-07-17 10:18:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct Case {
|
|
|
|
int caseidx;
|
|
|
|
char *string;
|
|
|
|
struct Case *next;
|
|
|
|
} Case;
|
|
|
|
|
2005-11-02 23:55:10 +00:00
|
|
|
static bool _masterlang;
|
2005-11-02 23:31:04 +00:00
|
|
|
static bool _translated;
|
2005-09-25 09:15:09 +00:00
|
|
|
static const char* _file = "(unknown file)";
|
2005-07-15 07:48:17 +00:00
|
|
|
static int _cur_line;
|
2005-07-15 14:53:44 +00:00
|
|
|
static int _errors, _warnings;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
typedef struct LangString {
|
2006-08-22 14:38:37 +00:00
|
|
|
char *name; // Name of the string
|
|
|
|
char *english; // English text
|
|
|
|
char *translated; // Translated text
|
|
|
|
uint16 hash_next; // next hash entry
|
2005-07-17 10:18:23 +00:00
|
|
|
uint16 index;
|
2006-08-22 14:38:37 +00:00
|
|
|
int line; // line of string in source-file
|
|
|
|
Case *english_case; // cases for english
|
|
|
|
Case *translated_case; // cases for foreign
|
2005-07-17 10:18:23 +00:00
|
|
|
} LangString;
|
|
|
|
|
|
|
|
static LangString *_strings[65536];
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
#define HASH_SIZE 32767
|
|
|
|
static uint16 _hash_head[HASH_SIZE];
|
|
|
|
|
|
|
|
static byte _put_buf[4096];
|
|
|
|
static int _put_pos;
|
|
|
|
static int _next_string_id;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
static uint32 _hash;
|
2005-07-15 14:53:44 +00:00
|
|
|
static char _lang_name[32], _lang_ownname[32], _lang_isocode[16];
|
2005-07-16 17:12:32 +00:00
|
|
|
static byte _lang_pluralform;
|
2005-07-17 10:18:23 +00:00
|
|
|
#define MAX_NUM_GENDER 8
|
|
|
|
static char _genders[MAX_NUM_GENDER][8];
|
2005-07-16 20:58:04 +00:00
|
|
|
static int _numgenders;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
// contains the name of all cases.
|
|
|
|
#define MAX_NUM_CASES 50
|
|
|
|
static char _cases[MAX_NUM_CASES][16];
|
|
|
|
static int _numcases;
|
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
// for each plural value, this is the number of plural forms.
|
2006-08-22 14:38:37 +00:00
|
|
|
static const byte _plural_form_counts[] = { 2, 1, 2, 3, 3, 3, 3, 3, 4 };
|
2005-07-16 17:12:32 +00:00
|
|
|
|
|
|
|
static const char *_cur_ident;
|
2005-07-15 07:48:17 +00:00
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
typedef struct CmdPair {
|
|
|
|
const CmdStruct *a;
|
|
|
|
char *v;
|
|
|
|
} CmdPair;
|
|
|
|
|
|
|
|
typedef struct ParsedCommandStruct {
|
|
|
|
int np;
|
|
|
|
CmdPair pairs[32];
|
|
|
|
const CmdStruct *cmd[32]; // ordered by param #
|
|
|
|
} ParsedCommandStruct;
|
|
|
|
|
|
|
|
// Used when generating some advanced commands.
|
|
|
|
static ParsedCommandStruct _cur_pcs;
|
|
|
|
static int _cur_argidx;
|
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
static uint HashStr(const char *s)
|
|
|
|
{
|
|
|
|
uint hash = 0;
|
2006-02-12 10:44:52 +00:00
|
|
|
for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s;
|
2004-08-09 17:04:08 +00:00
|
|
|
return hash % HASH_SIZE;
|
|
|
|
}
|
2005-07-15 07:48:17 +00:00
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
static void HashAdd(const char *s, LangString *ls)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
|
|
|
uint hash = HashStr(s);
|
2005-07-17 10:18:23 +00:00
|
|
|
ls->hash_next = _hash_head[hash];
|
|
|
|
_hash_head[hash] = ls->index + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
static LangString *HashFind(const char *s)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2005-07-15 14:53:44 +00:00
|
|
|
int idx = _hash_head[HashStr(s)];
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
while (--idx >= 0) {
|
2006-02-12 10:44:52 +00:00
|
|
|
LangString* ls = _strings[idx];
|
|
|
|
|
|
|
|
if (strcmp(ls->name, s) == 0) return ls;
|
2005-07-17 10:18:23 +00:00
|
|
|
idx = ls->hash_next;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-07-17 10:18:23 +00:00
|
|
|
return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define LINE_NUM_FMT "(%d)"
|
|
|
|
#else
|
|
|
|
# define LINE_NUM_FMT ":%d"
|
|
|
|
#endif
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-10-22 20:46:18 +00:00
|
|
|
static void CDECL warning(const char *s, ...)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
char buf[1024];
|
|
|
|
va_list va;
|
|
|
|
va_start(va, s);
|
2006-08-24 12:08:25 +00:00
|
|
|
vsnprintf(buf, lengthof(buf), s, va);
|
2004-08-09 17:04:08 +00:00
|
|
|
va_end(va);
|
2006-08-29 20:55:25 +00:00
|
|
|
fprintf(stderr, "%s" LINE_NUM_FMT ": warning: %s\n", _file, _cur_line, buf);
|
2005-07-15 14:53:44 +00:00
|
|
|
_warnings++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 20:46:18 +00:00
|
|
|
void CDECL error(const char *s, ...)
|
2005-07-15 14:53:44 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list va;
|
|
|
|
va_start(va, s);
|
2006-08-24 12:08:25 +00:00
|
|
|
vsnprintf(buf, lengthof(buf), s, va);
|
2005-07-15 14:53:44 +00:00
|
|
|
va_end(va);
|
2006-08-29 20:55:25 +00:00
|
|
|
fprintf(stderr, "%s" LINE_NUM_FMT ": error: %s\n", _file, _cur_line, buf);
|
2005-07-15 14:53:44 +00:00
|
|
|
_errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 20:46:18 +00:00
|
|
|
static void NORETURN CDECL fatal(const char *s, ...)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
char buf[1024];
|
|
|
|
va_list va;
|
|
|
|
va_start(va, s);
|
2006-08-24 12:08:25 +00:00
|
|
|
vsnprintf(buf, lengthof(buf), s, va);
|
2004-08-09 17:04:08 +00:00
|
|
|
va_end(va);
|
2006-05-27 16:12:16 +00:00
|
|
|
fprintf(stderr, "%s" LINE_NUM_FMT ": FATAL: %s\n", _file, _cur_line, buf);
|
2004-08-09 17:04:08 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
static void PutByte(byte c)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2006-10-22 20:46:18 +00:00
|
|
|
if (_put_pos == lengthof(_put_buf)) fatal("Put buffer too small");
|
2004-08-09 17:04:08 +00:00
|
|
|
_put_buf[_put_pos++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
static void PutUtf8(uint32 value)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2006-11-16 22:05:33 +00:00
|
|
|
if (value < 0x80) {
|
|
|
|
PutByte(value);
|
|
|
|
} else if (value < 0x800) {
|
|
|
|
PutByte(0xC0 + GB(value, 6, 5));
|
|
|
|
PutByte(0x80 + GB(value, 0, 6));
|
|
|
|
} else if (value < 0x10000) {
|
|
|
|
PutByte(0xE0 + GB(value, 12, 4));
|
|
|
|
PutByte(0x80 + GB(value, 6, 6));
|
|
|
|
PutByte(0x80 + GB(value, 0, 6));
|
|
|
|
} else if (value < 0x110000) {
|
|
|
|
PutByte(0xF0 + GB(value, 18, 3));
|
|
|
|
PutByte(0x80 + GB(value, 12, 6));
|
|
|
|
PutByte(0x80 + GB(value, 6, 6));
|
|
|
|
PutByte(0x80 + GB(value, 0, 6));
|
|
|
|
} else {
|
|
|
|
warning("Invalid unicode value U+0x%X\n", value);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-11-17 07:46:02 +00:00
|
|
|
size_t Utf8Validate(const char *s)
|
|
|
|
{
|
|
|
|
uint32 c;
|
|
|
|
|
|
|
|
if (!HASBIT(s[0], 7)) {
|
|
|
|
/* 1 byte */
|
|
|
|
return 1;
|
|
|
|
} else if (GB(s[0], 5, 3) == 6 && IsUtf8Part(s[1])) {
|
|
|
|
/* 2 bytes */
|
|
|
|
c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
|
|
|
|
if (c >= 0x80) return 2;
|
|
|
|
} else if (GB(s[0], 4, 4) == 14 && IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
|
|
|
|
/* 3 bytes */
|
|
|
|
c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
|
|
|
|
if (c >= 0x800) return 3;
|
|
|
|
} else if (GB(s[0], 3, 5) == 30 && IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
|
|
|
|
/* 4 bytes */
|
|
|
|
c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
|
|
|
|
if (c >= 0x10000 && c <= 0x10FFFF) return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
static void EmitSingleChar(char *buf, int value)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2006-10-22 20:46:18 +00:00
|
|
|
if (*buf != '\0') warning("Ignoring trailing letters in command");
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(value);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
static void EmitSetX(char *buf, int value)
|
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
char *err;
|
|
|
|
int x = strtol(buf, &err, 0);
|
2006-10-22 20:46:18 +00:00
|
|
|
if (*err != 0) fatal("SetX param invalid");
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_SETX);
|
2005-07-15 14:53:44 +00:00
|
|
|
PutByte((byte)x);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
static void EmitSetXY(char *buf, int value)
|
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
char *err;
|
2006-02-12 10:44:52 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
2005-07-15 07:48:17 +00:00
|
|
|
|
|
|
|
x = strtol(buf, &err, 0);
|
2006-10-22 20:46:18 +00:00
|
|
|
if (*err != ' ') fatal("SetXY param invalid");
|
2006-02-12 10:44:52 +00:00
|
|
|
y = strtol(err + 1, &err, 0);
|
2006-10-22 20:46:18 +00:00
|
|
|
if (*err != 0) fatal("SetXY param invalid");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_SETXY);
|
2005-07-15 14:53:44 +00:00
|
|
|
PutByte((byte)x);
|
|
|
|
PutByte((byte)y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
// The plural specifier looks like
|
|
|
|
// {NUM} {PLURAL -1 passenger passengers} then it picks either passenger/passengers depending on the count in NUM
|
|
|
|
|
|
|
|
// This is encoded like
|
|
|
|
// CommandByte <ARG#> <NUM> {Length of each string} {each string}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
bool ParseRelNum(char **buf, int *value)
|
2005-07-16 17:12:32 +00:00
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
const char* s = *buf;
|
|
|
|
char* end;
|
2005-07-16 17:12:32 +00:00
|
|
|
bool rel = false;
|
2005-07-17 10:18:23 +00:00
|
|
|
int v;
|
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
while (*s == ' ' || *s == '\t') s++;
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*s == '+') {
|
|
|
|
rel = true;
|
|
|
|
s++;
|
|
|
|
}
|
2005-07-17 10:18:23 +00:00
|
|
|
v = strtol(s, &end, 0);
|
2005-07-16 17:12:32 +00:00
|
|
|
if (end == s) return false;
|
2006-02-12 10:44:52 +00:00
|
|
|
if (rel || v < 0) {
|
2005-07-17 10:18:23 +00:00
|
|
|
*value += v;
|
2006-02-12 10:44:52 +00:00
|
|
|
} else {
|
2005-07-17 10:18:23 +00:00
|
|
|
*value = v;
|
2006-02-12 10:44:52 +00:00
|
|
|
}
|
2005-07-16 17:12:32 +00:00
|
|
|
*buf = end;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse out the next word, or NULL
|
|
|
|
char *ParseWord(char **buf)
|
|
|
|
{
|
|
|
|
char *s = *buf, *r;
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
while (*s == ' ' || *s == '\t') s++;
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*s == '\0') return NULL;
|
2005-07-16 17:12:32 +00:00
|
|
|
|
|
|
|
if (*s == '"') {
|
|
|
|
r = ++s;
|
|
|
|
// parse until next " or NUL
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*s == '\0') break;
|
2005-07-16 17:12:32 +00:00
|
|
|
if (*s == '"') {
|
2006-02-12 10:44:52 +00:00
|
|
|
*s++ = '\0';
|
2005-07-16 17:12:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// proceed until whitespace or NUL
|
|
|
|
r = s;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*s == '\0') break;
|
2005-07-16 17:12:32 +00:00
|
|
|
if (*s == ' ' || *s == '\t') {
|
2006-02-12 10:44:52 +00:00
|
|
|
*s++ = '\0';
|
2005-07-16 17:12:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*buf = s;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward declaration
|
2005-07-17 10:18:23 +00:00
|
|
|
static int TranslateArgumentIdx(int arg);
|
2005-07-16 17:12:32 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
static void EmitWordList(const char* const* words, uint nw)
|
2005-07-16 20:58:04 +00:00
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
uint i;
|
|
|
|
uint j;
|
2005-07-16 20:58:04 +00:00
|
|
|
|
|
|
|
PutByte(nw);
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i < nw; i++) PutByte(strlen(words[i]));
|
|
|
|
for (i = 0; i < nw; i++) {
|
2006-02-12 10:44:52 +00:00
|
|
|
for (j = 0; words[i][j] != '\0'; j++) PutByte(words[i][j]);
|
2005-07-16 20:58:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
static void EmitPlural(char *buf, int value)
|
|
|
|
{
|
2005-07-17 10:18:23 +00:00
|
|
|
int argidx = _cur_argidx;
|
2006-02-12 10:44:52 +00:00
|
|
|
const char* words[5];
|
2005-07-16 17:12:32 +00:00
|
|
|
int nw = 0;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
// Parse out the number, if one exists. Otherwise default to prev arg.
|
2006-02-12 10:44:52 +00:00
|
|
|
if (!ParseRelNum(&buf, &argidx)) argidx--;
|
2005-07-16 17:12:32 +00:00
|
|
|
|
|
|
|
// Parse each string
|
2005-10-30 00:16:48 +00:00
|
|
|
for (nw = 0; nw < 5; nw++) {
|
2005-07-16 17:12:32 +00:00
|
|
|
words[nw] = ParseWord(&buf);
|
2006-02-12 10:44:52 +00:00
|
|
|
if (words[nw] == NULL) break;
|
2005-07-16 17:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nw == 0)
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("%s: No plural words", _cur_ident);
|
2005-07-16 17:12:32 +00:00
|
|
|
|
2005-12-17 14:26:43 +00:00
|
|
|
if (_plural_form_counts[_lang_pluralform] != nw) {
|
2005-11-02 23:31:04 +00:00
|
|
|
if (_translated) {
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident,
|
2005-10-30 00:16:48 +00:00
|
|
|
_plural_form_counts[_lang_pluralform], nw);
|
|
|
|
} else {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
|
2005-10-30 00:16:48 +00:00
|
|
|
if (nw > _plural_form_counts[_lang_pluralform]) {
|
|
|
|
nw = _plural_form_counts[_lang_pluralform];
|
|
|
|
} else {
|
2006-02-01 07:36:15 +00:00
|
|
|
for (; nw < _plural_form_counts[_lang_pluralform]; nw++) {
|
2005-10-30 00:16:48 +00:00
|
|
|
words[nw] = words[nw - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-17 14:26:43 +00:00
|
|
|
}
|
2005-07-16 17:12:32 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_PLURAL_LIST);
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(TranslateArgumentIdx(argidx));
|
2005-07-16 20:58:04 +00:00
|
|
|
EmitWordList(words, nw);
|
2005-07-16 17:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-16 20:58:04 +00:00
|
|
|
static void EmitGender(char *buf, int value)
|
|
|
|
{
|
2005-07-17 10:18:23 +00:00
|
|
|
int argidx = _cur_argidx;
|
2006-02-12 10:44:52 +00:00
|
|
|
uint nw;
|
2005-07-16 20:58:04 +00:00
|
|
|
|
|
|
|
if (buf[0] == '=') {
|
|
|
|
buf++;
|
|
|
|
|
|
|
|
// This is a {G=DER} command
|
2006-02-01 07:36:15 +00:00
|
|
|
for (nw = 0; ; nw++) {
|
2006-10-22 20:46:18 +00:00
|
|
|
if (nw >= 8) fatal("G argument '%s' invalid", buf);
|
2006-02-12 10:44:52 +00:00
|
|
|
if (strcmp(buf, _genders[nw]) == 0) break;
|
2005-07-16 20:58:04 +00:00
|
|
|
}
|
|
|
|
// now nw contains the gender index
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_GENDER_INDEX);
|
2005-07-16 20:58:04 +00:00
|
|
|
PutByte(nw);
|
|
|
|
} else {
|
2006-02-12 10:44:52 +00:00
|
|
|
const char* words[8];
|
|
|
|
|
2005-07-16 20:58:04 +00:00
|
|
|
// This is a {G 0 foo bar two} command.
|
2005-07-17 10:18:23 +00:00
|
|
|
// If no relative number exists, default to +0
|
|
|
|
if (!ParseRelNum(&buf, &argidx)) {}
|
2005-07-16 20:58:04 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (nw = 0; nw < 8; nw++) {
|
2005-07-16 20:58:04 +00:00
|
|
|
words[nw] = ParseWord(&buf);
|
2006-02-12 10:44:52 +00:00
|
|
|
if (words[nw] == NULL) break;
|
2005-07-16 20:58:04 +00:00
|
|
|
}
|
2006-10-22 20:46:18 +00:00
|
|
|
if (nw != _numgenders) fatal("Bad # of arguments for gender command");
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_GENDER_LIST);
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(TranslateArgumentIdx(argidx));
|
2005-07-16 20:58:04 +00:00
|
|
|
EmitWordList(words, nw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static const CmdStruct _cmd_structs[] = {
|
|
|
|
// Update position
|
2006-11-16 22:05:33 +00:00
|
|
|
{"SETX", EmitSetX, SCC_SETX, 0, 0},
|
|
|
|
{"SETXY", EmitSetXY, SCC_SETXY, 0, 0},
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Font size
|
2006-11-16 22:05:33 +00:00
|
|
|
{"TINYFONT", EmitSingleChar, SCC_TINYFONT, 0, 0},
|
|
|
|
{"BIGFONT", EmitSingleChar, SCC_BIGFONT, 0, 0},
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Colors
|
2006-11-16 22:05:33 +00:00
|
|
|
{"BLUE", EmitSingleChar, SCC_BLUE, 0, 0},
|
|
|
|
{"SILVER", EmitSingleChar, SCC_SILVER, 0, 0},
|
|
|
|
{"GOLD", EmitSingleChar, SCC_GOLD, 0, 0},
|
|
|
|
{"RED", EmitSingleChar, SCC_RED, 0, 0},
|
|
|
|
{"PURPLE", EmitSingleChar, SCC_PURPLE, 0, 0},
|
|
|
|
{"LTBROWN", EmitSingleChar, SCC_LTBROWN, 0, 0},
|
|
|
|
{"ORANGE", EmitSingleChar, SCC_ORANGE, 0, 0},
|
|
|
|
{"GREEN", EmitSingleChar, SCC_GREEN, 0, 0},
|
|
|
|
{"YELLOW", EmitSingleChar, SCC_YELLOW, 0, 0},
|
|
|
|
{"DKGREEN", EmitSingleChar, SCC_DKGREEN, 0, 0},
|
|
|
|
{"CREAM", EmitSingleChar, SCC_CREAM, 0, 0},
|
|
|
|
{"BROWN", EmitSingleChar, SCC_BROWN, 0, 0},
|
|
|
|
{"WHITE", EmitSingleChar, SCC_WHITE, 0, 0},
|
|
|
|
{"LTBLUE", EmitSingleChar, SCC_LTBLUE, 0, 0},
|
|
|
|
{"GRAY", EmitSingleChar, SCC_GRAY, 0, 0},
|
|
|
|
{"DKBLUE", EmitSingleChar, SCC_DKBLUE, 0, 0},
|
|
|
|
{"BLACK", EmitSingleChar, SCC_BLACK, 0, 0},
|
|
|
|
|
|
|
|
{"CURRCOMPACT", EmitSingleChar, SCC_CURRENCY_COMPACT, 1, 0}, // compact currency (32 bits)
|
|
|
|
{"REV", EmitSingleChar, SCC_REVISION, 0, 0}, // openttd revision string
|
|
|
|
{"SHORTCARGO", EmitSingleChar, SCC_CARGO_SHORT, 2, 0}, // short cargo description, only ### tons, or ### litres
|
|
|
|
{"CURRCOMPACT64", EmitSingleChar, SCC_CURRENCY_COMPACT_64, 2, 0}, // compact currency 64 bits
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
// These are special versions of {STRING1}
|
|
|
|
// The first string includes the second string.
|
2006-11-16 22:05:33 +00:00
|
|
|
{"COMPANY", EmitSingleChar, SCC_STRING1, 1, 0},
|
|
|
|
{"PLAYERNAME", EmitSingleChar, SCC_STRING1, 1, 0},
|
|
|
|
{"VEHICLE", EmitSingleChar, SCC_STRING1, 1, 0},
|
|
|
|
|
|
|
|
{"STRING1", EmitSingleChar, SCC_STRING1, 1, C_CASE}, // included string that consumes ONE argument
|
|
|
|
{"STRING2", EmitSingleChar, SCC_STRING2, 2, C_CASE}, // included string that consumes TWO arguments
|
|
|
|
{"STRING3", EmitSingleChar, SCC_STRING3, 3, C_CASE}, // included string that consumes THREE arguments
|
|
|
|
{"STRING4", EmitSingleChar, SCC_STRING4, 4, C_CASE}, // included string that consumes FOUR arguments
|
|
|
|
{"STRING5", EmitSingleChar, SCC_STRING5, 5, C_CASE}, // included string that consumes FIVE arguments
|
|
|
|
|
|
|
|
{"STATIONFEATURES", EmitSingleChar, SCC_STATION_FEATURES, 1, 0}, // station features string, icons of the features
|
|
|
|
{"INDUSTRY", EmitSingleChar, SCC_INDUSTRY_NAME, 1, 0}, // industry, takes an industry #
|
|
|
|
{"CARGO", EmitSingleChar, SCC_CARGO, 2, 0},
|
|
|
|
{"POWER", EmitSingleChar, SCC_POWER, 1, 0},
|
|
|
|
{"VOLUME", EmitSingleChar, SCC_VOLUME, 1, 0},
|
|
|
|
{"VOLUME_S", EmitSingleChar, SCC_VOLUME_SHORT, 1, 0},
|
|
|
|
{"WEIGHT", EmitSingleChar, SCC_WEIGHT, 1, 0},
|
|
|
|
{"WEIGHT_S", EmitSingleChar, SCC_WEIGHT_SHORT, 1, 0},
|
|
|
|
{"FORCE", EmitSingleChar, SCC_FORCE, 1, 0},
|
|
|
|
{"VELOCITY", EmitSingleChar, SCC_VELOCITY, 1, 0},
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
{"P", EmitPlural, 0, 0, C_DONTCOUNT}, // plural specifier
|
|
|
|
{"G", EmitGender, 0, 0, C_DONTCOUNT}, // gender specifier
|
2005-07-16 17:12:32 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"DATE_TINY", EmitSingleChar, SCC_DATE_TINY, 1, 0},
|
|
|
|
{"DATE_SHORT", EmitSingleChar, SCC_DATE_SHORT, 1, 0},
|
|
|
|
{"DATE_LONG", EmitSingleChar, SCC_DATE_LONG, 1, 0},
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"SKIP", EmitSingleChar, SCC_SKIP, 1, 0},
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"STRING", EmitSingleChar, SCC_STRING, 1, C_CASE},
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-10 15:14:35 +00:00
|
|
|
// Numbers
|
2006-11-16 22:05:33 +00:00
|
|
|
{"COMMA", EmitSingleChar, SCC_COMMA, 1, 0}, // Number with comma
|
|
|
|
{"NUM", EmitSingleChar, SCC_NUM, 1, 0}, // Signed number
|
2005-09-10 15:14:35 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"CURRENCY", EmitSingleChar, SCC_CURRENCY, 1, 0},
|
|
|
|
{"CURRENCY64", EmitSingleChar, SCC_CURRENCY_64, 2, 0},
|
2005-12-17 14:26:43 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"WAYPOINT", EmitSingleChar, SCC_WAYPOINT_NAME, 1, 0}, // waypoint name
|
|
|
|
{"STATION", EmitSingleChar, SCC_STATION_NAME, 1, 0},
|
|
|
|
{"TOWN", EmitSingleChar, SCC_TOWN_NAME, 1, 0},
|
2005-12-17 14:26:43 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
// 0x9D is used for the pseudo command SETCASE
|
|
|
|
// 0x9E is used for case switching
|
2005-12-17 14:26:43 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
{"", EmitSingleChar, '\n', 0, C_DONTCOUNT},
|
|
|
|
{"{", EmitSingleChar, '{', 0, C_DONTCOUNT},
|
|
|
|
{"UPARROW", EmitSingleChar, SCC_UPARROW, 0, 0},
|
|
|
|
{"SMALLUPARROW", EmitSingleChar, SCC_SMALLUPARROW, 0, 0},
|
|
|
|
{"SMALLDOWNARROW", EmitSingleChar, SCC_SMALLDOWNARROW, 0, 0},
|
|
|
|
{"TRAIN", EmitSingleChar, SCC_TRAIN, 0, 0},
|
|
|
|
{"LORRY", EmitSingleChar, SCC_LORRY, 0, 0},
|
|
|
|
{"BUS", EmitSingleChar, SCC_BUS, 0, 0},
|
|
|
|
{"PLANE", EmitSingleChar, SCC_PLANE, 0, 0},
|
|
|
|
{"SHIP", EmitSingleChar, SCC_SHIP, 0, 0},
|
|
|
|
{"NBSP", EmitSingleChar, 0xA0, 0, C_DONTCOUNT},
|
|
|
|
{"CENT", EmitSingleChar, 0xA2, 0, C_DONTCOUNT},
|
|
|
|
{"POUNDSIGN", EmitSingleChar, 0xA3, 0, C_DONTCOUNT},
|
|
|
|
{"EURO", EmitSingleChar, 0x20AC, 0, C_DONTCOUNT},
|
|
|
|
{"YENSIGN", EmitSingleChar, 0xA5, 0, C_DONTCOUNT},
|
|
|
|
{"COPYRIGHT", EmitSingleChar, 0xA9, 0, C_DONTCOUNT},
|
|
|
|
{"DOWNARROW", EmitSingleChar, SCC_DOWNARROW, 0, C_DONTCOUNT},
|
|
|
|
{"CHECKMARK", EmitSingleChar, SCC_CHECKMARK, 0, C_DONTCOUNT},
|
|
|
|
{"CROSS", EmitSingleChar, SCC_CROSS, 0, C_DONTCOUNT},
|
|
|
|
{"REGISTERED", EmitSingleChar, 0xAE, 0, C_DONTCOUNT},
|
|
|
|
{"RIGHTARROW", EmitSingleChar, SCC_RIGHTARROW, 0, C_DONTCOUNT},
|
|
|
|
{"SMALLLEFTARROW", EmitSingleChar, SCC_LESSTHAN, 0, C_DONTCOUNT},
|
|
|
|
{"SMALLRIGHTARROW",EmitSingleChar, SCC_GREATERTHAN, 0, C_DONTCOUNT},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
static const CmdStruct *FindCmd(const char *s, int len)
|
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
const CmdStruct* cs;
|
|
|
|
|
|
|
|
for (cs = _cmd_structs; cs != endof(_cmd_structs); cs++) {
|
|
|
|
if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
static uint ResolveCaseName(const char *str, uint len)
|
2005-07-17 10:18:23 +00:00
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_NUM_CASES; i++) {
|
|
|
|
if (memcmp(_cases[i], str, len) == 0 && _cases[i][len] == 0) return i + 1;
|
|
|
|
}
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("Invalid case-name '%s'", str);
|
2005-07-17 10:18:23 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// returns NULL on eof
|
|
|
|
// else returns command struct
|
2005-07-17 10:18:23 +00:00
|
|
|
static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-17 10:18:23 +00:00
|
|
|
const char *s = *str, *start;
|
2004-08-09 17:04:08 +00:00
|
|
|
const CmdStruct *cmd;
|
2005-07-15 14:53:44 +00:00
|
|
|
byte c;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
*argno = -1;
|
2005-07-17 10:18:23 +00:00
|
|
|
*casei = -1;
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
// Scan to the next command, exit if there's no next command.
|
2006-02-01 07:36:15 +00:00
|
|
|
for (; *s != '{'; s++) {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*s == '\0') return NULL;
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
|
|
|
s++; // Skip past the {
|
|
|
|
|
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
char *end;
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
*argno = strtoul(s, &end, 0);
|
2006-10-22 20:46:18 +00:00
|
|
|
if (*end != ':') fatal("missing arg #");
|
2005-07-15 14:53:44 +00:00
|
|
|
s = end + 1;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// parse command name
|
|
|
|
start = s;
|
2005-07-17 10:18:23 +00:00
|
|
|
do {
|
2004-08-09 17:04:08 +00:00
|
|
|
c = *s++;
|
2005-07-17 10:18:23 +00:00
|
|
|
} while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
cmd = FindCmd(start, s - start - 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cmd == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("Undefined command '%.*s'", s - start - 1, start);
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
if (c == '.') {
|
|
|
|
const char *casep = s;
|
|
|
|
|
|
|
|
if (!(cmd->flags & C_CASE))
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("Command '%s' can't have a case", cmd->cmd);
|
2005-07-17 10:18:23 +00:00
|
|
|
|
|
|
|
do c = *s++; while (c != '}' && c != ' ' && c != '\0');
|
2006-02-12 10:44:52 +00:00
|
|
|
*casei = ResolveCaseName(casep, s - casep - 1);
|
2005-07-17 10:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\0') {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("Missing } from command '%s'", start);
|
2005-07-17 10:18:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-16 20:58:04 +00:00
|
|
|
if (c != '}') {
|
|
|
|
if (c == '=') s--;
|
2004-08-09 17:04:08 +00:00
|
|
|
// copy params
|
|
|
|
start = s;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
c = *s++;
|
|
|
|
if (c == '}') break;
|
2005-07-15 14:53:44 +00:00
|
|
|
if (c == '\0') {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("Missing } from command '%s'", start);
|
2004-08-09 17:04:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-10-22 20:46:18 +00:00
|
|
|
if (s - start == 250) fatal("param command too long");
|
2004-08-09 17:04:08 +00:00
|
|
|
*param++ = c;
|
|
|
|
}
|
|
|
|
}
|
2006-02-12 10:44:52 +00:00
|
|
|
*param = '\0';
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
*str = s;
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
static void HandlePragma(char *str)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-15 14:53:44 +00:00
|
|
|
if (!memcmp(str, "id ", 3)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
_next_string_id = strtoul(str + 3, NULL, 0);
|
2005-07-15 14:53:44 +00:00
|
|
|
} else if (!memcmp(str, "name ", 5)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ttd_strlcpy(_lang_name, str + 5, sizeof(_lang_name));
|
2005-07-15 14:53:44 +00:00
|
|
|
} else if (!memcmp(str, "ownname ", 8)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ttd_strlcpy(_lang_ownname, str + 8, sizeof(_lang_ownname));
|
2005-07-15 14:53:44 +00:00
|
|
|
} else if (!memcmp(str, "isocode ", 8)) {
|
2005-02-17 07:49:31 +00:00
|
|
|
ttd_strlcpy(_lang_isocode, str + 8, sizeof(_lang_isocode));
|
2005-07-16 17:12:32 +00:00
|
|
|
} else if (!memcmp(str, "plural ", 7)) {
|
|
|
|
_lang_pluralform = atoi(str + 7);
|
|
|
|
if (_lang_pluralform >= lengthof(_plural_form_counts))
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("Invalid pluralform %d", _lang_pluralform);
|
2005-07-16 20:58:04 +00:00
|
|
|
} else if (!memcmp(str, "gender ", 7)) {
|
2006-02-12 10:44:52 +00:00
|
|
|
char* buf = str + 7;
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2006-02-12 10:44:52 +00:00
|
|
|
const char* s = ParseWord(&buf);
|
|
|
|
|
|
|
|
if (s == NULL) break;
|
2006-10-22 20:46:18 +00:00
|
|
|
if (_numgenders >= MAX_NUM_GENDER) fatal("Too many genders, max %d", MAX_NUM_GENDER);
|
2005-07-17 10:18:23 +00:00
|
|
|
ttd_strlcpy(_genders[_numgenders], s, sizeof(_genders[_numgenders]));
|
2005-07-16 20:58:04 +00:00
|
|
|
_numgenders++;
|
|
|
|
}
|
2005-07-17 10:18:23 +00:00
|
|
|
} else if (!memcmp(str, "case ", 5)) {
|
2006-02-12 10:44:52 +00:00
|
|
|
char* buf = str + 5;
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2006-02-12 10:44:52 +00:00
|
|
|
const char* s = ParseWord(&buf);
|
|
|
|
|
|
|
|
if (s == NULL) break;
|
2006-10-22 20:46:18 +00:00
|
|
|
if (_numcases >= MAX_NUM_CASES) fatal("Too many cases, max %d", MAX_NUM_CASES);
|
2005-07-17 10:18:23 +00:00
|
|
|
ttd_strlcpy(_cases[_numcases], s, sizeof(_cases[_numcases]));
|
|
|
|
_numcases++;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("unknown pragma '%s'", str);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
static void ExtractCommandString(ParsedCommandStruct* p, const char* s, bool warnings)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
char param[100];
|
2005-07-15 14:53:44 +00:00
|
|
|
int argno;
|
|
|
|
int argidx = 0;
|
2005-07-17 10:18:23 +00:00
|
|
|
int casei;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
memset(p, 0, sizeof(*p));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2005-07-15 14:53:44 +00:00
|
|
|
// read until next command from a.
|
2006-02-12 10:44:52 +00:00
|
|
|
const CmdStruct* ar = ParseCommandString(&s, param, &argno, &casei);
|
|
|
|
|
|
|
|
if (ar == NULL) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// Sanity checking
|
2006-10-22 20:46:18 +00:00
|
|
|
if (argno != -1 && ar->consumes == 0) fatal("Non consumer param can't have a paramindex");
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
if (ar->consumes) {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (argno != -1) argidx = argno;
|
2006-10-22 20:46:18 +00:00
|
|
|
if (argidx < 0 || argidx >= lengthof(p->cmd)) fatal("invalid param idx %d", argidx);
|
|
|
|
if (p->cmd[argidx] != NULL && p->cmd[argidx] != ar) fatal("duplicate param idx %d", argidx);
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
p->cmd[argidx++] = ar;
|
2005-07-17 10:18:23 +00:00
|
|
|
} else if (!(ar->flags & C_DONTCOUNT)) { // Ignore some of them
|
2006-10-22 20:46:18 +00:00
|
|
|
if (p->np >= lengthof(p->pairs)) fatal("too many commands in string, max %d", lengthof(p->pairs));
|
2005-07-15 14:53:44 +00:00
|
|
|
p->pairs[p->np].a = ar;
|
2006-02-12 10:44:52 +00:00
|
|
|
p->pairs[p->np].v = param[0] != '\0' ? strdup(param) : "";
|
2005-07-15 14:53:44 +00:00
|
|
|
p->np++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
|
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
if (a == NULL) return NULL;
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (strcmp(a->cmd, "STRING1") == 0 ||
|
|
|
|
strcmp(a->cmd, "STRING2") == 0 ||
|
|
|
|
strcmp(a->cmd, "STRING3") == 0 ||
|
|
|
|
strcmp(a->cmd, "STRING4") == 0 ||
|
|
|
|
strcmp(a->cmd, "STRING5") == 0) {
|
2005-07-15 14:53:44 +00:00
|
|
|
return FindCmd("STRING", 6);
|
2006-02-12 10:44:52 +00:00
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (strcmp(a->cmd, "SKIP") == 0) return NULL;
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
return a;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-07-15 17:59:55 +00:00
|
|
|
static bool CheckCommandsMatch(char *a, char *b, const char *name)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2005-07-15 14:53:44 +00:00
|
|
|
ParsedCommandStruct templ;
|
|
|
|
ParsedCommandStruct lang;
|
|
|
|
int i,j;
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
ExtractCommandString(&templ, b, true);
|
|
|
|
ExtractCommandString(&lang, a, true);
|
|
|
|
|
|
|
|
// For each string in templ, see if we find it in lang
|
|
|
|
if (templ.np != lang.np) {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("%s: template string and language string have a different # of commands", name);
|
2005-07-15 14:53:44 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i < templ.np; i++) {
|
2005-07-15 14:53:44 +00:00
|
|
|
// see if we find it in lang, and zero it out
|
|
|
|
bool found = false;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (j = 0; j < lang.np; j++) {
|
2005-07-15 14:53:44 +00:00
|
|
|
if (templ.pairs[i].a == lang.pairs[j].a &&
|
2006-02-12 10:44:52 +00:00
|
|
|
strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) {
|
2005-07-15 14:53:44 +00:00
|
|
|
// it was found in both. zero it out from lang so we don't find it again
|
|
|
|
lang.pairs[j].a = NULL;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("%s: command '%s' exists in template file but not in language file", name, templ.pairs[i].a->cmd);
|
2005-07-15 14:53:44 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we reach here, all non consumer commands match up.
|
|
|
|
// Check if the non consumer commands match up also.
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i < lengthof(templ.cmd); i++) {
|
2005-07-15 14:53:44 +00:00
|
|
|
if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i,
|
2006-02-12 10:44:52 +00:00
|
|
|
lang.cmd[i] == NULL ? "<empty>" : lang.cmd[i]->cmd,
|
|
|
|
templ.cmd[i] == NULL ? "<empty>" : templ.cmd[i]->cmd);
|
2005-07-15 14:53:44 +00:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleString(char *str, bool master)
|
|
|
|
{
|
|
|
|
char *s,*t;
|
2005-07-17 10:18:23 +00:00
|
|
|
LangString *ent;
|
|
|
|
char *casep;
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (*str == '#') {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (str[1] == '#' && str[2] != '#') HandlePragma(str + 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore comments & blank lines
|
2006-02-12 10:44:52 +00:00
|
|
|
if (*str == ';' || *str == ' ' || *str == '\0') return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
s = strchr(str, ':');
|
|
|
|
if (s == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("Line has no ':' delimiter");
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// Trim spaces.
|
|
|
|
// After this str points to the command name, and s points to the command contents
|
2006-02-12 10:44:52 +00:00
|
|
|
for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--);
|
2005-07-15 14:53:44 +00:00
|
|
|
*t = 0;
|
|
|
|
s++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-17 07:46:02 +00:00
|
|
|
/* Check string is valid UTF-8 */
|
|
|
|
{
|
|
|
|
const char *tmp;
|
|
|
|
for (tmp = s; *tmp != '\0';) {
|
|
|
|
size_t len = Utf8Validate(tmp);
|
|
|
|
if (len == 0) fatal("Invalid UTF-8 sequence in '%s'", s);
|
|
|
|
tmp += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
// Check if the string has a case..
|
|
|
|
// The syntax for cases is IDENTNAME.case
|
|
|
|
casep = strchr(str, '.');
|
|
|
|
if (casep) *casep++ = 0;
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// Check if this string already exists..
|
2005-07-15 07:48:17 +00:00
|
|
|
ent = HashFind(str);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (master) {
|
2006-02-12 10:44:52 +00:00
|
|
|
if (ent != NULL && casep == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("String name '%s' is used multiple times", str);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (ent == NULL && casep != NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("Base string name '%s' doesn't exist yet. Define it before defining a case.", str);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
if (ent == NULL) {
|
|
|
|
if (_strings[_next_string_id]) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("String ID 0x%X for '%s' already in use by '%s'", ent, str, _strings[_next_string_id]->name);
|
2005-07-17 10:18:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new LangString
|
2006-05-02 14:34:53 +00:00
|
|
|
ent = calloc(1, sizeof(*ent));
|
2005-07-17 10:18:23 +00:00
|
|
|
_strings[_next_string_id] = ent;
|
|
|
|
ent->index = _next_string_id++;
|
|
|
|
ent->name = strdup(str);
|
2005-10-30 00:16:48 +00:00
|
|
|
ent->line = _cur_line;
|
2005-07-17 10:18:23 +00:00
|
|
|
|
|
|
|
HashAdd(str, ent);
|
|
|
|
}
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (casep != NULL) {
|
|
|
|
Case* c = malloc(sizeof(*c));
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
c->caseidx = ResolveCaseName(casep, strlen(casep));
|
|
|
|
c->string = strdup(s);
|
|
|
|
c->next = ent->english_case;
|
|
|
|
ent->english_case = c;
|
|
|
|
} else {
|
|
|
|
ent->english = strdup(s);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-07-17 10:18:23 +00:00
|
|
|
if (ent == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("String name '%s' does not exist in master file", str);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (ent->translated && casep == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
error("String name '%s' is used multiple times", str);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
if (s[0] == ':' && s[1] == '\0' && casep == NULL) {
|
2005-07-15 14:53:44 +00:00
|
|
|
// Special syntax :: means we should just inherit the master string
|
2005-07-17 10:18:23 +00:00
|
|
|
ent->translated = strdup(ent->english);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-07-17 10:18:23 +00:00
|
|
|
// make sure that the commands match
|
2006-02-12 10:44:52 +00:00
|
|
|
if (!CheckCommandsMatch(s, ent->english, str)) return;
|
|
|
|
|
|
|
|
if (casep != NULL) {
|
|
|
|
Case* c = malloc(sizeof(*c));
|
2005-07-17 10:18:23 +00:00
|
|
|
|
|
|
|
c->caseidx = ResolveCaseName(casep, strlen(casep));
|
|
|
|
c->string = strdup(s);
|
|
|
|
c->next = ent->translated_case;
|
|
|
|
ent->translated_case = c;
|
|
|
|
} else {
|
|
|
|
ent->translated = strdup(s);
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
static void rstrip(char *buf)
|
|
|
|
{
|
|
|
|
int i = strlen(buf);
|
2006-02-12 10:44:52 +00:00
|
|
|
while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--;
|
|
|
|
buf[i] = '\0';
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
static void ParseFile(const char *file, bool english)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
FILE *in;
|
2005-07-15 14:53:44 +00:00
|
|
|
char buf[2048];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-25 09:15:09 +00:00
|
|
|
_file = file;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
// For each new file we parse, reset the genders.
|
|
|
|
_numgenders = 0;
|
|
|
|
// TODO:!! We can't reset the cases. In case the translated strings
|
|
|
|
// derive some strings from english....
|
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
in = fopen(file, "r");
|
2006-10-22 20:46:18 +00:00
|
|
|
if (in == NULL) fatal("Cannot open file");
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_line = 1;
|
|
|
|
while (fgets(buf, sizeof(buf),in) != NULL) {
|
2005-07-15 14:53:44 +00:00
|
|
|
rstrip(buf);
|
|
|
|
HandleString(buf, english);
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_line++;
|
|
|
|
}
|
|
|
|
fclose(in);
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 07:48:17 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
static uint32 MyHashStr(uint32 hash, const char *s)
|
|
|
|
{
|
2006-02-12 10:44:52 +00:00
|
|
|
for (; *s != '\0'; s++) {
|
|
|
|
hash = ROL(hash, 3) ^ *s;
|
2006-06-27 21:25:53 +00:00
|
|
|
hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
2005-07-15 07:48:17 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// make a hash of the file to get a unique "version number"
|
2005-12-17 14:26:43 +00:00
|
|
|
static void MakeHashOfStrings(void)
|
2005-07-15 14:53:44 +00:00
|
|
|
{
|
|
|
|
uint32 hash = 0;
|
2006-02-12 10:44:52 +00:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i != lengthof(_strings); i++) {
|
|
|
|
const LangString* ls = _strings[i];
|
|
|
|
|
|
|
|
if (ls != NULL) {
|
|
|
|
const CmdStruct* cs;
|
|
|
|
const char* s;
|
|
|
|
char buf[256];
|
|
|
|
int argno;
|
|
|
|
int casei;
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
s = ls->name;
|
2005-07-15 14:53:44 +00:00
|
|
|
hash ^= i * 0x717239;
|
2006-06-27 21:25:53 +00:00
|
|
|
hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
|
2005-07-15 14:53:44 +00:00
|
|
|
hash = MyHashStr(hash, s + 1);
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
s = ls->english;
|
2006-02-12 10:44:52 +00:00
|
|
|
while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != NULL) {
|
|
|
|
if (cs->flags & C_DONTCOUNT) continue;
|
2005-07-17 10:18:23 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
hash ^= (cs - _cmd_structs) * 0x1234567;
|
2006-06-27 21:25:53 +00:00
|
|
|
hash = (hash & 1 ? hash >> 1 ^ 0xF00BAA4 : hash >> 1);
|
2004-09-12 21:49:38 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
_hash = hash;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
static uint CountInUse(uint grp)
|
2005-07-15 07:48:17 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
int i;
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
for (i = 0x800; --i >= 0;) if (_strings[(grp << 11) + i] != NULL) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
bool CompareFiles(const char *n1, const char *n2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FILE *f1, *f2;
|
|
|
|
char b1[4096];
|
|
|
|
char b2[4096];
|
|
|
|
size_t l1, l2;
|
|
|
|
|
|
|
|
f2 = fopen(n2, "rb");
|
|
|
|
if (f2 == NULL) return false;
|
|
|
|
|
|
|
|
f1 = fopen(n1, "rb");
|
2006-10-22 20:46:18 +00:00
|
|
|
if (f1 == NULL) fatal("can't open %s", n1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
l1 = fread(b1, 1, sizeof(b1), f1);
|
|
|
|
l2 = fread(b2, 1, sizeof(b2), f2);
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
if (l1 != l2 || memcmp(b1, b2, l1)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (l1);
|
|
|
|
|
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
static void WriteStringsH(const char *filename)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FILE *out;
|
|
|
|
int i;
|
|
|
|
int next = -1;
|
|
|
|
int lastgrp;
|
|
|
|
|
|
|
|
out = fopen("tmp.xxx", "w");
|
2006-10-22 20:46:18 +00:00
|
|
|
if (out == NULL) fatal("can't open tmp.xxx");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
fprintf(out, "enum {");
|
|
|
|
|
|
|
|
lastgrp = 0;
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
for (i = 0; i != lengthof(_strings); i++) {
|
|
|
|
if (_strings[i] != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (lastgrp != (i >> 11)) {
|
|
|
|
lastgrp = (i >> 11);
|
|
|
|
fprintf(out, "};\n\nenum {");
|
|
|
|
}
|
2004-09-12 21:49:38 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
fprintf(out, next == i ? "\t%s,\n" : "\n\t%s = 0x%X,\n", _strings[i]->name, i);
|
2004-08-09 17:04:08 +00:00
|
|
|
next = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out, "};\n");
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
fprintf(out,
|
2004-08-09 17:04:08 +00:00
|
|
|
"\nenum {\n"
|
2004-09-08 15:01:12 +00:00
|
|
|
"\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n"
|
2004-08-09 17:04:08 +00:00
|
|
|
"\tLANGUAGE_PACK_VERSION = 0x%X,\n"
|
2006-02-12 10:44:52 +00:00
|
|
|
"};\n", (uint)_hash
|
|
|
|
);
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(out);
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
if (CompareFiles("tmp.xxx", filename)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// files are equal. tmp.xxx is not needed
|
|
|
|
unlink("tmp.xxx");
|
|
|
|
} else {
|
|
|
|
// else rename tmp.xxx into filename
|
2005-10-02 22:39:56 +00:00
|
|
|
#if defined(WIN32) || defined(WIN64)
|
2004-08-09 17:04:08 +00:00
|
|
|
unlink(filename);
|
|
|
|
#endif
|
2006-10-22 20:46:18 +00:00
|
|
|
if (rename("tmp.xxx", filename) == -1) fatal("rename() failed");
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
static int TranslateArgumentIdx(int argidx)
|
2005-07-15 14:53:44 +00:00
|
|
|
{
|
|
|
|
int i, sum;
|
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
if (argidx < 0 || argidx >= lengthof(_cur_pcs.cmd))
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("invalid argidx %d", argidx);
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = sum = 0; i < argidx; i++) {
|
2005-08-28 14:45:44 +00:00
|
|
|
const CmdStruct *cs = _cur_pcs.cmd[i];
|
2006-02-12 10:44:52 +00:00
|
|
|
sum += (cs != NULL) ? cs->consumes : 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-07-16 17:12:32 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PutArgidxCommand(void)
|
|
|
|
{
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_ARG_INDEX);
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(TranslateArgumentIdx(_cur_argidx));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PutCommandString(const char *str)
|
|
|
|
{
|
|
|
|
const CmdStruct *cs;
|
|
|
|
char param[256];
|
|
|
|
int argno;
|
|
|
|
int casei;
|
|
|
|
|
|
|
|
_cur_argidx = 0;
|
|
|
|
|
|
|
|
while (*str != '\0') {
|
|
|
|
// Process characters as they are until we encounter a {
|
|
|
|
if (*str != '{') {
|
|
|
|
PutByte(*str++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cs = ParseCommandString(&str, param, &argno, &casei);
|
|
|
|
if (cs == NULL) break;
|
|
|
|
|
|
|
|
if (casei != -1) {
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_SETCASE); // {SETCASE}
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(casei);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For params that consume values, we need to handle the argindex properly
|
2006-02-12 10:44:52 +00:00
|
|
|
if (cs->consumes > 0) {
|
2005-07-17 10:18:23 +00:00
|
|
|
// Check if we need to output a move-param command
|
2006-02-12 10:44:52 +00:00
|
|
|
if (argno != -1 && argno != _cur_argidx) {
|
2005-07-17 10:18:23 +00:00
|
|
|
_cur_argidx = argno;
|
|
|
|
PutArgidxCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the one from the master string... it's always accurate.
|
|
|
|
cs = _cur_pcs.cmd[_cur_argidx++];
|
2006-02-12 10:44:52 +00:00
|
|
|
if (cs == NULL) {
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("%s: No argument exists at position %d", _cur_ident, _cur_argidx - 1);
|
2006-02-12 10:44:52 +00:00
|
|
|
}
|
2005-07-17 10:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs->proc(param, cs->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WriteLength(FILE *f, uint length)
|
|
|
|
{
|
|
|
|
if (length < 0xC0) {
|
|
|
|
fputc(length, f);
|
|
|
|
} else if (length < 0x4000) {
|
|
|
|
fputc((length >> 8) | 0xC0, f);
|
|
|
|
fputc(length & 0xFF, f);
|
|
|
|
} else {
|
2006-10-22 20:46:18 +00:00
|
|
|
fatal("string too long");
|
2005-07-17 10:18:23 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
static void WriteLangfile(const char *filename, int show_todo)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2006-02-12 10:44:52 +00:00
|
|
|
uint in_use[32];
|
2004-08-09 17:04:08 +00:00
|
|
|
LanguagePackHeader hdr;
|
2006-02-12 10:44:52 +00:00
|
|
|
uint i;
|
|
|
|
uint j;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
f = fopen(filename, "wb");
|
2006-10-22 20:46:18 +00:00
|
|
|
if (f == NULL) fatal("can't open %s", filename);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 32; i++) {
|
2006-02-12 10:44:52 +00:00
|
|
|
uint n = CountInUse(i);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
in_use[i] = n;
|
|
|
|
hdr.offsets[i] = TO_LE16(n);
|
|
|
|
}
|
|
|
|
|
2004-09-12 21:49:38 +00:00
|
|
|
// see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...)
|
2004-09-08 15:01:12 +00:00
|
|
|
hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG'
|
2004-08-09 17:04:08 +00:00
|
|
|
hdr.version = TO_LE32(_hash);
|
2005-07-16 17:12:32 +00:00
|
|
|
hdr.plural_form = _lang_pluralform;
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(hdr.name, _lang_name);
|
|
|
|
strcpy(hdr.own_name, _lang_ownname);
|
2005-02-17 07:49:31 +00:00
|
|
|
strcpy(hdr.isocode, _lang_isocode);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
fwrite(&hdr, sizeof(hdr), 1, f);
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 32; i++) {
|
|
|
|
for (j = 0; j != in_use[i]; j++) {
|
2006-02-12 10:44:52 +00:00
|
|
|
const LangString* ls = _strings[(i << 11) + j];
|
|
|
|
const Case* casep;
|
|
|
|
const char* cmdp;
|
2005-07-15 14:53:44 +00:00
|
|
|
|
|
|
|
// For undefined strings, just set that it's an empty string
|
2005-07-17 10:18:23 +00:00
|
|
|
if (ls == NULL) {
|
2005-07-15 14:53:44 +00:00
|
|
|
WriteLength(f, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
_cur_ident = ls->name;
|
2005-10-30 00:16:48 +00:00
|
|
|
_cur_line = ls->line;
|
2005-07-16 17:12:32 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
// Produce a message if a string doesn't have a translation.
|
2006-02-12 10:44:52 +00:00
|
|
|
if (show_todo > 0 && ls->translated == NULL) {
|
2005-07-15 14:53:44 +00:00
|
|
|
if (show_todo == 2) {
|
2006-10-22 20:46:18 +00:00
|
|
|
warning("'%s' is untranslated", ls->name);
|
2005-07-15 14:53:44 +00:00
|
|
|
} else {
|
|
|
|
const char *s = "<TODO> ";
|
2006-02-12 10:44:52 +00:00
|
|
|
while (*s != '\0') PutByte(*s++);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the strings and stuff from the english command string
|
2005-07-17 10:18:23 +00:00
|
|
|
ExtractCommandString(&_cur_pcs, ls->english, false);
|
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (ls->translated_case != NULL || ls->translated != NULL) {
|
2005-07-17 10:18:23 +00:00
|
|
|
casep = ls->translated_case;
|
|
|
|
cmdp = ls->translated;
|
|
|
|
} else {
|
|
|
|
casep = ls->english_case;
|
|
|
|
cmdp = ls->english;
|
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2005-11-02 23:55:10 +00:00
|
|
|
_translated = _masterlang || (cmdp != ls->english);
|
2005-11-02 23:31:04 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (casep != NULL) {
|
|
|
|
const Case* c;
|
|
|
|
uint num;
|
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
// Need to output a case-switch.
|
|
|
|
// It has this format
|
|
|
|
// <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
|
|
|
|
// Each LEN is printed using 2 bytes in big endian order.
|
2006-11-16 22:05:33 +00:00
|
|
|
PutUtf8(SCC_SWITCH_CASE);
|
2005-07-17 10:18:23 +00:00
|
|
|
// Count the number of cases
|
2006-02-01 07:36:15 +00:00
|
|
|
for (num = 0, c = casep; c; c = c->next) num++;
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(num);
|
|
|
|
|
|
|
|
// Write each case
|
2006-02-12 10:44:52 +00:00
|
|
|
for (c = casep; c != NULL; c = c->next) {
|
2005-07-17 10:18:23 +00:00
|
|
|
int pos;
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2005-07-17 10:18:23 +00:00
|
|
|
PutByte(c->caseidx);
|
|
|
|
// Make some space for the 16-bit length
|
|
|
|
pos = _put_pos;
|
|
|
|
PutByte(0);
|
|
|
|
PutByte(0);
|
|
|
|
// Write string
|
|
|
|
PutCommandString(c->string);
|
|
|
|
PutByte(0); // terminate with a zero
|
|
|
|
// Fill in the length
|
2006-02-12 10:44:52 +00:00
|
|
|
_put_buf[pos + 0] = GB(_put_pos - (pos + 2), 8, 8);
|
|
|
|
_put_buf[pos + 1] = GB(_put_pos - (pos + 2), 0, 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2006-02-12 10:44:52 +00:00
|
|
|
if (cmdp != NULL) PutCommandString(cmdp);
|
2005-07-17 10:18:23 +00:00
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
WriteLength(f, _put_pos);
|
|
|
|
fwrite(_put_buf, 1, _put_pos, f);
|
|
|
|
_put_pos = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc(0, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
/** Multi-OS mkdirectory function */
|
|
|
|
static inline void ottd_mkdir(const char *directory)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(__WATCOMC__)
|
|
|
|
mkdir(directory);
|
|
|
|
#else
|
|
|
|
mkdir(directory, 0755);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a path consisting of an already existing path, a possible
|
|
|
|
* path seperator and the filename. The seperator is only appended if the path
|
|
|
|
* does not already end with a seperator */
|
|
|
|
static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
ttd_strlcpy(buf, path, buflen); // copy directory into buffer
|
|
|
|
|
|
|
|
p = strchr(buf, '\0'); // add path seperator if necessary
|
|
|
|
if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR;
|
|
|
|
ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // catenate filename at end of buffer
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2006-08-25 12:26:34 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
|
|
|
/**
|
|
|
|
* On MingW, it is common that both / as \ are accepted in the
|
|
|
|
* params. To go with those flow, we rewrite all incoming /
|
|
|
|
* simply to \, so internally we can safely assume \.
|
|
|
|
*/
|
|
|
|
static inline char *replace_pathsep(char *s)
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
for (c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline char *replace_pathsep(char *s) { return s; }
|
|
|
|
#endif
|
2005-07-15 14:53:44 +00:00
|
|
|
|
2004-09-14 18:19:29 +00:00
|
|
|
int CDECL main(int argc, char* argv[])
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-24 12:08:25 +00:00
|
|
|
char pathbuf[256];
|
2006-08-30 20:08:58 +00:00
|
|
|
const char *src_dir = ".";
|
|
|
|
const char *dest_dir = NULL;
|
2006-08-24 12:08:25 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
int show_todo = 0;
|
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
while (argc > 1 && *argv[1] == '-') {
|
|
|
|
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
|
|
|
|
puts("$Revision$");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--todo") == 0) {
|
|
|
|
show_todo = 1;
|
|
|
|
argc--, argv++;
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--warning") == 0) {
|
|
|
|
show_todo = 2;
|
|
|
|
argc--, argv++;
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) {
|
|
|
|
puts(
|
|
|
|
"strgen - $Revision$\n"
|
|
|
|
" -v | --version print version information and exit\n"
|
|
|
|
" -t | --todo replace any untranslated strings with '<TODO>'\n"
|
|
|
|
" -w | --warning print a warning for any untranslated strings\n"
|
|
|
|
" -h | -? | --help print this help message and exit\n"
|
|
|
|
" -s | --source_dir search for english.txt in the specified directory\n"
|
|
|
|
" -d | --dest_dir put output file in the specified directory, create if needed\n"
|
|
|
|
" Run without parameters and strgen will search for english.txt and parse it,\n"
|
|
|
|
" creating strings.h. Passing an argument, strgen will translate that language\n"
|
|
|
|
" file using english.txt as a reference and output <language>.lng."
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-28 11:10:52 +00:00
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (argc > 2 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--source_dir") == 0)) {
|
|
|
|
src_dir = replace_pathsep(argv[2]);
|
|
|
|
argc -= 2, argv += 2;
|
2006-08-30 22:32:25 +00:00
|
|
|
continue;
|
2006-08-30 20:08:58 +00:00
|
|
|
}
|
2006-08-24 12:08:25 +00:00
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (argc > 2 && (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--dest_dir") == 0)) {
|
|
|
|
dest_dir = replace_pathsep(argv[2]);
|
|
|
|
argc -= 2, argv += 2;
|
2006-08-30 22:32:25 +00:00
|
|
|
continue;
|
2006-08-30 20:08:58 +00:00
|
|
|
}
|
2006-08-30 22:32:25 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "Invalid arguments\n");
|
|
|
|
return 0;
|
2006-08-24 12:08:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-30 20:08:58 +00:00
|
|
|
if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
|
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
/* strgen has two modes of operation. If no (free) arguments are passed
|
|
|
|
* strgen generates strings.h to the destination directory. If it is supplied
|
|
|
|
* with a (free) parameter the program will translate that language to destination
|
|
|
|
* directory. As input english.txt is parsed from the source directory */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (argc == 1) {
|
2006-08-24 12:08:25 +00:00
|
|
|
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
|
|
|
|
|
|
|
|
/* parse master file */
|
2005-11-02 23:55:10 +00:00
|
|
|
_masterlang = true;
|
2006-08-24 12:08:25 +00:00
|
|
|
ParseFile(pathbuf, true);
|
2005-07-15 14:53:44 +00:00
|
|
|
MakeHashOfStrings();
|
|
|
|
if (_errors) return 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
/* write strings.h */
|
|
|
|
ottd_mkdir(dest_dir);
|
|
|
|
mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
|
|
|
|
WriteStringsH(pathbuf);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (argc == 2) {
|
2006-08-24 12:08:25 +00:00
|
|
|
char *r;
|
2006-02-12 10:44:52 +00:00
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
|
|
|
|
|
|
|
|
/* parse master file and check if target file is correct */
|
2005-11-02 23:55:10 +00:00
|
|
|
_masterlang = false;
|
2006-08-24 12:08:25 +00:00
|
|
|
ParseFile(pathbuf, true);
|
2005-07-15 14:53:44 +00:00
|
|
|
MakeHashOfStrings();
|
2006-08-25 12:26:34 +00:00
|
|
|
ParseFile(replace_pathsep(argv[1]), false); // target file
|
2005-07-15 14:53:44 +00:00
|
|
|
if (_errors) return 1;
|
2005-07-15 07:48:17 +00:00
|
|
|
|
2006-08-24 12:08:25 +00:00
|
|
|
/* get the targetfile, strip any directories and append to destination path */
|
|
|
|
r = strrchr(argv[1], PATHSEPCHAR);
|
|
|
|
mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : argv[1]);
|
|
|
|
|
|
|
|
/* rename the .txt (input-extension) to .lng */
|
|
|
|
r = strrchr(pathbuf, '.');
|
|
|
|
if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
|
|
|
|
ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
|
|
|
|
WriteLangfile(pathbuf, show_todo);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-08-24 12:08:25 +00:00
|
|
|
fprintf(stderr, "Invalid arguments\n");
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|