tron@2186: /* $Id$ */ tron@2186: truelight@1721: #define STRGEN truelight@1721: truelight@0: #include "../stdafx.h" tron@3015: #include "../macros.h" truelight@0: #include truelight@0: #include truelight@0: #include truelight@0: #include truelight@0: Darkvater@2482: #if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__) truelight@0: #include truelight@0: #endif truelight@0: darkvater@181: #ifdef __MORPHOS__ darkvater@181: #ifdef stderr darkvater@181: #undef stderr darkvater@181: #endif darkvater@181: #define stderr stdout darkvater@181: #endif // __MORPHOS__ darkvater@181: orudge@3395: #ifdef __WATCOMC__ orudge@3395: uint _map_log_x; // an unpleasant hack required because Watcom is insisting on orudge@3395: uint _map_size_x; // these variables being valid references in map.h orudge@3395: uint _map_size_y; orudge@3395: uint _map_tile_mask; orudge@3395: uint _map_size; orudge@3395: #endif orudge@3395: truelight@0: /* Compiles a list of strings into a compiled string list */ truelight@0: truelight@0: typedef void (*ParseCmdProc)(char *buf, int value); truelight@0: tron@3015: typedef struct LanguagePackHeader { truelight@0: uint32 ident; truelight@0: uint32 version; // 32-bits of auto generated version info which is basically a hash of strings.h truelight@0: char name[32]; // the international name of this language truelight@0: char own_name[32]; // the localized name of this language miham@1376: char isocode[16]; // the ISO code for the language (not country code) truelight@0: uint16 offsets[32]; // the offsets ludde@2082: byte plural_form; // plural form index ludde@2082: byte pad[3]; // pad header to be a multiple of 4 truelight@0: } LanguagePackHeader; truelight@0: truelight@0: typedef struct CmdStruct { truelight@0: const char *cmd; truelight@0: ParseCmdProc proc; truelight@0: long value; ludde@2063: int8 consumes; ludde@2087: byte flags; truelight@0: } CmdStruct; truelight@0: ludde@2087: enum { ludde@2087: C_DONTCOUNT = 1, ludde@2087: C_CASE = 2, ludde@2087: }; ludde@2087: ludde@2087: ludde@2087: typedef struct Case { ludde@2087: int caseidx; ludde@2087: char *string; ludde@2087: struct Case *next; ludde@2087: } Case; ludde@2087: Darkvater@2586: static bool _masterlang; Darkvater@2585: static bool _translated; tron@2459: static const char* _file = "(unknown file)"; tron@2059: static int _cur_line; ludde@2063: static int _errors, _warnings; ludde@2063: ludde@2087: typedef struct LangString { ludde@2087: char *name; // Name of the string ludde@2087: char *english; // English text ludde@2087: char *translated; // Translated text ludde@2087: uint16 hash_next; // next hash entry ludde@2087: uint16 index; Darkvater@2566: int line; // line of string in source-file ludde@2087: Case *english_case; // cases for english ludde@2087: Case *translated_case; // cases for foreign ludde@2087: } LangString; ludde@2087: ludde@2087: static LangString *_strings[65536]; ludde@2087: ludde@2063: ludde@2063: #define HASH_SIZE 32767 ludde@2063: static uint16 _hash_head[HASH_SIZE]; ludde@2063: ludde@2063: static byte _put_buf[4096]; ludde@2063: static int _put_pos; ludde@2063: static int _next_string_id; truelight@0: tron@2059: static uint32 _hash; ludde@2063: static char _lang_name[32], _lang_ownname[32], _lang_isocode[16]; ludde@2082: static byte _lang_pluralform; ludde@2087: #define MAX_NUM_GENDER 8 ludde@2087: static char _genders[MAX_NUM_GENDER][8]; ludde@2087: static int _numgenders; truelight@0: ludde@2087: // contains the name of all cases. ludde@2087: #define MAX_NUM_CASES 50 ludde@2087: static char _cases[MAX_NUM_CASES][16]; ludde@2087: static int _numcases; ludde@2084: ludde@2082: // for each plural value, this is the number of plural forms. ludde@2082: static const byte _plural_form_counts[] = { 2,1,2,3,3,3,3,3,4 }; ludde@2082: ludde@2082: static const char *_cur_ident; tron@2059: ludde@2087: typedef struct CmdPair { ludde@2087: const CmdStruct *a; ludde@2087: char *v; ludde@2087: } CmdPair; ludde@2087: ludde@2087: typedef struct ParsedCommandStruct { ludde@2087: int np; ludde@2087: CmdPair pairs[32]; ludde@2087: const CmdStruct *cmd[32]; // ordered by param # ludde@2087: } ParsedCommandStruct; ludde@2087: ludde@2087: // Used when generating some advanced commands. ludde@2087: static ParsedCommandStruct _cur_pcs; ludde@2087: static int _cur_argidx; ludde@2087: tron@2059: static uint HashStr(const char *s) tron@2059: { tron@2059: uint hash = 0; tron@3015: for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s; truelight@0: return hash % HASH_SIZE; truelight@0: } tron@2059: ludde@2087: static void HashAdd(const char *s, LangString *ls) tron@2059: { tron@2059: uint hash = HashStr(s); ludde@2087: ls->hash_next = _hash_head[hash]; ludde@2087: _hash_head[hash] = ls->index + 1; truelight@0: } truelight@0: ludde@2087: static LangString *HashFind(const char *s) tron@2059: { ludde@2063: int idx = _hash_head[HashStr(s)]; tron@3015: ludde@2063: while (--idx >= 0) { tron@3015: LangString* ls = _strings[idx]; tron@3015: tron@3015: if (strcmp(ls->name, s) == 0) return ls; ludde@2087: idx = ls->hash_next; truelight@0: } ludde@2087: return NULL; truelight@0: } truelight@0: KUDr@3900: #ifdef _MSC_VER KUDr@3900: # define LINE_NUM_FMT "(%d)" KUDr@3900: #else KUDr@3900: # define LINE_NUM_FMT ":%d" KUDr@3900: #endif ludde@2063: ludde@2063: static void CDECL Warning(const char *s, ...) tron@2059: { truelight@0: char buf[1024]; truelight@0: va_list va; truelight@0: va_start(va, s); truelight@0: vsprintf(buf, s, va); truelight@0: va_end(va); KUDr@3900: fprintf(stderr, "%s" LINE_NUM_FMT ": Warning: %s\n", _file, _cur_line, buf); ludde@2063: _warnings++; truelight@0: } truelight@0: ludde@2063: ludde@2063: static void CDECL Error(const char *s, ...) ludde@2063: { ludde@2063: char buf[1024]; ludde@2063: va_list va; ludde@2063: va_start(va, s); ludde@2063: vsprintf(buf, s, va); ludde@2063: va_end(va); KUDr@3900: fprintf(stderr, "%s" LINE_NUM_FMT ": Error: %s\n", _file, _cur_line, buf); ludde@2063: _errors++; ludde@2063: } ludde@2063: ludde@2063: ludde@2063: static void NORETURN CDECL Fatal(const char *s, ...) tron@2059: { truelight@0: char buf[1024]; truelight@0: va_list va; truelight@0: va_start(va, s); truelight@0: vsprintf(buf, s, va); truelight@0: va_end(va); KUDr@3900: fprintf(stderr, "%s" LINE_NUM_FMT ": FATAL: %s\n", _file, _cur_line, buf); truelight@0: exit(1); truelight@0: } truelight@0: ludde@2063: tron@2059: static void ttd_strlcpy(char *dst, const char *src, size_t len) truelight@0: { truelight@0: assert(len > 0); tron@3015: while (--len > 0 && *src != '\0') *dst++ = *src++; tron@3015: *dst = '\0'; truelight@0: } truelight@0: truelight@0: ludde@2063: static void PutByte(byte c) tron@2059: { tron@3015: if (_put_pos == lengthof(_put_buf)) Fatal("Put buffer too small"); truelight@0: _put_buf[_put_pos++] = c; truelight@0: } truelight@0: truelight@0: tron@2059: static void EmitSingleByte(char *buf, int value) tron@2059: { tron@3015: if (*buf != '\0') Warning("Ignoring trailing letters in command"); ludde@2063: PutByte((byte)value); truelight@0: } truelight@0: truelight@0: ludde@2063: static void EmitEscapedByte(char *buf, int value) tron@2059: { tron@3015: if (*buf != '\0') Warning("Ignoring trailing letters in command"); tron@3015: PutByte(0x85); ludde@2063: PutByte((byte)value); ludde@2063: } truelight@0: tron@2059: static void EmitSetX(char *buf, int value) tron@2059: { truelight@0: char *err; truelight@0: int x = strtol(buf, &err, 0); tron@3015: if (*err != 0) Fatal("SetX param invalid"); ludde@2063: PutByte(1); ludde@2063: PutByte((byte)x); ludde@2063: } tron@2059: truelight@0: tron@2059: static void EmitSetXY(char *buf, int value) tron@2059: { truelight@0: char *err; tron@3015: int x; tron@3015: int y; tron@2059: tron@2059: x = strtol(buf, &err, 0); ludde@2082: if (*err != ' ') Fatal("SetXY param invalid"); tron@3015: y = strtol(err + 1, &err, 0); ludde@2063: if (*err != 0) Fatal("SetXY param invalid"); truelight@0: ludde@2087: PutByte(2); ludde@2063: PutByte((byte)x); ludde@2063: PutByte((byte)y); truelight@0: } truelight@0: ludde@2082: // The plural specifier looks like ludde@2082: // {NUM} {PLURAL -1 passenger passengers} then it picks either passenger/passengers depending on the count in NUM ludde@2082: ludde@2082: // This is encoded like ludde@2082: // CommandByte {Length of each string} {each string} ludde@2082: ludde@2087: bool ParseRelNum(char **buf, int *value) ludde@2082: { tron@3015: const char* s = *buf; tron@3015: char* end; ludde@2082: bool rel = false; ludde@2087: int v; ludde@2087: ludde@2082: while (*s == ' ' || *s == '\t') s++; tron@3015: if (*s == '+') { tron@3015: rel = true; tron@3015: s++; tron@3015: } ludde@2087: v = strtol(s, &end, 0); ludde@2082: if (end == s) return false; tron@3015: if (rel || v < 0) { ludde@2087: *value += v; tron@3015: } else { ludde@2087: *value = v; tron@3015: } ludde@2082: *buf = end; ludde@2082: return true; ludde@2082: } ludde@2082: ludde@2082: // Parse out the next word, or NULL ludde@2082: char *ParseWord(char **buf) ludde@2082: { ludde@2082: char *s = *buf, *r; tron@3015: ludde@2082: while (*s == ' ' || *s == '\t') s++; tron@3015: if (*s == '\0') return NULL; ludde@2082: ludde@2082: if (*s == '"') { ludde@2082: r = ++s; ludde@2082: // parse until next " or NUL tron@2952: for (;;) { tron@3015: if (*s == '\0') break; ludde@2082: if (*s == '"') { tron@3015: *s++ = '\0'; ludde@2082: break; ludde@2082: } ludde@2082: s++; ludde@2082: } ludde@2082: } else { ludde@2082: // proceed until whitespace or NUL ludde@2082: r = s; tron@2952: for (;;) { tron@3015: if (*s == '\0') break; ludde@2082: if (*s == ' ' || *s == '\t') { tron@3015: *s++ = '\0'; ludde@2082: break; ludde@2082: } ludde@2082: s++; ludde@2082: } ludde@2082: } ludde@2082: *buf = s; ludde@2082: return r; ludde@2082: } ludde@2082: ludde@2082: // Forward declaration ludde@2087: static int TranslateArgumentIdx(int arg); ludde@2082: tron@3015: static void EmitWordList(const char* const* words, uint nw) ludde@2084: { tron@3015: uint i; tron@3015: uint j; ludde@2084: ludde@2084: PutByte(nw); tron@2952: for (i = 0; i < nw; i++) PutByte(strlen(words[i])); tron@2952: for (i = 0; i < nw; i++) { tron@3015: for (j = 0; words[i][j] != '\0'; j++) PutByte(words[i][j]); ludde@2084: } ludde@2084: } ludde@2084: ludde@2082: static void EmitPlural(char *buf, int value) ludde@2082: { ludde@2087: int argidx = _cur_argidx; tron@3015: const char* words[5]; ludde@2082: int nw = 0; ludde@2082: ludde@2087: // Parse out the number, if one exists. Otherwise default to prev arg. tron@3015: if (!ParseRelNum(&buf, &argidx)) argidx--; ludde@2082: ludde@2082: // Parse each string Darkvater@2566: for (nw = 0; nw < 5; nw++) { ludde@2082: words[nw] = ParseWord(&buf); tron@3015: if (words[nw] == NULL) break; ludde@2082: } ludde@2082: ludde@2082: if (nw == 0) Darkvater@2566: Fatal("%s: No plural words", _cur_ident); ludde@2082: tron@2765: if (_plural_form_counts[_lang_pluralform] != nw) { Darkvater@2585: if (_translated) { Darkvater@2566: Fatal("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident, Darkvater@2566: _plural_form_counts[_lang_pluralform], nw); Darkvater@2566: } else { Darkvater@2566: Warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident); Darkvater@2566: if (nw > _plural_form_counts[_lang_pluralform]) { Darkvater@2566: nw = _plural_form_counts[_lang_pluralform]; Darkvater@2566: } else { tron@2952: for (; nw < _plural_form_counts[_lang_pluralform]; nw++) { Darkvater@2566: words[nw] = words[nw - 1]; Darkvater@2566: } Darkvater@2566: } Darkvater@2566: } tron@2765: } ludde@2082: tron@2410: PutByte(0x8D); ludde@2087: PutByte(TranslateArgumentIdx(argidx)); ludde@2084: EmitWordList(words, nw); ludde@2084: } ludde@2084: ludde@2084: ludde@2084: static void EmitGender(char *buf, int value) ludde@2084: { ludde@2087: int argidx = _cur_argidx; tron@3015: uint nw; ludde@2084: ludde@2084: if (buf[0] == '=') { ludde@2084: buf++; ludde@2084: ludde@2084: // This is a {G=DER} command tron@2952: for (nw = 0; ; nw++) { tron@2952: if (nw >= 8) Fatal("G argument '%s' invalid", buf); tron@3015: if (strcmp(buf, _genders[nw]) == 0) break; ludde@2084: } ludde@2084: // now nw contains the gender index ludde@2084: PutByte(0x87); ludde@2084: PutByte(nw); tron@3015: } else { tron@3015: const char* words[8]; ludde@2084: ludde@2084: // This is a {G 0 foo bar two} command. ludde@2087: // If no relative number exists, default to +0 ludde@2087: if (!ParseRelNum(&buf, &argidx)) {} ludde@2084: tron@2952: for (nw = 0; nw < 8; nw++) { ludde@2084: words[nw] = ParseWord(&buf); tron@3015: if (words[nw] == NULL) break; ludde@2084: } ludde@2084: if (nw != _numgenders) Fatal("Bad # of arguments for gender command"); ludde@2084: PutByte(0x85); ludde@2084: PutByte(13); ludde@2087: PutByte(TranslateArgumentIdx(argidx)); ludde@2084: EmitWordList(words, nw); ludde@2082: } ludde@2082: } ludde@2082: ludde@2082: truelight@0: static const CmdStruct _cmd_structs[] = { truelight@0: // Update position tron@2765: {"SETX", EmitSetX, 1, 0, 0}, tron@2765: {"SETXY", EmitSetXY, 2, 0, 0}, truelight@0: truelight@0: // Font size tron@2765: {"TINYFONT", EmitSingleByte, 8, 0, 0}, tron@2765: {"BIGFONT", EmitSingleByte, 9, 0, 0}, truelight@0: truelight@0: // Colors tron@2765: {"BLUE", EmitSingleByte, 15, 0, 0}, tron@2765: {"SILVER", EmitSingleByte, 16, 0, 0}, tron@2765: {"GOLD", EmitSingleByte, 17, 0, 0}, tron@2765: {"RED", EmitSingleByte, 18, 0, 0}, tron@2765: {"PURPLE", EmitSingleByte, 19, 0, 0}, tron@2765: {"LTBROWN", EmitSingleByte, 20, 0, 0}, tron@2765: {"ORANGE", EmitSingleByte, 21, 0, 0}, tron@2765: {"GREEN", EmitSingleByte, 22, 0, 0}, tron@2765: {"YELLOW", EmitSingleByte, 23, 0, 0}, tron@2765: {"DKGREEN", EmitSingleByte, 24, 0, 0}, tron@2765: {"CREAM", EmitSingleByte, 25, 0, 0}, tron@2765: {"BROWN", EmitSingleByte, 26, 0, 0}, tron@2765: {"WHITE", EmitSingleByte, 27, 0, 0}, tron@2765: {"LTBLUE", EmitSingleByte, 28, 0, 0}, tron@2765: {"GRAY", EmitSingleByte, 29, 0, 0}, tron@2765: {"DKBLUE", EmitSingleByte, 30, 0, 0}, tron@2765: {"BLACK", EmitSingleByte, 31, 0, 0}, darkvater@222: tron@2765: {"CURRCOMPACT", EmitEscapedByte, 0, 1, 0}, // compact currency (32 bits) tron@2765: {"REV", EmitEscapedByte, 2, 0, 0}, // openttd revision string tron@2765: {"SHORTCARGO", EmitEscapedByte, 3, 2, 0}, // short cargo description, only ### tons, or ### litres tron@2765: {"CURRCOMPACT64", EmitEscapedByte, 4, 2, 0}, // compact currency 64 bits truelight@0: tron@3015: // These are special versions of {STRING1} tron@3015: // The first string includes the second string. tron@3015: {"COMPANY", EmitEscapedByte, 5, 1, 0}, tron@3015: {"PLAYERNAME", EmitEscapedByte, 5, 1, 0}, tron@3015: {"VEHICLE", EmitEscapedByte, 5, 1, 0}, truelight@0: tron@3015: {"STRING1", EmitEscapedByte, 5, 1, C_CASE}, // included string that consumes ONE argument tron@3015: {"STRING2", EmitEscapedByte, 6, 2, C_CASE}, // included string that consumes TWO arguments tron@3015: {"STRING3", EmitEscapedByte, 7, 3, C_CASE}, // included string that consumes THREE arguments tron@3015: {"STRING4", EmitEscapedByte, 8, 4, C_CASE}, // included string that consumes FOUR arguments tron@3015: {"STRING5", EmitEscapedByte, 9, 5, C_CASE}, // included string that consumes FIVE arguments ludde@2063: tron@2765: {"STATIONFEATURES", EmitEscapedByte, 10, 1, 0}, // station features string, icons of the features tron@2765: {"INDUSTRY", EmitEscapedByte, 11, 1, 0}, // industry, takes an industry # tron@2765: {"VOLUME", EmitEscapedByte, 12, 1, 0}, tron@2765: {"DATE_TINY", EmitEscapedByte, 14, 1, 0}, tron@2765: {"CARGO", EmitEscapedByte, 15, 2, 0}, peter1138@3342: {"POWER", EmitEscapedByte, 16, 1, 0}, peter1138@3342: {"VOLUME_S", EmitEscapedByte, 17, 1, 0}, peter1138@3342: {"WEIGHT", EmitEscapedByte, 18, 1, 0}, peter1138@3342: {"WEIGHT_S", EmitEscapedByte, 19, 1, 0}, peter1138@3489: {"FORCE", EmitEscapedByte, 20, 1, 0}, ludde@2063: ludde@2087: {"P", EmitPlural, 0, 0, C_DONTCOUNT}, // plural specifier ludde@2087: {"G", EmitGender, 0, 0, C_DONTCOUNT}, // gender specifier ludde@2082: tron@2765: {"DATE_LONG", EmitSingleByte, 0x82, 1, 0}, tron@2765: {"DATE_SHORT", EmitSingleByte, 0x83, 1, 0}, ludde@2063: tron@2765: {"VELOCITY", EmitSingleByte, 0x84, 1, 0}, ludde@2063: tron@3015: // 0x85 is the marker for escaped commands tron@3015: tron@2765: {"SKIP", EmitSingleByte, 0x86, 1, 0}, ludde@2063: ludde@2087: {"STRING", EmitSingleByte, 0x88, 1, C_CASE}, ludde@2063: tron@2410: // Numbers tron@2765: {"COMMA", EmitSingleByte, 0x8B, 1, 0}, // Number with comma tron@2765: {"NUM", EmitSingleByte, 0x8E, 1, 0}, // Signed number tron@2410: tron@3015: {"CURRENCY", EmitSingleByte, 0x8F, 1, 0}, tron@3015: {"CURRENCY64", EmitSingleByte, 0x9C, 2, 0}, tron@2410: tron@3015: {"WAYPOINT", EmitSingleByte, 0x99, 1, 0}, // waypoint name tron@3015: {"STATION", EmitSingleByte, 0x9A, 1, 0}, tron@3015: {"TOWN", EmitSingleByte, 0x9B, 1, 0}, tron@3015: ludde@2087: // 0x9D is used for the pseudo command SETCASE ludde@2087: // 0x9E is used for case switching ludde@2087: tron@3015: {"", EmitSingleByte, '\n', 0, C_DONTCOUNT}, tron@3015: {"{", EmitSingleByte, '{', 0, C_DONTCOUNT}, tron@3015: {"UPARROW", EmitSingleByte, 0x80, 0, 0}, tron@2765: {"SMALLUPARROW", EmitSingleByte, 0x90, 0, 0}, tron@3015: {"SMALLDOWNARROW", EmitSingleByte, 0x91, 0, 0}, tron@3015: {"TRAIN", EmitSingleByte, 0x94, 0, 0}, tron@3015: {"LORRY", EmitSingleByte, 0x95, 0, 0}, tron@3015: {"BUS", EmitSingleByte, 0x96, 0, 0}, tron@3015: {"PLANE", EmitSingleByte, 0x97, 0, 0}, tron@3015: {"SHIP", EmitSingleByte, 0x98, 0, 0}, tron@3015: {"NBSP", EmitSingleByte, 0xA0, 0, C_DONTCOUNT}, tron@3015: {"CENT", EmitSingleByte, '¢', 0, C_DONTCOUNT}, tron@3015: {"POUNDSIGN", EmitSingleByte, '£', 0, C_DONTCOUNT}, tron@3015: {"EURO", EmitSingleByte, '¤', 0, C_DONTCOUNT}, tron@3015: {"YENSIGN", EmitSingleByte, '¥', 0, C_DONTCOUNT}, tron@3015: {"COPYRIGHT", EmitSingleByte, '©', 0, C_DONTCOUNT}, tron@3015: {"DOWNARROW", EmitSingleByte, 0xAA, 0, C_DONTCOUNT}, tron@3015: {"CHECKMARK", EmitSingleByte, 0xAC, 0, C_DONTCOUNT}, tron@3015: {"CROSS", EmitSingleByte, 0xAD, 0, C_DONTCOUNT}, tron@3015: {"REGISTERED", EmitSingleByte, '®', 0, C_DONTCOUNT}, tron@3015: {"RIGHTARROW", EmitSingleByte, 0xAF, 0, C_DONTCOUNT}, truelight@0: }; truelight@0: ludde@2063: tron@2059: static const CmdStruct *FindCmd(const char *s, int len) tron@2059: { tron@3015: const CmdStruct* cs; tron@3015: tron@3015: for (cs = _cmd_structs; cs != endof(_cmd_structs); cs++) { tron@3015: if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs; truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: tron@3015: static uint ResolveCaseName(const char *str, uint len) ludde@2087: { tron@3015: uint i; tron@3015: tron@3015: for (i = 0; i < MAX_NUM_CASES; i++) { tron@3015: if (memcmp(_cases[i], str, len) == 0 && _cases[i][len] == 0) return i + 1; tron@3015: } ludde@2087: Fatal("Invalid case-name '%s'", str); ludde@2087: } ludde@2087: truelight@0: ludde@2063: // returns NULL on eof ludde@2063: // else returns command struct ludde@2087: static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei) truelight@0: { ludde@2087: const char *s = *str, *start; ludde@2063: const CmdStruct *cmd; tron@2060: byte c; truelight@0: ludde@2063: *argno = -1; ludde@2087: *casei = -1; ludde@2063: ludde@2063: // Scan to the next command, exit if there's no next command. tron@2952: for (; *s != '{'; s++) { tron@3015: if (*s == '\0') return NULL; ludde@2063: } ludde@2063: s++; // Skip past the { ludde@2063: ludde@2063: if (*s >= '0' && *s <= '9') { ludde@2063: char *end; tron@3015: ludde@2063: *argno = strtoul(s, &end, 0); tron@3015: if (*end != ':') Fatal("missing arg #"); ludde@2063: s = end + 1; ludde@2063: } truelight@0: truelight@0: // parse command name truelight@0: start = s; ludde@2087: do { truelight@0: c = *s++; ludde@2087: } while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0); truelight@0: tron@2059: cmd = FindCmd(start, s - start - 1); truelight@0: if (cmd == NULL) { ludde@2063: Error("Undefined command '%.*s'", s - start - 1, start); truelight@0: return NULL; truelight@0: } truelight@0: ludde@2087: if (c == '.') { ludde@2087: const char *casep = s; ludde@2087: ludde@2087: if (!(cmd->flags & C_CASE)) ludde@2087: Fatal("Command '%s' can't have a case", cmd->cmd); ludde@2087: ludde@2087: do c = *s++; while (c != '}' && c != ' ' && c != '\0'); tron@3015: *casei = ResolveCaseName(casep, s - casep - 1); ludde@2087: } ludde@2087: ludde@2087: if (c == '\0') { ludde@2087: Error("Missing } from command '%s'", start); ludde@2087: return NULL; ludde@2087: } ludde@2087: ludde@2087: ludde@2084: if (c != '}') { ludde@2084: if (c == '=') s--; truelight@0: // copy params truelight@0: start = s; tron@2952: for (;;) { truelight@0: c = *s++; truelight@0: if (c == '}') break; ludde@2063: if (c == '\0') { ludde@2063: Error("Missing } from command '%s'", start); truelight@0: return NULL; truelight@0: } tron@3015: if (s - start == 250) Fatal("param command too long"); truelight@0: *param++ = c; truelight@0: } truelight@0: } tron@3015: *param = '\0'; truelight@0: truelight@0: *str = s; truelight@0: truelight@0: return cmd; truelight@0: } truelight@0: truelight@0: ludde@2063: static void HandlePragma(char *str) truelight@0: { ludde@2063: if (!memcmp(str, "id ", 3)) { truelight@0: _next_string_id = strtoul(str + 3, NULL, 0); ludde@2063: } else if (!memcmp(str, "name ", 5)) { truelight@0: ttd_strlcpy(_lang_name, str + 5, sizeof(_lang_name)); ludde@2063: } else if (!memcmp(str, "ownname ", 8)) { truelight@0: ttd_strlcpy(_lang_ownname, str + 8, sizeof(_lang_ownname)); ludde@2063: } else if (!memcmp(str, "isocode ", 8)) { miham@1376: ttd_strlcpy(_lang_isocode, str + 8, sizeof(_lang_isocode)); ludde@2082: } else if (!memcmp(str, "plural ", 7)) { ludde@2082: _lang_pluralform = atoi(str + 7); ludde@2082: if (_lang_pluralform >= lengthof(_plural_form_counts)) ludde@2082: Fatal("Invalid pluralform %d", _lang_pluralform); ludde@2084: } else if (!memcmp(str, "gender ", 7)) { tron@3015: char* buf = str + 7; tron@3015: tron@2952: for (;;) { tron@3015: const char* s = ParseWord(&buf); tron@3015: tron@3015: if (s == NULL) break; ludde@2087: if (_numgenders >= MAX_NUM_GENDER) Fatal("Too many genders, max %d", MAX_NUM_GENDER); ludde@2087: ttd_strlcpy(_genders[_numgenders], s, sizeof(_genders[_numgenders])); ludde@2084: _numgenders++; ludde@2084: } ludde@2087: } else if (!memcmp(str, "case ", 5)) { tron@3015: char* buf = str + 5; tron@3015: tron@2952: for (;;) { tron@3015: const char* s = ParseWord(&buf); tron@3015: tron@3015: if (s == NULL) break; ludde@2087: if (_numcases >= MAX_NUM_CASES) Fatal("Too many cases, max %d", MAX_NUM_CASES); ludde@2087: ttd_strlcpy(_cases[_numcases], s, sizeof(_cases[_numcases])); ludde@2087: _numcases++; ludde@2087: } truelight@0: } else { ludde@2063: Fatal("unknown pragma '%s'", str); truelight@0: } truelight@0: } truelight@0: tron@3015: static void ExtractCommandString(ParsedCommandStruct* p, const char* s, bool warnings) truelight@0: { truelight@0: char param[100]; ludde@2063: int argno; ludde@2063: int argidx = 0; ludde@2087: int casei; truelight@0: ludde@2063: memset(p, 0, sizeof(*p)); truelight@0: tron@2952: for (;;) { ludde@2063: // read until next command from a. tron@3015: const CmdStruct* ar = ParseCommandString(&s, param, &argno, &casei); tron@3015: tron@3015: if (ar == NULL) break; truelight@0: ludde@2063: // Sanity checking tron@3015: if (argno != -1 && ar->consumes == 0) Fatal("Non consumer param can't have a paramindex"); ludde@2063: ludde@2063: if (ar->consumes) { tron@3015: if (argno != -1) argidx = argno; ludde@2063: if (argidx < 0 || argidx >= lengthof(p->cmd)) Fatal("invalid param idx %d", argidx); ludde@2063: if (p->cmd[argidx] != NULL && p->cmd[argidx] != ar) Fatal("duplicate param idx %d", argidx); ludde@2063: ludde@2063: p->cmd[argidx++] = ar; ludde@2087: } else if (!(ar->flags & C_DONTCOUNT)) { // Ignore some of them ludde@2063: if (p->np >= lengthof(p->pairs)) Fatal("too many commands in string, max %d", lengthof(p->pairs)); ludde@2063: p->pairs[p->np].a = ar; tron@3015: p->pairs[p->np].v = param[0] != '\0' ? strdup(param) : ""; ludde@2063: p->np++; ludde@2063: } ludde@2063: } truelight@0: } truelight@0: ludde@2063: ludde@2063: static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a) tron@2059: { tron@3015: if (a == NULL) return NULL; ludde@2063: tron@3015: if (strcmp(a->cmd, "STRING1") == 0 || tron@3015: strcmp(a->cmd, "STRING2") == 0 || tron@3015: strcmp(a->cmd, "STRING3") == 0 || tron@3015: strcmp(a->cmd, "STRING4") == 0 || tron@3015: strcmp(a->cmd, "STRING5") == 0) { ludde@2063: return FindCmd("STRING", 6); tron@3015: } ludde@2063: tron@3015: if (strcmp(a->cmd, "SKIP") == 0) return NULL; ludde@2063: ludde@2063: return a; ludde@2063: } ludde@2063: ludde@2063: ludde@2069: static bool CheckCommandsMatch(char *a, char *b, const char *name) ludde@2063: { ludde@2063: ParsedCommandStruct templ; ludde@2063: ParsedCommandStruct lang; ludde@2063: int i,j; ludde@2063: bool result = true; ludde@2063: ludde@2063: ExtractCommandString(&templ, b, true); ludde@2063: ExtractCommandString(&lang, a, true); ludde@2063: ludde@2063: // For each string in templ, see if we find it in lang ludde@2063: if (templ.np != lang.np) { ludde@2069: Warning("%s: template string and language string have a different # of commands", name); ludde@2063: result = false; ludde@2063: } ludde@2063: tron@2952: for (i = 0; i < templ.np; i++) { ludde@2063: // see if we find it in lang, and zero it out ludde@2063: bool found = false; tron@2952: for (j = 0; j < lang.np; j++) { ludde@2063: if (templ.pairs[i].a == lang.pairs[j].a && tron@3015: strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) { ludde@2063: // it was found in both. zero it out from lang so we don't find it again ludde@2063: lang.pairs[j].a = NULL; ludde@2063: found = true; ludde@2063: break; ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: if (!found) { ludde@2069: Warning("%s: command '%s' exists in template file but not in language file", name, templ.pairs[i].a->cmd); ludde@2063: result = false; ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: // if we reach here, all non consumer commands match up. ludde@2063: // Check if the non consumer commands match up also. tron@2952: for (i = 0; i < lengthof(templ.cmd); i++) { ludde@2063: if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) { ludde@2069: Warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i, tron@3015: lang.cmd[i] == NULL ? "" : lang.cmd[i]->cmd, tron@3015: templ.cmd[i] == NULL ? "" : templ.cmd[i]->cmd); ludde@2063: result = false; ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: return result; ludde@2063: } ludde@2063: ludde@2063: static void HandleString(char *str, bool master) ludde@2063: { ludde@2063: char *s,*t; ludde@2087: LangString *ent; ludde@2087: char *casep; darkvater@222: truelight@0: if (*str == '#') { tron@3015: if (str[1] == '#' && str[2] != '#') HandlePragma(str + 2); truelight@0: return; truelight@0: } truelight@0: truelight@0: // Ignore comments & blank lines tron@3015: if (*str == ';' || *str == ' ' || *str == '\0') return; truelight@0: truelight@0: s = strchr(str, ':'); truelight@0: if (s == NULL) { ludde@2063: Error("Line has no ':' delimiter"); truelight@0: return; truelight@0: } truelight@0: ludde@2063: // Trim spaces. ludde@2063: // After this str points to the command name, and s points to the command contents tron@3015: for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--); ludde@2063: *t = 0; ludde@2063: s++; truelight@0: ludde@2087: // Check if the string has a case.. ludde@2087: // The syntax for cases is IDENTNAME.case ludde@2087: casep = strchr(str, '.'); ludde@2087: if (casep) *casep++ = 0; ludde@2087: ludde@2063: // Check if this string already exists.. ludde@2063: ent = HashFind(str); truelight@0: truelight@0: if (master) { tron@3015: if (ent != NULL && casep == NULL) { ludde@2063: Error("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: tron@3015: if (ent == NULL && casep != NULL) { ludde@2087: Error("Base string name '%s' doesn't exist yet. Define it before defining a case.", str); truelight@0: return; truelight@0: } truelight@0: ludde@2087: if (ent == NULL) { ludde@2087: if (_strings[_next_string_id]) { ludde@2087: Error("String ID 0x%X for '%s' already in use by '%s'", ent, str, _strings[_next_string_id]->name); ludde@2087: return; ludde@2087: } ludde@2087: ludde@2087: // Allocate a new LangString peter1138@3718: ent = calloc(1, sizeof(*ent)); ludde@2087: _strings[_next_string_id] = ent; ludde@2087: ent->index = _next_string_id++; ludde@2087: ent->name = strdup(str); Darkvater@2566: ent->line = _cur_line; ludde@2087: ludde@2087: HashAdd(str, ent); ludde@2087: } ludde@2087: tron@3015: if (casep != NULL) { tron@3015: Case* c = malloc(sizeof(*c)); tron@3015: ludde@2087: c->caseidx = ResolveCaseName(casep, strlen(casep)); ludde@2087: c->string = strdup(s); ludde@2087: c->next = ent->english_case; ludde@2087: ent->english_case = c; ludde@2087: } else { ludde@2087: ent->english = strdup(s); ludde@2087: } ludde@2087: truelight@0: } else { ludde@2087: if (ent == NULL) { ludde@2069: Warning("String name '%s' does not exist in master file", str); truelight@0: return; truelight@0: } truelight@0: tron@3015: if (ent->translated && casep == NULL) { ludde@2063: Error("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: ludde@2087: if (s[0] == ':' && s[1] == '\0' && casep == NULL) { ludde@2063: // Special syntax :: means we should just inherit the master string ludde@2087: ent->translated = strdup(ent->english); truelight@0: } else { ludde@2087: // make sure that the commands match tron@3015: if (!CheckCommandsMatch(s, ent->english, str)) return; ludde@2087: tron@3015: if (casep != NULL) { tron@3015: Case* c = malloc(sizeof(*c)); tron@3015: ludde@2087: c->caseidx = ResolveCaseName(casep, strlen(casep)); ludde@2087: c->string = strdup(s); ludde@2087: c->next = ent->translated_case; ludde@2087: ent->translated_case = c; ludde@2087: } else { ludde@2087: ent->translated = strdup(s); ludde@2063: } truelight@0: } truelight@0: } truelight@0: } truelight@0: ludde@2063: ludde@2063: static void rstrip(char *buf) truelight@0: { ludde@2063: int i = strlen(buf); tron@3015: while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--; tron@3015: buf[i] = '\0'; truelight@0: } truelight@0: ludde@2063: ludde@2063: static void ParseFile(const char *file, bool english) tron@2059: { ludde@2063: FILE *in; truelight@0: char buf[2048]; truelight@0: tron@2459: _file = file; tron@2459: ludde@2087: // For each new file we parse, reset the genders. ludde@2087: _numgenders = 0; ludde@2087: // TODO:!! We can't reset the cases. In case the translated strings ludde@2087: // derive some strings from english.... ludde@2087: ludde@2087: truelight@0: in = fopen(file, "r"); tron@2459: if (in == NULL) Fatal("Cannot open file"); truelight@0: _cur_line = 1; truelight@0: while (fgets(buf, sizeof(buf),in) != NULL) { ludde@2063: rstrip(buf); ludde@2063: HandleString(buf, english); truelight@0: _cur_line++; truelight@0: } truelight@0: fclose(in); ludde@2063: } tron@2059: tron@2059: ludde@2063: static uint32 MyHashStr(uint32 hash, const char *s) ludde@2063: { tron@3015: for (; *s != '\0'; s++) { tron@3015: hash = ROL(hash, 3) ^ *s; tron@3015: if (hash & 1) hash = (hash >> 1) ^ 0xDEADBEEF; else hash >>= 1; ludde@2063: } ludde@2063: return hash; ludde@2063: } truelight@0: ludde@2063: ludde@2063: // make a hash of the file to get a unique "version number" tron@2765: static void MakeHashOfStrings(void) ludde@2063: { ludde@2063: uint32 hash = 0; tron@3015: uint i; ludde@2063: tron@3015: for (i = 0; i != lengthof(_strings); i++) { tron@3015: const LangString* ls = _strings[i]; tron@3015: tron@3015: if (ls != NULL) { tron@3015: const CmdStruct* cs; tron@3015: const char* s; tron@3015: char buf[256]; tron@3015: int argno; tron@3015: int casei; tron@3015: ludde@2087: s = ls->name; ludde@2063: hash ^= i * 0x717239; tron@3015: if (hash & 1) hash = (hash >> 1) ^ 0xDEADBEEF; else hash >>= 1; ludde@2063: hash = MyHashStr(hash, s + 1); ludde@2063: ludde@2087: s = ls->english; tron@3015: while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != NULL) { tron@3015: if (cs->flags & C_DONTCOUNT) continue; ludde@2087: ludde@2063: hash ^= (cs - _cmd_structs) * 0x1234567; tron@3015: if (hash & 1) hash = (hash >> 1) ^ 0xF00BAA4; else hash >>= 1; darkvater@222: } truelight@0: } truelight@0: } ludde@2063: _hash = hash; truelight@0: } truelight@0: ludde@2063: tron@3015: static uint CountInUse(uint grp) tron@2059: { truelight@0: int i; truelight@0: tron@3015: for (i = 0x800; --i >= 0;) if (_strings[(grp << 11) + i] != NULL) break; truelight@0: return i + 1; truelight@0: } truelight@0: truelight@0: ludde@2063: bool CompareFiles(const char *n1, const char *n2) truelight@0: { truelight@0: FILE *f1, *f2; truelight@0: char b1[4096]; truelight@0: char b2[4096]; truelight@0: size_t l1, l2; truelight@0: truelight@0: f2 = fopen(n2, "rb"); truelight@0: if (f2 == NULL) return false; truelight@0: truelight@0: f1 = fopen(n1, "rb"); ludde@2063: if (f1 == NULL) Fatal("can't open %s", n1); truelight@0: truelight@0: do { truelight@0: l1 = fread(b1, 1, sizeof(b1), f1); truelight@0: l2 = fread(b2, 1, sizeof(b2), f2); truelight@0: ludde@2063: if (l1 != l2 || memcmp(b1, b2, l1)) { truelight@0: fclose(f2); truelight@0: fclose(f1); truelight@0: return false; truelight@0: } truelight@0: } while (l1); truelight@0: truelight@0: fclose(f2); truelight@0: fclose(f1); truelight@0: return true; truelight@0: } truelight@0: ludde@2063: ludde@2063: static void WriteStringsH(const char *filename) truelight@0: { truelight@0: FILE *out; truelight@0: int i; truelight@0: int next = -1; truelight@0: int lastgrp; truelight@0: truelight@0: out = fopen("tmp.xxx", "w"); tron@3015: if (out == NULL) Fatal("can't open tmp.xxx"); truelight@0: truelight@0: fprintf(out, "enum {"); truelight@0: truelight@0: lastgrp = 0; truelight@0: tron@3015: for (i = 0; i != lengthof(_strings); i++) { tron@3015: if (_strings[i] != NULL) { truelight@0: if (lastgrp != (i >> 11)) { truelight@0: lastgrp = (i >> 11); truelight@0: fprintf(out, "};\n\nenum {"); truelight@0: } darkvater@222: tron@3015: fprintf(out, next == i ? "\t%s,\n" : "\n\t%s = 0x%X,\n", _strings[i]->name, i); truelight@0: next = i + 1; truelight@0: } truelight@0: } truelight@0: truelight@0: fprintf(out, "};\n"); truelight@0: darkvater@222: fprintf(out, truelight@0: "\nenum {\n" darkvater@181: "\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n" truelight@0: "\tLANGUAGE_PACK_VERSION = 0x%X,\n" tron@3015: "};\n", (uint)_hash tron@3015: ); ludde@2063: truelight@0: fclose(out); truelight@0: ludde@2063: if (CompareFiles("tmp.xxx", filename)) { truelight@0: // files are equal. tmp.xxx is not needed truelight@0: unlink("tmp.xxx"); truelight@0: } else { truelight@0: // else rename tmp.xxx into filename Darkvater@2482: #if defined(WIN32) || defined(WIN64) truelight@0: unlink(filename); truelight@0: #endif ludde@2063: if (rename("tmp.xxx", filename) == -1) Fatal("rename() failed"); truelight@0: } truelight@0: } truelight@0: ludde@2087: static int TranslateArgumentIdx(int argidx) ludde@2063: { ludde@2063: int i, sum; ludde@2063: ludde@2082: if (argidx < 0 || argidx >= lengthof(_cur_pcs.cmd)) ludde@2063: Fatal("invalid argidx %d", argidx); ludde@2063: tron@2952: for (i = sum = 0; i < argidx; i++) { tron@2374: const CmdStruct *cs = _cur_pcs.cmd[i]; tron@3015: sum += (cs != NULL) ? cs->consumes : 1; ludde@2063: } ludde@2063: ludde@2082: return sum; ludde@2082: } ludde@2082: ludde@2082: static void PutArgidxCommand(void) ludde@2082: { tron@2410: PutByte(0x8C); ludde@2087: PutByte(TranslateArgumentIdx(_cur_argidx)); ludde@2087: } ludde@2087: ludde@2087: ludde@2087: static void PutCommandString(const char *str) ludde@2087: { ludde@2087: const CmdStruct *cs; ludde@2087: char param[256]; ludde@2087: int argno; ludde@2087: int casei; ludde@2087: ludde@2087: _cur_argidx = 0; ludde@2087: ludde@2087: while (*str != '\0') { ludde@2087: // Process characters as they are until we encounter a { ludde@2087: if (*str != '{') { ludde@2087: PutByte(*str++); ludde@2087: continue; ludde@2087: } ludde@2087: cs = ParseCommandString(&str, param, &argno, &casei); ludde@2087: if (cs == NULL) break; ludde@2087: ludde@2087: if (casei != -1) { ludde@2087: PutByte(0x9D); // {SETCASE} ludde@2087: PutByte(casei); ludde@2087: } ludde@2087: ludde@2087: // For params that consume values, we need to handle the argindex properly tron@3015: if (cs->consumes > 0) { ludde@2087: // Check if we need to output a move-param command tron@3015: if (argno != -1 && argno != _cur_argidx) { ludde@2087: _cur_argidx = argno; ludde@2087: PutArgidxCommand(); ludde@2087: } ludde@2087: ludde@2087: // Output the one from the master string... it's always accurate. ludde@2087: cs = _cur_pcs.cmd[_cur_argidx++]; tron@3015: if (cs == NULL) { tron@3015: Fatal("%s: No argument exists at position %d", _cur_ident, _cur_argidx - 1); tron@3015: } ludde@2087: } ludde@2087: ludde@2087: cs->proc(param, cs->value); ludde@2087: } ludde@2087: } ludde@2087: ludde@2087: static void WriteLength(FILE *f, uint length) ludde@2087: { ludde@2087: if (length < 0xC0) { ludde@2087: fputc(length, f); ludde@2087: } else if (length < 0x4000) { ludde@2087: fputc((length >> 8) | 0xC0, f); ludde@2087: fputc(length & 0xFF, f); ludde@2087: } else { ludde@2087: Fatal("string too long"); ludde@2087: } ludde@2063: } ludde@2063: ludde@2063: ludde@2063: static void WriteLangfile(const char *filename, int show_todo) truelight@0: { truelight@0: FILE *f; tron@3015: uint in_use[32]; truelight@0: LanguagePackHeader hdr; tron@3015: uint i; tron@3015: uint j; truelight@0: truelight@0: f = fopen(filename, "wb"); ludde@2063: if (f == NULL) Fatal("can't open %s", filename); truelight@0: truelight@0: memset(&hdr, 0, sizeof(hdr)); tron@2952: for (i = 0; i != 32; i++) { tron@3015: uint n = CountInUse(i); tron@3015: truelight@0: in_use[i] = n; truelight@0: hdr.offsets[i] = TO_LE16(n); truelight@0: } truelight@0: darkvater@222: // see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...) darkvater@181: hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG' truelight@0: hdr.version = TO_LE32(_hash); ludde@2082: hdr.plural_form = _lang_pluralform; truelight@0: strcpy(hdr.name, _lang_name); truelight@0: strcpy(hdr.own_name, _lang_ownname); miham@1376: strcpy(hdr.isocode, _lang_isocode); truelight@0: truelight@0: fwrite(&hdr, sizeof(hdr), 1, f); truelight@0: tron@2952: for (i = 0; i != 32; i++) { tron@2952: for (j = 0; j != in_use[i]; j++) { tron@3015: const LangString* ls = _strings[(i << 11) + j]; tron@3015: const Case* casep; tron@3015: const char* cmdp; truelight@0: ludde@2063: // For undefined strings, just set that it's an empty string ludde@2087: if (ls == NULL) { ludde@2063: WriteLength(f, 0); ludde@2063: continue; ludde@2063: } truelight@0: ludde@2087: _cur_ident = ls->name; Darkvater@2566: _cur_line = ls->line; ludde@2082: ludde@2063: // Produce a message if a string doesn't have a translation. tron@3015: if (show_todo > 0 && ls->translated == NULL) { ludde@2063: if (show_todo == 2) { ludde@2087: Warning("'%s' is untranslated", ls->name); ludde@2063: } else { ludde@2063: const char *s = " "; tron@3015: while (*s != '\0') PutByte(*s++); ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: // Extract the strings and stuff from the english command string ludde@2087: ExtractCommandString(&_cur_pcs, ls->english, false); ludde@2063: tron@3015: if (ls->translated_case != NULL || ls->translated != NULL) { ludde@2087: casep = ls->translated_case; ludde@2087: cmdp = ls->translated; ludde@2087: } else { ludde@2087: casep = ls->english_case; ludde@2087: cmdp = ls->english; ludde@2087: } ludde@2063: Darkvater@2586: _translated = _masterlang || (cmdp != ls->english); Darkvater@2585: tron@3015: if (casep != NULL) { tron@3015: const Case* c; tron@3015: uint num; tron@3015: ludde@2087: // Need to output a case-switch. ludde@2087: // It has this format ludde@2087: // <0x9E> ludde@2087: // Each LEN is printed using 2 bytes in big endian order. ludde@2087: PutByte(0x9E); ludde@2087: // Count the number of cases tron@2952: for (num = 0, c = casep; c; c = c->next) num++; ludde@2087: PutByte(num); ludde@2087: ludde@2087: // Write each case tron@3015: for (c = casep; c != NULL; c = c->next) { ludde@2087: int pos; tron@3015: ludde@2087: PutByte(c->caseidx); ludde@2087: // Make some space for the 16-bit length ludde@2087: pos = _put_pos; ludde@2087: PutByte(0); ludde@2087: PutByte(0); ludde@2087: // Write string ludde@2087: PutCommandString(c->string); ludde@2087: PutByte(0); // terminate with a zero ludde@2087: // Fill in the length tron@3015: _put_buf[pos + 0] = GB(_put_pos - (pos + 2), 8, 8); tron@3015: _put_buf[pos + 1] = GB(_put_pos - (pos + 2), 0, 8); truelight@0: } ludde@2087: } truelight@0: tron@3015: if (cmdp != NULL) PutCommandString(cmdp); truelight@0: ludde@2063: WriteLength(f, _put_pos); ludde@2063: fwrite(_put_buf, 1, _put_pos, f); ludde@2063: _put_pos = 0; truelight@0: } truelight@0: } truelight@0: truelight@0: fputc(0, f); truelight@0: fclose(f); truelight@0: } truelight@0: ludde@2063: darkvater@250: int CDECL main(int argc, char* argv[]) truelight@0: { truelight@0: int show_todo = 0; truelight@0: tron@3015: if (argc > 1 && (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)) { Darkvater@2418: puts("$Revision$"); truelight@0: return 0; truelight@0: } truelight@0: tron@3015: if (argc > 1 && tron@3015: (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--todo") == 0)) { truelight@0: show_todo = 1; ludde@2063: argc--, argv++; truelight@0: } truelight@0: tron@3015: if (argc > 1 && tron@3015: (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--warning") == 0)) { truelight@0: show_todo = 2; ludde@2063: argc--, argv++; truelight@0: } truelight@0: tron@3015: if (argc > 1 && ( tron@3015: strcmp(argv[1], "-h") == 0 || tron@3015: strcmp(argv[1], "--help") == 0 || tron@3015: strcmp(argv[1], "-?") == 0 tron@3015: )) { tron@3015: puts( tron@3015: "strgen - $Revision$\n" tron@3015: " -v | --version print version information and exit\n" tron@3015: " -h | -? | --help print this help message and exit\n" tron@3015: " -t | --todo replace any untranslated strings with ''\n" tron@3015: " -w | --warning print a warning for any untranslated strings\n" tron@3015: " Run without parameters strgen will search for lang/english.txt and\n" tron@3015: " parse it. Passing an argument, strgen will translate that language\n" tron@3015: " file with lang/english.txt as a reference." tron@3015: ); Darkvater@2904: return 0; Darkvater@2904: } Darkvater@2904: truelight@0: if (argc == 1) { Darkvater@2586: _masterlang = true; truelight@0: // parse master file ludde@2063: ParseFile("lang/english.txt", true); ludde@2063: MakeHashOfStrings(); ludde@2063: if (_errors) return 1; truelight@0: truelight@0: // write english.lng and strings.h darkvater@222: ludde@2063: WriteLangfile("lang/english.lng", 0); ludde@2063: WriteStringsH("table/strings.h"); tron@3015: } else if (argc == 2) { tron@3015: char buf[256]; tron@3015: char* r; ludde@2063: Darkvater@2586: _masterlang = false; ludde@2063: ParseFile("lang/english.txt", true); ludde@2063: MakeHashOfStrings(); ludde@2063: ParseFile(argv[1], false); darkvater@222: ludde@2063: if (_errors) return 1; ludde@2063: truelight@0: strcpy(buf, argv[1]); truelight@0: r = strrchr(buf, '.'); tron@3015: if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(buf, 0); truelight@0: strcpy(r, ".lng"); ludde@2063: WriteLangfile(buf, show_todo); truelight@0: } else { truelight@0: fprintf(stderr, "invalid arguments\n"); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: }