truelight@1721: #define STRGEN truelight@1721: truelight@0: #include "../stdafx.h" truelight@0: #include truelight@0: #include truelight@0: #include truelight@0: #include truelight@0: truelight@0: #if !defined(WIN32) || 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: 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: truelight@0: typedef struct { 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 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; truelight@0: } CmdStruct; truelight@0: tron@2059: static int _cur_line; ludde@2063: static int _errors, _warnings; ludde@2063: ludde@2063: static char *_strname[65536]; // Name of the string, NULL if not exists ludde@2063: static char *_master[65536]; // English text ludde@2063: static char *_translated[65536]; // Translated text ludde@2063: static uint16 _hash_next[65536]; // next hash entry 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]; truelight@0: tron@2059: tron@2059: static uint HashStr(const char *s) tron@2059: { tron@2059: uint hash = 0; ludde@2063: for(; *s; s++) ludde@2063: hash = ((hash << 3) | (hash >> 29)) ^ *s; truelight@0: return hash % HASH_SIZE; truelight@0: } tron@2059: tron@2059: static void HashAdd(const char *s, int value) tron@2059: { tron@2059: uint hash = HashStr(s); ludde@2063: _hash_next[value] = _hash_head[hash]; ludde@2063: _hash_head[hash] = value + 1; truelight@0: } truelight@0: tron@2059: static int HashFind(const char *s) tron@2059: { ludde@2063: int idx = _hash_head[HashStr(s)]; ludde@2063: while (--idx >= 0) { ludde@2063: if (!strcmp(_strname[idx], s)) return idx; ludde@2063: idx = _hash_next[idx]; truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: 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); ludde@2063: fprintf(stderr, "%d: Warning: %s\n", _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); ludde@2063: fprintf(stderr, "%d: Error: %s\n", _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); truelight@0: fprintf(stderr, "%d: FATAL: %s\n", _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); truelight@0: while (--len && *src) truelight@0: *dst++=*src++; truelight@0: *dst = 0; truelight@0: } truelight@0: truelight@0: ludde@2063: static void PutByte(byte c) tron@2059: { truelight@0: if (_put_pos == lengthof(_put_buf)) ludde@2063: 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: { ludde@2063: if (*buf != '\0') ludde@2063: 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: { ludde@2063: if (*buf != '\0') ludde@2063: Warning("Ignoring trailing letters in command"); ludde@2063: PutByte((byte)0x85); ludde@2063: PutByte((byte)value); ludde@2063: } truelight@0: 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); ludde@2063: if (*err != 0) ludde@2063: 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; ludde@2063: int x,y; tron@2059: tron@2059: x = strtol(buf, &err, 0); ludde@2063: if (*err != 0) Fatal("SetXY param invalid"); ludde@2063: y = strtol(err+1, &err, 0); ludde@2063: if (*err != 0) Fatal("SetXY param invalid"); truelight@0: ludde@2063: PutByte(0x1F); ludde@2063: PutByte((byte)x); ludde@2063: PutByte((byte)y); truelight@0: } truelight@0: ludde@2063: truelight@0: static const CmdStruct _cmd_structs[] = { truelight@0: // Update position ludde@2063: {"SETX", EmitSetX, 1, 0}, ludde@2063: {"SETXY", EmitSetXY, 2, 0}, truelight@0: truelight@0: // Font size ludde@2063: {"TINYFONT", EmitSingleByte, 8, 0}, ludde@2063: {"BIGFONT", EmitSingleByte, 9, 0}, truelight@0: truelight@0: // New line ludde@2063: {"", EmitSingleByte, 10, 0}, truelight@0: truelight@0: // Colors ludde@2063: {"BLUE", EmitSingleByte, 15, 0}, ludde@2063: {"SILVER", EmitSingleByte, 16, 0}, ludde@2063: {"GOLD", EmitSingleByte, 17, 0}, ludde@2063: {"RED", EmitSingleByte, 18, 0}, ludde@2063: {"PURPLE", EmitSingleByte, 19, 0}, ludde@2063: {"LTBROWN", EmitSingleByte, 20, 0}, ludde@2063: {"ORANGE", EmitSingleByte, 21, 0}, ludde@2063: {"GREEN", EmitSingleByte, 22, 0}, ludde@2063: {"YELLOW", EmitSingleByte, 23, 0}, ludde@2063: {"DKGREEN", EmitSingleByte, 24, 0}, ludde@2063: {"CREAM", EmitSingleByte, 25, 0}, ludde@2063: {"BROWN", EmitSingleByte, 26, 0}, ludde@2063: {"WHITE", EmitSingleByte, 27, 0}, ludde@2063: {"LTBLUE", EmitSingleByte, 28, 0}, ludde@2063: {"GRAY", EmitSingleByte, 29, 0}, ludde@2063: {"DKBLUE", EmitSingleByte, 30, 0}, ludde@2063: {"BLACK", EmitSingleByte, 31, 0}, darkvater@222: truelight@0: // 0x7B=123 is the LAST special character we may use. truelight@0: truelight@0: // Numbers ludde@2063: {"COMMA", EmitSingleByte, 0x7B, 1}, // Number with comma ludde@2063: {"NUM", EmitSingleByte, 0x7E, 1}, // Signed number ludde@2052: ludde@2063: {"CURRENCY", EmitSingleByte, 0x7F, 1}, darkvater@222: darkvater@236: // 0x85 ludde@2063: {"CURRCOMPACT", EmitEscapedByte, 0, 1}, // compact currency (32 bits) ludde@2063: {"REV", EmitEscapedByte, 2, 0}, // openttd revision string ludde@2063: {"SHORTCARGO", EmitEscapedByte, 3, 2}, // short cargo description, only ### tons, or ### litres ludde@2063: {"CURRCOMPACT64", EmitEscapedByte, 4, 2}, // compact currency 64 bits truelight@0: ludde@2063: {"COMPANY", EmitEscapedByte, 5, 1}, // company string. This is actually a {STRING1} ludde@2063: // The first string includes the second string. truelight@0: ludde@2063: {"PLAYERNAME", EmitEscapedByte, 5, 1}, // playername string. This is actually a {STRING1} ludde@2063: // The first string includes the second string. truelight@0: ludde@2063: {"VEHICLE", EmitEscapedByte, 5, 1}, // playername string. This is actually a {STRING1} ludde@2063: // The first string includes the second string. ludde@2063: ludde@2063: ludde@2063: {"STRING1", EmitEscapedByte, 5, 1}, // included string that consumes ONE argument ludde@2063: {"STRING2", EmitEscapedByte, 6, 2}, // included string that consumes TWO arguments ludde@2063: {"STRING3", EmitEscapedByte, 7, 3}, // included string that consumes THREE arguments ludde@2063: {"STRING4", EmitEscapedByte, 8, 4}, // included string that consumes FOUR arguments ludde@2063: {"STRING5", EmitEscapedByte, 9, 5}, // included string that consumes FIVE arguments ludde@2063: ludde@2063: {"STATIONFEATURES", EmitEscapedByte, 10, 1}, // station features string, icons of the features ludde@2063: ludde@2063: {"DATE_LONG", EmitSingleByte, 0x82, 1}, ludde@2063: {"DATE_SHORT", EmitSingleByte, 0x83, 1}, ludde@2063: ludde@2063: {"VELOCITY", EmitSingleByte, 0x84, 1}, ludde@2063: ludde@2063: {"SKIP", EmitSingleByte, 0x86, 1}, ludde@2063: {"VOLUME", EmitSingleByte, 0x87, 1}, ludde@2063: ludde@2063: {"STRING", EmitSingleByte, 0x88, 1}, ludde@2063: ludde@2063: {"CARGO", EmitSingleByte, 0x99, 2}, ludde@2063: {"STATION", EmitSingleByte, 0x9A, 1}, ludde@2063: {"TOWN", EmitSingleByte, 0x9B, 1}, ludde@2063: {"CURRENCY64", EmitSingleByte, 0x9C, 2}, ludde@2063: {"WAYPOINT", EmitSingleByte, 0x9D, 1}, // waypoint name ludde@2063: {"DATE_TINY", EmitSingleByte, 0x9E, 1}, truelight@0: // 0x9E=158 is the LAST special character we may use. truelight@0: ludde@2063: {"UPARROW", EmitSingleByte, 0xA0, 0}, ludde@2063: {"POUNDSIGN", EmitSingleByte, 0xA3, 0}, ludde@2063: {"YENSIGN", EmitSingleByte, 0xA5, 0}, ludde@2063: {"COPYRIGHT", EmitSingleByte, 0xA9, 0}, ludde@2063: {"DOWNARROW", EmitSingleByte, 0xAA, 0}, ludde@2063: {"CHECKMARK", EmitSingleByte, 0xAC, 0}, ludde@2063: {"CROSS", EmitSingleByte, 0xAD, 0}, ludde@2063: {"RIGHTARROW", EmitSingleByte, 0xAF, 0}, truelight@0: ludde@2063: {"TRAIN", EmitSingleByte, 0xb4, 0}, ludde@2063: {"LORRY", EmitSingleByte, 0xb5, 0}, ludde@2063: {"BUS", EmitSingleByte, 0xb6, 0}, ludde@2063: {"PLANE", EmitSingleByte, 0xb7, 0}, ludde@2063: {"SHIP", EmitSingleByte, 0xb8, 0}, tron@520: ludde@2063: {"SMALLUPARROW", EmitSingleByte, 0xBC, 0}, ludde@2063: {"SMALLDOWNARROW", EmitSingleByte, 0xBD, 0}, ludde@2063: {"THREE_FOURTH", EmitSingleByte, 0xBE, 0}, truelight@0: }; truelight@0: ludde@2063: tron@2059: static const CmdStruct *FindCmd(const char *s, int len) tron@2059: { ludde@2063: int i; ludde@2063: const CmdStruct *cs = _cmd_structs; ludde@2063: for(i=0; i != lengthof(_cmd_structs); i++, cs++) { ludde@2063: if (!strncmp(cs->cmd, s, len) && cs->cmd[len] == '\0') ludde@2063: return cs; truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: ludde@2063: // returns NULL on eof ludde@2063: // else returns command struct ludde@2063: static const CmdStruct *ParseCommandString(char **str, char *param, int *argno, bool show_err) truelight@0: { ludde@2063: char *s = *str, *start; ludde@2063: const CmdStruct *cmd; ludde@2063: int plen = 0; tron@2060: byte c; truelight@0: ludde@2063: *argno = -1; ludde@2063: ludde@2063: // Scan to the next command, exit if there's no next command. ludde@2063: for(; *s != '{'; s++) { ludde@2063: if (*s == '\0') ludde@2063: return NULL; ludde@2063: } ludde@2063: s++; // Skip past the { ludde@2063: ludde@2063: if (*s >= '0' && *s <= '9') { ludde@2063: char *end; ludde@2063: *argno = strtoul(s, &end, 0); ludde@2063: if (*end != ':') { ludde@2063: Fatal("missing arg #"); ludde@2063: } ludde@2063: s = end + 1; ludde@2063: } truelight@0: truelight@0: // parse command name truelight@0: start = s; truelight@0: for(;;) { truelight@0: c = *s++; truelight@0: if (c == '}' || c == ' ') truelight@0: break; tron@2059: if (c == '\0') { ludde@2063: Error("Missing } from command '%s'", start); truelight@0: return NULL; truelight@0: } truelight@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: truelight@0: if (c == ' ') { truelight@0: // copy params truelight@0: start = s; truelight@0: 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: } ludde@2063: if ( s - start == 60) ludde@2063: Fatal("param command too long"); truelight@0: *param++ = c; truelight@0: } truelight@0: } truelight@0: *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)); truelight@0: } else { ludde@2063: Fatal("unknown pragma '%s'", str); truelight@0: } truelight@0: } truelight@0: ludde@2063: typedef struct CmdPair { ludde@2063: const CmdStruct *a; ludde@2063: char *v; ludde@2063: } CmdPair; ludde@2063: ludde@2063: typedef struct ParsedCommandStruct { ludde@2063: int np; ludde@2063: CmdPair pairs[32]; ludde@2063: const CmdStruct *cmd[32]; // ordered by param # ludde@2063: } ParsedCommandStruct; ludde@2063: ludde@2063: ludde@2063: static void ExtractCommandString(ParsedCommandStruct *p, char *s, bool warnings) truelight@0: { tron@2059: const CmdStruct *ar; truelight@0: char param[100]; ludde@2063: int argno; ludde@2063: int argidx = 0; truelight@0: ludde@2063: memset(p, 0, sizeof(*p)); truelight@0: ludde@2063: for(;;) { ludde@2063: // read until next command from a. ludde@2063: ar = ParseCommandString(&s, param, &argno, warnings); ludde@2063: if (ar == NULL) ludde@2063: break; truelight@0: ludde@2063: // Sanity checking ludde@2063: if (argno != -1 && !ar->consumes) Fatal("Non consumer param can't have a paramindex"); ludde@2063: ludde@2063: if (ar->consumes) { ludde@2063: if (argno != -1) ludde@2063: 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@2063: } else if (ar->cmd[0] != '\0') { // Ignore {}.. it can appear in any order. 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; ludde@2063: p->pairs[p->np].v = param[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: { ludde@2063: if (!a) return NULL; ludde@2063: ludde@2063: if (!strcmp(a->cmd, "STRING1") || ludde@2063: !strcmp(a->cmd, "STRING2") || ludde@2063: !strcmp(a->cmd, "STRING3") || ludde@2063: !strcmp(a->cmd, "STRING4") || ludde@2063: !strcmp(a->cmd, "STRING5")) ludde@2063: return FindCmd("STRING", 6); ludde@2063: ludde@2063: if (!strcmp(a->cmd, "SKIP")) ludde@2063: return NULL; ludde@2063: ludde@2063: return a; ludde@2063: } ludde@2063: ludde@2063: ludde@2063: static bool CheckCommandsMatch(char *a, char *b) 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@2063: Error("template string and language string have a different # of commands"); ludde@2063: result = false; ludde@2063: } ludde@2063: ludde@2063: 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; ludde@2063: for(j = 0; j < lang.np; j++) { ludde@2063: if (templ.pairs[i].a == lang.pairs[j].a && ludde@2063: !strcmp(templ.pairs[i].v, lang.pairs[j].v)) { 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@2063: Error("Command '%s' exists in template file but not in language file", 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. ludde@2063: for(i = 0; i < lengthof(templ.cmd); i++) { ludde@2063: if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) { ludde@2063: Error("Param idx #%d '%s' doesn't match with template command '%s'", i, ludde@2063: !lang.cmd[i] ? "" : lang.cmd[i]->cmd, ludde@2063: !templ.cmd[i] ? "" : templ.cmd[i]->cmd); ludde@2063: result = false; ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: return result; ludde@2063: } ludde@2063: ludde@2063: ludde@2063: static void HandleString(char *str, bool master) ludde@2063: { ludde@2063: char *s,*t; truelight@0: int ent; darkvater@222: truelight@0: if (*str == '#') { truelight@0: if (str[1] == '#' && str[2] != '#') ludde@2063: HandlePragma(str + 2); truelight@0: return; truelight@0: } truelight@0: truelight@0: // Ignore comments & blank lines tron@2059: if (*str == ';' || *str == ' ' || *str == '\0') truelight@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 ludde@2063: for(t = s; t > str && (t[-1]==' ' || t[-1]=='\t'); t--); ludde@2063: *t = 0; ludde@2063: s++; truelight@0: ludde@2063: // Check if this string already exists.. ludde@2063: ent = HashFind(str); truelight@0: truelight@0: if (master) { truelight@0: if (ent != -1) { ludde@2063: Error("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: truelight@0: ent = _next_string_id++; ludde@2063: if (_strname[ent]) { ludde@2063: Error("String ID 0x%X for '%s' already in use by '%s'", ent, str, _strname[ent]); truelight@0: return; truelight@0: } ludde@2063: _strname[ent] = strdup(str); ludde@2063: _master[ent] = strdup(s); truelight@0: truelight@0: // add to hash table tron@2059: HashAdd(str, ent); truelight@0: } else { truelight@0: if (ent == -1) { ludde@2063: Error("String name '%s' does not exist in master file", str); truelight@0: return; truelight@0: } truelight@0: ludde@2063: if (_translated[ent]) { ludde@2063: Error("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: tron@2059: if (s[0] == ':' && s[1] == '\0') { ludde@2063: // Special syntax :: means we should just inherit the master string ludde@2063: _translated[ent] = strdup(_master[ent]); truelight@0: } else { ludde@2063: // check that the commands match ludde@2063: if (!CheckCommandsMatch(s, _master[ent])) { ludde@2063: Error("String name '%s' does not match the layout of the master string\n", str); ludde@2063: return; ludde@2063: } ludde@2063: _translated[ent] = strdup(s); 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); ludde@2063: while (i>0 && (buf[i-1]=='\r' || buf[i-1]=='\n' || buf[i-1] == ' ')) i--; ludde@2063: 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: truelight@0: in = fopen(file, "r"); ludde@2063: if (in == NULL) { Fatal("Cannot open file '%s'", 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: { ludde@2063: for(; *s; s++) { ludde@2063: hash = ((hash << 3) | (hash >> 29)) ^ *s; ludde@2063: 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" ludde@2063: static void MakeHashOfStrings() ludde@2063: { ludde@2063: uint32 hash = 0; ludde@2063: char *s; ludde@2063: const CmdStruct *cs; ludde@2063: char buf[128]; ludde@2063: int i; ludde@2063: int argno; ludde@2063: ludde@2063: for(i = 0; i != 65536; i++) { ludde@2063: if ((s=_strname[i]) != NULL) { ludde@2063: hash ^= i * 0x717239; ludde@2063: if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1; ludde@2063: hash = MyHashStr(hash, s + 1); ludde@2063: ludde@2063: s = _master[i]; ludde@2063: while ((cs = ParseCommandString(&s, buf, &argno, false)) != NULL) { ludde@2063: hash ^= (cs - _cmd_structs) * 0x1234567; ludde@2063: 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: ludde@2063: static int CountInUse(int grp) tron@2059: { truelight@0: int i; truelight@0: ludde@2063: for(i = 0x800; --i >= 0;) { ludde@2063: if (_strname[(grp<<11)+i] != NULL) ludde@2063: break; truelight@0: } truelight@0: return i + 1; truelight@0: } truelight@0: truelight@0: ludde@2063: static void WriteLength(FILE *f, uint length) truelight@0: { truelight@0: if (length < 0xC0) { truelight@0: fputc(length, f); truelight@0: } else if (length < 0x4000) { truelight@0: fputc((length >> 8) | 0xC0, f); truelight@0: fputc(length & 0xFF, f); truelight@0: } else { ludde@2063: Fatal("string too long"); truelight@0: } 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"); ludde@2063: if (out == NULL) { Fatal("can't open tmp.xxx"); } truelight@0: truelight@0: fprintf(out, "enum {"); truelight@0: truelight@0: lastgrp = 0; truelight@0: ludde@2063: for(i = 0; i != 65536; i++) { ludde@2063: if (_strname[i]) { truelight@0: if (lastgrp != (i >> 11)) { truelight@0: lastgrp = (i >> 11); truelight@0: fprintf(out, "};\n\nenum {"); truelight@0: } darkvater@222: ludde@2063: fprintf(out, next == i ? "%s,\n" : "\n%s = 0x%X,\n", _strname[i], 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@2059: "};\n", (uint)_hash); truelight@0: 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 truelight@0: #if defined(WIN32) 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@2063: ludde@2063: static void PutArgidxCommand(ParsedCommandStruct *pcs, int argidx) ludde@2063: { ludde@2063: int i, sum; ludde@2063: ludde@2063: if (argidx >= lengthof(pcs->cmd)) ludde@2063: Fatal("invalid argidx %d", argidx); ludde@2063: ludde@2063: for(i = sum = 0; i < argidx; i++) { ludde@2063: const CmdStruct *cs = pcs->cmd[i++]; ludde@2063: sum += cs ? cs->consumes : 1; ludde@2063: } ludde@2063: ludde@2063: PutByte(0x7C); ludde@2063: PutByte((byte)sum); ludde@2063: } ludde@2063: ludde@2063: ludde@2063: static void WriteLangfile(const char *filename, int show_todo) truelight@0: { truelight@0: FILE *f; truelight@0: int in_use[32]; truelight@0: LanguagePackHeader hdr; ludde@2063: int i,j; truelight@0: const CmdStruct *cs; truelight@0: char param[128]; ludde@2063: int argno; ludde@2063: ParsedCommandStruct pcs; 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)); ludde@2063: for(i = 0; i != 32; i++) { ludde@2063: int n = CountInUse(i); 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); 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: ludde@2063: for(i = 0; i != 32; i++) { ludde@2063: for(j = 0; j != in_use[i]; j++) { ludde@2063: int idx = (i<<11)+j; ludde@2063: char *str; ludde@2063: int argidx; truelight@0: ludde@2063: // For undefined strings, just set that it's an empty string ludde@2063: if (_strname[idx] == NULL) { ludde@2063: WriteLength(f, 0); ludde@2063: continue; ludde@2063: } truelight@0: ludde@2063: // Produce a message if a string doesn't have a translation. ludde@2063: if (show_todo && _translated[idx] == NULL) { ludde@2063: if (show_todo == 2) { ludde@2063: Warning("'%s' is untranslated", _strname[idx]); ludde@2063: } else { ludde@2063: const char *s = " "; ludde@2063: while(*s) PutByte(*s++); ludde@2063: } ludde@2063: } ludde@2063: ludde@2063: // Extract the strings and stuff from the english command string ludde@2063: ExtractCommandString(&pcs, _master[idx], false); ludde@2063: ludde@2063: str = _translated[idx] ? _translated[idx] : _master[idx]; ludde@2063: argidx = 0; ludde@2063: ludde@2063: while (*str != '\0') { ludde@2063: // Process characters as they are until we encounter a { ludde@2063: if (*str != '{') { ludde@2063: PutByte(*str++); ludde@2063: continue; ludde@2063: } ludde@2063: cs = ParseCommandString(&str, param, &argno, false); ludde@2063: if (cs == NULL) break; ludde@2063: ludde@2063: // For params that consume values, we need to handle the argindex properly ludde@2063: if (cs->consumes) { ludde@2063: // Check if we need to output a move-param command ludde@2063: if (argno!=-1 && argno != argidx) { ludde@2063: argidx = argno; ludde@2063: PutArgidxCommand(&pcs, argidx); truelight@0: } ludde@2063: ludde@2063: // Output the one from the master string... it's always accurate. ludde@2063: cs = pcs.cmd[argidx++]; ludde@2063: if (!cs) ludde@2063: Fatal("cs == NULL"); truelight@0: } truelight@0: ludde@2063: cs->proc(param, cs->value); ludde@2063: } 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: truelight@0: fclose(f); truelight@0: } truelight@0: ludde@2063: darkvater@250: int CDECL main(int argc, char* argv[]) truelight@0: { ludde@2063: char *r; ludde@2063: char buf[256]; truelight@0: int show_todo = 0; truelight@0: ludde@2063: if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) { tron@520: puts("$Revision$"); truelight@0: return 0; truelight@0: } truelight@0: ludde@2063: if (argc > 1 && !strcmp(argv[1], "-t")) { truelight@0: show_todo = 1; ludde@2063: argc--, argv++; truelight@0: } truelight@0: ludde@2063: if (argc > 1 && !strcmp(argv[1], "-w")) { truelight@0: show_todo = 2; ludde@2063: argc--, argv++; truelight@0: } truelight@0: truelight@0: truelight@0: if (argc == 1) { 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"); ludde@2063: tron@2059: } else if (argc == 2) { 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, '.'); ludde@2063: if (!r || strcmp(r, ".txt")) 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: } ludde@2063: