tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "../stdafx.h" tron@3015: #include "../macros.h" truelight@4370: #include "../string.h" peter1138@5108: #include "../table/control_codes.h" rubidium@5838: #include "../helpers.hpp" 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 rubidium@5838: #include rubidium@5838: #include truelight@0: #endif truelight@0: truelight@4370: #if defined WIN32 || defined __WATCOMC__ truelight@4370: #include truelight@4370: #endif /* WIN32 || __WATCOMC__ */ truelight@4370: darkvater@181: #ifdef __MORPHOS__ darkvater@181: #ifdef stderr darkvater@181: #undef stderr darkvater@181: #endif darkvater@181: #define stderr stdout truelight@4370: #endif /* __MORPHOS__ */ darkvater@181: 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: bjarni@6298: struct LanguagePackHeader { truelight@0: uint32 ident; rubidium@4344: uint32 version; // 32-bits of auto generated version info which is basically a hash of strings.h rubidium@4344: char name[32]; // the international name of this language rubidium@4434: char own_name[32]; // the localized name of this language rubidium@4344: char isocode[16]; // the ISO code for the language (not country code) rubidium@4344: uint16 offsets[32]; // the offsets rubidium@4344: byte plural_form; // plural form index rubidium@4344: byte pad[3]; // pad header to be a multiple of 4 bjarni@6298: }; truelight@0: bjarni@6298: struct CmdStruct { truelight@0: const char *cmd; truelight@0: ParseCmdProc proc; truelight@0: long value; ludde@2063: int8 consumes; ludde@2087: byte flags; bjarni@6298: }; truelight@0: ludde@2087: enum { ludde@2087: C_DONTCOUNT = 1, rubidium@4344: C_CASE = 2, ludde@2087: }; ludde@2087: ludde@2087: bjarni@6298: struct Case { ludde@2087: int caseidx; ludde@2087: char *string; bjarni@6298: Case *next; bjarni@6298: }; 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: bjarni@6298: struct LangString { rubidium@4344: char *name; // Name of the string rubidium@4344: char *english; // English text rubidium@4344: char *translated; // Translated text rubidium@4344: uint16 hash_next; // next hash entry ludde@2087: uint16 index; rubidium@4344: int line; // line of string in source-file rubidium@4344: Case *english_case; // cases for english rubidium@4344: Case *translated_case; // cases for foreign bjarni@6298: }; 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. rubidium@4344: 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: bjarni@6298: struct CmdPair { ludde@2087: const CmdStruct *a; rubidium@5838: const char *v; bjarni@6298: }; ludde@2087: bjarni@6298: struct ParsedCommandStruct { ludde@2087: int np; ludde@2087: CmdPair pairs[32]; ludde@2087: const CmdStruct *cmd[32]; // ordered by param # bjarni@6298: }; 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: Darkvater@4922: 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@4370: vsnprintf(buf, lengthof(buf), s, va); truelight@0: va_end(va); Darkvater@4447: fprintf(stderr, "%s" LINE_NUM_FMT ": warning: %s\n", _file, _cur_line, buf); ludde@2063: _warnings++; truelight@0: } truelight@0: Darkvater@4922: void CDECL error(const char *s, ...) ludde@2063: { ludde@2063: char buf[1024]; ludde@2063: va_list va; ludde@2063: va_start(va, s); truelight@4370: vsnprintf(buf, lengthof(buf), s, va); ludde@2063: va_end(va); Darkvater@4447: fprintf(stderr, "%s" LINE_NUM_FMT ": error: %s\n", _file, _cur_line, buf); ludde@2063: _errors++; ludde@2063: } ludde@2063: ludde@2063: Darkvater@4922: 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@4370: vsnprintf(buf, lengthof(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: static void PutByte(byte c) tron@2059: { Darkvater@4922: if (_put_pos == lengthof(_put_buf)) fatal("Put buffer too small"); truelight@0: _put_buf[_put_pos++] = c; truelight@0: } truelight@0: truelight@0: peter1138@5108: static void PutUtf8(uint32 value) tron@2059: { peter1138@5108: if (value < 0x80) { peter1138@5108: PutByte(value); peter1138@5108: } else if (value < 0x800) { peter1138@5108: PutByte(0xC0 + GB(value, 6, 5)); peter1138@5108: PutByte(0x80 + GB(value, 0, 6)); peter1138@5108: } else if (value < 0x10000) { peter1138@5108: PutByte(0xE0 + GB(value, 12, 4)); peter1138@5108: PutByte(0x80 + GB(value, 6, 6)); peter1138@5108: PutByte(0x80 + GB(value, 0, 6)); peter1138@5108: } else if (value < 0x110000) { peter1138@5108: PutByte(0xF0 + GB(value, 18, 3)); peter1138@5108: PutByte(0x80 + GB(value, 12, 6)); peter1138@5108: PutByte(0x80 + GB(value, 6, 6)); peter1138@5108: PutByte(0x80 + GB(value, 0, 6)); peter1138@5108: } else { Darkvater@5548: warning("Invalid unicode value U+0x%X", value); peter1138@5108: } truelight@0: } truelight@0: truelight@0: peter1138@5111: size_t Utf8Validate(const char *s) peter1138@5111: { peter1138@5111: uint32 c; peter1138@5111: peter1138@5111: if (!HASBIT(s[0], 7)) { peter1138@5111: /* 1 byte */ peter1138@5111: return 1; peter1138@5111: } else if (GB(s[0], 5, 3) == 6 && IsUtf8Part(s[1])) { peter1138@5111: /* 2 bytes */ peter1138@5111: c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6); peter1138@5111: if (c >= 0x80) return 2; peter1138@5111: } else if (GB(s[0], 4, 4) == 14 && IsUtf8Part(s[1]) && IsUtf8Part(s[2])) { peter1138@5111: /* 3 bytes */ peter1138@5111: c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6); peter1138@5111: if (c >= 0x800) return 3; peter1138@5111: } else if (GB(s[0], 3, 5) == 30 && IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) { peter1138@5111: /* 4 bytes */ peter1138@5111: c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6); peter1138@5111: if (c >= 0x10000 && c <= 0x10FFFF) return 4; peter1138@5111: } peter1138@5111: peter1138@5111: return 0; peter1138@5111: } peter1138@5111: peter1138@5111: peter1138@5108: static void EmitSingleChar(char *buf, int value) tron@2059: { Darkvater@4922: if (*buf != '\0') warning("Ignoring trailing letters in command"); peter1138@5108: PutUtf8(value); ludde@2063: } truelight@0: peter1138@5108: tron@2059: static void EmitSetX(char *buf, int value) tron@2059: { truelight@0: char *err; truelight@0: int x = strtol(buf, &err, 0); Darkvater@4922: if (*err != 0) fatal("SetX param invalid"); peter1138@5108: PutUtf8(SCC_SETX); 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); Darkvater@4922: if (*err != ' ') fatal("SetXY param invalid"); tron@3015: y = strtol(err + 1, &err, 0); Darkvater@4922: if (*err != 0) fatal("SetXY param invalid"); truelight@0: peter1138@5108: PutUtf8(SCC_SETXY); 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@4922: fatal("%s: No plural words", _cur_ident); ludde@2082: tron@2765: if (_plural_form_counts[_lang_pluralform] != nw) { Darkvater@2585: if (_translated) { Darkvater@4922: 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@4922: 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: peter1138@5108: PutUtf8(SCC_PLURAL_LIST); 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++) { Darkvater@4922: 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 peter1138@5108: PutUtf8(SCC_GENDER_INDEX); 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: } Darkvater@4922: if (nw != _numgenders) fatal("Bad # of arguments for gender command"); peter1138@5108: PutUtf8(SCC_GENDER_LIST); 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 peter1138@5108: {"SETX", EmitSetX, SCC_SETX, 0, 0}, peter1138@5108: {"SETXY", EmitSetXY, SCC_SETXY, 0, 0}, truelight@0: truelight@0: // Font size peter1138@5108: {"TINYFONT", EmitSingleChar, SCC_TINYFONT, 0, 0}, peter1138@5108: {"BIGFONT", EmitSingleChar, SCC_BIGFONT, 0, 0}, truelight@0: truelight@0: // Colors peter1138@5108: {"BLUE", EmitSingleChar, SCC_BLUE, 0, 0}, peter1138@5108: {"SILVER", EmitSingleChar, SCC_SILVER, 0, 0}, peter1138@5108: {"GOLD", EmitSingleChar, SCC_GOLD, 0, 0}, peter1138@5108: {"RED", EmitSingleChar, SCC_RED, 0, 0}, peter1138@5108: {"PURPLE", EmitSingleChar, SCC_PURPLE, 0, 0}, peter1138@5108: {"LTBROWN", EmitSingleChar, SCC_LTBROWN, 0, 0}, peter1138@5108: {"ORANGE", EmitSingleChar, SCC_ORANGE, 0, 0}, peter1138@5108: {"GREEN", EmitSingleChar, SCC_GREEN, 0, 0}, peter1138@5108: {"YELLOW", EmitSingleChar, SCC_YELLOW, 0, 0}, peter1138@5108: {"DKGREEN", EmitSingleChar, SCC_DKGREEN, 0, 0}, peter1138@5108: {"CREAM", EmitSingleChar, SCC_CREAM, 0, 0}, peter1138@5108: {"BROWN", EmitSingleChar, SCC_BROWN, 0, 0}, peter1138@5108: {"WHITE", EmitSingleChar, SCC_WHITE, 0, 0}, peter1138@5108: {"LTBLUE", EmitSingleChar, SCC_LTBLUE, 0, 0}, peter1138@5108: {"GRAY", EmitSingleChar, SCC_GRAY, 0, 0}, peter1138@5108: {"DKBLUE", EmitSingleChar, SCC_DKBLUE, 0, 0}, peter1138@5108: {"BLACK", EmitSingleChar, SCC_BLACK, 0, 0}, darkvater@222: peter1138@5108: {"CURRCOMPACT", EmitSingleChar, SCC_CURRENCY_COMPACT, 1, 0}, // compact currency (32 bits) peter1138@5108: {"REV", EmitSingleChar, SCC_REVISION, 0, 0}, // openttd revision string peter1138@5108: {"SHORTCARGO", EmitSingleChar, SCC_CARGO_SHORT, 2, 0}, // short cargo description, only ### tons, or ### litres peter1138@5108: {"CURRCOMPACT64", EmitSingleChar, SCC_CURRENCY_COMPACT_64, 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. peter1138@5108: {"COMPANY", EmitSingleChar, SCC_STRING1, 1, 0}, peter1138@5108: {"PLAYERNAME", EmitSingleChar, SCC_STRING1, 1, 0}, peter1138@5108: {"VEHICLE", EmitSingleChar, SCC_STRING1, 1, 0}, ludde@2063: peter1138@5108: {"STRING1", EmitSingleChar, SCC_STRING1, 1, C_CASE}, // included string that consumes ONE argument peter1138@5108: {"STRING2", EmitSingleChar, SCC_STRING2, 2, C_CASE}, // included string that consumes TWO arguments peter1138@5108: {"STRING3", EmitSingleChar, SCC_STRING3, 3, C_CASE}, // included string that consumes THREE arguments peter1138@5108: {"STRING4", EmitSingleChar, SCC_STRING4, 4, C_CASE}, // included string that consumes FOUR arguments peter1138@5108: {"STRING5", EmitSingleChar, SCC_STRING5, 5, C_CASE}, // included string that consumes FIVE arguments peter1138@5108: peter1138@5108: {"STATIONFEATURES", EmitSingleChar, SCC_STATION_FEATURES, 1, 0}, // station features string, icons of the features peter1138@5108: {"INDUSTRY", EmitSingleChar, SCC_INDUSTRY_NAME, 1, 0}, // industry, takes an industry # peter1138@5108: {"CARGO", EmitSingleChar, SCC_CARGO, 2, 0}, peter1138@5108: {"POWER", EmitSingleChar, SCC_POWER, 1, 0}, peter1138@5108: {"VOLUME", EmitSingleChar, SCC_VOLUME, 1, 0}, peter1138@5108: {"VOLUME_S", EmitSingleChar, SCC_VOLUME_SHORT, 1, 0}, peter1138@5108: {"WEIGHT", EmitSingleChar, SCC_WEIGHT, 1, 0}, peter1138@5108: {"WEIGHT_S", EmitSingleChar, SCC_WEIGHT_SHORT, 1, 0}, peter1138@5108: {"FORCE", EmitSingleChar, SCC_FORCE, 1, 0}, peter1138@5108: {"VELOCITY", EmitSingleChar, SCC_VELOCITY, 1, 0}, ludde@2063: rubidium@4434: {"P", EmitPlural, 0, 0, C_DONTCOUNT}, // plural specifier rubidium@4434: {"G", EmitGender, 0, 0, C_DONTCOUNT}, // gender specifier ludde@2082: peter1138@5108: {"DATE_TINY", EmitSingleChar, SCC_DATE_TINY, 1, 0}, peter1138@5108: {"DATE_SHORT", EmitSingleChar, SCC_DATE_SHORT, 1, 0}, peter1138@5108: {"DATE_LONG", EmitSingleChar, SCC_DATE_LONG, 1, 0}, ludde@2063: peter1138@5108: {"SKIP", EmitSingleChar, SCC_SKIP, 1, 0}, tron@3015: peter1138@5108: {"STRING", EmitSingleChar, SCC_STRING, 1, C_CASE}, ludde@2063: tron@2410: // Numbers peter1138@5108: {"COMMA", EmitSingleChar, SCC_COMMA, 1, 0}, // Number with comma peter1138@5108: {"NUM", EmitSingleChar, SCC_NUM, 1, 0}, // Signed number tron@2410: peter1138@5108: {"CURRENCY", EmitSingleChar, SCC_CURRENCY, 1, 0}, peter1138@5108: {"CURRENCY64", EmitSingleChar, SCC_CURRENCY_64, 2, 0}, tron@2410: peter1138@5108: {"WAYPOINT", EmitSingleChar, SCC_WAYPOINT_NAME, 1, 0}, // waypoint name peter1138@5108: {"STATION", EmitSingleChar, SCC_STATION_NAME, 1, 0}, peter1138@5108: {"TOWN", EmitSingleChar, SCC_TOWN_NAME, 1, 0}, tron@3015: ludde@2087: // 0x9D is used for the pseudo command SETCASE ludde@2087: // 0x9E is used for case switching ludde@2087: peter1138@5108: {"", EmitSingleChar, '\n', 0, C_DONTCOUNT}, peter1138@5108: {"{", EmitSingleChar, '{', 0, C_DONTCOUNT}, peter1138@5108: {"UPARROW", EmitSingleChar, SCC_UPARROW, 0, 0}, peter1138@5108: {"SMALLUPARROW", EmitSingleChar, SCC_SMALLUPARROW, 0, 0}, peter1138@5108: {"SMALLDOWNARROW", EmitSingleChar, SCC_SMALLDOWNARROW, 0, 0}, peter1138@5108: {"TRAIN", EmitSingleChar, SCC_TRAIN, 0, 0}, peter1138@5108: {"LORRY", EmitSingleChar, SCC_LORRY, 0, 0}, peter1138@5108: {"BUS", EmitSingleChar, SCC_BUS, 0, 0}, peter1138@5108: {"PLANE", EmitSingleChar, SCC_PLANE, 0, 0}, peter1138@5108: {"SHIP", EmitSingleChar, SCC_SHIP, 0, 0}, peter1138@5108: {"NBSP", EmitSingleChar, 0xA0, 0, C_DONTCOUNT}, peter1138@5108: {"CENT", EmitSingleChar, 0xA2, 0, C_DONTCOUNT}, peter1138@5108: {"POUNDSIGN", EmitSingleChar, 0xA3, 0, C_DONTCOUNT}, peter1138@5108: {"EURO", EmitSingleChar, 0x20AC, 0, C_DONTCOUNT}, peter1138@5108: {"YENSIGN", EmitSingleChar, 0xA5, 0, C_DONTCOUNT}, peter1138@5108: {"COPYRIGHT", EmitSingleChar, 0xA9, 0, C_DONTCOUNT}, peter1138@5108: {"DOWNARROW", EmitSingleChar, SCC_DOWNARROW, 0, C_DONTCOUNT}, peter1138@5108: {"CHECKMARK", EmitSingleChar, SCC_CHECKMARK, 0, C_DONTCOUNT}, peter1138@5108: {"CROSS", EmitSingleChar, SCC_CROSS, 0, C_DONTCOUNT}, peter1138@5108: {"REGISTERED", EmitSingleChar, 0xAE, 0, C_DONTCOUNT}, peter1138@5108: {"RIGHTARROW", EmitSingleChar, SCC_RIGHTARROW, 0, C_DONTCOUNT}, peter1138@5108: {"SMALLLEFTARROW", EmitSingleChar, SCC_LESSTHAN, 0, C_DONTCOUNT}, peter1138@5108: {"SMALLRIGHTARROW",EmitSingleChar, SCC_GREATERTHAN, 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: } Darkvater@4922: 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); Darkvater@4922: 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) { Darkvater@4922: 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)) Darkvater@4922: 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') { Darkvater@4922: 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') { Darkvater@4922: error("Missing } from command '%s'", start); truelight@0: return NULL; truelight@0: } Darkvater@4922: 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)) Darkvater@4922: 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; Darkvater@4922: 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; Darkvater@4922: 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 { Darkvater@4922: 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 Darkvater@4922: 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; Darkvater@4922: if (argidx < 0 || argidx >= lengthof(p->cmd)) fatal("invalid param idx %d", argidx); Darkvater@4922: 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 Darkvater@4922: 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; KUDr@6308: 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) { Darkvater@4922: 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) { Darkvater@4922: 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])) { Darkvater@4922: 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) { Darkvater@4922: 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: peter1138@5111: /* Check string is valid UTF-8 */ peter1138@5111: { peter1138@5111: const char *tmp; peter1138@5111: for (tmp = s; *tmp != '\0';) { peter1138@5111: size_t len = Utf8Validate(tmp); peter1138@5111: if (len == 0) fatal("Invalid UTF-8 sequence in '%s'", s); peter1138@5111: tmp += len; peter1138@5111: } peter1138@5111: } peter1138@5111: 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) { Darkvater@4922: error("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: tron@3015: if (ent == NULL && casep != NULL) { Darkvater@4922: 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]) { Darkvater@4922: 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 KUDr@5860: ent = CallocT(1); 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) { KUDr@5860: Case* c = MallocT(1); 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) { Darkvater@4922: 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) { Darkvater@4922: 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) { KUDr@5860: Case* c = MallocT(1); 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: Darkvater@5976: /* For each new file we parse, reset the genders, and language codes */ ludde@2087: _numgenders = 0; Darkvater@5976: _lang_name[0] = _lang_ownname[0] = _lang_isocode[0] = '\0'; ludde@2087: // TODO:!! We can't reset the cases. In case the translated strings ludde@2087: // derive some strings from english.... ludde@2087: truelight@0: in = fopen(file, "r"); Darkvater@4922: if (in == NULL) fatal("Cannot open file"); truelight@0: _cur_line = 1; KUDr@6308: 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); Darkvater@5976: Darkvater@5976: if (StrEmpty(_lang_name) || StrEmpty(_lang_ownname) || StrEmpty(_lang_isocode)) { Darkvater@5976: fatal("Language must include ##name, ##ownname and ##isocode"); Darkvater@5976: } 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@4077: hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : 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" bjarni@6298: static void MakeHashOfStrings() 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@4077: hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : 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@4077: hash = (hash & 1 ? hash >> 1 ^ 0xF00BAA4 : 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"); Darkvater@4922: 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"); Darkvater@4922: if (out == NULL) fatal("can't open tmp.xxx"); truelight@0: rubidium@5838: fprintf(out, "enum StringIdEnum {"); truelight@0: truelight@0: lastgrp = 0; truelight@0: tron@3015: for (i = 0; i != lengthof(_strings); i++) { tron@3015: if (_strings[i] != NULL) { 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 Darkvater@4922: 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)) Darkvater@4922: 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: bjarni@6298: static void PutArgidxCommand() ludde@2082: { peter1138@5108: PutUtf8(SCC_ARG_INDEX); 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) { peter1138@5108: PutUtf8(SCC_SETCASE); // {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) { Darkvater@4922: 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 { Darkvater@4922: 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"); Darkvater@4922: 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) { Darkvater@4922: 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. peter1138@5108: PutUtf8(SCC_SWITCH_CASE); 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: truelight@4370: /** Multi-OS mkdirectory function */ truelight@4370: static inline void ottd_mkdir(const char *directory) truelight@4370: { truelight@4370: #if defined(WIN32) || defined(__WATCOMC__) truelight@4370: mkdir(directory); truelight@4370: #else truelight@4370: mkdir(directory, 0755); truelight@4370: #endif truelight@4370: } truelight@4370: truelight@4370: /** Create a path consisting of an already existing path, a possible truelight@4370: * path seperator and the filename. The seperator is only appended if the path truelight@4370: * does not already end with a seperator */ truelight@4370: static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file) truelight@4370: { truelight@4370: char *p; truelight@4370: ttd_strlcpy(buf, path, buflen); // copy directory into buffer truelight@4370: truelight@4370: p = strchr(buf, '\0'); // add path seperator if necessary truelight@4370: if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR; truelight@4370: ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // catenate filename at end of buffer truelight@4370: return buf; truelight@4370: } truelight@4370: rubidium@5754: #if defined(__MINGW32__) truelight@4379: /** truelight@4379: * On MingW, it is common that both / as \ are accepted in the truelight@4379: * params. To go with those flow, we rewrite all incoming / truelight@4379: * simply to \, so internally we can safely assume \. truelight@4379: */ truelight@4379: static inline char *replace_pathsep(char *s) truelight@4379: { truelight@4379: char *c; truelight@4379: truelight@4379: for (c = s; *c != '\0'; c++) if (*c == '/') *c = '\\'; truelight@4379: return s; truelight@4379: } truelight@4379: #else truelight@4379: static inline char *replace_pathsep(char *s) { return s; } truelight@4379: #endif ludde@2063: darkvater@250: int CDECL main(int argc, char* argv[]) truelight@0: { truelight@4370: char pathbuf[256]; glx@4461: const char *src_dir = "."; glx@4461: const char *dest_dir = NULL; truelight@4370: truelight@0: int show_todo = 0; truelight@0: glx@4461: while (argc > 1 && *argv[1] == '-') { glx@4461: if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { glx@4461: puts("$Revision$"); glx@4461: return 0; glx@4461: } truelight@0: glx@4461: if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--todo") == 0) { glx@4461: show_todo = 1; glx@4461: argc--, argv++; glx@4461: continue; glx@4461: } truelight@0: glx@4461: if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--warning") == 0) { glx@4461: show_todo = 2; glx@4461: argc--, argv++; glx@4461: continue; glx@4461: } glx@4461: glx@4461: if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { glx@4461: puts( glx@4461: "strgen - $Revision$\n" glx@4461: " -v | --version print version information and exit\n" glx@4461: " -t | --todo replace any untranslated strings with ''\n" glx@4461: " -w | --warning print a warning for any untranslated strings\n" glx@4461: " -h | -? | --help print this help message and exit\n" glx@4461: " -s | --source_dir search for english.txt in the specified directory\n" glx@4461: " -d | --dest_dir put output file in the specified directory, create if needed\n" glx@4461: " Run without parameters and strgen will search for english.txt and parse it,\n" glx@4461: " creating strings.h. Passing an argument, strgen will translate that language\n" glx@4461: " file using english.txt as a reference and output .lng." glx@4461: ); glx@4461: return 0; glx@4461: } glx@4461: glx@4461: if (argc > 2 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--source_dir") == 0)) { glx@4461: src_dir = replace_pathsep(argv[2]); glx@4461: argc -= 2, argv += 2; glx@4464: continue; glx@4461: } glx@4461: glx@4461: if (argc > 2 && (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--dest_dir") == 0)) { glx@4461: dest_dir = replace_pathsep(argv[2]); glx@4461: argc -= 2, argv += 2; glx@4464: continue; glx@4461: } glx@4464: glx@4464: fprintf(stderr, "Invalid arguments\n"); glx@4464: return 0; truelight@0: } truelight@0: glx@4461: if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir truelight@4370: truelight@4370: /* strgen has two modes of operation. If no (free) arguments are passed truelight@4370: * strgen generates strings.h to the destination directory. If it is supplied truelight@4370: * with a (free) parameter the program will translate that language to destination truelight@4370: * directory. As input english.txt is parsed from the source directory */ truelight@0: if (argc == 1) { truelight@4370: mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt"); truelight@4370: truelight@4370: /* parse master file */ Darkvater@2586: _masterlang = true; truelight@4370: ParseFile(pathbuf, true); ludde@2063: MakeHashOfStrings(); ludde@2063: if (_errors) return 1; truelight@0: truelight@4370: /* write strings.h */ truelight@4370: ottd_mkdir(dest_dir); truelight@4370: mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h"); truelight@4370: WriteStringsH(pathbuf); tron@3015: } else if (argc == 2) { truelight@4370: char *r; ludde@2063: truelight@4370: mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt"); truelight@4370: truelight@4370: /* parse master file and check if target file is correct */ Darkvater@2586: _masterlang = false; truelight@4370: ParseFile(pathbuf, true); ludde@2063: MakeHashOfStrings(); truelight@4379: ParseFile(replace_pathsep(argv[1]), false); // target file ludde@2063: if (_errors) return 1; ludde@2063: truelight@4370: /* get the targetfile, strip any directories and append to destination path */ truelight@4370: r = strrchr(argv[1], PATHSEPCHAR); truelight@4370: mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : argv[1]); truelight@4370: truelight@4370: /* rename the .txt (input-extension) to .lng */ truelight@4370: r = strrchr(pathbuf, '.'); truelight@4370: if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0'); truelight@4370: ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf)); truelight@4370: WriteLangfile(pathbuf, show_todo); Darkvater@5548: Darkvater@5548: /* if showing warnings, print a summary of the language */ Darkvater@5548: if (show_todo == 2) { Darkvater@5693: fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf); Darkvater@5548: } truelight@0: } else { truelight@4370: fprintf(stderr, "Invalid arguments\n"); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: }