39 #include "table/strings.h"
61 assert(this->
type !=
nullptr);
73 Debug(misc, 0,
"Trying to read invalid string parameter");
76 if (this->type !=
nullptr) {
77 if (this->type[this->
offset] != 0 && this->type[this->
offset] != type) {
78 Debug(misc, 0,
"Trying to read string parameter with wrong type");
97 while (max_value >= 10) {
115 uint64 val = count > 1 ? front : next;
116 for (; count > 1; count--) {
117 val = 10 * val + next;
158 for (
int i = 0; i < num; i++) {
160 strings[i] =
stredup((
const char *)(
size_t)_global_string_params.GetParam(i));
161 dst[i] = (size_t)strings[i];
163 strings[i] =
nullptr;
168 static char *StationGetSpecialString(
char *buff,
int x,
const char *last);
169 static char *GetSpecialTownNameString(
char *buff,
int ind, uint32 seed,
const char *last);
170 static char *GetSpecialNameString(
char *buff,
int ind,
StringParameters *args,
const char *last);
172 static char *
FormatString(
char *buff,
const char *str,
StringParameters *args,
const char *last, uint case_index = 0,
bool game_script =
false,
bool dry_run =
false);
182 delete[]
reinterpret_cast<char*
>(langpack);
187 std::unique_ptr<LanguagePack, LanguagePackDeleter> langpack;
189 std::vector<char *> offsets;
200 const char *GetStringPtr(
StringID string)
205 case TEXT_TAB_OLD_NEWGRF: NOT_REACHED();
230 if (index >= 0xC0 && !game_script) {
231 return GetSpecialTownNameString(buffr, index - 0xC0, args->
GetInt32(), last);
235 case TEXT_TAB_SPECIAL:
236 if (index >= 0xE4 && !game_script) {
237 return GetSpecialNameString(buffr, index - 0xE4, args, last);
241 case TEXT_TAB_OLD_CUSTOM:
244 error(
"Incorrect conversion of custom name string.");
251 case TEXT_TAB_OLD_NEWGRF:
265 error(
"String 0x%X is invalid. You are probably using an old version of the .lng file.\n",
string);
268 return FormatString(buffr, GetStringPtr(
string), args, last, case_index);
274 _global_string_params.
offset = 0;
323 static char *
FormatNumber(
char *buff, int64 number,
const char *last,
const char *separator,
int zerofill = 1,
int fractional_digits = 0)
325 static const int max_digits = 20;
326 uint64 divisor = 10000000000000000000ULL;
327 zerofill += fractional_digits;
328 int thousands_offset = (max_digits - fractional_digits - 1) % 3;
337 for (
int i = 0; i < max_digits; i++) {
338 if (i == max_digits - fractional_digits) {
340 if (
StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
341 buff +=
seprintf(buff, last,
"%s", decimal_separator);
345 if (num >= divisor) {
346 quot = num / divisor;
349 if ((tot |= quot) || i >= max_digits - zerofill) {
350 buff +=
seprintf(buff, last,
"%i", (
int)quot);
351 if ((i % 3) == thousands_offset && i < max_digits - 1 - fractional_digits) buff =
strecpy(buff, separator, last);
362 static char *FormatCommaNumber(
char *buff, int64 number,
const char *last,
int fractional_digits = 0)
365 if (
StrEmpty(separator)) separator = _langpack.langpack->digit_group_separator;
366 return FormatNumber(buff, number, last, separator, 1, fractional_digits);
369 static char *FormatNoCommaNumber(
char *buff, int64 number,
const char *last)
374 static char *FormatZerofillNumber(
char *buff, int64 number, int64 count,
const char *last)
379 static char *FormatHexNumber(
char *buff, uint64 number,
const char *last)
381 return buff +
seprintf(buff, last,
"0x" OTTD_PRINTFHEX64, number);
391 static char *
FormatBytes(
char *buff, int64 number,
const char *last)
396 const char *
const iec_prefixes[] = {
"",
"Ki",
"Mi",
"Gi",
"Ti",
"Pi",
"Ei"};
398 while (number >= 1024 * 1024) {
404 if (
StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
408 buff +=
seprintf(buff, last,
"%i", (
int)number);
409 }
else if (number < 1024 * 10) {
410 buff +=
seprintf(buff, last,
"%i%s%02i", (
int)number / 1024, decimal_separator, (
int)(number % 1024) * 100 / 1024);
411 }
else if (number < 1024 * 100) {
412 buff +=
seprintf(buff, last,
"%i%s%01i", (
int)number / 1024, decimal_separator, (
int)(number % 1024) * 10 / 1024);
414 assert(number < 1024 * 1024);
415 buff +=
seprintf(buff, last,
"%i", (
int)number / 1024);
418 assert(
id <
lengthof(iec_prefixes));
419 buff +=
seprintf(buff, last,
NBSP "%sB", iec_prefixes[
id]);
424 static char *FormatYmdString(
char *buff,
Date date,
const char *last, uint case_index)
429 int64 args[] = {ymd.
day + STR_DAY_NUMBER_1ST - 1, STR_MONTH_ABBREV_JAN + ymd.
month, ymd.
year};
431 return FormatString(buff, GetStringPtr(STR_FORMAT_DATE_LONG), &tmp_params, last, case_index);
434 static char *FormatMonthAndYear(
char *buff,
Date date,
const char *last, uint case_index)
439 int64 args[] = {STR_MONTH_JAN + ymd.
month, ymd.
year};
441 return FormatString(buff, GetStringPtr(STR_FORMAT_DATE_SHORT), &tmp_params, last, case_index);
444 static char *FormatTinyOrISODate(
char *buff,
Date date,
StringID str,
const char *last)
455 int64 args[] = {(int64)(
size_t)day, (int64)(
size_t)month, ymd.
year};
457 return FormatString(buff, GetStringPtr(str), &tmp_params, last);
460 static char *FormatGenericCurrency(
char *buff,
const CurrencySpec *spec,
Money number,
bool compact,
const char *last)
464 bool negative = number < 0;
465 const char *multiplier =
"";
467 number *= spec->
rate;
471 if (buff +
Utf8CharLen(SCC_PUSH_COLOUR) > last)
return buff;
473 if (buff +
Utf8CharLen(SCC_RED) > last)
return buff;
475 buff =
strecpy(buff,
"-", last);
488 if (number >= 1000000000 - 500) {
489 number = (number + 500000) / 1000000;
490 multiplier =
NBSP "M";
491 }
else if (number >= 1000000) {
492 number = (number + 500) / 1000;
493 multiplier =
NBSP "k";
498 if (
StrEmpty(separator)) separator = _currency->separator.c_str();
499 if (
StrEmpty(separator)) separator = _langpack.langpack->digit_group_separator_currency;
501 buff =
strecpy(buff, multiplier, last);
509 if (buff +
Utf8CharLen(SCC_POP_COLOUR) > last)
return buff;
526 uint64 n =
abs(count);
528 switch (plural_form) {
537 return n != 1 ? 1 : 0;
549 return n > 1 ? 1 : 0;
556 return n % 10 == 1 && n % 100 != 11 ? 0 : n != 0 ? 1 : 2;
562 return n == 1 ? 0 : n == 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4;
568 return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
574 return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
580 return n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2;
586 return n % 100 == 1 ? 0 : n % 100 == 2 ? 1 : n % 100 == 3 || n % 100 == 4 ? 2 : 3;
592 return n % 10 == 1 && n % 100 != 11 ? 0 : 1;
598 return n == 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2;
629 return (n == 1 ? 0 : n == 0 || (n % 100 > 1 && n % 100 < 11) ? 1 : (n % 100 > 10 && n % 100 < 20) ? 2 : 3);
634 return ((n == 1 || n == 11) ? 0 : (n == 2 || n == 12) ? 1 : ((n > 2 && n < 11) || (n > 12 && n < 20)) ? 2 : 3);
640 return n == 1 ? 0 : (n == 0 || (n % 100 > 0 && n % 100 < 20)) ? 1 : 2;
644 static const char *ParseStringChoice(
const char *b, uint form,
char **dst,
const char *last)
648 uint pos, i, mypos = 0;
650 for (i = pos = 0; i != n; i++) {
651 uint len = (byte)*b++;
652 if (i == form) mypos = pos;
656 *dst +=
seprintf(*dst, last,
"%s", b + mypos);
673 return ((input * this->multiplier) + (round && this->shift != 0 ? 1 << (this->shift - 1) : 0)) >> this->
shift;
683 int64
FromDisplay(int64 input,
bool round =
true, int64 divider = 1)
const
685 return ((input << this->shift) + (round ? (this->multiplier * divider) - 1 : 0)) / (this->multiplier * divider);
705 { { 1, 0}, STR_UNITS_VELOCITY_IMPERIAL, 0 },
706 { { 103, 6}, STR_UNITS_VELOCITY_METRIC, 0 },
707 { { 1831, 12}, STR_UNITS_VELOCITY_SI, 0 },
708 { {37888, 16}, STR_UNITS_VELOCITY_GAMEUNITS, 1 },
713 { { 1, 0}, STR_UNITS_POWER_IMPERIAL, 0 },
714 { {4153, 12}, STR_UNITS_POWER_METRIC, 0 },
715 { {6109, 13}, STR_UNITS_POWER_SI, 0 },
720 { { 29, 5}, STR_UNITS_POWER_IMPERIAL_TO_WEIGHT_IMPERIAL, 1},
721 { { 1, 0}, STR_UNITS_POWER_IMPERIAL_TO_WEIGHT_METRIC, 1},
722 { { 1, 0}, STR_UNITS_POWER_IMPERIAL_TO_WEIGHT_SI, 1},
723 { { 59, 6}, STR_UNITS_POWER_METRIC_TO_WEIGHT_IMPERIAL, 1},
724 { { 65, 6}, STR_UNITS_POWER_METRIC_TO_WEIGHT_METRIC, 1},
725 { { 65, 6}, STR_UNITS_POWER_METRIC_TO_WEIGHT_SI, 1},
726 { { 173, 8}, STR_UNITS_POWER_SI_TO_WEIGHT_IMPERIAL, 1},
727 { { 3, 2}, STR_UNITS_POWER_SI_TO_WEIGHT_METRIC, 1},
728 { { 3, 2}, STR_UNITS_POWER_SI_TO_WEIGHT_SI, 1},
733 { {4515, 12}, STR_UNITS_WEIGHT_SHORT_IMPERIAL, STR_UNITS_WEIGHT_LONG_IMPERIAL },
734 { { 1, 0}, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC },
735 { {1000, 0}, STR_UNITS_WEIGHT_SHORT_SI, STR_UNITS_WEIGHT_LONG_SI },
740 { {4227, 4}, STR_UNITS_VOLUME_SHORT_IMPERIAL, STR_UNITS_VOLUME_LONG_IMPERIAL },
741 { {1000, 0}, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC },
742 { { 1, 0}, STR_UNITS_VOLUME_SHORT_SI, STR_UNITS_VOLUME_LONG_SI },
747 { {3597, 4}, STR_UNITS_FORCE_IMPERIAL, 0 },
748 { {3263, 5}, STR_UNITS_FORCE_METRIC, 0 },
749 { { 1, 0}, STR_UNITS_FORCE_SI, 0 },
754 { { 3, 0}, STR_UNITS_HEIGHT_IMPERIAL, 0 },
755 { { 1, 0}, STR_UNITS_HEIGHT_METRIC, 0 },
756 { { 1, 0}, STR_UNITS_HEIGHT_SI, 0 },
811 uint orig_offset = args->
offset;
823 FormatString(buff, str_arg, args, last, case_index, game_script,
true);
826 FormatString(buff, str_arg, args, last, case_index, game_script,
true);
829 args->
offset = orig_offset;
832 uint next_substr_case_index = 0;
833 char *buf_start = buff;
834 std::stack<const char *, std::vector<const char *>> str_stack;
835 str_stack.push(str_arg);
838 while (!str_stack.empty() && (b = Utf8Consume(&str_stack.top())) ==
'\0') {
841 if (str_stack.empty())
break;
842 const char *&str = str_stack.top();
848 if (b == 0)
continue;
853 uint64 sub_args_data[20];
854 WChar sub_args_type[20];
855 bool sub_args_need_free[20];
859 memset(sub_args_need_free, 0,
sizeof(sub_args_need_free));
862 uint32 stringid = strtoul(str, &p, 16);
863 if (*p !=
':' && *p !=
'\0') {
864 while (*p !=
'\0') p++;
866 buff =
strecat(buff,
"(invalid SCC_ENCODED)", last);
870 while (*p !=
'\0') p++;
872 buff =
strecat(buff,
"(invalid StringID)", last);
877 while (*p !=
'\0' && i < 20) {
882 bool instring =
false;
889 if (*p ==
'"' && escape) {
896 instring = !instring;
903 if (*p ==
':')
break;
904 if (*p ==
'\0')
break;
911 bool lookup = (l == SCC_ENCODED);
912 if (lookup) s += len;
914 param = strtoull(s, &p, 16);
918 while (*p !=
'\0') p++;
920 buff =
strecat(buff,
"(invalid sub-StringID)", last);
926 sub_args.SetParam(i++, param);
931 sub_args_need_free[i] =
true;
932 sub_args.SetParam(i++, (uint64)(
size_t)g);
941 for (
int i = 0; i < 20; i++) {
942 if (sub_args_need_free[i])
free((
void *)sub_args.GetParam(i));
948 StringID substr = Utf8Consume(&str);
949 str_stack.push(GetStringPtr(substr));
955 str_stack.push(GetStringPtr(substr));
956 case_index = next_substr_case_index;
957 next_substr_case_index = 0;
962 case SCC_GENDER_LIST: {
964 uint offset = orig_offset + (byte)*str++;
985 WChar c = Utf8Consume(&s);
987 if (c == SCC_GENDER_INDEX) gender = (byte)s[0];
989 str = ParseStringChoice(str, gender, &buff, last);
995 case SCC_GENDER_INDEX:
1004 case SCC_PLURAL_LIST: {
1005 int plural_form = *str++;
1006 uint offset = orig_offset + (byte)*str++;
1012 case SCC_ARG_INDEX: {
1013 args->
offset = orig_offset + (byte)*str++;
1017 case SCC_SET_CASE: {
1020 next_substr_case_index = (byte)*str++;
1024 case SCC_SWITCH_CASE: {
1027 uint num = (byte)*str++;
1029 if ((
byte)str[0] == case_index) {
1035 str += 3 + (str[1] << 8) + str[2];
1042 buff =
strecpy(buff, _openttd_revision, last);
1045 case SCC_RAW_STRING_POINTER: {
1046 if (game_script)
break;
1047 const char *str = (
const char *)(
size_t)args->
GetInt64(SCC_RAW_STRING_POINTER);
1059 buff =
GetStringWithArgs(buff, str, &tmp_params, last, next_substr_case_index, game_script);
1060 next_substr_case_index = 0;
1074 uint size = b - SCC_STRING1 + 1;
1076 buff =
strecat(buff,
"(too many parameters)", last);
1079 buff =
GetStringWithArgs(buff, str, &sub_args, last, next_substr_case_index, game_script);
1081 next_substr_case_index = 0;
1086 buff = FormatCommaNumber(buff, args->
GetInt64(SCC_COMMA), last);
1090 int64 number = args->
GetInt64(SCC_DECIMAL);
1091 int digits = args->
GetInt32(SCC_DECIMAL);
1092 buff = FormatCommaNumber(buff, number, last, digits);
1097 buff = FormatNoCommaNumber(buff, args->
GetInt64(SCC_NUM), last);
1100 case SCC_ZEROFILL_NUM: {
1102 buff = FormatZerofillNumber(buff, num, args->
GetInt64(), last);
1107 buff = FormatHexNumber(buff, (uint64)args->
GetInt64(SCC_HEX), last);
1114 case SCC_CARGO_TINY: {
1123 switch (cargo_str) {
1138 buff = FormatCommaNumber(buff, amount, last);
1142 case SCC_CARGO_SHORT: {
1150 switch (cargo_str) {
1176 case SCC_CARGO_LONG: {
1187 case SCC_CARGO_LIST: {
1188 CargoTypes cmask = args->
GetInt64(SCC_CARGO_LIST);
1192 if (!
HasBit(cmask, cs->Index()))
continue;
1194 if (buff >= last - 2)
break;
1204 buff =
GetStringWithArgs(buff, cs->name, args, last, next_substr_case_index, game_script);
1208 if (first) buff =
GetStringWithArgs(buff, STR_JUST_NOTHING, args, last, next_substr_case_index, game_script);
1211 next_substr_case_index = 0;
1214 assert(buff < last);
1218 case SCC_CURRENCY_SHORT:
1219 buff = FormatGenericCurrency(buff, _currency, args->
GetInt64(),
true, last);
1222 case SCC_CURRENCY_LONG:
1223 buff = FormatGenericCurrency(buff, _currency, args->
GetInt64(SCC_CURRENCY_LONG),
false, last);
1227 buff = FormatTinyOrISODate(buff, args->
GetInt32(SCC_DATE_TINY), STR_FORMAT_DATE_TINY, last);
1230 case SCC_DATE_SHORT:
1231 buff = FormatMonthAndYear(buff, args->
GetInt32(SCC_DATE_SHORT), last, next_substr_case_index);
1232 next_substr_case_index = 0;
1236 buff = FormatYmdString(buff, args->
GetInt32(SCC_DATE_LONG), last, next_substr_case_index);
1237 next_substr_case_index = 0;
1241 buff = FormatTinyOrISODate(buff, args->
GetInt32(), STR_FORMAT_DATE_ISO, last);
1268 case SCC_POWER_TO_WEIGHT: {
1274 int64 args_array[] = {x.c.ToDisplay(args->
GetInt64()), x.decimal_places};
1277 buff =
FormatString(buff, GetStringPtr(x.s), &tmp_params, last);
1281 case SCC_VELOCITY: {
1290 case SCC_VOLUME_SHORT: {
1298 case SCC_VOLUME_LONG: {
1306 case SCC_WEIGHT_SHORT: {
1314 case SCC_WEIGHT_LONG: {
1322 case SCC_COMPANY_NAME: {
1324 if (c ==
nullptr)
break;
1326 if (!c->
name.empty()) {
1327 int64 args_array[] = {(int64)(
size_t)c->
name.c_str()};
1331 int64 args_array[] = {c->
name_2};
1338 case SCC_COMPANY_NUM: {
1343 int64 args_array[] = {company + 1};
1350 case SCC_DEPOT_NAME: {
1353 uint64 args_array[] = {(uint64)args->
GetInt32()};
1354 WChar types_array[] = {SCC_STATION_NAME};
1356 buff =
GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_AIRCRAFT, &tmp_params, last);
1361 if (!d->name.empty()) {
1362 int64 args_array[] = {(int64)(
size_t)d->name.c_str()};
1368 buff =
GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_TRAIN + 2 * vt + (d->
town_cn == 0 ? 0 : 1), &tmp_params, last);
1373 case SCC_ENGINE_NAME: {
1375 if (e ==
nullptr)
break;
1378 int64 args_array[] = {(int64)(
size_t)e->
name.c_str()};
1388 case SCC_GROUP_NAME: {
1390 if (g ==
nullptr)
break;
1392 if (!g->
name.empty()) {
1393 int64 args_array[] = {(int64)(
size_t)g->
name.c_str()};
1397 int64 args_array[] = {g->
index};
1405 case SCC_INDUSTRY_NAME: {
1407 if (i ==
nullptr)
break;
1419 buff =
FormatString(buff, GetStringPtr(STR_FORMAT_INDUSTRY_NAME), &tmp_params, last, next_substr_case_index);
1421 next_substr_case_index = 0;
1425 case SCC_PRESIDENT_NAME: {
1427 if (c ==
nullptr)
break;
1441 case SCC_STATION_NAME: {
1442 StationID sid = args->
GetInt32(SCC_STATION_NAME);
1445 if (st ==
nullptr) {
1454 if (!st->
name.empty()) {
1455 int64 args_array[] = {(int64)(
size_t)st->
name.c_str()};
1460 if (st->
indtype != IT_INVALID) {
1472 uint64 args_array[] = {STR_TOWN_NAME, st->
town->
index, st->
index};
1473 WChar types_array[] = {0, SCC_TOWN_NAME, SCC_NUM};
1480 case SCC_TOWN_NAME: {
1482 if (t ==
nullptr)
break;
1484 if (!t->
name.empty()) {
1485 int64 args_array[] = {(int64)(
size_t)t->
name.c_str()};
1494 case SCC_WAYPOINT_NAME: {
1496 if (wp ==
nullptr)
break;
1498 if (!wp->
name.empty()) {
1499 int64 args_array[] = {(int64)(
size_t)wp->
name.c_str()};
1505 StringID str = ((wp->
string_id == STR_SV_STNAME_BUOY) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME);
1512 case SCC_VEHICLE_NAME: {
1514 if (v ==
nullptr)
break;
1516 if (!v->
name.empty()) {
1517 int64 args_array[] = {(int64)(
size_t)v->
name.c_str()};
1524 buff =
GetStringWithArgs(buff, STR_FORMAT_GROUP_VEHICLE_NAME, &tmp_params, last);
1531 default: str = STR_INVALID_VEHICLE;
break;
1532 case VEH_TRAIN: str = STR_SV_TRAIN_NAME;
break;
1533 case VEH_ROAD: str = STR_SV_ROAD_VEHICLE_NAME;
break;
1534 case VEH_SHIP: str = STR_SV_SHIP_NAME;
break;
1543 case SCC_SIGN_NAME: {
1545 if (si ==
nullptr)
break;
1547 if (!si->name.empty()) {
1548 int64 args_array[] = {(int64)(
size_t)si->name.c_str()};
1558 case SCC_STATION_FEATURES: {
1559 buff = StationGetSpecialString(buff, args->
GetInt32(SCC_STATION_FEATURES), last);
1573 static char *StationGetSpecialString(
char *buff,
int x,
const char *last)
1584 static char *GetSpecialTownNameString(
char *buff,
int ind, uint32 seed,
const char *last)
1589 static const char *
const _silly_company_names[] = {
1591 "Tiny Transport Ltd.",
1593 "Comfy-Coach & Co.",
1594 "Crush & Bump Ltd.",
1595 "Broken & Late Ltd.",
1597 "Supersonic Travel",
1599 "Lightning International",
1600 "Pannik & Loozit Ltd.",
1601 "Inter-City Transport",
1602 "Getout & Pushit Ltd."
1605 static const char *
const _surname_list[] = {
1637 static const char *
const _silly_surname_list[] = {
1652 static const char _initial_name_letters[] = {
1653 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
1654 'K',
'L',
'M',
'N',
'P',
'R',
'S',
'T',
'W',
1657 static char *GenAndCoName(
char *buff, uint32 arg,
const char *last)
1659 const char *
const *base;
1663 base = _silly_surname_list;
1664 num =
lengthof(_silly_surname_list);
1666 base = _surname_list;
1670 buff =
strecpy(buff, base[num *
GB(arg, 16, 8) >> 8], last);
1671 buff =
strecpy(buff,
" & Co.", last);
1676 static char *GenPresidentName(
char *buff, uint32 x,
const char *last)
1678 char initial[] =
"?. ";
1679 const char *
const *base;
1683 initial[0] = _initial_name_letters[
sizeof(_initial_name_letters) *
GB(x, 0, 8) >> 8];
1684 buff =
strecpy(buff, initial, last);
1686 i = (
sizeof(_initial_name_letters) + 35) *
GB(x, 8, 8) >> 8;
1687 if (i <
sizeof(_initial_name_letters)) {
1688 initial[0] = _initial_name_letters[i];
1689 buff =
strecpy(buff, initial, last);
1693 base = _silly_surname_list;
1694 num =
lengthof(_silly_surname_list);
1696 base = _surname_list;
1700 buff =
strecpy(buff, base[num *
GB(x, 16, 8) >> 8], last);
1705 static char *GetSpecialNameString(
char *buff,
int ind,
StringParameters *args,
const char *last)
1709 return strecpy(buff, _silly_company_names[std::min<uint>(args->
GetInt32() & 0xFFFF,
lengthof(_silly_company_names) - 1)], last);
1712 return GenAndCoName(buff, args->
GetInt32(), last);
1715 return GenPresidentName(buff, args->
GetInt32(), last);
1719 if (
IsInsideMM(ind - 6, 0, SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1)) {
1720 buff = GetSpecialTownNameString(buff, ind - 6, args->
GetInt32(), last);
1721 return strecpy(buff,
" Transport", last);
1734 this->
version == TO_LE32(LANGUAGE_PACK_VERSION) &&
1754 return 4 * this->
missing < LANGUAGE_TOTAL_STRINGS;
1766 std::unique_ptr<LanguagePack, LanguagePackDeleter> lang_pack(
reinterpret_cast<LanguagePack *
>(
ReadFileToMem(lang->
file, len, 1U << 20).release()));
1767 if (!lang_pack)
return false;
1770 const char *end = (
char *)lang_pack.get() + len + 1;
1773 if (end <= lang_pack->data || !lang_pack->IsValid()) {
1777 #if TTD_ENDIAN == TTD_BIG_ENDIAN
1779 lang_pack->offsets[i] = ReadLE16Aligned(&lang_pack->offsets[i]);
1783 std::array<uint, TEXT_TAB_END> tab_start, tab_num;
1787 uint16 num = lang_pack->offsets[i];
1790 tab_start[i] = count;
1796 std::vector<char *> offs(count);
1799 char *s = lang_pack->data;
1801 for (uint i = 0; i < count; i++) {
1802 if (s + len >= end)
return false;
1805 len = ((len & 0x3F) << 8) + (byte)*s++;
1806 if (s + len >= end)
return false;
1814 _langpack.langpack = std::move(lang_pack);
1815 _langpack.offsets = std::move(offs);
1826 extern void Win32SetCurrentLocaleName(
const char *iso_code);
1835 #ifdef WITH_ICU_I18N
1837 UErrorCode status = U_ZERO_ERROR;
1842 if (U_FAILURE(status)) {
1866 #if !(defined(_WIN32) || defined(__APPLE__))
1879 env = std::getenv(
"LANGUAGE");
1880 if (env !=
nullptr)
return env;
1882 env = std::getenv(
"LC_ALL");
1883 if (env !=
nullptr)
return env;
1885 if (param !=
nullptr) {
1886 env = std::getenv(param);
1887 if (env !=
nullptr)
return env;
1890 return std::getenv(
"LANG");
1914 if (newgrflangid == lang.newgrflangid)
return ⟨
1928 FILE *f = fopen(file,
"rb");
1929 if (f ==
nullptr)
return false;
1931 size_t read = fread(hdr,
sizeof(*hdr), 1, f);
1934 bool ret = read == 1 && hdr->
IsValid();
1951 if (dir !=
nullptr) {
1952 struct dirent *dirent;
1953 while ((dirent = readdir(dir)) !=
nullptr) {
1954 std::string d_name =
FS2OTTD(dirent->d_name);
1955 const char *extension = strrchr(d_name.c_str(),
'.');
1958 if (extension ==
nullptr || strcmp(extension,
".lng") != 0)
continue;
1965 Debug(misc, 3,
"{} is not a valid language file", lmd.
file);
1967 Debug(misc, 3,
"{}'s language ID is already known", lmd.
file);
1983 std::string path = FioGetDirectory(sp,
LANG_DIR);
1990 if (lang ==
nullptr) lang =
"en_GB";
2001 const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1;
2003 chosen_language = &lng;
2007 if (strcmp (lng.isocode,
"en_GB") == 0) en_GB_fallback = &lng;
2010 if (!lng.IsReasonablyFinished())
continue;
2012 if (strncmp(lng.isocode, lang, 5) == 0) chosen_language = &lng;
2013 if (strncmp(lng.isocode, lang, 2) == 0) language_fallback = &lng;
2018 if (chosen_language ==
nullptr) {
2019 chosen_language = (language_fallback !=
nullptr) ? language_fallback : en_GB_fallback;
2031 return _langpack.langpack->isocode;
2041 const Sprite *question_mark[FS_END];
2044 question_mark[size] =
GetGlyph(size,
'?');
2050 for (
WChar c = Utf8Consume(&text); c !=
'\0'; c = Utf8Consume(&text)) {
2051 if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
2052 size = (
FontSize)(c - SCC_FIRST_FONT);
2055 std::string size_name;
2058 case 0: size_name =
"medium";
break;
2059 case 1: size_name =
"small";
break;
2060 case 2: size_name =
"large";
break;
2061 case 3: size_name =
"mono";
break;
2062 default: NOT_REACHED();
2065 Debug(fontcache, 0,
"Font is missing glyphs to display char 0x{:X} in {} font size", (
int)c, size_name);
2093 const char *ret = _langpack.offsets[_langpack.
langtab_start[this->
i] + this->
j];
2096 while (this->i < TEXT_TAB_END && this->
j >= _langpack.
langtab_num[this->i]) {
2111 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
2116 settings->small.os_handle = os_data;
2117 settings->medium.os_handle = os_data;
2118 settings->large.os_handle = os_data;
2139 if (searcher ==
nullptr) searcher = &pack_searcher;
2141 #if defined(WITH_FREETYPE) || defined(_WIN32) || defined(WITH_COCOA)
2145 bool any_font_configured = !_fcsettings.
medium.
font.empty();
2151 bad_font = !
SetFallbackFont(&_fcsettings, _langpack.langpack->isocode, _langpack.langpack->winlangid, searcher);
2153 _fcsettings = backup;
2155 if (!bad_font && any_font_configured) {
2162 static char *err_str =
stredup(
"XXXThe current font is missing some of the characters used in the texts for this language. Using system fallback font instead.");
2168 if (bad_font && base_font) {
2183 static char *err_str =
stredup(
"XXXThe current font is missing some of the characters used in the texts for this language. Read the readme to see how to solve this.");
2196 #if !defined(WITH_ICU_LX) && !defined(WITH_UNISCRIBE) && !defined(WITH_COCOA)
2211 static char *err_str =
stredup(
"XXXThis version of OpenTTD does not support right-to-left languages. Recompile with icu enabled.");