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: #define lengthof(x) (sizeof(x)/sizeof(x[0])) 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 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; truelight@0: } CmdStruct; truelight@0: truelight@0: typedef struct LineName { truelight@0: struct LineName *hash_next; truelight@0: int value; truelight@0: char str[1]; darkvater@222: } LineName; truelight@0: truelight@0: int _cur_line; truelight@0: bool _warnings; truelight@0: truelight@0: uint32 _hash; truelight@0: char _lang_name[32], _lang_ownname[32]; truelight@0: truelight@0: #define HASH_SIZE 1023 truelight@0: LineName *_hash_head[HASH_SIZE]; truelight@0: unsigned int hash_str(const char *s) { truelight@0: unsigned int hash = 0; truelight@0: for(;*s;s++) truelight@0: hash = ((hash << 3) | (hash >> 29)) ^ *s; truelight@0: return hash % HASH_SIZE; truelight@0: } truelight@0: void hash_add(const char *s, int value) { truelight@0: unsigned int len = strlen(s); truelight@0: LineName *ln = (LineName*)malloc(sizeof(LineName) + len); truelight@0: unsigned int hash = hash_str(s); truelight@0: ln->hash_next = _hash_head[hash]; truelight@0: _hash_head[hash] = ln; truelight@0: ln->value = value; truelight@0: ln->str[len] = 0; truelight@0: memcpy(ln->str, s, len); truelight@0: } truelight@0: truelight@0: int hash_find(const char *s) { truelight@0: LineName *ln = _hash_head[hash_str(s)]; truelight@0: while (ln != NULL) { truelight@0: if (!strcmp(ln->str, s)) { truelight@0: return ln->value; truelight@0: } truelight@0: ln = ln->hash_next; truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: truelight@0: void warning(const char *s, ...) { 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: ERROR: %s\n", _cur_line, buf); truelight@0: _warnings = true; truelight@0: } truelight@0: truelight@0: void NORETURN error(const char *s, ...) { 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: truelight@0: 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: truelight@0: // first byte tells if it's english.lng or a custom language file truelight@0: // second part is the name of the string truelight@0: // third part is the actual string contents truelight@0: char *allstr[65536]; truelight@0: truelight@0: byte _put_buf[4096]; truelight@0: int _put_pos; truelight@0: int _next_string_id; truelight@0: truelight@0: void put_byte(byte c) { truelight@0: if (_put_pos == lengthof(_put_buf)) truelight@0: error("Put buffer too small"); truelight@0: _put_buf[_put_pos++] = c; truelight@0: } truelight@0: truelight@0: void emit_buf(int ent) { truelight@0: char *s; truelight@0: truelight@0: if (ent < 0 || ent >= 0x10000) { truelight@0: warning("Invalid string ID %d\n", ent); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (allstr[ent] != 0) { truelight@0: warning("Duplicate string ID %d\n", ent); truelight@0: return; truelight@0: } truelight@0: truelight@0: // Allocate the string, and put the uint16 before with the length. truelight@0: s = (char*)malloc(sizeof(uint16) + _put_pos); truelight@0: *((uint16*)s) = _put_pos; truelight@0: memcpy(s + sizeof(uint16), _put_buf, _put_pos); truelight@0: truelight@0: allstr[ent] = s; truelight@0: truelight@0: _put_pos = 0; truelight@0: } truelight@0: truelight@0: void EmitSingleByte(char *buf, int value) { truelight@0: if (*buf != 0) truelight@0: warning("Ignoring trailing letters in command"); truelight@0: put_byte((byte)value); truelight@0: } truelight@0: truelight@0: void EmitEscapedByte(char *buf, int value) { truelight@0: if (*buf != 0) truelight@0: warning("Ignoring trailing letters in command"); truelight@0: put_byte((byte)0x85); truelight@0: put_byte((byte)value); truelight@0: } truelight@0: truelight@0: truelight@0: void EmitStringInl(char *buf, int value) { truelight@0: int id; truelight@0: truelight@0: if (*buf>='0' && *buf <= '9') { truelight@0: id = strtol(buf, NULL, 0); truelight@0: if (id < 0 || id>=0x10000) { truelight@0: warning("Invalid inline num %s\n", buf); truelight@0: return; truelight@0: } truelight@0: } else { truelight@0: id = hash_find(buf); truelight@0: if (id == -1) { truelight@0: warning("Invalid inline string '%s'", buf); truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: put_byte(0x81); truelight@0: put_byte((byte)(id & 0xFF)); truelight@0: put_byte((byte)(id >> 8)); truelight@0: } truelight@0: truelight@0: void EmitSetX(char *buf, int value) { truelight@0: char *err; truelight@0: int x = strtol(buf, &err, 0); truelight@0: if (*err != 0) truelight@0: error("SetX param invalid"); truelight@0: put_byte(1); truelight@0: put_byte((byte)x); truelight@0: } truelight@0: truelight@0: void EmitSetXY(char *buf, int value) { truelight@0: char *err; truelight@0: int x = strtol(buf, &err, 0), y; truelight@0: if (*err != 0) error("SetXY param invalid"); truelight@0: y = strtol(err+1, &err, 0); truelight@0: if (*err != 0) error("SetXY param invalid"); truelight@0: truelight@0: put_byte(0x1F); truelight@0: put_byte((byte)x); truelight@0: put_byte((byte)y); truelight@0: } truelight@0: truelight@0: static const CmdStruct _cmd_structs[] = { truelight@0: // Update position truelight@0: {"SETX", EmitSetX, 1}, truelight@0: {"SETXY", EmitSetXY, 2}, truelight@0: truelight@0: // Font size truelight@0: {"TINYFONT", EmitSingleByte, 8}, truelight@0: {"BIGFONT", EmitSingleByte, 9}, truelight@0: truelight@0: // New line truelight@0: {"", EmitSingleByte, 10}, truelight@0: truelight@0: // Colors darkvater@222: {"BLUE", EmitSingleByte, 15}, truelight@0: {"SILVER", EmitSingleByte, 16}, truelight@0: {"GOLD", EmitSingleByte, 17}, truelight@0: {"RED", EmitSingleByte, 18}, truelight@0: {"PURPLE", EmitSingleByte, 19}, truelight@0: {"LTBROWN", EmitSingleByte, 20}, truelight@0: {"ORANGE", EmitSingleByte, 21}, truelight@0: {"GREEN", EmitSingleByte, 22}, truelight@0: {"YELLOW", EmitSingleByte, 23}, truelight@0: {"DKGREEN", EmitSingleByte, 24}, truelight@0: {"CREAM", EmitSingleByte, 25}, truelight@0: {"BROWN", EmitSingleByte, 26}, truelight@0: {"WHITE", EmitSingleByte, 27}, truelight@0: {"LTBLUE", EmitSingleByte, 28}, truelight@0: {"GRAY", EmitSingleByte, 29}, truelight@0: {"DKBLUE", EmitSingleByte, 30}, truelight@0: {"BLACK", EmitSingleByte, 31}, darkvater@222: truelight@0: // 0x7B=123 is the LAST special character we may use. truelight@0: truelight@0: // Numbers truelight@0: {"COMMA32", EmitSingleByte, 0x7B}, truelight@0: {"COMMA16", EmitSingleByte, 0x7C}, truelight@0: {"COMMA8", EmitSingleByte, 0x7D}, truelight@0: {"NUMU16", EmitSingleByte, 0x7E}, truelight@0: truelight@0: {"CURRENCY", EmitSingleByte, 0x7F}, darkvater@222: darkvater@236: // 0x85 darkvater@236: {"CURRCOMPACT", EmitEscapedByte, 0}, // compact currency (32 bits) darkvater@236: {"INT32", EmitEscapedByte, 1}, // signed 32 bit integer darkvater@236: {"REV", EmitEscapedByte, 2}, // openttd revision string darkvater@236: {"SHORTCARGO", EmitEscapedByte, 3}, // short cargo description, only ### tons, or ### litres darkvater@236: {"CURRCOMPACT64", EmitEscapedByte, 4}, // compact currency 64 bits darkvater@222: truelight@0: {"STRINL", EmitStringInl, 0x81}, truelight@0: truelight@0: {"DATE_LONG", EmitSingleByte, 0x82}, truelight@0: {"DATE_SHORT", EmitSingleByte, 0x83}, truelight@0: truelight@0: {"VELOCITY", EmitSingleByte, 0x84}, truelight@0: {"SKIP16", EmitSingleByte, 0x85}, truelight@0: {"SKIP", EmitSingleByte, 0x86}, truelight@0: {"VOLUME", EmitSingleByte, 0x87}, truelight@0: truelight@0: {"STRING", EmitSingleByte, 0x88}, truelight@0: truelight@0: {"CARGO", EmitSingleByte, 0x99}, truelight@0: {"STATION", EmitSingleByte, 0x9A}, truelight@0: {"TOWN", EmitSingleByte, 0x9B}, truelight@0: {"CURRENCY64", EmitSingleByte, 0x9C}, darkvater@395: {"WAYPOINT", EmitSingleByte, 0x9D}, // waypoint name dominik@1097: {"DATE_TINY", EmitSingleByte, 0x9E}, truelight@0: // 0x9E=158 is the LAST special character we may use. truelight@0: truelight@0: {"UPARROW", EmitSingleByte, 0xA0}, truelight@0: {"POUNDSIGN", EmitSingleByte, 0xA3}, truelight@0: {"YENSIGN", EmitSingleByte, 0xA5}, truelight@0: {"COPYRIGHT", EmitSingleByte, 0xA9}, truelight@0: {"DOWNARROW", EmitSingleByte, 0xAA}, truelight@0: {"CHECKMARK", EmitSingleByte, 0xAC}, truelight@0: {"CROSS", EmitSingleByte, 0xAD}, truelight@0: {"RIGHTARROW", EmitSingleByte, 0xAF}, truelight@0: tron@520: {"TRAIN", EmitSingleByte, 0xb4}, tron@520: {"LORRY", EmitSingleByte, 0xb5}, tron@520: {"BUS", EmitSingleByte, 0xb6}, tron@520: {"PLANE", EmitSingleByte, 0xb7}, tron@520: {"SHIP", EmitSingleByte, 0xb8}, tron@520: truelight@0: {"SMALLUPARROW", EmitSingleByte, 0xBC}, truelight@0: {"SMALLDOWNARROW", EmitSingleByte, 0xBD}, truelight@0: {"THREE_FOURTH", EmitSingleByte, 0xBE}, truelight@0: }; truelight@0: truelight@0: const CmdStruct *find_cmd(const char *s, int len) { truelight@0: int i; truelight@0: const CmdStruct *cs = _cmd_structs; truelight@0: for(i=0; i!=lengthof(_cmd_structs); i++,cs++) { truelight@0: if (!strncmp(cs->cmd, s, len) && cs->cmd[len] == 0) truelight@0: return cs; truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: truelight@0: // returns 0 on EOL truelight@0: // returns 1-255 on literal byte truelight@0: // else returns command struct truelight@0: const CmdStruct *parse_command_string(char **str, char *param, const char *errortext) truelight@0: { truelight@0: char *s = *str, *start; truelight@0: byte c = *s++; truelight@0: const CmdStruct *cmd; truelight@0: truelight@0: if (c != '{') { truelight@0: *str = s; truelight@0: return (CmdStruct*) (int)c; truelight@0: } 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; truelight@0: if (c == 0) { truelight@0: if (errortext) warning("Missing } from command '%s' in '%s'", start, errortext); truelight@0: return NULL; truelight@0: } truelight@0: } truelight@0: truelight@0: cmd = find_cmd(start, s - start - 1); truelight@0: if (cmd == NULL) { truelight@0: if (errortext) warning("Undefined command '%.*s' in '%s'", s - start - 1, start, errortext); 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; truelight@0: if (c == 0) { truelight@0: if (errortext) warning("Missing } from command '%s' in '%s'", start, errortext); truelight@0: return NULL; truelight@0: } truelight@0: *param++ = c; truelight@0: } 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: truelight@0: void handle_pragma(char *str) truelight@0: { truelight@0: if (!memcmp(str, "id ", 3)) { truelight@0: _next_string_id = strtoul(str + 3, NULL, 0); truelight@0: } else if (!memcmp(str, "name ", 5)) { truelight@0: ttd_strlcpy(_lang_name, str + 5, sizeof(_lang_name)); truelight@0: } else if (!memcmp(str, "ownname ", 8)) { truelight@0: ttd_strlcpy(_lang_ownname, str + 8, sizeof(_lang_ownname)); truelight@0: } else { truelight@0: error("unknown pragma '%s'", str); truelight@0: } truelight@0: } truelight@0: truelight@0: bool check_commands_match(char *a, char *b) truelight@0: { truelight@0: const CmdStruct *ar, *br; truelight@0: char param[100]; truelight@0: truelight@0: do { truelight@0: // read until next command from a. truelight@0: do { truelight@0: ar = parse_command_string(&a, param, NULL); truelight@0: } while (ar != NULL && (unsigned long)ar <= 255); truelight@0: truelight@0: // read until next command from b. truelight@0: do { truelight@0: br = parse_command_string(&b, param, NULL); truelight@0: } while (br != NULL && (unsigned long)br <= 255); truelight@0: truelight@0: // make sure they are identical truelight@0: if (ar != br) return false; truelight@0: } while (ar); truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: void handle_string(char *str, bool master) { truelight@0: char *s,*t,*r; truelight@0: int ent; darkvater@222: truelight@0: if (*str == '#') { truelight@0: if (str[1] == '#' && str[2] != '#') truelight@0: handle_pragma(str + 2); truelight@0: return; truelight@0: } truelight@0: truelight@0: // Ignore comments & blank lines truelight@0: if (*str == ';' || *str == ' ' || *str == 0) truelight@0: return; truelight@0: truelight@0: s = strchr(str, ':'); truelight@0: if (s == NULL) { truelight@0: warning("Line has no ':' delimiter"); truelight@0: return; truelight@0: } truelight@0: truelight@0: // Trim spaces truelight@0: for(t=s;t > str && (t[-1]==' ' || t[-1]=='\t'); t--); truelight@0: *t = 0; truelight@0: truelight@0: ent = hash_find(str); truelight@0: truelight@0: s++; // skip : truelight@0: truelight@0: // allocate string entry truelight@0: r = malloc(strlen(str) + strlen(s) + 3); truelight@0: *r = master; truelight@0: strcpy(r + 1, str); truelight@0: strcpy(r + 2 + strlen(str), s); truelight@0: truelight@0: if (master) { truelight@0: if (ent != -1) { truelight@0: warning("String name '%s' is used multiple times", str); truelight@0: return; truelight@0: } truelight@0: truelight@0: ent = _next_string_id++; truelight@0: if (allstr[ent]) { truelight@0: warning("String ID 0x%X for '%s' already in use by '%s'", ent, str, allstr[ent] + 1); truelight@0: return; truelight@0: } truelight@0: allstr[ent] = r; truelight@0: truelight@0: // add to hash table truelight@0: hash_add(str, ent); truelight@0: } else { truelight@0: if (ent == -1) { truelight@0: warning("String name '%s' does not exist in master file", str); truelight@0: _warnings = 0; // non-fatal truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!allstr[ent][0]) { truelight@0: warning("String name '%s' is used multiple times", str); truelight@0: _warnings = 0; // non-fatal truelight@0: return; truelight@0: } truelight@0: truelight@0: // check that the commands match truelight@0: if (!check_commands_match(s, allstr[ent] + 2 + strlen(allstr[ent] + 1))) { truelight@0: fprintf(stderr, "Warning: String name '%s' does not match the layout of the master string\n", str); truelight@0: _warnings = 0; // non-fatal truelight@0: return; truelight@0: } truelight@0: truelight@0: if (s[0] == ':' && s[1] == 0) { truelight@0: allstr[ent][0] = 0; // use string from master file legitiately truelight@0: free(r); truelight@0: } else { truelight@0: free(allstr[ent]); truelight@0: allstr[ent] = r; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: uint32 my_hash_str(uint32 hash, const char *s) truelight@0: { truelight@0: for(;*s;s++) { truelight@0: hash = ((hash << 3) | (hash >> 29)) ^ *s; truelight@0: if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1; truelight@0: } truelight@0: return hash; darkvater@222: truelight@0: } truelight@0: truelight@0: void parse_file(const char *file, bool english) { truelight@0: char buf[2048]; truelight@0: int i; truelight@0: FILE *in; truelight@0: truelight@0: in = fopen(file, "r"); truelight@0: if (in == NULL) { error("Cannot open file '%s'", file); } darkvater@222: truelight@0: _cur_line = 1; truelight@0: while (fgets(buf, sizeof(buf),in) != NULL) { truelight@0: i = strlen(buf); truelight@0: while (i>0 && (buf[i-1]=='\r' || buf[i-1]=='\n' || buf[i-1] == ' ')) i--; truelight@0: buf[i] = 0; truelight@0: truelight@0: handle_string(buf, english); truelight@0: _cur_line++; truelight@0: } truelight@0: truelight@0: fclose(in); truelight@0: truelight@0: // make a hash of the file to get a unique "version number" truelight@0: if (english) { truelight@0: uint32 hash = 0; truelight@0: char *s; truelight@0: const CmdStruct *cs; truelight@0: for(i = 0; i!=65536; i++) { truelight@0: if ((s=allstr[i]) != NULL) { truelight@0: hash ^= i * 0x717239; truelight@0: if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1; truelight@0: hash = my_hash_str(hash, s + 1); truelight@0: truelight@0: s = s + 2 + strlen(s + 1); truelight@0: while ((cs = parse_command_string(&s, buf, NULL)) != 0) { truelight@0: if ( (uint)cs >= 256) { truelight@0: hash ^= (cs - _cmd_structs) * 0x1234567; truelight@0: if (hash & 1) hash = (hash>>1) ^ 0xF00BAA4; else hash >>= 1; truelight@0: } truelight@0: } darkvater@222: } truelight@0: } truelight@0: _hash = hash; truelight@0: } truelight@0: } truelight@0: truelight@0: int count_inuse(int grp) { truelight@0: int i; truelight@0: truelight@0: for(i=0x800; --i >= 0;) { truelight@0: if (allstr[(grp<<11)+i] != NULL) truelight@0: break; truelight@0: } truelight@0: return i + 1; truelight@0: } truelight@0: truelight@0: void check_all_used() { truelight@0: int i; truelight@0: LineName *ln; truelight@0: int num_warn = 10; truelight@0: truelight@0: for (i=0; i!=HASH_SIZE; i++) { truelight@0: for(ln = _hash_head[i]; ln!=NULL; ln = ln->hash_next) { truelight@0: if (allstr[ln->value] == 0) { truelight@0: if (++num_warn < 50) { truelight@0: warning("String %s has no definition. Using NULL value", ln->str); truelight@0: } truelight@0: _put_pos = 0; truelight@0: emit_buf(ln->value); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void write_length(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 { truelight@0: error("string too long"); truelight@0: } truelight@0: } truelight@0: truelight@0: void gen_output(FILE *f) { truelight@0: uint16 in_use[32]; truelight@0: uint16 in_use_file[32]; truelight@0: int i,j; truelight@0: int tot_str = 0; truelight@0: truelight@0: check_all_used(); truelight@0: truelight@0: for(i=0; i!=32; i++) { truelight@0: int n = count_inuse(i); truelight@0: in_use[i] = n; truelight@0: in_use_file[i] = TO_LE16(n); truelight@0: tot_str += n; truelight@0: } truelight@0: truelight@0: fwrite(in_use_file, 32*sizeof(uint16), 1, f); truelight@0: truelight@0: for(i=0; i!=32; i++) { truelight@0: for(j=0; j!=in_use[i]; j++) { truelight@0: char *s = allstr[(i<<11)+j]; truelight@0: if (s == NULL) error("Internal error, s==NULL"); darkvater@222: truelight@0: write_length(f, *(uint16*)s); truelight@0: fwrite(s + sizeof(uint16), *(uint16*)s , 1, f); truelight@0: tot_str--; truelight@0: } truelight@0: } truelight@0: truelight@0: fputc(0, f); // write trailing nul character. truelight@0: truelight@0: if (tot_str != 0) { truelight@0: error("Internal error, tot_str != 0"); truelight@0: } truelight@0: } truelight@0: truelight@0: bool compare_files(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"); truelight@0: if (f1 == NULL) error("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: truelight@0: 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: truelight@0: void write_strings_h(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"); truelight@0: if (out == NULL) { error("can't open tmp.xxx"); } truelight@0: truelight@0: fprintf(out, "enum {"); truelight@0: truelight@0: lastgrp = 0; truelight@0: truelight@0: for(i = 0; i!=65536; i++) { truelight@0: if (allstr[i]) { truelight@0: if (lastgrp != (i >> 11)) { truelight@0: lastgrp = (i >> 11); truelight@0: fprintf(out, "};\n\nenum {"); truelight@0: } darkvater@222: truelight@0: fprintf(out, next == i ? "%s,\n" : "\n%s = 0x%X,\n", allstr[i] + 1, 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" truelight@0: "};\n", (unsigned int)_hash); truelight@0: truelight@0: truelight@0: fclose(out); truelight@0: truelight@0: if (compare_files("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 truelight@0: if (rename("tmp.xxx", filename) == -1) error("rename() failed"); truelight@0: } truelight@0: } truelight@0: truelight@0: void write_langfile(const char *filename, int show_todo) truelight@0: { truelight@0: FILE *f; truelight@0: int in_use[32]; truelight@0: LanguagePackHeader hdr; truelight@0: int i,j; truelight@0: const CmdStruct *cs; truelight@0: char param[128]; truelight@0: truelight@0: f = fopen(filename, "wb"); truelight@0: if (f == NULL) error("can't open %s", filename); truelight@0: truelight@0: memset(&hdr, 0, sizeof(hdr)); truelight@0: for(i=0; i!=32; i++) { truelight@0: int n = count_inuse(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); truelight@0: truelight@0: fwrite(&hdr, sizeof(hdr), 1, f); truelight@0: truelight@0: for(i=0; i!=32; i++) { truelight@0: for(j=0; j!=in_use[i]; j++) { truelight@0: char *s = allstr[(i<<11)+j], *str; truelight@0: truelight@0: if (s == NULL) { truelight@0: write_length(f, 0); truelight@0: } else { truelight@0: // move to string truelight@0: str = s + 2 + strlen(s + 1); truelight@0: truelight@0: if (show_todo && s[0]) { truelight@0: if (show_todo == 2) { truelight@0: fprintf(stderr, "Warning:%s: String '%s' is untranslated\n", filename, s + 1); truelight@0: } else { darkvater@222: const char *s = " "; truelight@0: while(*s) put_byte(*s++); truelight@0: } truelight@0: } truelight@0: truelight@0: for(;;) { truelight@0: cs = parse_command_string(&str, param, s[0] ? "english.lng" : filename); truelight@0: if (cs == NULL) break; truelight@0: if ( (unsigned long) cs <= 255) { truelight@0: put_byte( (byte) (int)cs); truelight@0: } else { truelight@0: cs->proc(param, cs->value); truelight@0: } truelight@0: } truelight@0: truelight@0: write_length(f, _put_pos); truelight@0: fwrite(_put_buf, 1, _put_pos, f); truelight@0: _put_pos = 0; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: fputc(0, f); truelight@0: truelight@0: fclose(f); truelight@0: } truelight@0: darkvater@250: int CDECL main(int argc, char* argv[]) truelight@0: { truelight@0: char *r; truelight@0: char buf[256]; truelight@0: int show_todo = 0; truelight@0: truelight@0: if (argc > 1 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) { tron@520: puts("$Revision$"); truelight@0: return 0; truelight@0: } truelight@0: truelight@0: if (argc > 1 && !strcmp(argv[1], "-t")) { truelight@0: show_todo = 1; truelight@0: argc--, argv++; truelight@0: } truelight@0: truelight@0: if (argc > 1 && !strcmp(argv[1], "-w")) { truelight@0: show_todo = 2; truelight@0: argc--, argv++; truelight@0: } truelight@0: truelight@0: truelight@0: if (argc == 1) { truelight@0: // parse master file truelight@0: parse_file("lang/english.txt", true); truelight@0: if (_warnings) return 1; truelight@0: truelight@0: // write english.lng and strings.h darkvater@222: truelight@0: write_langfile("lang/english.lng", 0); truelight@0: write_strings_h("table/strings.h"); darkvater@222: truelight@0: } else if (argc == 2) { truelight@0: parse_file("lang/english.txt", true); truelight@0: parse_file(argv[1], false); truelight@0: if (_warnings) return 1; truelight@0: strcpy(buf, argv[1]); truelight@0: r = strrchr(buf, '.'); truelight@0: if (!r || strcmp(r, ".txt")) r = strchr(buf, 0); truelight@0: strcpy(r, ".lng"); truelight@0: write_langfile(buf, show_todo); truelight@0: } else { truelight@0: fprintf(stderr, "invalid arguments\n"); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: