strgen.cpp

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

Generated on Wed Jun 3 19:05:15 2009 for OpenTTD by  doxygen 1.5.6