strgen_base.cpp

Go to the documentation of this file.
00001 /* $Id: strgen_base.cpp 26050 2013-11-22 21:43:47Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../core/endian_func.hpp"
00014 #include "../string_func.h"
00015 #include "../table/control_codes.h"
00016 
00017 #include "strgen.h"
00018 
00019 
00020 #include "../table/strgen_tables.h"
00021 
00022 /* Compiles a list of strings into a compiled string list */
00023 
00024 static bool _translated;              
00025 static bool _translation;             
00026 const char *_file = "(unknown file)"; 
00027 int _cur_line;                        
00028 int _errors, _warnings, _show_todo;
00029 LanguagePackHeader _lang;             
00030 
00031 static const ptrdiff_t MAX_COMMAND_PARAM_SIZE = 100; 
00032 static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei);
00033 
00040 Case::Case(int caseidx, const char *string, Case *next) :
00041     caseidx(caseidx), string(strdup(string)), next(next)
00042 {
00043 }
00044 
00046 Case::~Case()
00047 {
00048   free(this->string);
00049   delete this->next;
00050 }
00051 
00059 LangString::LangString(const char *name, const char *english, int index, int line) :
00060     name(strdup(name)), english(strdup(english)), translated(NULL),
00061     hash_next(0), index(index), line(line), translated_case(NULL)
00062 {
00063 }
00064 
00066 LangString::~LangString()
00067 {
00068   free(this->name);
00069   free(this->english);
00070   free(this->translated);
00071   delete this->translated_case;
00072 }
00073 
00075 void LangString::FreeTranslation()
00076 {
00077   free(this->translated);
00078   this->translated = NULL;
00079 
00080   delete this->translated_case;
00081   this->translated_case = NULL;
00082 }
00083 
00088 StringData::StringData(size_t tabs) : tabs(tabs), max_strings(tabs * TAB_SIZE)
00089 {
00090   this->strings = CallocT<LangString *>(max_strings);
00091   this->hash_heads = CallocT<uint16>(max_strings);
00092   this->next_string_id = 0;
00093 }
00094 
00096 StringData::~StringData()
00097 {
00098   for (size_t i = 0; i < this->max_strings; i++) delete this->strings[i];
00099   free(this->strings);
00100   free(this->hash_heads);
00101 }
00102 
00104 void StringData::FreeTranslation()
00105 {
00106   for (size_t i = 0; i < this->max_strings; i++) {
00107     LangString *ls = this->strings[i];
00108     if (ls != NULL) ls->FreeTranslation();
00109   }
00110 }
00111 
00117 uint StringData::HashStr(const char *s) const
00118 {
00119   uint hash = 0;
00120   for (; *s != '\0'; s++) hash = ROL(hash, 3) ^ *s;
00121   return hash % this->max_strings;
00122 }
00123 
00129 void StringData::Add(const char *s, LangString *ls)
00130 {
00131   uint hash = this->HashStr(s);
00132   ls->hash_next = this->hash_heads[hash];
00133   /* Off-by-one for hash find. */
00134   this->hash_heads[hash] = ls->index + 1;
00135   this->strings[ls->index] = ls;
00136 }
00137 
00143 LangString *StringData::Find(const char *s)
00144 {
00145   int idx = this->hash_heads[this->HashStr(s)];
00146 
00147   while (--idx >= 0) {
00148     LangString *ls = this->strings[idx];
00149 
00150     if (strcmp(ls->name, s) == 0) return ls;
00151     idx = ls->hash_next;
00152   }
00153   return NULL;
00154 }
00155 
00162 uint StringData::VersionHashStr(uint hash, const char *s) const
00163 {
00164   for (; *s != '\0'; s++) {
00165     hash = ROL(hash, 3) ^ *s;
00166     hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
00167   }
00168   return hash;
00169 }
00170 
00175 uint StringData::Version() const
00176 {
00177   uint hash = 0;
00178 
00179   for (size_t i = 0; i < this->max_strings; i++) {
00180     const LangString *ls = this->strings[i];
00181 
00182     if (ls != NULL) {
00183       const CmdStruct *cs;
00184       const char *s;
00185       char buf[MAX_COMMAND_PARAM_SIZE];
00186       int argno;
00187       int casei;
00188 
00189       s = ls->name;
00190       hash ^= i * 0x717239;
00191       hash = (hash & 1 ? hash >> 1 ^ 0xDEADBEEF : hash >> 1);
00192       hash = this->VersionHashStr(hash, s + 1);
00193 
00194       s = ls->english;
00195       while ((cs = ParseCommandString(&s, buf, &argno, &casei)) != NULL) {
00196         if (cs->flags & C_DONTCOUNT) continue;
00197 
00198         hash ^= (cs - _cmd_structs) * 0x1234567;
00199         hash = (hash & 1 ? hash >> 1 ^ 0xF00BAA4 : hash >> 1);
00200       }
00201     }
00202   }
00203 
00204   return hash;
00205 }
00206 
00211 uint StringData::CountInUse(uint tab) const
00212 {
00213   int i;
00214   for (i = TAB_SIZE; --i >= 0;) if (this->strings[(tab * TAB_SIZE) + i] != NULL) break;
00215   return i + 1;
00216 }
00217 
00218 static const char *_cur_ident;
00219 
00220 struct CmdPair {
00221   const CmdStruct *a;
00222   const char *v;
00223 };
00224 
00225 struct ParsedCommandStruct {
00226   uint np;
00227   CmdPair pairs[32];
00228   const CmdStruct *cmd[32]; // ordered by param #
00229 };
00230 
00231 /* Used when generating some advanced commands. */
00232 static ParsedCommandStruct _cur_pcs;
00233 static int _cur_argidx;
00234 
00236 struct Buffer : SmallVector<byte, 256> {
00241   void AppendByte(byte value)
00242   {
00243     *this->Append() = value;
00244   }
00245 
00250   void AppendUtf8(uint32 value)
00251   {
00252     if (value < 0x80) {
00253       *this->Append() = value;
00254     } else if (value < 0x800) {
00255       *this->Append() = 0xC0 + GB(value,  6, 5);
00256       *this->Append() = 0x80 + GB(value,  0, 6);
00257     } else if (value < 0x10000) {
00258       *this->Append() = 0xE0 + GB(value, 12, 4);
00259       *this->Append() = 0x80 + GB(value,  6, 6);
00260       *this->Append() = 0x80 + GB(value,  0, 6);
00261     } else if (value < 0x110000) {
00262       *this->Append() = 0xF0 + GB(value, 18, 3);
00263       *this->Append() = 0x80 + GB(value, 12, 6);
00264       *this->Append() = 0x80 + GB(value,  6, 6);
00265       *this->Append() = 0x80 + GB(value,  0, 6);
00266     } else {
00267       strgen_warning("Invalid unicode value U+0x%X", value);
00268     }
00269   }
00270 };
00271 
00272 size_t Utf8Validate(const char *s)
00273 {
00274   uint32 c;
00275 
00276   if (!HasBit(s[0], 7)) {
00277     /* 1 byte */
00278     return 1;
00279   } else if (GB(s[0], 5, 3) == 6 && IsUtf8Part(s[1])) {
00280     /* 2 bytes */
00281     c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
00282     if (c >= 0x80) return 2;
00283   } else if (GB(s[0], 4, 4) == 14 && IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
00284     /* 3 bytes */
00285     c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
00286     if (c >= 0x800) return 3;
00287   } else if (GB(s[0], 3, 5) == 30 && IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
00288     /* 4 bytes */
00289     c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
00290     if (c >= 0x10000 && c <= 0x10FFFF) return 4;
00291   }
00292 
00293   return 0;
00294 }
00295 
00296 
00297 void EmitSingleChar(Buffer *buffer, char *buf, int value)
00298 {
00299   if (*buf != '\0') strgen_warning("Ignoring trailing letters in command");
00300   buffer->AppendUtf8(value);
00301 }
00302 
00303 
00304 /* The plural specifier looks like
00305  * {NUM} {PLURAL -1 passenger passengers} then it picks either passenger/passengers depending on the count in NUM */
00306 
00307 /* This is encoded like
00308  *  CommandByte <ARG#> <NUM> {Length of each string} {each string} */
00309 
00310 bool ParseRelNum(char **buf, int *value, int *offset)
00311 {
00312   const char *s = *buf;
00313   char *end;
00314   bool rel = false;
00315 
00316   while (*s == ' ' || *s == '\t') s++;
00317   if (*s == '+') {
00318     rel = true;
00319     s++;
00320   }
00321   int v = strtol(s, &end, 0);
00322   if (end == s) return false;
00323   if (rel || v < 0) {
00324     *value += v;
00325   } else {
00326     *value = v;
00327   }
00328   if (offset != NULL && *end == ':') {
00329     /* Take the Nth within */
00330     s = end + 1;
00331     *offset = strtol(s, &end, 0);
00332     if (end == s) return false;
00333   }
00334   *buf = end;
00335   return true;
00336 }
00337 
00338 /* Parse out the next word, or NULL */
00339 char *ParseWord(char **buf)
00340 {
00341   char *s = *buf, *r;
00342 
00343   while (*s == ' ' || *s == '\t') s++;
00344   if (*s == '\0') return NULL;
00345 
00346   if (*s == '"') {
00347     r = ++s;
00348     /* parse until next " or NUL */
00349     for (;;) {
00350       if (*s == '\0') break;
00351       if (*s == '"') {
00352         *s++ = '\0';
00353         break;
00354       }
00355       s++;
00356     }
00357   } else {
00358     /* proceed until whitespace or NUL */
00359     r = s;
00360     for (;;) {
00361       if (*s == '\0') break;
00362       if (*s == ' ' || *s == '\t') {
00363         *s++ = '\0';
00364         break;
00365       }
00366       s++;
00367     }
00368   }
00369   *buf = s;
00370   return r;
00371 }
00372 
00373 /* Forward declaration */
00374 static int TranslateArgumentIdx(int arg, int offset = 0);
00375 
00376 static void EmitWordList(Buffer *buffer, const char * const *words, uint nw)
00377 {
00378   buffer->AppendByte(nw);
00379   for (uint i = 0; i < nw; i++) buffer->AppendByte((uint)strlen(words[i]) + 1);
00380   for (uint i = 0; i < nw; i++) {
00381     for (uint j = 0; words[i][j] != '\0'; j++) buffer->AppendByte(words[i][j]);
00382     buffer->AppendByte(0);
00383   }
00384 }
00385 
00386 void EmitPlural(Buffer *buffer, char *buf, int value)
00387 {
00388   int argidx = _cur_argidx;
00389   int offset = 0;
00390   int expected = _plural_forms[_lang.plural_form].plural_count;
00391   const char **words = AllocaM(const char *, max(expected, MAX_PLURALS));
00392   int nw = 0;
00393 
00394   /* Parse out the number, if one exists. Otherwise default to prev arg. */
00395   if (!ParseRelNum(&buf, &argidx, &offset)) argidx--;
00396 
00397   /* Parse each string */
00398   for (nw = 0; nw < MAX_PLURALS; nw++) {
00399     words[nw] = ParseWord(&buf);
00400     if (words[nw] == NULL) break;
00401   }
00402 
00403   if (nw == 0) {
00404     strgen_fatal("%s: No plural words", _cur_ident);
00405   }
00406 
00407   if (expected != nw) {
00408     if (_translated) {
00409       strgen_fatal("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident,
00410         expected, nw);
00411     } else {
00412       if ((_show_todo & 2) != 0) strgen_warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident);
00413       if (nw > expected) {
00414         nw = expected;
00415       } else {
00416         for (; nw < expected; nw++) {
00417           words[nw] = words[nw - 1];
00418         }
00419       }
00420     }
00421   }
00422 
00423   buffer->AppendUtf8(SCC_PLURAL_LIST);
00424   buffer->AppendByte(_lang.plural_form);
00425   buffer->AppendByte(TranslateArgumentIdx(argidx, offset));
00426   EmitWordList(buffer, words, nw);
00427 }
00428 
00429 
00430 void EmitGender(Buffer *buffer, char *buf, int value)
00431 {
00432   int argidx = _cur_argidx;
00433   int offset = 0;
00434   uint nw;
00435 
00436   if (buf[0] == '=') {
00437     buf++;
00438 
00439     /* This is a {G=DER} command */
00440     nw = _lang.GetGenderIndex(buf);
00441     if (nw >= MAX_NUM_GENDERS) strgen_fatal("G argument '%s' invalid", buf);
00442 
00443     /* now nw contains the gender index */
00444     buffer->AppendUtf8(SCC_GENDER_INDEX);
00445     buffer->AppendByte(nw);
00446   } else {
00447     const char *words[MAX_NUM_GENDERS];
00448 
00449     /* This is a {G 0 foo bar two} command.
00450      * If no relative number exists, default to +0 */
00451     if (!ParseRelNum(&buf, &argidx, &offset)) {}
00452 
00453     const CmdStruct *cmd = _cur_pcs.cmd[argidx];
00454     if (cmd == NULL || (cmd->flags & C_GENDER) == 0) {
00455       strgen_fatal("Command '%s' can't have a gender", cmd == NULL ? "<empty>" : cmd->cmd);
00456     }
00457 
00458     for (nw = 0; nw < MAX_NUM_GENDERS; nw++) {
00459       words[nw] = ParseWord(&buf);
00460       if (words[nw] == NULL) break;
00461     }
00462     if (nw != _lang.num_genders) strgen_fatal("Bad # of arguments for gender command");
00463 
00464     assert(IsInsideBS(cmd->value, SCC_CONTROL_START, UINT8_MAX));
00465     buffer->AppendUtf8(SCC_GENDER_LIST);
00466     buffer->AppendByte(TranslateArgumentIdx(argidx, offset));
00467     EmitWordList(buffer, words, nw);
00468   }
00469 }
00470 
00471 static const CmdStruct *FindCmd(const char *s, int len)
00472 {
00473   for (const CmdStruct *cs = _cmd_structs; cs != endof(_cmd_structs); cs++) {
00474     if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs;
00475   }
00476   return NULL;
00477 }
00478 
00479 static uint ResolveCaseName(const char *str, size_t len)
00480 {
00481   /* First get a clean copy of only the case name, then resolve it. */
00482   char case_str[CASE_GENDER_LEN];
00483   len = min(lengthof(case_str) - 1, len);
00484   memcpy(case_str, str, len);
00485   case_str[len] = '\0';
00486 
00487   uint8 case_idx = _lang.GetCaseIndex(case_str);
00488   if (case_idx >= MAX_NUM_CASES) strgen_fatal("Invalid case-name '%s'", case_str);
00489   return case_idx + 1;
00490 }
00491 
00492 
00493 /* returns NULL on eof
00494  * else returns command struct */
00495 static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei)
00496 {
00497   const char *s = *str, *start;
00498   char c;
00499 
00500   *argno = -1;
00501   *casei = -1;
00502 
00503   /* Scan to the next command, exit if there's no next command. */
00504   for (; *s != '{'; s++) {
00505     if (*s == '\0') return NULL;
00506   }
00507   s++; // Skip past the {
00508 
00509   if (*s >= '0' && *s <= '9') {
00510     char *end;
00511 
00512     *argno = strtoul(s, &end, 0);
00513     if (*end != ':') strgen_fatal("missing arg #");
00514     s = end + 1;
00515   }
00516 
00517   /* parse command name */
00518   start = s;
00519   do {
00520     c = *s++;
00521   } while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0);
00522 
00523   const CmdStruct *cmd = FindCmd(start, s - start - 1);
00524   if (cmd == NULL) {
00525     strgen_error("Undefined command '%.*s'", (int)(s - start - 1), start);
00526     return NULL;
00527   }
00528 
00529   if (c == '.') {
00530     const char *casep = s;
00531 
00532     if (!(cmd->flags & C_CASE)) {
00533       strgen_fatal("Command '%s' can't have a case", cmd->cmd);
00534     }
00535 
00536     do {
00537       c = *s++;
00538     } while (c != '}' && c != ' ' && c != '\0');
00539     *casei = ResolveCaseName(casep, s - casep - 1);
00540   }
00541 
00542   if (c == '\0') {
00543     strgen_error("Missing } from command '%s'", start);
00544     return NULL;
00545   }
00546 
00547 
00548   if (c != '}') {
00549     if (c == '=') s--;
00550     /* copy params */
00551     start = s;
00552     for (;;) {
00553       c = *s++;
00554       if (c == '}') break;
00555       if (c == '\0') {
00556         strgen_error("Missing } from command '%s'", start);
00557         return NULL;
00558       }
00559       if (s - start == MAX_COMMAND_PARAM_SIZE) error("param command too long");
00560       *param++ = c;
00561     }
00562   }
00563   *param = '\0';
00564 
00565   *str = s;
00566 
00567   return cmd;
00568 }
00569 
00577 StringReader::StringReader(StringData &data, const char *file, bool master, bool translation) :
00578     data(data), file(strdup(file)), master(master), translation(translation)
00579 {
00580 }
00581 
00583 StringReader::~StringReader()
00584 {
00585   free(file);
00586 }
00587 
00588 static void ExtractCommandString(ParsedCommandStruct *p, const char *s, bool warnings)
00589 {
00590   char param[MAX_COMMAND_PARAM_SIZE];
00591   int argno;
00592   int argidx = 0;
00593   int casei;
00594 
00595   memset(p, 0, sizeof(*p));
00596 
00597   for (;;) {
00598     /* read until next command from a. */
00599     const CmdStruct *ar = ParseCommandString(&s, param, &argno, &casei);
00600 
00601     if (ar == NULL) break;
00602 
00603     /* Sanity checking */
00604     if (argno != -1 && ar->consumes == 0) strgen_fatal("Non consumer param can't have a paramindex");
00605 
00606     if (ar->consumes) {
00607       if (argno != -1) argidx = argno;
00608       if (argidx < 0 || (uint)argidx >= lengthof(p->cmd)) strgen_fatal("invalid param idx %d", argidx);
00609       if (p->cmd[argidx] != NULL && p->cmd[argidx] != ar) strgen_fatal("duplicate param idx %d", argidx);
00610 
00611       p->cmd[argidx++] = ar;
00612     } else if (!(ar->flags & C_DONTCOUNT)) { // Ignore some of them
00613       if (p->np >= lengthof(p->pairs)) strgen_fatal("too many commands in string, max " PRINTF_SIZE, lengthof(p->pairs));
00614       p->pairs[p->np].a = ar;
00615       p->pairs[p->np].v = param[0] != '\0' ? strdup(param) : "";
00616       p->np++;
00617     }
00618   }
00619 }
00620 
00621 
00622 static const CmdStruct *TranslateCmdForCompare(const CmdStruct *a)
00623 {
00624   if (a == NULL) return NULL;
00625 
00626   if (strcmp(a->cmd, "STRING1") == 0 ||
00627       strcmp(a->cmd, "STRING2") == 0 ||
00628       strcmp(a->cmd, "STRING3") == 0 ||
00629       strcmp(a->cmd, "STRING4") == 0 ||
00630       strcmp(a->cmd, "STRING5") == 0 ||
00631       strcmp(a->cmd, "STRING6") == 0 ||
00632       strcmp(a->cmd, "STRING7") == 0 ||
00633       strcmp(a->cmd, "RAW_STRING") == 0) {
00634     return FindCmd("STRING", 6);
00635   }
00636 
00637   return a;
00638 }
00639 
00640 
00641 static bool CheckCommandsMatch(char *a, char *b, const char *name)
00642 {
00643   /* If we're not translating, i.e. we're compiling the base language,
00644    * it is pointless to do all these checks as it'll always be correct.
00645    * After all, all checks are based on the base language.
00646    */
00647   if (!_translation) return true;
00648 
00649   ParsedCommandStruct templ;
00650   ParsedCommandStruct lang;
00651   bool result = true;
00652 
00653   ExtractCommandString(&templ, b, true);
00654   ExtractCommandString(&lang, a, true);
00655 
00656   /* For each string in templ, see if we find it in lang */
00657   if (templ.np != lang.np) {
00658     strgen_warning("%s: template string and language string have a different # of commands", name);
00659     result = false;
00660   }
00661 
00662   for (uint i = 0; i < templ.np; i++) {
00663     /* see if we find it in lang, and zero it out */
00664     bool found = false;
00665     for (uint j = 0; j < lang.np; j++) {
00666       if (templ.pairs[i].a == lang.pairs[j].a &&
00667           strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) {
00668         /* it was found in both. zero it out from lang so we don't find it again */
00669         lang.pairs[j].a = NULL;
00670         found = true;
00671         break;
00672       }
00673     }
00674 
00675     if (!found) {
00676       strgen_warning("%s: command '%s' exists in template file but not in language file", name, templ.pairs[i].a->cmd);
00677       result = false;
00678     }
00679   }
00680 
00681   /* if we reach here, all non consumer commands match up.
00682    * Check if the non consumer commands match up also. */
00683   for (uint i = 0; i < lengthof(templ.cmd); i++) {
00684     if (TranslateCmdForCompare(templ.cmd[i]) != lang.cmd[i]) {
00685       strgen_warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i,
00686         lang.cmd[i]  == NULL ? "<empty>" : TranslateCmdForCompare(lang.cmd[i])->cmd,
00687         templ.cmd[i] == NULL ? "<empty>" : templ.cmd[i]->cmd);
00688       result = false;
00689     }
00690   }
00691 
00692   return result;
00693 }
00694 
00695 void StringReader::HandleString(char *str)
00696 {
00697   if (*str == '#') {
00698     if (str[1] == '#' && str[2] != '#') this->HandlePragma(str + 2);
00699     return;
00700   }
00701 
00702   /* Ignore comments & blank lines */
00703   if (*str == ';' || *str == ' ' || *str == '\0') return;
00704 
00705   char *s = strchr(str, ':');
00706   if (s == NULL) {
00707     strgen_error("Line has no ':' delimiter");
00708     return;
00709   }
00710 
00711   char *t;
00712   /* Trim spaces.
00713    * After this str points to the command name, and s points to the command contents */
00714   for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--) {}
00715   *t = 0;
00716   s++;
00717 
00718   /* Check string is valid UTF-8 */
00719   const char *tmp;
00720   for (tmp = s; *tmp != '\0';) {
00721     size_t len = Utf8Validate(tmp);
00722     if (len == 0) strgen_fatal("Invalid UTF-8 sequence in '%s'", s);
00723 
00724     WChar c;
00725     Utf8Decode(&c, tmp);
00726     if (c <= 0x001F || // ASCII control character range
00727         c == 0x200B || // Zero width space
00728         (c >= 0xE000 && c <= 0xF8FF) || // Private range
00729         (c >= 0xFFF0 && c <= 0xFFFF)) { // Specials range
00730       strgen_fatal("Unwanted UTF-8 character U+%04X in sequence '%s'", c, s);
00731     }
00732 
00733     tmp += len;
00734   }
00735 
00736   /* Check if the string has a case..
00737    * The syntax for cases is IDENTNAME.case */
00738   char *casep = strchr(str, '.');
00739   if (casep != NULL) *casep++ = '\0';
00740 
00741   /* Check if this string already exists.. */
00742   LangString *ent = this->data.Find(str);
00743 
00744   if (this->master) {
00745     if (casep != NULL) {
00746       strgen_error("Cases in the base translation are not supported.");
00747       return;
00748     }
00749 
00750     if (ent != NULL) {
00751       strgen_error("String name '%s' is used multiple times", str);
00752       return;
00753     }
00754 
00755     if (this->data.strings[this->data.next_string_id] != NULL) {
00756       strgen_error("String ID 0x%X for '%s' already in use by '%s'", this->data.next_string_id, str, this->data.strings[this->data.next_string_id]->name);
00757       return;
00758     }
00759 
00760     /* Allocate a new LangString */
00761     this->data.Add(str, new LangString(str, s, this->data.next_string_id++, _cur_line));
00762   } else {
00763     if (ent == NULL) {
00764       strgen_warning("String name '%s' does not exist in master file", str);
00765       return;
00766     }
00767 
00768     if (ent->translated && casep == NULL) {
00769       strgen_error("String name '%s' is used multiple times", str);
00770       return;
00771     }
00772 
00773     /* make sure that the commands match */
00774     if (!CheckCommandsMatch(s, ent->english, str)) return;
00775 
00776     if (casep != NULL) {
00777       ent->translated_case = new Case(ResolveCaseName(casep, strlen(casep)), s, ent->translated_case);
00778     } else {
00779       ent->translated = strdup(s);
00780       /* If the string was translated, use the line from the
00781        * translated language so errors in the translated file
00782        * are properly referenced to. */
00783       ent->line = _cur_line;
00784     }
00785   }
00786 }
00787 
00788 void StringReader::HandlePragma(char *str)
00789 {
00790   if (!memcmp(str, "plural ", 7)) {
00791     _lang.plural_form = atoi(str + 7);
00792     if (_lang.plural_form >= lengthof(_plural_forms)) {
00793       strgen_fatal("Invalid pluralform %d", _lang.plural_form);
00794     }
00795   } else {
00796     strgen_fatal("unknown pragma '%s'", str);
00797   }
00798 }
00799 
00800 static void rstrip(char *buf)
00801 {
00802   size_t i = strlen(buf);
00803   while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--;
00804   buf[i] = '\0';
00805 }
00806 
00807 void StringReader::ParseFile()
00808 {
00809   char buf[2048];
00810   _warnings = _errors = 0;
00811 
00812   _translation = this->master || this->translation;
00813   _file = this->file;
00814 
00815   /* For each new file we parse, reset the genders, and language codes. */
00816   MemSetT(&_lang, 0);
00817   strecpy(_lang.digit_group_separator, ",", lastof(_lang.digit_group_separator));
00818   strecpy(_lang.digit_group_separator_currency, ",", lastof(_lang.digit_group_separator_currency));
00819   strecpy(_lang.digit_decimal_separator, ".", lastof(_lang.digit_decimal_separator));
00820 
00821   _cur_line = 1;
00822   while (this->ReadLine(buf, sizeof(buf)) != NULL) {
00823     rstrip(buf);
00824     this->HandleString(buf);
00825     _cur_line++;
00826   }
00827 }
00828 
00833 void HeaderWriter::WriteHeader(const StringData &data)
00834 {
00835   int last = 0;
00836   for (size_t i = 0; i < data.max_strings; i++) {
00837     if (data.strings[i] != NULL) {
00838       this->WriteStringID(data.strings[i]->name, (int)i);
00839       last = (int)i;
00840     }
00841   }
00842 
00843   this->WriteStringID("STR_LAST_STRINGID", last);
00844 }
00845 
00846 static int TranslateArgumentIdx(int argidx, int offset)
00847 {
00848   int sum;
00849 
00850   if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd)) {
00851     strgen_fatal("invalid argidx %d", argidx);
00852   }
00853   const CmdStruct *cs = _cur_pcs.cmd[argidx];
00854   if (cs != NULL && cs->consumes <= offset) {
00855     strgen_fatal("invalid argidx offset %d:%d", argidx, offset);
00856   }
00857 
00858   if (_cur_pcs.cmd[argidx] == NULL) {
00859     strgen_fatal("no command for this argidx %d", argidx);
00860   }
00861 
00862   for (int i = sum = 0; i < argidx; i++) {
00863     const CmdStruct *cs = _cur_pcs.cmd[i];
00864 
00865     sum += (cs != NULL) ? cs->consumes : 1;
00866   }
00867 
00868   return sum + offset;
00869 }
00870 
00871 static void PutArgidxCommand(Buffer *buffer)
00872 {
00873   buffer->AppendUtf8(SCC_ARG_INDEX);
00874   buffer->AppendByte(TranslateArgumentIdx(_cur_argidx));
00875 }
00876 
00877 
00878 static void PutCommandString(Buffer *buffer, const char *str)
00879 {
00880   _cur_argidx = 0;
00881 
00882   while (*str != '\0') {
00883     /* Process characters as they are until we encounter a { */
00884     if (*str != '{') {
00885       buffer->AppendByte(*str++);
00886       continue;
00887     }
00888 
00889     char param[MAX_COMMAND_PARAM_SIZE];
00890     int argno;
00891     int casei;
00892     const CmdStruct *cs = ParseCommandString(&str, param, &argno, &casei);
00893     if (cs == NULL) break;
00894 
00895     if (casei != -1) {
00896       buffer->AppendUtf8(SCC_SET_CASE); // {SET_CASE}
00897       buffer->AppendByte(casei);
00898     }
00899 
00900     /* For params that consume values, we need to handle the argindex properly */
00901     if (cs->consumes > 0) {
00902       /* Check if we need to output a move-param command */
00903       if (argno != -1 && argno != _cur_argidx) {
00904         _cur_argidx = argno;
00905         PutArgidxCommand(buffer);
00906       }
00907 
00908       /* Output the one from the master string... it's always accurate. */
00909       cs = _cur_pcs.cmd[_cur_argidx++];
00910       if (cs == NULL) {
00911         strgen_fatal("%s: No argument exists at position %d", _cur_ident, _cur_argidx - 1);
00912       }
00913     }
00914 
00915     cs->proc(buffer, param, cs->value);
00916   }
00917 }
00918 
00923 void LanguageWriter::WriteLength(uint length)
00924 {
00925   char buffer[2];
00926   int offs = 0;
00927   if (length >= 0x4000) {
00928     strgen_fatal("string too long");
00929   }
00930 
00931   if (length >= 0xC0) {
00932     buffer[offs++] = (length >> 8) | 0xC0;
00933   }
00934   buffer[offs++] = length & 0xFF;
00935   this->Write((byte*)buffer, offs);
00936 }
00937 
00942 void LanguageWriter::WriteLang(const StringData &data)
00943 {
00944   uint *in_use = AllocaM(uint, data.tabs);
00945   for (size_t tab = 0; tab < data.tabs; tab++) {
00946     uint n = data.CountInUse((uint)tab);
00947 
00948     in_use[tab] = n;
00949     _lang.offsets[tab] = TO_LE16(n);
00950 
00951     for (uint j = 0; j != in_use[tab]; j++) {
00952       const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
00953       if (ls != NULL && ls->translated == NULL) _lang.missing++;
00954     }
00955   }
00956 
00957   _lang.ident = TO_LE32(LanguagePackHeader::IDENT);
00958   _lang.version = TO_LE32(data.Version());
00959   _lang.missing = TO_LE16(_lang.missing);
00960   _lang.winlangid = TO_LE16(_lang.winlangid);
00961 
00962   this->WriteHeader(&_lang);
00963   Buffer buffer;
00964 
00965   for (size_t tab = 0; tab < data.tabs; tab++) {
00966     for (uint j = 0; j != in_use[tab]; j++) {
00967       const LangString *ls = data.strings[(tab * TAB_SIZE) + j];
00968       const Case *casep;
00969       const char *cmdp;
00970 
00971       /* For undefined strings, just set that it's an empty string */
00972       if (ls == NULL) {
00973         this->WriteLength(0);
00974         continue;
00975       }
00976 
00977       _cur_ident = ls->name;
00978       _cur_line = ls->line;
00979 
00980       /* Produce a message if a string doesn't have a translation. */
00981       if (_show_todo > 0 && ls->translated == NULL) {
00982         if ((_show_todo & 2) != 0) {
00983           strgen_warning("'%s' is untranslated", ls->name);
00984         }
00985         if ((_show_todo & 1) != 0) {
00986           const char *s = "<TODO> ";
00987           while (*s != '\0') buffer.AppendByte(*s++);
00988         }
00989       }
00990 
00991       /* Extract the strings and stuff from the english command string */
00992       ExtractCommandString(&_cur_pcs, ls->english, false);
00993 
00994       if (ls->translated_case != NULL || ls->translated != NULL) {
00995         casep = ls->translated_case;
00996         cmdp = ls->translated;
00997       } else {
00998         casep = NULL;
00999         cmdp = ls->english;
01000       }
01001 
01002       _translated = cmdp != ls->english;
01003 
01004       if (casep != NULL) {
01005         const Case *c;
01006         uint num;
01007 
01008         /* Need to output a case-switch.
01009          * It has this format
01010          * <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
01011          * Each LEN is printed using 2 bytes in big endian order. */
01012         buffer.AppendUtf8(SCC_SWITCH_CASE);
01013         /* Count the number of cases */
01014         for (num = 0, c = casep; c; c = c->next) num++;
01015         buffer.AppendByte(num);
01016 
01017         /* Write each case */
01018         for (c = casep; c != NULL; c = c->next) {
01019           buffer.AppendByte(c->caseidx);
01020           /* Make some space for the 16-bit length */
01021           uint pos = buffer.Length();
01022           buffer.AppendByte(0);
01023           buffer.AppendByte(0);
01024           /* Write string */
01025           PutCommandString(&buffer, c->string);
01026           buffer.AppendByte(0); // terminate with a zero
01027           /* Fill in the length */
01028           uint size = buffer.Length() - (pos + 2);
01029           buffer[pos + 0] = GB(size, 8, 8);
01030           buffer[pos + 1] = GB(size, 0, 8);
01031         }
01032       }
01033 
01034       if (cmdp != NULL) PutCommandString(&buffer, cmdp);
01035 
01036       this->WriteLength(buffer.Length());
01037       this->Write(buffer.Begin(), buffer.Length());
01038       buffer.Clear();
01039     }
01040   }
01041 }