From a89fed21c78df05b18e30dcb72cef60331d24556 Mon Sep 17 00:00:00 2001 From: rubidium Date: Sun, 7 Nov 2010 18:20:18 +0000 Subject: (svn r21112) -Codechange: reduce the amount (of copying) variables --- src/strgen/strgen.cpp | 82 +++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index 121301cdb..e950e5e99 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -13,6 +13,7 @@ #include "../core/alloc_func.hpp" #include "../core/endian_func.hpp" #include "../core/math_func.hpp" +#include "../core/mem_func.hpp" #include "../string_func.h" #include "../strings_type.h" #include "strgen.h" @@ -67,6 +68,7 @@ struct LangString { }; static LangString *_strings[65536]; +static LanguagePackHeader _lang; ///< Header information about a language. #define HASH_SIZE 32767 @@ -77,14 +79,6 @@ static int _put_pos; static int _next_string_id; static uint32 _hash; -static char _lang_name[32], _lang_ownname[32], _lang_isocode[16]; -static char _lang_digit_group_separator[8]; -static char _lang_digit_group_separator_currency[8]; -static char _lang_digit_decimal_separator[8]; -static byte _lang_pluralform; -static byte _lang_textdir; -static uint16 _lang_winlangid; -static uint8 _lang_newgrflangid; #define MAX_NUM_GENDER 8 static char _genders[MAX_NUM_GENDER][16]; static uint _numgenders; @@ -352,16 +346,16 @@ static void EmitPlural(char *buf, int value) error("%s: No plural words", _cur_ident); } - if (_plural_forms[_lang_pluralform].plural_count != nw) { + if (_plural_forms[_lang.plural_form].plural_count != nw) { if (_translated) { error("%s: Invalid number of plural forms. Expecting %d, found %d.", _cur_ident, - _plural_forms[_lang_pluralform].plural_count, nw); + _plural_forms[_lang.plural_form].plural_count, nw); } else { if ((_show_todo & 2) != 0) strgen_warning("'%s' is untranslated. Tweaking english string to allow compilation for plural forms", _cur_ident); - if (nw > _plural_forms[_lang_pluralform].plural_count) { - nw = _plural_forms[_lang_pluralform].plural_count; + if (nw > _plural_forms[_lang.plural_form].plural_count) { + nw = _plural_forms[_lang.plural_form].plural_count; } else { - for (; nw < _plural_forms[_lang_pluralform].plural_count; nw++) { + for (; nw < _plural_forms[_lang.plural_form].plural_count; nw++) { words[nw] = words[nw - 1]; } } @@ -517,47 +511,47 @@ static void HandlePragma(char *str) if (!memcmp(str, "id ", 3)) { _next_string_id = strtoul(str + 3, NULL, 0); } else if (!memcmp(str, "name ", 5)) { - strecpy(_lang_name, str + 5, lastof(_lang_name)); + strecpy(_lang.name, str + 5, lastof(_lang.name)); } else if (!memcmp(str, "ownname ", 8)) { - strecpy(_lang_ownname, str + 8, lastof(_lang_ownname)); + strecpy(_lang.own_name, str + 8, lastof(_lang.own_name)); } else if (!memcmp(str, "isocode ", 8)) { - strecpy(_lang_isocode, str + 8, lastof(_lang_isocode)); + strecpy(_lang.isocode, str + 8, lastof(_lang.isocode)); } else if (!memcmp(str, "plural ", 7)) { - _lang_pluralform = atoi(str + 7); - if (_lang_pluralform >= lengthof(_plural_forms)) { - error("Invalid pluralform %d", _lang_pluralform); + _lang.plural_form = atoi(str + 7); + if (_lang.plural_form >= lengthof(_plural_forms)) { + error("Invalid pluralform %d", _lang.plural_form); } } else if (!memcmp(str, "textdir ", 8)) { if (!memcmp(str + 8, "ltr", 3)) { - _lang_textdir = TD_LTR; + _lang.text_dir = TD_LTR; } else if (!memcmp(str + 8, "rtl", 3)) { - _lang_textdir = TD_RTL; + _lang.text_dir = TD_RTL; } else { error("Invalid textdir %s", str + 8); } } else if (!memcmp(str, "digitsep ", 9)) { str += 9; - strecpy(_lang_digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang_digit_group_separator)); + strecpy(_lang.digit_group_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator)); } else if (!memcmp(str, "digitsepcur ", 12)) { str += 12; - strecpy(_lang_digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang_digit_group_separator_currency)); + strecpy(_lang.digit_group_separator_currency, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_group_separator_currency)); } else if (!memcmp(str, "decimalsep ", 11)) { str += 11; - strecpy(_lang_digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang_digit_decimal_separator)); + strecpy(_lang.digit_decimal_separator, strcmp(str, "{NBSP}") == 0 ? NBSP : str, lastof(_lang.digit_decimal_separator)); } else if (!memcmp(str, "winlangid ", 10)) { const char *buf = str + 10; long langid = strtol(buf, NULL, 16); if (langid > (long)UINT16_MAX || langid < 0) { error("Invalid winlangid %s", buf); } - _lang_winlangid = (uint16)langid; + _lang.winlangid = (uint16)langid; } else if (!memcmp(str, "grflangid ", 10)) { const char *buf = str + 10; long langid = strtol(buf, NULL, 16); if (langid >= 0x7F || langid < 0) { error("Invalid grflangid %s", buf); } - _lang_newgrflangid = (uint8)langid; + _lang.newgrflangid = (uint8)langid; } else if (!memcmp(str, "gender ", 7)) { char *buf = str + 7; @@ -824,14 +818,11 @@ static void ParseFile(const char *file, bool english) _file = file; /* For each new file we parse, reset the genders, and language codes */ + MemSetT(&_lang, 0); _numgenders = 0; - _lang_name[0] = _lang_ownname[0] = _lang_isocode[0] = '\0'; - strecpy(_lang_digit_group_separator, ",", lastof(_lang_digit_group_separator)); - strecpy(_lang_digit_group_separator_currency, ",", lastof(_lang_digit_group_separator_currency)); - strecpy(_lang_digit_decimal_separator, ".", lastof(_lang_digit_decimal_separator)); - _lang_textdir = TD_LTR; - _lang_winlangid = 0x0000; // neutral language code - _lang_newgrflangid = 0; // standard english + strecpy(_lang.digit_group_separator, ",", lastof(_lang.digit_group_separator)); + strecpy(_lang.digit_group_separator_currency, ",", lastof(_lang.digit_group_separator_currency)); + strecpy(_lang.digit_decimal_separator, ".", lastof(_lang.digit_decimal_separator)); /* TODO:!! We can't reset the cases. In case the translated strings * derive some strings from english.... */ @@ -845,7 +836,7 @@ static void ParseFile(const char *file, bool english) } fclose(in); - if (StrEmpty(_lang_name) || StrEmpty(_lang_ownname) || StrEmpty(_lang_isocode)) { + if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) { error("Language must include ##name, ##ownname and ##isocode"); } } @@ -1066,35 +1057,24 @@ static void WriteLength(FILE *f, uint length) static void WriteLangfile(const char *filename) { uint in_use[32]; - LanguagePackHeader hdr; _output_filename = filename; _output_file = fopen(filename, "wb"); if (_output_file == NULL) error("can't open %s", filename); - memset(&hdr, 0, sizeof(hdr)); for (int i = 0; i != 32; i++) { uint n = CountInUse(i); in_use[i] = n; - hdr.offsets[i] = TO_LE16(n); + _lang.offsets[i] = TO_LE16(n); } /* see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...) */ - hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG' - hdr.version = TO_LE32(_hash); - hdr.plural_form = _lang_pluralform; - hdr.text_dir = _lang_textdir; - hdr.winlangid = TO_LE16(_lang_winlangid); - hdr.newgrflangid = _lang_newgrflangid; - strecpy(hdr.name, _lang_name, lastof(hdr.name)); - strecpy(hdr.own_name, _lang_ownname, lastof(hdr.own_name)); - strecpy(hdr.isocode, _lang_isocode, lastof(hdr.isocode)); - strecpy(hdr.digit_group_separator, _lang_digit_group_separator, lastof(hdr.digit_group_separator)); - strecpy(hdr.digit_group_separator_currency, _lang_digit_group_separator_currency, lastof(hdr.digit_group_separator_currency)); - strecpy(hdr.digit_decimal_separator, _lang_digit_decimal_separator, lastof(hdr.digit_decimal_separator)); - - fwrite(&hdr, sizeof(hdr), 1, _output_file); + _lang.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG' + _lang.version = TO_LE32(_hash); + _lang.winlangid = TO_LE16(_lang.winlangid); + + fwrite(&_lang, sizeof(_lang), 1, _output_file); for (int i = 0; i != 32; i++) { for (uint j = 0; j != in_use[i]; j++) { -- cgit v1.2.3-54-g00ecf