strgen.cpp

Go to the documentation of this file.
00001 /* $Id: strgen.cpp 15718 2009-03-15 00:32:18Z rubidium $ */
00002 
00005 #include "../stdafx.h"
00006 #include "../core/alloc_func.hpp"
00007 #include "../core/endian_func.hpp"
00008 #include "../string_func.h"
00009 #include "../strings_type.h"
00010 #include "strgen.h"
00011 #include "../table/control_codes.h"
00012 
00013 #include <stdio.h>
00014 #include <string.h>
00015 #include <stdlib.h>
00016 #include <stdarg.h>
00017 
00018 #if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
00019 #include <unistd.h>
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #endif
00023 
00024 #if defined WIN32 || defined __WATCOMC__
00025 #include <direct.h>
00026 #endif /* WIN32 || __WATCOMC__ */
00027 
00028 #ifdef __MORPHOS__
00029 #ifdef stderr
00030 #undef stderr
00031 #endif
00032 #define stderr stdout
00033 #endif /* __MORPHOS__ */
00034 
00035 /* Compiles a list of strings into a compiled string list */
00036 
00037 typedef void (*ParseCmdProc)(char *buf, int value);
00038 
00039 struct CmdStruct {
00040   const char *cmd;
00041   ParseCmdProc proc;
00042   long value;
00043   int8 consumes;
00044   byte flags;
00045 };
00046 
00047 enum {
00048   C_DONTCOUNT = 1,
00049   C_CASE      = 2,
00050 };
00051 
00052 
00053 struct Case {
00054   int caseidx;
00055   char *string;
00056   Case *next;
00057 };
00058 
00059 static bool _masterlang;
00060 static bool _translated;
00061 static const char *_file = "(unknown file)";
00062 static int _cur_line;
00063 static int _errors, _warnings, _show_todo;
00064 
00065 struct LangString {
00066   char *name;            // Name of the string
00067   char *english;         // English text
00068   char *translated;      // Translated text
00069   uint16 hash_next;      // next hash entry
00070   uint16 index;
00071   int line;              // line of string in source-file
00072   Case *english_case;    // cases for english
00073   Case *translated_case; // cases for foreign
00074 };
00075 
00076 static LangString *_strings[65536];
00077 
00078 
00079 #define HASH_SIZE 32767
00080 static uint16 _hash_head[HASH_SIZE];
00081 
00082 static byte _put_buf[4096];
00083 static int _put_pos;
00084 static int _next_string_id;
00085 
00086 static uint32 _hash;
00087 static char _lang_name[32], _lang_ownname[32], _lang_isocode[16];
00088 static byte _lang_pluralform;
00089 static byte _lang_textdir;
00090 static uint16 _lang_winlangid;
00091 static uint8 _lang_newgrflangid;
00092 #define MAX_NUM_GENDER 8
00093 static char _genders[MAX_NUM_GENDER][16];
00094 static uint _numgenders;
00095 
00096 /* contains the name of all cases. */
00097 #define MAX_NUM_CASES 50
00098 static char _cases[MAX_NUM_CASES][16];
00099 static uint _numcases;
00100 
00101 /* for each plural value, this is the number of plural forms. */
00102 static const byte _plural_form_counts[] = { 2, 1, 2, 3, 3, 3, 3, 3, 4, 2 };
00103 
00104 static const char *_cur_ident;
00105 
00106 struct CmdPair {
00107   const CmdStruct *a;
00108   const char *v;
00109 };
00110 
00111 struct ParsedCommandStruct {
00112   uint np;
00113   CmdPair pairs[32];
00114   const CmdStruct *cmd[32]; // ordered by param #
00115 };
00116 
00117 /* Used when generating some advanced commands. */
00118 static ParsedCommandStruct _cur_pcs;
00119 static int _cur_argidx;
00120 
00121 static uint HashStr(const char *s)
00122 {
00123   uint hash = 0;
00124   for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s;
00125   return hash % HASH_SIZE;
00126 }
00127 
00128 static void HashAdd(const char *s, LangString *ls)
00129 {
00130   uint hash = HashStr(s);
00131   ls->hash_next = _hash_head[hash];
00132   _hash_head[hash] = ls->index + 1;
00133 }
00134 
00135 static LangString *HashFind(const char *s)
00136 {
00137   int idx = _hash_head[HashStr(s)];
00138 
00139   while (--idx >= 0) {
00140     LangString *ls = _strings[idx];
00141 
00142     if (strcmp(ls->name, s) == 0) return ls;
00143     idx = ls->hash_next;
00144   }
00145   return NULL;
00146 }
00147 
00148 #ifdef _MSC_VER
00149 # define LINE_NUM_FMT "(%d)"
00150 #else
00151 # define LINE_NUM_FMT ":%d"
00152 #endif
00153 
00154 static void CDECL strgen_warning(const char *s, ...)
00155 {
00156   char buf[1024];
00157   va_list va;
00158   va_start(va, s);
00159   vsnprintf(buf, lengthof(buf), s, va);
00160   va_end(va);
00161   fprintf(stderr, "%s" LINE_NUM_FMT ": warning: %s\n", _file, _cur_line, buf);
00162   _warnings++;
00163 }
00164 
00165 static void CDECL strgen_error(const char *s, ...)
00166 {
00167   char buf[1024];
00168   va_list va;
00169   va_start(va, s);
00170   vsnprintf(buf, lengthof(buf), s, va);
00171   va_end(va);
00172   fprintf(stderr, "%s" LINE_NUM_FMT ": error: %s\n", _file, _cur_line, buf);
00173   _errors++;
00174 }
00175 
00176 void NORETURN CDECL error(const char *s, ...)
00177 {
00178   char buf[1024];
00179   va_list va;
00180   va_start(va, s);
00181   vsnprintf(buf, lengthof(buf), s, va);
00182   va_end(va);
00183   fprintf(stderr, "%s" LINE_NUM_FMT ": FATAL: %s\n", _file, _cur_line, buf);
00184   exit(1);
00185 }
00186 
00187 static void PutByte(byte c)
00188 {
00189   if (_put_pos == lengthof(_put_buf)) error("Put buffer too small");
00190   _put_buf[_put_pos++] = c;
00191 }
00192 
00193 
00194 static void PutUtf8(uint32 value)
00195 {
00196   if (value < 0x80) {
00197     PutByte(value);
00198   } else if (value < 0x800) {
00199     PutByte(0xC0 + GB(value,  6, 5));
00200     PutByte(0x80 + GB(value,  0, 6));
00201   } else if (value < 0x10000) {
00202     PutByte(0xE0 + GB(value, 12, 4));
00203     PutByte(0x80 + GB(value,  6, 6));
00204     PutByte(0x80 + GB(value,  0, 6));
00205   } else if (value < 0x110000) {
00206     PutByte(0xF0 + GB(value, 18, 3));
00207     PutByte(0x80 + GB(value, 12, 6));
00208     PutByte(0x80 + GB(value,  6, 6));
00209     PutByte(0x80 + GB(value,  0, 6));
00210   } else {
00211     strgen_warning("Invalid unicode value U+0x%X", value);
00212   }
00213 }
00214 
00215 
00216 size_t Utf8Validate(const char *s)
00217 {
00218   uint32 c;
00219 
00220   if (!HasBit(s[0], 7)) {
00221     /* 1 byte */
00222     return 1;
00223   } else if (GB(s[0], 5, 3) == 6 && IsUtf8Part(s[1])) {
00224     /* 2 bytes */
00225     c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00226     if (c >= 0x80) return 2;
00227   } else if (GB(s[0], 4, 4) == 14 && IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00228     /* 3 bytes */
00229     c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00230     if (c >= 0x800) return 3;
00231   } else if (GB(s[0], 3, 5) == 30 && IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00232     /* 4 bytes */
00233     c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00234     if (c >= 0x10000 && c <= 0x10FFFF) return 4;
00235   }
00236 
00237   return 0;
00238 }
00239 
00240 
00241 static void EmitSingleChar(char *buf, int value)
00242 {
00243   if (*buf != '\0') strgen_warning("Ignoring trailing letters in command");
00244   PutUtf8(value);
00245 }
00246 
00247 
00248 static void EmitSetX(char *buf, int value)
00249 {
00250   char *err;
00251   int x = strtol(buf, &err, 0);
00252   if (*err != 0) error("SetX param invalid");
00253   PutUtf8(SCC_SETX);
00254   PutByte((byte)x);
00255 }
00256 
00257 
00258 static void EmitSetXY(char *buf, int value)
00259 {
00260   char *err;
00261   int x;
00262   int y;
00263 
00264   x = strtol(buf, &err, 0);
00265   if (*err != ' ') error("SetXY param invalid");
00266   y = strtol(err + 1, &err, 0);
00267   if (*err != 0) error("SetXY param invalid");
00268 
00269   PutUtf8(SCC_SETXY);
00270   PutByte((byte)x);
00271   PutByte((byte)y);
00272 }
00273 
00274 /* The plural specifier looks like
00275  * {NUM} {PLURAL -1 passenger passengers} then it picks either passenger/passengers depending on the count in NUM */
00276 
00277 /* This is encoded like
00278  *  CommandByte <ARG#> <NUM> {Length of each string} {each string} */
00279 
00280 bool ParseRelNum(char **buf, int *value)
00281 {
00282   const char *s = *buf;
00283   char *end;
00284   bool rel = false;
00285   int v;
00286 
00287   while (*s == ' ' || *s == '\t') s++;
00288   if (*s == '+') {
00289     rel = true;
00290     s++;
00291   }
00292   v = strtol(s, &end, 0);
00293   if (end == s) return false;
00294   if (rel || v < 0) {
00295     *value += v;
00296   } else {
00297     *value = v;
00298   }
00299   *buf = end;
00300   return true;
00301 }
00302 
00303 /* Parse out the next word, or NULL */
00304 char *ParseWord(char **buf)
00305 {
00306   char *s = *buf, *r;
00307 
00308   while (*s == ' ' || *s == '\t') s++;
00309   if (*s == '\0') return NULL;
00310 
00311   if (*s == '"') {
00312     r = ++s;
00313     /* parse until next " or NUL */
00314     for (;;) {
00315       if (*s == '\0') break;
00316       if (*s == '"') {
00317         *s++ = '\0';
00318         break;
00319       }
00320       s++;
00321     }
00322   } else {
00323     /* proceed until whitespace or NUL */
00324     r = s;
00325     for (;;) {
00326       if (*s == '\0') break;
00327       if (*s == ' ' || *s == '\t') {
00328         *s++ = '\0';
00329         break;
00330       }
00331       s++;
00332     }
00333   }
00334   *buf = s;
00335   return r;
00336 }
00337 
00338 /* Forward declaration */
00339 static int TranslateArgumentIdx(int arg);
00340 
00341 static void EmitWordList(const char * const *words, uint nw)
00342 {
00343   uint i;
00344   uint j;
00345 
00346   PutByte(nw);
00347   for (i = 0; i < nw; i++) PutByte(strlen(words[i]));
00348   for (i = 0; i < nw; i++) {
00349     for (j = 0; words[i][j] != '\0'; j++) PutByte(words[i][j]);
00350   }
00351 }
00352 
00353 static void EmitPlural(char *buf, int value)
00354 {
00355   int argidx = _cur_argidx;
00356   const char *words[5];
00357   int nw = 0;
00358 
00359   /* Parse out the number, if one exists. Otherwise default to prev arg. */
00360   if (!ParseRelNum(&buf, &argidx)) argidx--;
00361 
00362   /* Parse each string */
00363   for (nw = 0; nw < 5; nw++) {
00364     words[nw] = ParseWord(&buf);
00365     if (words[nw] == NULL) break;
00366   }
00367 
00368   if (nw == 0)
00369     error("%s: No plural words", _cur_ident);
00370 
00371   if (_plural_form_counts[_lang_pluralform] != nw) {
00372     if (_translated) {
00373       error("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident,
00374         _plural_form_counts[_lang_pluralform], nw);
00375     } else {
00376       if ((_show_todo & 2) != 0) strgen_warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
00377       if (nw > _plural_form_counts[_lang_pluralform]) {
00378         nw = _plural_form_counts[_lang_pluralform];
00379       } else {
00380         for (; nw < _plural_form_counts[_lang_pluralform]; nw++) {
00381           words[nw] = words[nw - 1];
00382         }
00383       }
00384     }
00385   }
00386 
00387   PutUtf8(SCC_PLURAL_LIST);
00388   PutByte(TranslateArgumentIdx(argidx));
00389   EmitWordList(words, nw);
00390 }
00391 
00392 
00393 static void EmitGender(char *buf, int value)
00394 {
00395   int argidx = _cur_argidx;
00396   uint nw;
00397 
00398   if (buf[0] == '=') {
00399     buf++;
00400 
00401     /* This is a {G=DER} command */
00402     for (nw = 0; ; nw++) {
00403       if (nw >= 8) error("G argument '%s' invalid", buf);
00404       if (strcmp(buf, _genders[nw]) == 0) break;
00405     }
00406     /* now nw contains the gender index */
00407     PutUtf8(SCC_GENDER_INDEX);
00408     PutByte(nw);
00409   } else {
00410     const char *words[8];
00411 
00412     /* This is a {G 0 foo bar two} command.
00413      * If no relative number exists, default to +0 */
00414     if (!ParseRelNum(&buf, &argidx)) {}
00415 
00416     for (nw = 0; nw < 8; nw++) {
00417       words[nw] = ParseWord(&buf);
00418       if (words[nw] == NULL) break;
00419     }
00420     if (nw != _numgenders) error("Bad # of arguments for gender command");
00421     PutUtf8(SCC_GENDER_LIST);
00422     PutByte(TranslateArgumentIdx(argidx));
00423     EmitWordList(words, nw);
00424   }
00425 }
00426 
00427 
00428 static const CmdStruct _cmd_structs[] = {
00429   /* Update position */
00430   {"SETX",  EmitSetX,  SCC_SETX,  0, 0},
00431   {"SETXY", EmitSetXY, SCC_SETXY, 0, 0},
00432 
00433   /* Font size */
00434   {"TINYFONT", EmitSingleChar, SCC_TINYFONT, 0, 0},
00435   {"BIGFONT",  EmitSingleChar, SCC_BIGFONT,  0, 0},
00436 
00437   /* Colors */
00438   {"BLUE",    EmitSingleChar, SCC_BLUE,    0, 0},
00439   {"SILVER",  EmitSingleChar, SCC_SILVER,  0, 0},
00440   {"GOLD",    EmitSingleChar, SCC_GOLD,    0, 0},
00441   {"RED",     EmitSingleChar, SCC_RED,     0, 0},
00442   {"PURPLE",  EmitSingleChar, SCC_PURPLE,  0, 0},
00443   {"LTBROWN", EmitSingleChar, SCC_LTBROWN, 0, 0},
00444   {"ORANGE",  EmitSingleChar, SCC_ORANGE,  0, 0},
00445   {"GREEN",   EmitSingleChar, SCC_GREEN,   0, 0},
00446   {"YELLOW",  EmitSingleChar, SCC_YELLOW,  0, 0},
00447   {"DKGREEN", EmitSingleChar, SCC_DKGREEN, 0, 0},
00448   {"CREAM",   EmitSingleChar, SCC_CREAM,   0, 0},
00449   {"BROWN",   EmitSingleChar, SCC_BROWN,   0, 0},
00450   {"WHITE",   EmitSingleChar, SCC_WHITE,   0, 0},
00451   {"LTBLUE",  EmitSingleChar, SCC_LTBLUE,  0, 0},
00452   {"GRAY",    EmitSingleChar, SCC_GRAY,    0, 0},
00453   {"DKBLUE",  EmitSingleChar, SCC_DKBLUE,  0, 0},
00454   {"BLACK",   EmitSingleChar, SCC_BLACK,   0, 0},
00455 
00456   {"CURRCOMPACT",   EmitSingleChar, SCC_CURRENCY_COMPACT,    1, 0}, // compact currency
00457   {"REV",           EmitSingleChar, SCC_REVISION,            0, 0}, // openttd revision string
00458   {"SHORTCARGO",    EmitSingleChar, SCC_CARGO_SHORT,         2, 0}, // short cargo description, only ### tons, or ### litres
00459 
00460   {"STRING1", EmitSingleChar, SCC_STRING1, 2, C_CASE}, // included string that consumes the string id and ONE argument
00461   {"STRING2", EmitSingleChar, SCC_STRING2, 3, C_CASE}, // included string that consumes the string id and TWO arguments
00462   {"STRING3", EmitSingleChar, SCC_STRING3, 4, C_CASE}, // included string that consumes the string id and THREE arguments
00463   {"STRING4", EmitSingleChar, SCC_STRING4, 5, C_CASE}, // included string that consumes the string id and FOUR arguments
00464   {"STRING5", EmitSingleChar, SCC_STRING5, 6, C_CASE}, // included string that consumes the string id and FIVE arguments
00465 
00466   {"STATIONFEATURES", EmitSingleChar, SCC_STATION_FEATURES, 1, 0}, // station features string, icons of the features
00467   {"INDUSTRY",        EmitSingleChar, SCC_INDUSTRY_NAME,    1, 0}, // industry, takes an industry #
00468   {"CARGO",           EmitSingleChar, SCC_CARGO,            2, 0},
00469   {"POWER",           EmitSingleChar, SCC_POWER,            1, 0},
00470   {"VOLUME",          EmitSingleChar, SCC_VOLUME,           1, 0},
00471   {"VOLUME_S",        EmitSingleChar, SCC_VOLUME_SHORT,     1, 0},
00472   {"WEIGHT",          EmitSingleChar, SCC_WEIGHT,           1, 0},
00473   {"WEIGHT_S",        EmitSingleChar, SCC_WEIGHT_SHORT,     1, 0},
00474   {"FORCE",           EmitSingleChar, SCC_FORCE,            1, 0},
00475   {"VELOCITY",        EmitSingleChar, SCC_VELOCITY,         1, 0},
00476 
00477   {"P", EmitPlural, 0, 0, C_DONTCOUNT}, // plural specifier
00478   {"G", EmitGender, 0, 0, C_DONTCOUNT}, // gender specifier
00479 
00480   {"DATE_TINY",  EmitSingleChar, SCC_DATE_TINY, 1, 0},
00481   {"DATE_SHORT", EmitSingleChar, SCC_DATE_SHORT, 1, 0},
00482   {"DATE_LONG",  EmitSingleChar, SCC_DATE_LONG, 1, 0},
00483   {"DATE_ISO",   EmitSingleChar, SCC_DATE_ISO, 1, 0},
00484 
00485   {"SKIP", EmitSingleChar, SCC_SKIP, 1, 0},
00486 
00487   {"STRING", EmitSingleChar, SCC_STRING, 1, C_CASE},
00488   {"RAW_STRING", EmitSingleChar, SCC_RAW_STRING_POINTER, 1, 0},
00489 
00490   /* Numbers */
00491   {"COMMA", EmitSingleChar, SCC_COMMA, 1, 0}, // Number with comma
00492   {"NUM",   EmitSingleChar, SCC_NUM,   1, 0}, // Signed number
00493   {"BYTES", EmitSingleChar, SCC_BYTES, 1, 0}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB"
00494 
00495   {"CURRENCY",   EmitSingleChar, SCC_CURRENCY,    1, 0},
00496 
00497   {"WAYPOINT", EmitSingleChar, SCC_WAYPOINT_NAME, 1, 0}, // waypoint name
00498   {"STATION",  EmitSingleChar, SCC_STATION_NAME,  1, 0},
00499   {"TOWN",     EmitSingleChar, SCC_TOWN_NAME,     1, 0},
00500   {"GROUP",    EmitSingleChar, SCC_GROUP_NAME,    1, 0},
00501   {"SIGN",     EmitSingleChar, SCC_SIGN_NAME,     1, 0},
00502   {"ENGINE",   EmitSingleChar, SCC_ENGINE_NAME,   1, 0},
00503   {"VEHICLE",  EmitSingleChar, SCC_VEHICLE_NAME,  1, 0},
00504   {"COMPANY",  EmitSingleChar, SCC_COMPANY_NAME,  1, 0},
00505   {"COMPANYNUM", EmitSingleChar, SCC_COMPANY_NUM, 1, 0},
00506   {"PRESIDENTNAME", EmitSingleChar, SCC_PRESIDENT_NAME, 1, 0},
00507 
00508   // 0x9D is used for the pseudo command SETCASE
00509   // 0x9E is used for case switching
00510 
00511   {"",               EmitSingleChar, '\n',               0, C_DONTCOUNT},
00512   {"{",              EmitSingleChar, '{',                0, C_DONTCOUNT},
00513   {"UPARROW",        EmitSingleChar, SCC_UPARROW,        0, 0},
00514   {"SMALLUPARROW",   EmitSingleChar, SCC_SMALLUPARROW,   0, 0},
00515   {"SMALLDOWNARROW", EmitSingleChar, SCC_SMALLDOWNARROW, 0, 0},
00516   {"TRAIN",          EmitSingleChar, SCC_TRAIN,          0, 0},
00517   {"LORRY",          EmitSingleChar, SCC_LORRY,          0, 0},
00518   {"BUS",            EmitSingleChar, SCC_BUS,            0, 0},
00519   {"PLANE",          EmitSingleChar, SCC_PLANE,          0, 0},
00520   {"SHIP",           EmitSingleChar, SCC_SHIP,           0, 0},
00521   {"NBSP",           EmitSingleChar, 0xA0,               0, C_DONTCOUNT},
00522   {"CENT",           EmitSingleChar, 0xA2,               0, C_DONTCOUNT},
00523   {"POUNDSIGN",      EmitSingleChar, 0xA3,               0, C_DONTCOUNT},
00524   {"EURO",           EmitSingleChar, 0x20AC,             0, C_DONTCOUNT},
00525   {"YENSIGN",        EmitSingleChar, 0xA5,               0, C_DONTCOUNT},
00526   {"COPYRIGHT",      EmitSingleChar, 0xA9,               0, C_DONTCOUNT},
00527   {"DOWNARROW",      EmitSingleChar, SCC_DOWNARROW,      0, C_DONTCOUNT},
00528   {"CHECKMARK",      EmitSingleChar, SCC_CHECKMARK,      0, C_DONTCOUNT},
00529   {"CROSS",          EmitSingleChar, SCC_CROSS,          0, C_DONTCOUNT},
00530   {"REGISTERED",     EmitSingleChar, 0xAE,               0, C_DONTCOUNT},
00531   {"RIGHTARROW",     EmitSingleChar, SCC_RIGHTARROW,     0, C_DONTCOUNT},
00532   {"SMALLLEFTARROW", EmitSingleChar, SCC_LESSTHAN,       0, C_DONTCOUNT},
00533   {"SMALLRIGHTARROW",EmitSingleChar, SCC_GREATERTHAN,    0, C_DONTCOUNT},
00534 
00535   /* The following are directional formatting codes used to get the RTL strings right:
00536    * http://www.unicode.org/unicode/reports/tr9/#Directional_Formatting_Codes */
00537   {"LRM",            EmitSingleChar, 0x200E,             0, C_DONTCOUNT},
00538   {"RLM",            EmitSingleChar, 0x200F,             0, C_DONTCOUNT},
00539   {"LRE",            EmitSingleChar, 0x202A,             0, C_DONTCOUNT},
00540   {"RLE",            EmitSingleChar, 0x202B,             0, C_DONTCOUNT},
00541   {"LRO",            EmitSingleChar, 0x202D,             0, C_DONTCOUNT},
00542   {"RLO",            EmitSingleChar, 0x202E,             0, C_DONTCOUNT},
00543   {"PDF",            EmitSingleChar, 0x202C,             0, C_DONTCOUNT},
00544 };
00545 
00546 
00547 static const CmdStruct *FindCmd(const char *s, int len)
00548 {
00549   const CmdStruct *cs;
00550 
00551   for (cs = _cmd_structs; cs != endof(_cmd_structs); cs++) {
00552     if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs;
00553   }
00554   return NULL;
00555 }
00556 
00557 static uint ResolveCaseName(const char *str, uint len)
00558 {
00559   uint i;
00560 
00561   for (i = 0; i < MAX_NUM_CASES; i++) {
00562     if (memcmp(_cases[i], str, len) == 0 && _cases[i][len] == 0) return i + 1;
00563   }
00564   error("Invalid case-name '%s'", str);
00565 }
00566 
00567 
00568 /* returns NULL on eof
00569  * else returns command struct */
00570 static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei)
00571 {
00572   const char *s = *str, *start;
00573   const CmdStruct *cmd;
00574   byte c;
00575 
00576   *argno = -1;
00577   *casei = -1;
00578 
00579   /* Scan to the next command, exit if there's no next command. */
00580   for (; *s != '{'; s++) {
00581     if (*s == '\0') return NULL;
00582   }
00583   s++; // Skip past the {
00584 
00585   if (*s >= '0' && *s <= '9') {
00586     char *end;
00587 
00588     *argno = strtoul(s, &end, 0);
00589     if (*end != ':') error("missing arg #");
00590     s = end + 1;
00591   }
00592 
00593   /* parse command name */
00594   start = s;
00595   do {
00596     c = *s++;
00597   } while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0);
00598 
00599   cmd = FindCmd(start, s - start - 1);
00600   if (cmd == NULL) {
00601     strgen_error("Undefined command '%.*s'", s - start - 1, start);
00602     return NULL;
00603   }
00604 
00605   if (c == '.') {
00606     const char *casep = s;
00607 
00608     if (!(cmd->flags & C_CASE))
00609       error("Command '%s' can't have a case", cmd->cmd);
00610 
00611     do c = *s++; while (c != '}' && c != ' ' && c != '\0');
00612     *casei = ResolveCaseName(casep, s - casep - 1);
00613   }
00614 
00615   if (c == '\0') {
00616     strgen_error("Missing } from command '%s'", start);
00617     return NULL;
00618   }
00619 
00620 
00621   if (c != '}') {
00622     if (c == '=') s--;
00623     /* copy params */
00624     start = s;
00625     for (;;) {
00626       c = *s++;
00627       if (c == '}') break;
00628       if (c == '\0') {
00629         strgen_error("Missing } from command '%s'", start);
00630         return NULL;
00631       }
00632       if (s - start == 250) error("param command too long");
00633       *param++ = c;
00634     }
00635   }
00636   *param = '\0';
00637 
00638   *str = s;
00639 
00640   return cmd;
00641 }
00642 
00643 
00644 static void HandlePragma(char *str)
00645 {
00646   if (!memcmp(str, "id ", 3)) {
00647     _next_string_id = strtoul(str + 3, NULL, 0);
00648   } else if (!memcmp(str, "name ", 5)) {
00649     strecpy(_lang_name, str + 5, lastof(_lang_name));
00650   } else if (!memcmp(str, "ownname ", 8)) {
00651     strecpy(_lang_ownname, str + 8, lastof(_lang_ownname));
00652   } else if (!memcmp(str, "isocode ", 8)) {
00653     strecpy(_lang_isocode, str + 8, lastof(_lang_isocode));
00654   } else if (!memcmp(str, "plural ", 7)) {
00655     _lang_pluralform = atoi(str + 7);
00656     if (_lang_pluralform >= lengthof(_plural_form_counts))
00657       error("Invalid pluralform %d", _lang_pluralform);
00658   } else if (!memcmp(str, "textdir ", 8)) {
00659     if (!memcmp(str + 8, "ltr", 3)) {
00660       _lang_textdir = TD_LTR;
00661     } else if (!memcmp(str + 8, "rtl", 3)) {
00662       _lang_textdir = TD_RTL;
00663     } else {
00664       error("Invalid textdir %s", str + 8);
00665     }
00666   } else if (!memcmp(str, "winlangid ", 10)) {
00667     const char *buf = str + 10;
00668     long langid = strtol(buf, NULL, 16);
00669     if (langid > UINT16_MAX || langid < 0) {
00670       error("Invalid winlangid %s", buf);
00671     }
00672     _lang_winlangid = (uint16)langid;
00673   } else if (!memcmp(str, "grflangid ", 10)) {
00674     const char *buf = str + 10;
00675     long langid = strtol(buf, NULL, 16);
00676     if (langid >= 0x7F || langid < 0) {
00677       error("Invalid grflangid %s", buf);
00678     }
00679     _lang_newgrflangid = (uint8)langid;
00680   } else if (!memcmp(str, "gender ", 7)) {
00681     char *buf = str + 7;
00682 
00683     for (;;) {
00684       const char *s = ParseWord(&buf);
00685 
00686       if (s == NULL) break;
00687       if (_numgenders >= MAX_NUM_GENDER) error("Too many genders, max %d", MAX_NUM_GENDER);
00688       strecpy(_genders[_numgenders], s, lastof(_genders[_numgenders]));
00689       _numgenders++;
00690     }
00691   } else if (!memcmp(str, "case ", 5)) {
00692     char *buf = str + 5;
00693 
00694     for (;;) {
00695       const char *s = ParseWord(&buf);
00696 
00697       if (s == NULL) break;
00698       if (_numcases >= MAX_NUM_CASES) error("Too many cases, max %d", MAX_NUM_CASES);
00699       strecpy(_cases[_numcases], s, lastof(_cases[_numcases]));
00700       _numcases++;
00701     }
00702   } else {
00703     error("unknown pragma '%s'", str);
00704   }
00705 }
00706 
00707 static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings)
00708 {
00709   char param[100];
00710   int argno;
00711   int argidx = 0;
00712   int casei;
00713 
00714   memset(p, 0, sizeof(*p));
00715 
00716   for (;;) {
00717     /* read until next command from a. */
00718     const CmdStruct *ar = ParseCommandString(&s, param, &argno, &casei);
00719 
00720     if (ar == NULL) break;
00721 
00722     /* Sanity checking */
00723     if (argno != -1 && ar->consumes == 0) error("Non consumer param can't have a paramindex");
00724 
00725     if (ar->consumes) {
00726       if (argno != -1) argidx = argno;
00727       if (argidx < 0 || (uint)argidx >= lengthof(p->cmd)) error("invalid param idx %d", argidx);
00728       if (p->cmd[argidx] != NULL && p->cmd[argidx] != ar) error("duplicate param idx %d", argidx);
00729 
00730       p->cmd[argidx++] = ar;
00731     } else if (!(ar->flags & C_DONTCOUNT)) { // Ignore some of them
00732       if (p->np >= lengthof(p->pairs)) error("too many commands in string, max %d", lengthof(p->pairs));
00733       p->pairs[p->np].a = ar;
00734       p->pairs[p->np].v = param[0] != '\0' ? strdup(param) : "";
00735       p->np++;
00736     }
00737   }
00738 }
00739 
00740 
00741 static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
00742 {
00743   if (a == NULL) return NULL;
00744 
00745   if (strcmp(a->cmd, "STRING1") == 0 ||
00746       strcmp(a->cmd, "STRING2") == 0 ||
00747       strcmp(a->cmd, "STRING3") == 0 ||
00748       strcmp(a->cmd, "STRING4") == 0 ||
00749       strcmp(a->cmd, "STRING5") == 0 ||
00750       strcmp(a->cmd, "RAW_STRING") == 0){
00751     return FindCmd("STRING", 6);
00752   }
00753 
00754   if (strcmp(a->cmd, "SKIP") == 0) return NULL;
00755 
00756   return a;
00757 }
00758 
00759 
00760 static bool CheckCommandsMatch(char *a, char *b, const char *name)
00761 {
00762   ParsedCommandStruct templ;
00763   ParsedCommandStruct lang;
00764   uint i, j;
00765   bool result = true;
00766 
00767   ExtractCommandString(&templ, b, true);
00768   ExtractCommandString(&lang, a, true);
00769 
00770   /* For each string in templ, see if we find it in lang */
00771   if (templ.np != lang.np) {
00772     strgen_warning("%s: template string and language string have a different # of commands", name);
00773     result = false;
00774   }
00775 
00776   for (i = 0; i < templ.np; i++) {
00777     /* see if we find it in lang, and zero it out */
00778     bool found = false;
00779     for (j = 0; j < lang.np; j++) {
00780       if (templ.pairs[i].a == lang.pairs[j].a &&
00781           strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) {
00782         /* it was found in both. zero it out from lang so we don't find it again */
00783         lang.pairs[j].a = NULL;
00784         found = true;
00785         break;
00786       }
00787     }
00788 
00789     if (!found) {
00790       strgen_warning("%s: command '%s' exists in template file but not in language file", name, templ.pairs[i].a->cmd);
00791       result = false;
00792     }
00793   }
00794 
00795   /* if we reach here, all non consumer commands match up.
00796    * Check if the non consumer commands match up also. */
00797   for (i = 0; i < lengthof(templ.cmd); i++) {
00798     if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) {
00799       strgen_warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i,
00800         lang.cmd[i]  == NULL ? "<empty>" : lang.cmd[i]->cmd,
00801         templ.cmd[i] == NULL ? "<empty>" : templ.cmd[i]->cmd);
00802       result = false;
00803     }
00804   }
00805 
00806   return result;
00807 }
00808 
00809 static void HandleString(char *str, bool master)
00810 {
00811   char *s, *t;
00812   LangString *ent;
00813   char *casep;
00814 
00815   if (*str == '#') {
00816     if (str[1] == '#' && str[2] != '#') HandlePragma(str + 2);
00817     return;
00818   }
00819 
00820   /* Ignore comments & blank lines */
00821   if (*str == ';' || *str == ' ' || *str == '\0') return;
00822 
00823   s = strchr(str, ':');
00824   if (s == NULL) {
00825     strgen_error("Line has no ':' delimiter");
00826     return;
00827   }
00828 
00829   /* Trim spaces.
00830    * After this str points to the command name, and s points to the command contents */
00831   for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--);
00832   *t = 0;
00833   s++;
00834 
00835   /* Check string is valid UTF-8 */
00836   {
00837     const char *tmp;
00838     for (tmp = s; *tmp != '\0';) {
00839       size_t len = Utf8Validate(tmp);
00840       if (len == 0) error("Invalid UTF-8 sequence in '%s'", s);
00841       tmp += len;
00842     }
00843   }
00844 
00845   /* Check if the string has a case..
00846    * The syntax for cases is IDENTNAME.case */
00847   casep = strchr(str, '.');
00848   if (casep) *casep++ = 0;
00849 
00850   /* Check if this string already exists.. */
00851   ent = HashFind(str);
00852 
00853   if (master) {
00854     if (ent != NULL && casep == NULL) {
00855       strgen_error("String name '%s' is used multiple times", str);
00856       return;
00857     }
00858 
00859     if (ent == NULL && casep != NULL) {
00860       strgen_error("Base string name '%s' doesn't exist yet. Define it before defining a case.", str);
00861       return;
00862     }
00863 
00864     if (ent == NULL) {
00865       if (_strings[_next_string_id]) {
00866         strgen_error("String ID 0x%X for '%s' already in use by '%s'", ent, str, _strings[_next_string_id]->name);
00867         return;
00868       }
00869 
00870       /* Allocate a new LangString */
00871       ent = CallocT<LangString>(1);
00872       _strings[_next_string_id] = ent;
00873       ent->index = _next_string_id++;
00874       ent->name = strdup(str);
00875       ent->line = _cur_line;
00876 
00877       HashAdd(str, ent);
00878     }
00879 
00880     if (casep != NULL) {
00881       Case *c = MallocT<Case>(1);
00882 
00883       c->caseidx = ResolveCaseName(casep, strlen(casep));
00884       c->string = strdup(s);
00885       c->next = ent->english_case;
00886       ent->english_case = c;
00887     } else {
00888       ent->english = strdup(s);
00889     }
00890 
00891   } else {
00892     if (ent == NULL) {
00893       strgen_warning("String name '%s' does not exist in master file", str);
00894       return;
00895     }
00896 
00897     if (ent->translated && casep == NULL) {
00898       strgen_error("String name '%s' is used multiple times", str);
00899       return;
00900     }
00901 
00902     if (s[0] == ':' && s[1] == '\0' && casep == NULL) {
00903       /* Special syntax :: means we should just inherit the master string */
00904       ent->translated = strdup(ent->english);
00905     } else {
00906       /* make sure that the commands match */
00907       if (!CheckCommandsMatch(s, ent->english, str)) return;
00908 
00909       if (casep != NULL) {
00910         Case *c = MallocT<Case>(1);
00911 
00912         c->caseidx = ResolveCaseName(casep, strlen(casep));
00913         c->string = strdup(s);
00914         c->next = ent->translated_case;
00915         ent->translated_case = c;
00916       } else {
00917         ent->translated = strdup(s);
00918       }
00919     }
00920   }
00921 }
00922 
00923 
00924 static void rstrip(char *buf)
00925 {
00926   int i = strlen(buf);
00927   while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--;
00928   buf[i] = '\0';
00929 }
00930 
00931 
00932 static void ParseFile(const char *file, bool english)
00933 {
00934   FILE *in;
00935   char buf[2048];
00936 
00937   _file = file;
00938 
00939   /* For each new file we parse, reset the genders, and language codes */
00940   _numgenders = 0;
00941   _lang_name[0] = _lang_ownname[0] = _lang_isocode[0] = '\0';
00942   _lang_textdir = TD_LTR;
00943   _lang_winlangid = 0x0000; // neutral language code
00944   _lang_newgrflangid = 0; // standard english
00945   /* TODO:!! We can't reset the cases. In case the translated strings
00946    * derive some strings from english.... */
00947 
00948   in = fopen(file, "r");
00949   if (in == NULL) error("Cannot open file");
00950   _cur_line = 1;
00951   while (fgets(buf, sizeof(buf), in) != NULL) {
00952     rstrip(buf);
00953     HandleString(buf, english);
00954     _cur_line++;
00955   }
00956   fclose(in);
00957 
00958   if (StrEmpty(_lang_name) || StrEmpty(_lang_ownname) || StrEmpty(_lang_isocode)) {
00959     error("Language must include ##name, ##ownname and ##isocode");
00960   }
00961 }
00962 
00963 
00964 static uint32 MyHashStr(uint32 hash, const char *s)
00965 {
00966   for (; *s != '\0'; s++) {
00967     hash = ROL(hash, 3) ^ *s;
00968     hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
00969   }
00970   return hash;
00971 }
00972 
00973 
00974 /* make a hash of the file to get a unique "version number" */
00975 static void MakeHashOfStrings()
00976 {
00977   uint32 hash = 0;
00978   uint i;
00979 
00980   for (i = 0; i != lengthof(_strings); i++) {
00981     const LangString *ls = _strings[i];
00982 
00983     if (ls != NULL) {
00984       const CmdStruct *cs;
00985       const char *s;
00986       char buf[256];
00987       int argno;
00988       int casei;
00989 
00990       s = ls->name;
00991       hash ^= i * 0x717239;
00992       hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
00993       hash = MyHashStr(hash, s + 1);
00994 
00995       s = ls->english;
00996       while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != NULL) {
00997         if (cs->flags & C_DONTCOUNT) continue;
00998 
00999         hash ^= (cs - _cmd_structs) * 0x1234567;
01000         hash = (hash & 1 ? hash >> 1 ^ 0xF00BAA4 : hash >> 1);
01001       }
01002     }
01003   }
01004   _hash = hash;
01005 }
01006 
01007 
01008 static uint CountInUse(uint grp)
01009 {
01010   int i;
01011 
01012   for (i = 0x800; --i >= 0;) if (_strings[(grp << 11) + i] != NULL) break;
01013   return i + 1;
01014 }
01015 
01016 
01017 bool CompareFiles(const char *n1, const char *n2)
01018 {
01019   FILE *f1, *f2;
01020   char b1[4096];
01021   char b2[4096];
01022   size_t l1, l2;
01023 
01024   f2 = fopen(n2, "rb");
01025   if (f2 == NULL) return false;
01026 
01027   f1 = fopen(n1, "rb");
01028   if (f1 == NULL) error("can't open %s", n1);
01029 
01030   do {
01031     l1 = fread(b1, 1, sizeof(b1), f1);
01032     l2 = fread(b2, 1, sizeof(b2), f2);
01033 
01034     if (l1 != l2 || memcmp(b1, b2, l1)) {
01035       fclose(f2);
01036       fclose(f1);
01037       return false;
01038     }
01039   } while (l1);
01040 
01041   fclose(f2);
01042   fclose(f1);
01043   return true;
01044 }
01045 
01046 
01047 static void WriteStringsH(const char *filename)
01048 {
01049   FILE *out;
01050   int i;
01051   int next = -1;
01052 
01053   out = fopen("tmp.xxx", "w");
01054   if (out == NULL) error("can't open tmp.xxx");
01055 
01056   fprintf(out, "/* This file is automatically generated. Do not modify */\n\n");
01057   fprintf(out, "#ifndef TABLE_STRINGS_H\n");
01058   fprintf(out, "#define TABLE_STRINGS_H\n");
01059 
01060   for (i = 0; i != lengthof(_strings); i++) {
01061     if (_strings[i] != NULL) {
01062       if (next != i) fprintf(out, "\n");
01063       fprintf(out, "static const StringID %s = 0x%X;\n", _strings[i]->name, i);
01064       next = i + 1;
01065     }
01066   }
01067 
01068   fprintf(out, "\nstatic const StringID STR_LAST_STRINGID = 0x%X;\n", next - 1);
01069 
01070   fprintf(out,
01071     "\nenum {\n"
01072     "\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n"
01073     "\tLANGUAGE_PACK_VERSION = 0x%X,\n"
01074     "};\n", (uint)_hash
01075   );
01076 
01077   fprintf(out, "\n#endif /* TABLE_STRINGS_H */\n");
01078 
01079   fclose(out);
01080 
01081   if (CompareFiles("tmp.xxx", filename)) {
01082     /* files are equal. tmp.xxx is not needed */
01083     unlink("tmp.xxx");
01084   } else {
01085     /* else rename tmp.xxx into filename */
01086 #if defined(WIN32) || defined(WIN64)
01087     unlink(filename);
01088 #endif
01089     if (rename("tmp.xxx", filename) == -1) error("rename() failed");
01090   }
01091 }
01092 
01093 static int TranslateArgumentIdx(int argidx)
01094 {
01095   int i, sum;
01096 
01097   if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd))
01098     error("invalid argidx %d", argidx);
01099 
01100   for (i = sum = 0; i < argidx; i++) {
01101     const CmdStruct *cs = _cur_pcs.cmd[i];
01102     sum += (cs != NULL) ? cs->consumes : 1;
01103   }
01104 
01105   return sum;
01106 }
01107 
01108 static void PutArgidxCommand()
01109 {
01110   PutUtf8(SCC_ARG_INDEX);
01111   PutByte(TranslateArgumentIdx(_cur_argidx));
01112 }
01113 
01114 
01115 static void PutCommandString(const char *str)
01116 {
01117   const CmdStruct *cs;
01118   char param[256];
01119   int argno;
01120   int casei;
01121 
01122   _cur_argidx = 0;
01123 
01124   while (*str != '\0') {
01125     /* Process characters as they are until we encounter a { */
01126     if (*str != '{') {
01127       PutByte(*str++);
01128       continue;
01129     }
01130     cs = ParseCommandString(&str, param, &argno, &casei);
01131     if (cs == NULL) break;
01132 
01133     if (casei != -1) {
01134       PutUtf8(SCC_SETCASE); // {SETCASE}
01135       PutByte(casei);
01136     }
01137 
01138     /* For params that consume values, we need to handle the argindex properly */
01139     if (cs->consumes > 0) {
01140       /* Check if we need to output a move-param command */
01141       if (argno != -1 && argno != _cur_argidx) {
01142         _cur_argidx = argno;
01143         PutArgidxCommand();
01144       }
01145 
01146       /* Output the one from the master string... it's always accurate. */
01147       cs = _cur_pcs.cmd[_cur_argidx++];
01148       if (cs == NULL) {
01149         error("%s: No argument exists at position %d", _cur_ident, _cur_argidx - 1);
01150       }
01151     }
01152 
01153     cs->proc(param, cs->value);
01154   }
01155 }
01156 
01157 static void WriteLength(FILE *f, uint length)
01158 {
01159   if (length < 0xC0) {
01160     fputc(length, f);
01161   } else if (length < 0x4000) {
01162     fputc((length >> 8) | 0xC0, f);
01163     fputc(length & 0xFF, f);
01164   } else {
01165     error("string too long");
01166   }
01167 }
01168 
01169 
01170 static void WriteLangfile(const char *filename)
01171 {
01172   FILE *f;
01173   uint in_use[32];
01174   LanguagePackHeader hdr;
01175   uint i;
01176   uint j;
01177 
01178   f = fopen(filename, "wb");
01179   if (f == NULL) error("can't open %s", filename);
01180 
01181   memset(&hdr, 0, sizeof(hdr));
01182   for (i = 0; i != 32; i++) {
01183     uint n = CountInUse(i);
01184 
01185     in_use[i] = n;
01186     hdr.offsets[i] = TO_LE16(n);
01187   }
01188 
01189   /* see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...) */
01190   hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG'
01191   hdr.version = TO_LE32(_hash);
01192   hdr.plural_form = _lang_pluralform;
01193   hdr.text_dir = _lang_textdir;
01194   hdr.winlangid = TO_LE16(_lang_winlangid);
01195   hdr.newgrflangid = _lang_newgrflangid;
01196   strcpy(hdr.name, _lang_name);
01197   strcpy(hdr.own_name, _lang_ownname);
01198   strcpy(hdr.isocode, _lang_isocode);
01199 
01200   fwrite(&hdr, sizeof(hdr), 1, f);
01201 
01202   for (i = 0; i != 32; i++) {
01203     for (j = 0; j != in_use[i]; j++) {
01204       const LangString *ls = _strings[(i << 11) + j];
01205       const Case *casep;
01206       const char *cmdp;
01207 
01208       /* For undefined strings, just set that it's an empty string */
01209       if (ls == NULL) {
01210         WriteLength(f, 0);
01211         continue;
01212       }
01213 
01214       _cur_ident = ls->name;
01215       _cur_line = ls->line;
01216 
01217       /* Produce a message if a string doesn't have a translation. */
01218       if (_show_todo > 0 && ls->translated == NULL) {
01219         if ((_show_todo & 2) != 0) {
01220           strgen_warning("'%s' is untranslated", ls->name);
01221         }
01222         if ((_show_todo & 1) != 0) {
01223           const char *s = "<TODO> ";
01224           while (*s != '\0') PutByte(*s++);
01225         }
01226       }
01227 
01228       /* Extract the strings and stuff from the english command string */
01229       ExtractCommandString(&_cur_pcs, ls->english, false);
01230 
01231       if (ls->translated_case != NULL || ls->translated != NULL) {
01232         casep = ls->translated_case;
01233         cmdp = ls->translated;
01234       } else {
01235         casep = ls->english_case;
01236         cmdp = ls->english;
01237       }
01238 
01239       _translated = _masterlang || (cmdp != ls->english);
01240 
01241       if (casep != NULL) {
01242         const Case *c;
01243         uint num;
01244 
01245         /* Need to output a case-switch.
01246          * It has this format
01247          * <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
01248          * Each LEN is printed using 2 bytes in big endian order. */
01249         PutUtf8(SCC_SWITCH_CASE);
01250         /* Count the number of cases */
01251         for (num = 0, c = casep; c; c = c->next) num++;
01252         PutByte(num);
01253 
01254         /* Write each case */
01255         for (c = casep; c != NULL; c = c->next) {
01256           int pos;
01257 
01258           PutByte(c->caseidx);
01259           /* Make some space for the 16-bit length */
01260           pos = _put_pos;
01261           PutByte(0);
01262           PutByte(0);
01263           /* Write string */
01264           PutCommandString(c->string);
01265           PutByte(0); // terminate with a zero
01266           /* Fill in the length */
01267           _put_buf[pos + 0] = GB(_put_pos - (pos + 2), 8, 8);
01268           _put_buf[pos + 1] = GB(_put_pos - (pos + 2), 0, 8);
01269         }
01270       }
01271 
01272       if (cmdp != NULL) PutCommandString(cmdp);
01273 
01274       WriteLength(f, _put_pos);
01275       fwrite(_put_buf, 1, _put_pos, f);
01276       _put_pos = 0;
01277     }
01278   }
01279 
01280   fputc(0, f);
01281   fclose(f);
01282 }
01283 
01285 static inline void ottd_mkdir(const char *directory)
01286 {
01287 #if defined(WIN32) || defined(__WATCOMC__)
01288     mkdir(directory);
01289 #else
01290     mkdir(directory, 0755);
01291 #endif
01292 }
01293 
01297 static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file)
01298 {
01299   char *p;
01300   ttd_strlcpy(buf, path, buflen); // copy directory into buffer
01301 
01302   p = strchr(buf, '\0'); // add path seperator if necessary
01303   if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR;
01304   ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // catenate filename at end of buffer
01305   return buf;
01306 }
01307 
01308 #if defined(__MINGW32__)
01309 
01314 static inline char *replace_pathsep(char *s)
01315 {
01316   char *c;
01317 
01318   for (c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
01319   return s;
01320 }
01321 #else
01322 static inline char *replace_pathsep(char *s) { return s; }
01323 #endif
01324 
01325 int CDECL main(int argc, char *argv[])
01326 {
01327   char pathbuf[MAX_PATH];
01328   const char *src_dir = ".";
01329   const char *dest_dir = NULL;
01330 
01331   while (argc > 1 && *argv[1] == '-') {
01332     if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
01333       puts("$Revision: 15718 $");
01334       return 0;
01335     }
01336 
01337     if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--todo") == 0) {
01338       _show_todo |= 1;
01339       argc--, argv++;
01340       continue;
01341     }
01342 
01343     if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--warning") == 0) {
01344       _show_todo |= 2;
01345       argc--, argv++;
01346       continue;
01347     }
01348 
01349     if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) {
01350       puts(
01351         "strgen - $Revision: 15718 $\n"
01352         " -v | --version    print version information and exit\n"
01353         " -t | --todo       replace any untranslated strings with '<TODO>'\n"
01354         " -w | --warning    print a warning for any untranslated strings\n"
01355         " -h | -? | --help  print this help message and exit\n"
01356         " -s | --source_dir search for english.txt in the specified directory\n"
01357         " -d | --dest_dir   put output file in the specified directory, create if needed\n"
01358         " Run without parameters and strgen will search for english.txt and parse it,\n"
01359         " creating strings.h. Passing an argument, strgen will translate that language\n"
01360         " file using english.txt as a reference and output <language>.lng."
01361       );
01362       return 0;
01363     }
01364 
01365     if (argc > 2 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--source_dir") == 0)) {
01366       src_dir = replace_pathsep(argv[2]);
01367       argc -= 2, argv += 2;
01368       continue;
01369     }
01370 
01371     if (argc > 2 && (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--dest_dir") == 0)) {
01372       dest_dir = replace_pathsep(argv[2]);
01373       argc -= 2, argv += 2;
01374       continue;
01375     }
01376 
01377     fprintf(stderr, "Invalid arguments\n");
01378     return 0;
01379   }
01380 
01381   if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
01382 
01383   /* strgen has two modes of operation. If no (free) arguments are passed
01384    * strgen generates strings.h to the destination directory. If it is supplied
01385    * with a (free) parameter the program will translate that language to destination
01386    * directory. As input english.txt is parsed from the source directory */
01387   if (argc == 1) {
01388     mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
01389 
01390     /* parse master file */
01391     _masterlang = true;
01392     ParseFile(pathbuf, true);
01393     MakeHashOfStrings();
01394     if (_errors) return 1;
01395 
01396     /* write strings.h */
01397     ottd_mkdir(dest_dir);
01398     mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
01399     WriteStringsH(pathbuf);
01400   } else if (argc == 2) {
01401     char *r;
01402 
01403     mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
01404 
01405     /* parse master file and check if target file is correct */
01406     _masterlang = false;
01407     ParseFile(pathbuf, true);
01408     MakeHashOfStrings();
01409     ParseFile(replace_pathsep(argv[1]), false); // target file
01410     if (_errors) return 1;
01411 
01412     /* get the targetfile, strip any directories and append to destination path */
01413     r = strrchr(argv[1], PATHSEPCHAR);
01414     mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : argv[1]);
01415 
01416     /* rename the .txt (input-extension) to .lng */
01417     r = strrchr(pathbuf, '.');
01418     if (r == NULL || strcmp(r, ".txt") != 0) r = strchr(pathbuf, '\0');
01419     ttd_strlcpy(r, ".lng", (size_t)(r - pathbuf));
01420     WriteLangfile(pathbuf);
01421 
01422     /* if showing warnings, print a summary of the language */
01423     if ((_show_todo & 2) != 0) {
01424       fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf);
01425     }
01426   } else {
01427     fprintf(stderr, "Invalid arguments\n");
01428   }
01429 
01430   return 0;
01431 }

Generated on Sun Mar 15 22:49:50 2009 for openttd by  doxygen 1.5.6