2005-07-24 16:12:37 +02:00
/* $Id$ */
2009-08-21 22:21:05 +02:00
/*
* This file is part of OpenTTD .
* 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.
* 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 .
* 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/>.
*/
2008-05-06 17:11:33 +02:00
/** @file strings.cpp Handling of translated strings. */
2007-04-04 03:35:16 +02:00
2004-08-09 19:04:08 +02:00
# include "stdafx.h"
2005-08-06 16:59:54 +02:00
# include "currency.h"
2008-03-31 02:06:17 +02:00
# include "station_base.h"
2004-08-09 19:04:08 +02:00
# include "town.h"
2004-11-15 20:25:59 +01:00
# include "screenshot.h"
2009-07-22 12:18:19 +02:00
# include "waypoint_base.h"
2010-05-12 20:19:36 +02:00
# include "depot_base.h"
2005-07-15 20:30:13 +02:00
# include "industry.h"
2006-04-21 05:00:20 +02:00
# include "newgrf_text.h"
2008-08-31 12:50:05 +02:00
# include "fileio_func.h"
2007-05-19 11:40:18 +02:00
# include "group.h"
2008-03-31 09:25:49 +02:00
# include "signs_base.h"
2009-01-31 21:16:06 +01:00
# include "cargotype.h"
2007-12-16 19:38:19 +01:00
# include "fontcache.h"
2011-12-10 14:54:10 +01:00
# include "error.h"
2007-12-21 20:49:27 +01:00
# include "strings_func.h"
2008-05-04 23:53:36 +02:00
# include "rev.h"
2011-03-18 14:34:52 +01:00
# include "core/alloc_type.hpp"
2007-12-22 22:01:49 +01:00
# include "core/endian_func.hpp"
2007-12-26 14:50:40 +01:00
# include "date_func.h"
2007-12-27 14:35:39 +01:00
# include "vehicle_base.h"
2008-04-29 23:31:29 +02:00
# include "engine_base.h"
2010-11-13 12:11:02 +01:00
# include "language.h"
2009-09-22 15:54:54 +02:00
# include "townname_func.h"
2010-01-15 17:41:15 +01:00
# include "string_func.h"
# include "company_base.h"
2010-03-28 14:25:09 +02:00
# include "smallmap_gui.h"
2010-11-27 23:43:58 +01:00
# include "window_func.h"
2010-11-10 18:48:31 +01:00
# include "debug.h"
2011-01-22 00:10:02 +01:00
# include <stack>
2004-08-09 19:04:08 +02:00
2008-01-13 02:21:35 +01:00
# include "table/strings.h"
# include "table/control_codes.h"
2006-08-05 01:45:20 +02:00
2010-11-13 12:28:55 +01:00
char _config_language_file [ MAX_PATH ] ; ///< The file (name) stored in the configuration.
2010-11-13 13:09:30 +01:00
LanguageList _languages ; ///< The actual list of language meta data.
2010-11-13 12:25:58 +01:00
const LanguageMetadata * _current_language = NULL ; ///< The currently loaded language.
2010-11-13 12:28:55 +01:00
TextDirection _current_text_dir ; ///< Text direction of the currently selected language.
2005-07-26 21:04:19 +02:00
2010-11-27 23:47:29 +01:00
# ifdef WITH_ICU
Collator * _current_collator = NULL ; ///< Collator for the language currently in use.
# endif /* WITH_ICU */
2011-03-18 14:34:52 +01:00
static uint64 _global_string_params_data [ 20 ] ; ///< Global array of string parameters. To access, use #SetDParam.
static WChar _global_string_params_type [ 20 ] ; ///< Type of parameters stored in #_decode_parameters
StringParameters _global_string_params ( _global_string_params_data , 20 , _global_string_params_type ) ;
2004-08-09 19:04:08 +02:00
2011-03-18 14:34:52 +01:00
/** Reset the type array. */
void StringParameters : : ClearTypeInformation ( )
{
assert ( this - > type ! = NULL ) ;
MemSetT ( this - > type , 0 , this - > num_param ) ;
}
2004-08-09 19:04:08 +02:00
2010-12-09 22:41:31 +01:00
/**
2011-03-18 14:34:52 +01:00
* Shift all data in the data array by the given amount to make
* room for some extra parameters .
2010-12-09 22:41:31 +01:00
*/
2011-03-18 14:34:52 +01:00
void StringParameters : : ShiftParameters ( uint amount )
2005-07-15 16:53:44 +02:00
{
2011-03-18 14:34:52 +01:00
assert ( amount < = this - > num_param ) ;
MemMoveT ( this - > data + amount , this - > data , this - > num_param - amount ) ;
2005-07-15 16:53:44 +02:00
}
2011-03-18 14:34:52 +01:00
/**
* Copy \ a num string parameters from array \ a src into the global string parameter array .
* @ param offs Index in the global array to copy the first string parameter to .
* @ param src Source array of string parameters .
* @ param num Number of string parameters to copy .
*/
void CopyInDParam ( int offs , const uint64 * src , int num )
2005-07-15 16:53:44 +02:00
{
2011-03-18 14:34:52 +01:00
MemCpyT ( _global_string_params . GetPointerToOffset ( offs ) , src , num ) ;
2005-07-15 16:53:44 +02:00
}
2010-12-09 22:41:31 +01:00
/**
2011-03-18 14:34:52 +01:00
* Copy \ a num string parameters from the global string parameter array to the \ a dst array .
* @ param dst Destination array of string parameters .
* @ param offs Index in the global array to copy the first string parameter from .
* @ param num Number of string parameters to copy .
2010-12-09 22:41:31 +01:00
*/
2011-03-18 14:34:52 +01:00
void CopyOutDParam ( uint64 * dst , int offs , int num )
2005-07-15 16:53:44 +02:00
{
2011-03-18 14:34:52 +01:00
MemCpyT ( dst , _global_string_params . GetPointerToOffset ( offs ) , num ) ;
2005-07-15 16:53:44 +02:00
}
2011-12-10 17:03:12 +01:00
/**
* Copy \ a num string parameters from the global string parameter array to the \ a dst array .
* Furthermore clone raw string parameters into \ a strings and amend the data in \ a dst .
* @ param dst Destination array of string parameters .
* @ param strings Destination array for clone of the raw strings . Must be of same length as dst . Deallocation left to the caller .
* @ param string The string used to determine where raw strings are and where there are no raw strings .
* @ param num Number of string parameters to copy .
*/
void CopyOutDParam ( uint64 * dst , const char * * strings , StringID string , int num )
{
char buf [ DRAW_STRING_BUFFER ] ;
GetString ( buf , string , lastof ( buf ) ) ;
MemCpyT ( dst , _global_string_params . GetPointerToOffset ( 0 ) , num ) ;
for ( int i = 0 ; i < num ; i + + ) {
if ( _global_string_params . HasTypeInformation ( ) & & _global_string_params . GetTypeAtOffset ( i ) = = SCC_RAW_STRING_POINTER ) {
strings [ i ] = strdup ( ( const char * ) ( size_t ) _global_string_params . GetParam ( i ) ) ;
dst [ i ] = ( size_t ) strings [ i ] ;
} else {
strings [ i ] = NULL ;
}
}
}
2011-03-18 14:34:52 +01:00
static char * StationGetSpecialString ( char * buff , int x , const char * last ) ;
static char * GetSpecialTownNameString ( char * buff , int ind , uint32 seed , const char * last ) ;
static char * GetSpecialNameString ( char * buff , int ind , StringParameters * args , const char * last ) ;
2011-10-20 18:53:34 +02:00
static char * FormatString ( char * buff , const char * str , StringParameters * args , const char * last , uint case_index = 0 , bool dry_run = false ) ;
2011-03-18 14:34:52 +01:00
struct LanguagePack : public LanguagePackHeader {
char data [ ] ; // list of strings
} ;
static char * * _langpack_offs ;
static LanguagePack * _langpack ;
2011-12-18 00:16:16 +01:00
static uint _langtab_num [ TAB_COUNT ] ; ///< Offset into langpack offs
static uint _langtab_start [ TAB_COUNT ] ; ///< Offset into langpack offs
2011-03-18 14:34:52 +01:00
static bool _keep_gender_data = false ; ///< Should we retain the gender data in the current string?
2005-07-15 16:53:44 +02:00
2008-01-29 18:09:00 +01:00
const char * GetStringPtr ( StringID string )
2004-08-09 19:04:08 +02:00
{
2011-12-18 00:16:16 +01:00
switch ( GB ( string , TAB_COUNT_OFFSET , TAB_COUNT_BITS ) ) {
2011-01-22 00:10:02 +01:00
/* GetGRFStringPtr doesn't handle 0xD4xx ids, we need to convert those to 0xD0xx. */
2011-12-18 00:16:16 +01:00
case 26 : return GetStringPtr ( GetGRFStringID ( 0 , 0xD000 + GB ( string , TAB_SIZE_OFFSET , 10 ) ) ) ;
case 28 : return GetGRFStringPtr ( GB ( string , TAB_SIZE_OFFSET , TAB_SIZE_BITS ) ) ;
case 29 : return GetGRFStringPtr ( GB ( string , TAB_SIZE_OFFSET , TAB_SIZE_BITS ) + 0x0800 ) ;
case 30 : return GetGRFStringPtr ( GB ( string , TAB_SIZE_OFFSET , TAB_SIZE_BITS ) + 0x1000 ) ;
default : return _langpack_offs [ _langtab_start [ GB ( string , TAB_COUNT_OFFSET , TAB_COUNT_BITS ) ] + GB ( string , TAB_SIZE_OFFSET , TAB_SIZE_BITS ) ] ;
2008-01-29 18:09:00 +01:00
}
2004-08-09 19:04:08 +02:00
}
2010-08-01 21:22:34 +02:00
/**
2011-10-20 18:51:54 +02:00
* Get a parsed string with most special stringcodes replaced by the string parameters .
2010-12-09 22:41:31 +01:00
* @ param buffr Pointer to a string buffer where the formatted string should be written to .
2007-04-04 03:35:16 +02:00
* @ param string
2011-03-18 14:34:52 +01:00
* @ param args Arguments for the string .
2010-12-09 22:41:31 +01:00
* @ param last Pointer just past the end of buffr .
2011-10-20 18:51:54 +02:00
* @ param case_index The " case index " . This will only be set when FormatString wants to print the string in a different case .
2010-12-09 22:41:31 +01:00
* @ return Pointer to the final zero byte of the formatted string .
2007-04-04 03:35:16 +02:00
*/
2011-10-20 18:51:54 +02:00
char * GetStringWithArgs ( char * buffr , StringID string , StringParameters * args , const char * last , uint case_index )
2004-08-09 19:04:08 +02:00
{
2011-10-20 18:51:54 +02:00
if ( string = = 0 ) return GetStringWithArgs ( buffr , STR_UNDEFINED , args , last ) ;
2007-10-30 19:32:26 +01:00
2011-12-18 00:16:16 +01:00
uint index = GB ( string , TAB_SIZE_OFFSET , TAB_SIZE_BITS ) ;
uint tab = GB ( string , TAB_COUNT_OFFSET , TAB_COUNT_BITS ) ;
2004-08-09 19:04:08 +02:00
2005-02-06 17:56:04 +01:00
switch ( tab ) {
case 4 :
2010-07-24 12:14:39 +02:00
if ( index > = 0xC0 ) {
2011-03-18 14:34:52 +01:00
return GetSpecialTownNameString ( buffr , index - 0xC0 , args - > GetInt32 ( ) , last ) ;
2010-07-24 12:14:39 +02:00
}
2005-02-06 17:56:04 +01:00
break ;
2004-08-09 19:04:08 +02:00
2005-02-06 17:56:04 +01:00
case 14 :
2010-07-24 12:14:39 +02:00
if ( index > = 0xE4 ) {
2011-03-18 14:34:52 +01:00
return GetSpecialNameString ( buffr , index - 0xE4 , args , last ) ;
2010-07-24 12:14:39 +02:00
}
2005-02-06 17:56:04 +01:00
break ;
2004-08-09 19:04:08 +02:00
2005-02-06 17:56:04 +01:00
case 15 :
2008-01-29 18:09:00 +01:00
/* Old table for custom names. This is no longer used */
error ( " Incorrect conversion of custom name string. " ) ;
2004-08-09 19:04:08 +02:00
2006-10-03 11:25:42 +02:00
case 26 :
/* Include string within newgrf text (format code 81) */
2007-11-19 22:02:30 +01:00
if ( HasBit ( index , 10 ) ) {
2006-10-03 11:25:42 +02:00
StringID string = GetGRFStringID ( 0 , 0xD000 + GB ( index , 0 , 10 ) ) ;
2011-10-20 18:51:54 +02:00
return GetStringWithArgs ( buffr , string , args , last , case_index ) ;
2006-10-03 11:25:42 +02:00
}
break ;
2006-04-21 05:00:20 +02:00
case 28 :
2011-10-20 18:53:34 +02:00
return FormatString ( buffr , GetGRFStringPtr ( index ) , args , last , case_index ) ;
2006-04-21 05:00:20 +02:00
case 29 :
2011-10-20 18:53:34 +02:00
return FormatString ( buffr , GetGRFStringPtr ( index + 0x0800 ) , args , last , case_index ) ;
2006-04-21 05:00:20 +02:00
case 30 :
2011-10-20 18:53:34 +02:00
return FormatString ( buffr , GetGRFStringPtr ( index + 0x1000 ) , args , last , case_index ) ;
2006-04-21 05:00:20 +02:00
2005-07-15 16:53:44 +02:00
case 31 :
2008-07-17 15:47:04 +02:00
NOT_REACHED ( ) ;
2004-08-09 19:04:08 +02:00
}
2005-11-14 20:48:04 +01:00
if ( index > = _langtab_num [ tab ] ) {
2009-07-24 11:14:36 +02:00
error ( " String 0x%X is invalid. You are probably using an old version of the .lng file. \n " , string ) ;
2005-11-14 20:48:04 +01:00
}
2004-08-09 19:04:08 +02:00
2011-10-20 18:53:34 +02:00
return FormatString ( buffr , GetStringPtr ( string ) , args , last , case_index ) ;
2005-07-15 16:53:44 +02:00
}
2008-11-24 21:51:55 +01:00
char * GetString ( char * buffr , StringID string , const char * last )
2005-07-15 16:53:44 +02:00
{
2011-03-18 14:34:52 +01:00
_global_string_params . ClearTypeInformation ( ) ;
_global_string_params . offset = 0 ;
return GetStringWithArgs ( buffr , string , & _global_string_params , last ) ;
2004-08-09 19:04:08 +02:00
}
2005-07-15 16:53:44 +02:00
2006-11-16 23:05:33 +01:00
char * InlineString ( char * buf , StringID string )
{
buf + = Utf8Encode ( buf , SCC_STRING_ID ) ;
buf + = Utf8Encode ( buf , string ) ;
return buf ;
}
2010-08-01 21:22:34 +02:00
/**
* This function is used to " bind " a C string to a OpenTTD dparam slot .
2007-04-04 03:35:16 +02:00
* @ param n slot of the string
* @ param str string to bind
*/
2005-07-14 11:43:59 +02:00
void SetDParamStr ( uint n , const char * str )
{
2008-08-01 00:55:12 +02:00
SetDParam ( n , ( uint64 ) ( size_t ) str ) ;
2005-07-14 11:43:59 +02:00
}
2009-07-25 09:49:49 +02:00
/**
* Shift the string parameters in the global string parameter array by \ a amount positions , making room at the beginning .
* @ param amount Number of positions to shift .
*/
2008-09-28 15:50:11 +02:00
void InjectDParam ( uint amount )
2004-08-09 19:04:08 +02:00
{
2011-03-18 14:34:52 +01:00
_global_string_params . ShiftParameters ( amount ) ;
2004-08-09 19:04:08 +02:00
}
2011-04-09 22:12:45 +02:00
/**
* Format a number into a string .
* @ param buff the buffer to write to
* @ param number the number to write down
* @ param last the last element in the buffer
* @ param separator the thousands - separator to use
2011-04-09 22:26:14 +02:00
* @ param zerofill minimum number of digits to print for the integer part . The number will be filled with zeros at the front if necessary .
* @ param fractional_digits number of fractional digits to display after a decimal separator . The decimal separator is inserted
* in front of the \ a fractional_digits last digit of \ a number .
2011-04-09 22:12:45 +02:00
* @ return till where we wrote
*/
2011-04-09 22:26:14 +02:00
static char * FormatNumber ( char * buff , int64 number , const char * last , const char * separator , int zerofill = 1 , int fractional_digits = 0 )
2004-08-09 19:04:08 +02:00
{
2011-04-09 22:12:45 +02:00
static const int max_digits = 20 ;
2007-07-29 14:13:22 +02:00
uint64 divisor = 10000000000000000000ULL ;
2011-04-09 22:26:14 +02:00
zerofill + = fractional_digits ;
int thousands_offset = ( max_digits - fractional_digits - 1 ) % 3 ;
2004-08-09 19:04:08 +02:00
if ( number < 0 ) {
2009-08-06 22:21:53 +02:00
buff + = seprintf ( buff , last , " - " ) ;
2004-08-09 19:04:08 +02:00
number = - number ;
}
2009-11-19 14:53:54 +01:00
uint64 num = number ;
uint64 tot = 0 ;
2011-04-09 22:12:45 +02:00
for ( int i = 0 ; i < max_digits ; i + + ) {
2011-04-09 22:26:14 +02:00
if ( i = = max_digits - fractional_digits ) {
const char * decimal_separator = _settings_game . locale . digit_decimal_separator ;
if ( decimal_separator = = NULL ) decimal_separator = _langpack - > digit_decimal_separator ;
buff + = seprintf ( buff , last , " %s " , decimal_separator ) ;
}
2009-11-19 14:53:54 +01:00
uint64 quot = 0 ;
2007-07-29 14:13:22 +02:00
if ( num > = divisor ) {
quot = num / divisor ;
num = num % divisor ;
2004-08-09 19:04:08 +02:00
}
2011-04-09 22:12:45 +02:00
if ( tot | = quot | | i > = max_digits - zerofill ) {
2009-04-30 14:11:51 +02:00
buff + = seprintf ( buff , last , " %i " , ( int ) quot ) ;
2011-04-17 19:17:09 +02:00
if ( ( i % 3 ) = = thousands_offset & & i < max_digits - 1 - fractional_digits ) buff = strecpy ( buff , separator , last ) ;
2004-08-09 19:04:08 +02:00
}
2007-07-29 14:13:22 +02:00
divisor / = 10 ;
2004-08-09 19:04:08 +02:00
}
2005-02-06 12:23:41 +01:00
* buff = ' \0 ' ;
2004-08-09 19:04:08 +02:00
return buff ;
}
2011-04-09 22:26:14 +02:00
static char * FormatCommaNumber ( char * buff , int64 number , const char * last , int fractional_digits = 0 )
2004-08-09 19:04:08 +02:00
{
2009-04-23 23:05:00 +02:00
const char * separator = _settings_game . locale . digit_group_separator ;
if ( separator = = NULL ) separator = _langpack - > digit_group_separator ;
2011-04-09 22:26:14 +02:00
return FormatNumber ( buff , number , last , separator , 1 , fractional_digits ) ;
2009-04-23 18:17:10 +02:00
}
2004-08-09 19:04:08 +02:00
2009-04-23 18:17:10 +02:00
static char * FormatNoCommaNumber ( char * buff , int64 number , const char * last )
{
return FormatNumber ( buff , number , last , " " ) ;
2004-08-09 19:04:08 +02:00
}
2009-11-19 14:53:54 +01:00
static char * FormatZerofillNumber ( char * buff , int64 number , int64 count , const char * last )
{
2011-04-09 22:12:45 +02:00
return FormatNumber ( buff , number , last , " " , count ) ;
2009-11-19 14:53:54 +01:00
}
2010-04-07 01:45:10 +02:00
static char * FormatHexNumber ( char * buff , uint64 number , const char * last )
2008-09-14 16:31:37 +02:00
{
2010-04-07 01:45:10 +02:00
return buff + seprintf ( buff , last , " 0x " OTTD_PRINTFHEX64 , number ) ;
2008-09-14 16:31:37 +02:00
}
2004-08-09 19:04:08 +02:00
2009-01-16 13:59:47 +01:00
/**
* Format a given number as a number of bytes with the SI prefix .
* @ param buff the buffer to write to
* @ param number the number of bytes to write down
* @ param last the last element in the buffer
* @ return till where we wrote
*/
static char * FormatBytes ( char * buff , int64 number , const char * last )
{
assert ( number > = 0 ) ;
2011-03-22 23:13:20 +01:00
/* 1 2^10 2^20 2^30 2^40 2^50 2^60 */
const char * const iec_prefixes [ ] = { " " , " Ki " , " Mi " , " Gi " , " Ti " , " Pi " , " Ei " } ;
2009-01-16 15:02:40 +01:00
uint id = 1 ;
2009-01-16 13:59:47 +01:00
while ( number > = 1024 * 1024 ) {
number / = 1024 ;
id + + ;
}
2009-08-12 03:28:11 +02:00
const char * decimal_separator = _settings_game . locale . digit_decimal_separator ;
if ( decimal_separator = = NULL ) decimal_separator = _langpack - > digit_decimal_separator ;
2009-01-16 13:59:47 +01:00
if ( number < 1024 ) {
id = 0 ;
buff + = seprintf ( buff , last , " %i " , ( int ) number ) ;
} else if ( number < 1024 * 10 ) {
2009-08-12 03:28:11 +02:00
buff + = seprintf ( buff , last , " %i%s%02i " , ( int ) number / 1024 , decimal_separator , ( int ) ( number % 1024 ) * 100 / 1024 ) ;
2009-01-16 13:59:47 +01:00
} else if ( number < 1024 * 100 ) {
2009-08-12 03:28:11 +02:00
buff + = seprintf ( buff , last , " %i%s%01i " , ( int ) number / 1024 , decimal_separator , ( int ) ( number % 1024 ) * 10 / 1024 ) ;
2009-01-16 13:59:47 +01:00
} else {
assert ( number < 1024 * 1024 ) ;
buff + = seprintf ( buff , last , " %i " , ( int ) number / 1024 ) ;
}
2009-08-05 18:14:40 +02:00
assert ( id < lengthof ( iec_prefixes ) ) ;
buff + = seprintf ( buff , last , " %sB " , iec_prefixes [ id ] ) ;
2009-01-16 13:59:47 +01:00
return buff ;
}
2011-10-20 18:53:34 +02:00
static char * FormatYmdString ( char * buff , Date date , const char * last , uint case_index )
2004-08-09 19:04:08 +02:00
{
YearMonthDay ymd ;
2006-08-15 18:55:40 +02:00
ConvertDateToYMD ( date , & ymd ) ;
2004-08-09 19:04:08 +02:00
2011-03-22 23:13:20 +01:00
int64 args [ ] = { ymd . day + STR_ORDINAL_NUMBER_1ST - 1 , STR_MONTH_ABBREV_JAN + ymd . month , ymd . year } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args ) ;
2011-10-20 18:53:34 +02:00
return FormatString ( buff , GetStringPtr ( STR_FORMAT_DATE_LONG ) , & tmp_params , last , case_index ) ;
2004-08-09 19:04:08 +02:00
}
2011-10-20 18:53:34 +02:00
static char * FormatMonthAndYear ( char * buff , Date date , const char * last , uint case_index )
2004-08-09 19:04:08 +02:00
{
YearMonthDay ymd ;
2006-08-15 18:55:40 +02:00
ConvertDateToYMD ( date , & ymd ) ;
2004-08-09 19:04:08 +02:00
2011-03-22 23:13:20 +01:00
int64 args [ ] = { STR_MONTH_JAN + ymd . month , ymd . year } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args ) ;
2011-10-20 18:53:34 +02:00
return FormatString ( buff , GetStringPtr ( STR_FORMAT_DATE_SHORT ) , & tmp_params , last , case_index ) ;
2004-08-09 19:04:08 +02:00
}
2009-01-03 01:56:16 +01:00
static char * FormatTinyOrISODate ( char * buff , Date date , StringID str , const char * last )
2005-01-23 00:13:20 +01:00
{
YearMonthDay ymd ;
2006-08-15 18:55:40 +02:00
ConvertDateToYMD ( date , & ymd ) ;
2005-01-23 00:13:20 +01:00
2007-02-26 01:36:57 +01:00
char day [ 3 ] ;
char month [ 3 ] ;
/* We want to zero-pad the days and months */
snprintf ( day , lengthof ( day ) , " %02i " , ymd . day ) ;
snprintf ( month , lengthof ( month ) , " %02i " , ymd . month + 1 ) ;
2011-03-22 23:13:20 +01:00
int64 args [ ] = { ( int64 ) ( size_t ) day , ( int64 ) ( size_t ) month , ymd . year } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args ) ;
2011-10-20 18:53:34 +02:00
return FormatString ( buff , GetStringPtr ( str ) , & tmp_params , last ) ;
2005-01-23 00:13:20 +01:00
}
2008-11-24 21:51:55 +01:00
static char * FormatGenericCurrency ( char * buff , const CurrencySpec * spec , Money number , bool compact , const char * last )
2004-08-09 19:04:08 +02:00
{
2007-10-20 16:51:09 +02:00
/* We are going to make number absolute for printing, so
* keep this piece of data as we need it later on */
bool negative = number < 0 ;
const char * multiplier = " " ;
2004-08-09 19:04:08 +02:00
2007-10-20 16:51:09 +02:00
number * = spec - > rate ;
2004-08-09 19:04:08 +02:00
2007-04-04 03:35:16 +02:00
/* convert from negative */
2005-02-06 12:23:41 +01:00
if ( number < 0 ) {
2007-08-07 17:20:31 +02:00
if ( buff + Utf8CharLen ( SCC_RED ) > last ) return buff ;
buff + = Utf8Encode ( buff , SCC_RED ) ;
2006-10-22 01:31:34 +02:00
buff = strecpy ( buff , " - " , last ) ;
2005-02-06 12:23:41 +01:00
number = - number ;
}
2004-09-10 21:02:27 +02:00
2006-09-15 04:52:17 +02:00
/* Add prefix part, folowing symbol_pos specification.
* Here , it can can be either 0 ( prefix ) or 2 ( both prefix anf suffix ) .
* The only remaining value is 1 ( suffix ) , so everything that is not 1 */
2006-10-22 01:31:34 +02:00
if ( spec - > symbol_pos ! = 1 ) buff = strecpy ( buff , spec - > prefix , last ) ;
2004-08-09 19:04:08 +02:00
2007-04-04 03:35:16 +02:00
/* for huge numbers, compact the number into k or M */
2004-08-09 19:04:08 +02:00
if ( compact ) {
2010-10-22 18:30:09 +02:00
/* Take care of the 'k' rounding. Having 1 000 000 k
* and 1 000 M is inconsistent , so always use 1 000 M . */
if ( number > = 1000000000 - 500 ) {
2004-08-09 19:04:08 +02:00
number = ( number + 500000 ) / 1000000 ;
2006-10-22 01:31:34 +02:00
multiplier = " M " ;
2004-08-09 19:04:08 +02:00
} else if ( number > = 1000000 ) {
number = ( number + 500 ) / 1000 ;
2006-10-22 01:31:34 +02:00
multiplier = " k " ;
2004-09-10 21:02:27 +02:00
}
2004-08-09 19:04:08 +02:00
}
2004-09-10 21:02:27 +02:00
2009-04-23 23:05:00 +02:00
const char * separator = _settings_game . locale . digit_group_separator_currency ;
2009-04-25 22:16:56 +02:00
if ( separator = = NULL & & ! StrEmpty ( _currency - > separator ) ) separator = _currency - > separator ;
2009-04-23 23:05:00 +02:00
if ( separator = = NULL ) separator = _langpack - > digit_group_separator_currency ;
buff = FormatNumber ( buff , number , last , separator ) ;
2006-10-22 01:31:34 +02:00
buff = strecpy ( buff , multiplier , last ) ;
2004-08-09 19:04:08 +02:00
2006-09-15 04:52:17 +02:00
/* Add suffix part, folowing symbol_pos specification.
* Here , it can can be either 1 ( suffix ) or 2 ( both prefix anf suffix ) .
* The only remaining value is 1 ( prefix ) , so everything that is not 0 */
2006-10-22 01:31:34 +02:00
if ( spec - > symbol_pos ! = 0 ) buff = strecpy ( buff , spec - > suffix , last ) ;
2004-08-09 19:04:08 +02:00
2007-10-20 16:51:09 +02:00
if ( negative ) {
2007-08-07 17:20:31 +02:00
if ( buff + Utf8CharLen ( SCC_PREVIOUS_COLOUR ) > last ) return buff ;
buff + = Utf8Encode ( buff , SCC_PREVIOUS_COLOUR ) ;
* buff = ' \0 ' ;
}
2004-08-09 19:04:08 +02:00
return buff ;
}
2010-11-16 22:01:56 +01:00
/**
* Determine the " plural " index given a plural form and a number .
* @ param count The number to get the plural index of .
* @ param plural_form The plural form we want an index for .
* @ return The plural index for the given form .
*/
static int DeterminePluralForm ( int64 count , int plural_form )
2005-07-16 19:12:32 +02:00
{
2007-04-04 03:35:16 +02:00
/* The absolute value determines plurality */
2008-03-25 20:56:16 +01:00
uint64 n = abs ( count ) ;
2005-07-16 19:12:32 +02:00
2010-11-16 22:01:56 +01:00
switch ( plural_form ) {
2008-03-25 20:56:16 +01:00
default :
NOT_REACHED ( ) ;
/* Two forms, singular used for one only
* Used in :
* Danish , Dutch , English , German , Norwegian , Swedish , Estonian , Finnish ,
* Greek , Hebrew , Italian , Portuguese , Spanish , Esperanto */
case 0 :
return n ! = 1 ;
/* Only one form
* Used in :
* Hungarian , Japanese , Korean , Turkish */
case 1 :
return 0 ;
/* Two forms, singular used for zero and one
* Used in :
* French , Brazilian Portuguese */
case 2 :
return n > 1 ;
2010-04-17 13:16:34 +02:00
/* Three forms, special case for 0 and ending in 1, except those ending in 11
2008-03-25 20:56:16 +01:00
* Used in :
* Latvian */
case 3 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n ! = 0 ? 1 : 2 ;
2010-04-17 13:16:34 +02:00
/* Five forms, special case for one, two, 3 to 6 and 7 to 10
2008-03-25 20:56:16 +01:00
* Used in :
* Gaelige ( Irish ) */
case 4 :
2010-04-17 13:16:34 +02:00
return n = = 1 ? 0 : n = = 2 ? 1 : n < 7 ? 2 : n < 11 ? 3 : 4 ;
2008-03-25 20:56:16 +01:00
/* Three forms, special case for numbers ending in 1[2-9]
* Used in :
* Lithuanian */
case 5 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n % 10 > = 2 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Three forms, special cases for numbers ending in 1 and 2, 3, 4, except those ending in 1[1-4]
* Used in :
2010-03-18 17:34:50 +01:00
* Croatian , Russian , Ukrainian */
2008-03-25 20:56:16 +01:00
case 6 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : n % 10 > = 2 & & n % 10 < = 4 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Three forms, special case for one and some numbers ending in 2, 3, or 4
* Used in :
* Polish */
case 7 :
return n = = 1 ? 0 : n % 10 > = 2 & & n % 10 < = 4 & & ( n % 100 < 10 | | n % 100 > = 20 ) ? 1 : 2 ;
/* Four forms, special case for one and all numbers ending in 02, 03, or 04
* Used in :
* Slovenian */
case 8 :
return n % 100 = = 1 ? 0 : n % 100 = = 2 ? 1 : n % 100 = = 3 | | n % 100 = = 4 ? 2 : 3 ;
/* Two forms; singular used for everything ending in 1 but not in 11.
* Used in :
* Icelandic */
case 9 :
return n % 10 = = 1 & & n % 100 ! = 11 ? 0 : 1 ;
2009-04-07 02:05:44 +02:00
/* Three forms, special cases for one and 2, 3, or 4
* Used in :
2010-03-18 17:34:50 +01:00
* Czech , Slovak */
2009-04-07 02:05:44 +02:00
case 10 :
return n = = 1 ? 0 : n > = 2 & & n < = 4 ? 1 : 2 ;
2009-07-13 16:42:09 +02:00
/* Two forms, special 'hack' for Korean; singular for numbers ending
* in a consonant and plural for numbers ending in a vowel .
* Korean doesn ' t have the concept of plural , but depending on how a
* number is pronounced it needs another version of a particle .
* As such the plural system is misused to give this distinction .
*/
case 11 :
switch ( n % 10 ) {
case 0 : // yeong
case 1 : // il
case 3 : // sam
case 6 : // yuk
case 7 : // chil
case 8 : // pal
return 0 ;
case 2 : // i
case 4 : // sa
case 5 : // o
case 9 : // gu
return 1 ;
default :
NOT_REACHED ( ) ;
}
2010-04-17 13:16:34 +02:00
/* Four forms: one, 0 and everything ending in 02..10, everything ending in 11..19.
* Used in :
* Maltese */
case 12 :
return ( n = = 1 ? 0 : n = = 0 | | ( n % 100 > 1 & & n % 100 < 11 ) ? 1 : ( n % 100 > 10 & & n % 100 < 20 ) ? 2 : 3 ) ;
2005-07-16 19:12:32 +02:00
}
}
2009-08-06 22:21:53 +02:00
static const char * ParseStringChoice ( const char * b , uint form , char * * dst , const char * last )
2005-07-16 19:12:32 +02:00
{
2009-03-15 01:32:18 +01:00
/* <NUM> {Length of each string} {each string} */
2005-07-16 19:12:32 +02:00
uint n = ( byte ) * b + + ;
2009-08-06 22:21:53 +02:00
uint pos , i , mypos = 0 ;
2006-02-01 08:36:15 +01:00
for ( i = pos = 0 ; i ! = n ; i + + ) {
2005-07-16 19:12:32 +02:00
uint len = ( byte ) * b + + ;
2009-08-06 22:21:53 +02:00
if ( i = = form ) mypos = pos ;
2005-07-16 19:12:32 +02:00
pos + = len ;
}
2009-08-06 22:21:53 +02:00
* dst + = seprintf ( * dst , last , " %s " , b + mypos ) ;
2005-07-16 19:12:32 +02:00
return b + pos ;
}
2011-02-09 13:59:39 +01:00
/** Helper for unit conversion. */
struct UnitConversion {
int multiplier ; ///< Amount to multiply upon conversion.
int shift ; ///< Amount to shift upon conversion.
/**
* Convert value from OpenTTD ' s internal unit into the displayed value .
* @ param input The input to convert .
2011-02-10 21:54:46 +01:00
* @ param round Whether to round the value or not .
2011-02-09 13:59:39 +01:00
* @ return The converted value .
*/
2011-02-10 21:54:46 +01:00
int64 ToDisplay ( int64 input , bool round = true ) const
2011-02-09 13:59:39 +01:00
{
2011-02-10 21:54:46 +01:00
return ( ( input * this - > multiplier ) + ( round & & this - > shift ! = 0 ? 1 < < ( this - > shift - 1 ) : 0 ) ) > > this - > shift ;
2011-02-09 13:59:39 +01:00
}
/**
* Convert the displayed value back into a value of OpenTTD ' s internal unit .
* @ param input The input to convert .
2011-02-10 21:54:46 +01:00
* @ param round Whether to round the value or not .
2011-02-09 13:59:39 +01:00
* @ return The converted value .
*/
2011-02-10 21:54:46 +01:00
int64 FromDisplay ( int64 input , bool round = true ) const
2011-02-09 13:59:39 +01:00
{
2011-02-10 21:54:46 +01:00
return ( ( input < < this - > shift ) + ( round ? this - > multiplier / 2 : 0 ) ) / this - > multiplier ;
2011-02-09 13:59:39 +01:00
}
} ;
2007-03-07 13:11:48 +01:00
struct Units {
2011-02-09 14:05:04 +01:00
UnitConversion c_velocity ; ///< Conversion for velocity
StringID velocity ; ///< String for velocity
UnitConversion c_power ; ///< Conversion for power
2011-02-09 14:21:50 +01:00
StringID power ; ///< String for power
2011-02-09 14:05:04 +01:00
UnitConversion c_weight ; ///< Conversion for weight
StringID s_weight ; ///< Short string for weight
StringID l_weight ; ///< Long string for weight
UnitConversion c_volume ; ///< Conversion for volume
StringID s_volume ; ///< Short string for volume
StringID l_volume ; ///< Long string for volume
UnitConversion c_force ; ///< Conversion for force
StringID force ; ///< String for force
UnitConversion c_height ; ///< Conversion for height
StringID height ; ///< String for height
2007-03-07 13:11:48 +01:00
} ;
2006-03-26 23:50:57 +02:00
2006-04-09 20:25:31 +02:00
/* Unit conversions */
2011-02-09 14:05:04 +01:00
static const Units _units [ ] = {
2010-06-08 22:14:58 +02:00
{ // Imperial (Original, mph, hp, metric ton, litre, kN, ft)
2011-02-09 13:59:39 +01:00
{ 1 , 0 } , STR_UNITS_VELOCITY_IMPERIAL ,
{ 1 , 0 } , STR_UNITS_POWER_IMPERIAL ,
{ 1 , 0 } , STR_UNITS_WEIGHT_SHORT_METRIC , STR_UNITS_WEIGHT_LONG_METRIC ,
{ 1000 , 0 } , STR_UNITS_VOLUME_SHORT_METRIC , STR_UNITS_VOLUME_LONG_METRIC ,
{ 1 , 0 } , STR_UNITS_FORCE_SI ,
2011-02-10 21:54:46 +01:00
{ 3 , 0 } , STR_UNITS_HEIGHT_IMPERIAL , // "Wrong" conversion factor for more nicer GUI values
2006-03-26 23:50:57 +02:00
} ,
2010-06-08 22:14:58 +02:00
{ // Metric (km/h, hp, metric ton, litre, kN, metre)
2011-02-09 13:59:39 +01:00
{ 103 , 6 } , STR_UNITS_VELOCITY_METRIC ,
2011-02-11 09:17:12 +01:00
{ 4153 , 12 } , STR_UNITS_POWER_METRIC ,
2011-02-09 13:59:39 +01:00
{ 1 , 0 } , STR_UNITS_WEIGHT_SHORT_METRIC , STR_UNITS_WEIGHT_LONG_METRIC ,
{ 1000 , 0 } , STR_UNITS_VOLUME_SHORT_METRIC , STR_UNITS_VOLUME_LONG_METRIC ,
{ 1 , 0 } , STR_UNITS_FORCE_SI ,
{ 1 , 0 } , STR_UNITS_HEIGHT_SI ,
2006-03-26 23:50:57 +02:00
} ,
2010-06-08 22:14:58 +02:00
{ // SI (m/s, kilowatt, kilogram, cubic metre, kilonewton, metre)
2011-02-09 13:59:39 +01:00
{ 1831 , 12 } , STR_UNITS_VELOCITY_SI ,
2011-02-11 09:17:12 +01:00
{ 6109 , 13 } , STR_UNITS_POWER_SI ,
2011-02-09 13:59:39 +01:00
{ 1000 , 0 } , STR_UNITS_WEIGHT_SHORT_SI , STR_UNITS_WEIGHT_LONG_SI ,
{ 1 , 0 } , STR_UNITS_VOLUME_SHORT_SI , STR_UNITS_VOLUME_LONG_SI ,
{ 1 , 0 } , STR_UNITS_FORCE_SI ,
{ 1 , 0 } , STR_UNITS_HEIGHT_SI ,
2006-03-26 23:50:57 +02:00
} ,
} ;
2005-07-16 19:12:32 +02:00
2008-04-12 12:00:52 +02:00
/**
* Convert the given ( internal ) speed to the display speed .
* @ param speed the speed to convert
* @ return the converted speed .
*/
uint ConvertSpeedToDisplaySpeed ( uint speed )
{
2011-02-10 21:54:46 +01:00
/* For historical reasons we don't want to mess with the
* conversion for speed . So , don ' t round it and keep the
* original conversion factors instead of the real ones . */
return _units [ _settings_game . locale . units ] . c_velocity . ToDisplay ( speed , false ) ;
2008-04-12 12:00:52 +02:00
}
/**
* Convert the given display speed to the ( internal ) speed .
* @ param speed the speed to convert
* @ return the converted speed .
*/
uint ConvertDisplaySpeedToSpeed ( uint speed )
{
2011-02-09 14:05:04 +01:00
return _units [ _settings_game . locale . units ] . c_velocity . FromDisplay ( speed ) ;
2008-04-12 12:00:52 +02:00
}
2010-12-09 22:41:31 +01:00
/**
* Parse most format codes within a string and write the result to a buffer .
* @ param buff The buffer to write the final string to .
* @ param str The original string with format codes .
2011-03-18 14:34:52 +01:00
* @ param args Pointer to extra arguments used by various string codes .
2011-10-20 18:50:57 +02:00
* @ param case_index
2010-12-09 22:41:31 +01:00
* @ param last Pointer to just past the end of the buff array .
* @ param dry_run True when the argt array is not yet initialized .
*/
2011-10-20 18:53:34 +02:00
static char * FormatString ( char * buff , const char * str_arg , StringParameters * args , const char * last , uint case_index , bool dry_run )
2004-08-09 19:04:08 +02:00
{
2011-03-18 14:34:52 +01:00
uint orig_offset = args - > offset ;
2010-12-09 22:41:31 +01:00
/* When there is no array with types there is no need to do a dry run. */
2011-10-14 12:54:51 +02:00
if ( args - > HasTypeInformation ( ) & & ! dry_run ) {
2011-03-18 14:34:52 +01:00
if ( UsingNewGRFTextStack ( ) ) {
/* Values from the NewGRF text stack are only copied to the normal
* argv array at the time they are encountered . That means that if
* another string command references a value later in the string it
* would fail . We solve that by running FormatString twice . The first
* pass makes sure the argv array is correctly filled and the second
* pass can reference later values without problems . */
struct TextRefStack * backup = CreateTextRefStackBackup ( ) ;
2011-10-20 18:53:34 +02:00
FormatString ( buff , str_arg , args , last , case_index , true ) ;
2011-03-18 14:34:52 +01:00
RestoreTextRefStackBackup ( backup ) ;
} else {
2011-10-20 18:53:34 +02:00
FormatString ( buff , str_arg , args , last , case_index , true ) ;
2011-03-18 14:34:52 +01:00
}
/* We have to restore the original offset here to to read the correct values. */
args - > offset = orig_offset ;
2010-12-08 14:44:01 +01:00
}
2006-11-16 23:05:33 +01:00
WChar b ;
2011-10-20 18:51:54 +02:00
uint next_substr_case_index = 0 ;
2010-04-06 23:16:36 +02:00
char * buf_start = buff ;
2011-01-22 00:10:02 +01:00
std : : stack < const char * > str_stack ;
str_stack . push ( str_arg ) ;
2011-02-25 22:53:43 +01:00
for ( ; ; ) {
2011-01-22 00:10:02 +01:00
while ( ! str_stack . empty ( ) & & ( b = Utf8Consume ( & str_stack . top ( ) ) ) = = ' \0 ' ) {
str_stack . pop ( ) ;
}
if ( str_stack . empty ( ) ) break ;
const char * & str = str_stack . top ( ) ;
2004-08-09 19:04:08 +02:00
2007-09-23 01:55:34 +02:00
if ( SCC_NEWGRF_FIRST < = b & & b < = SCC_NEWGRF_LAST ) {
/* We need to pass some stuff as it might be modified; oh boy. */
2010-12-09 01:15:38 +01:00
//todo: should argve be passed here too?
2011-10-12 21:25:52 +02:00
b = RemapNewGRFStringControlCode ( b , buf_start , & buff , & str , ( int64 * ) args - > GetDataPointer ( ) , dry_run ) ;
2007-09-23 01:55:34 +02:00
if ( b = = 0 ) continue ;
}
2005-02-06 12:23:41 +01:00
switch ( b ) {
2011-01-22 00:10:02 +01:00
case SCC_NEWGRF_STRINL : {
StringID substr = Utf8Consume ( & str ) ;
str_stack . push ( GetStringPtr ( substr ) ) ;
break ;
}
2011-08-21 00:06:38 +02:00
case SCC_NEWGRF_PRINT_WORD_STRING_ID : {
StringID substr = args - > GetInt32 ( SCC_NEWGRF_PRINT_WORD_STRING_ID ) ;
2011-01-22 00:10:02 +01:00
str_stack . push ( GetStringPtr ( substr ) ) ;
2011-10-20 18:51:54 +02:00
case_index = next_substr_case_index ;
next_substr_case_index = 0 ;
2011-01-22 00:10:02 +01:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_GENDER_LIST : { // {G 0 Der Die Das}
/* First read the meta data from the language file. */
uint offset = orig_offset + ( byte ) * str + + ;
int gender = 0 ;
if ( ! dry_run & & args - > GetTypeAtOffset ( offset ) ! = 0 ) {
/* Now we need to figure out what text to resolve, i.e.
* what do we need to draw ? So get the actual raw string
* first using the control code to get said string . */
char input [ 4 + 1 ] ;
char * p = input + Utf8Encode ( input , args - > GetTypeAtOffset ( offset ) ) ;
* p = ' \0 ' ;
/* Now do the string formatting. */
char buf [ 256 ] ;
bool old_kgd = _keep_gender_data ;
_keep_gender_data = true ;
StringParameters tmp_params ( args - > GetPointerToOffset ( offset ) , args - > num_param - offset , NULL ) ;
p = FormatString ( buf , input , & tmp_params , lastof ( buf ) ) ;
_keep_gender_data = old_kgd ;
* p = ' \0 ' ;
/* And determine the string. */
const char * s = buf ;
WChar c = Utf8Consume ( & s ) ;
/* Does this string have a gender, if so, set it */
if ( c = = SCC_GENDER_INDEX ) gender = ( byte ) s [ 0 ] ;
2006-11-16 23:05:33 +01:00
}
2011-11-19 23:15:44 +01:00
str = ParseStringChoice ( str , gender , & buff , last ) ;
2006-11-16 23:05:33 +01:00
break ;
2011-11-19 23:15:44 +01:00
}
2006-11-16 23:05:33 +01:00
2011-11-19 23:15:44 +01:00
/* This sets up the gender for the string.
* We just ignore this one . It ' s used in { G 0 Der Die Das } to determine the case . */
case SCC_GENDER_INDEX : // {GENDER 0}
if ( _keep_gender_data ) {
buff + = Utf8Encode ( buff , SCC_GENDER_INDEX ) ;
2006-11-16 23:05:33 +01:00
* buff + + = * str + + ;
2011-11-19 23:15:44 +01:00
} else {
str + + ;
2006-11-16 23:05:33 +01:00
}
break ;
2011-11-19 23:15:44 +01:00
case SCC_PLURAL_LIST : { // {P}
int plural_form = * str + + ; // contains the plural form for this string
uint offset = orig_offset + ( byte ) * str + + ;
int64 v = * args - > GetPointerToOffset ( offset ) ; // contains the number that determines plural
str = ParseStringChoice ( str , DeterminePluralForm ( v , plural_form ) , & buff , last ) ;
2008-07-17 15:47:04 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_ARG_INDEX : { // Move argument pointer
args - > offset = orig_offset + ( byte ) * str + + ;
2006-11-16 23:05:33 +01:00
break ;
2011-11-19 23:15:44 +01:00
}
2006-11-16 23:05:33 +01:00
2011-11-19 23:15:44 +01:00
case SCC_SET_CASE : { // {SET_CASE}
/* This is a pseudo command, it's outputted when someone does {STRING.ack}
* The modifier is added to all subsequent GetStringWithArgs that accept the modifier . */
next_substr_case_index = ( byte ) * str + + ;
2006-11-16 23:05:33 +01:00
break ;
2011-11-19 23:15:44 +01:00
}
2006-11-16 23:05:33 +01:00
2011-11-19 23:15:44 +01:00
case SCC_SWITCH_CASE : { // {Used to implement case switching}
/* <0x9E> <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
* Each LEN is printed using 2 bytes in big endian order . */
uint num = ( byte ) * str + + ;
while ( num ) {
if ( ( byte ) str [ 0 ] = = case_index ) {
/* Found the case, adjust str pointer and continue */
str + = 3 ;
break ;
}
/* Otherwise skip to the next case */
str + = 3 + ( str [ 1 ] < < 8 ) + str [ 2 ] ;
num - - ;
}
2006-11-16 23:05:33 +01:00
break ;
2006-10-22 01:31:34 +02:00
}
2005-09-10 17:14:35 +02:00
2011-11-19 23:15:44 +01:00
case SCC_SETX : // {SETX}
if ( buff + Utf8CharLen ( SCC_SETX ) + 1 < last ) {
buff + = Utf8Encode ( buff , SCC_SETX ) ;
* buff + + = * str + + ;
}
2010-05-05 00:04:28 +02:00
break ;
2011-11-19 23:15:44 +01:00
case SCC_SETXY : // {SETXY}
if ( buff + Utf8CharLen ( SCC_SETXY ) + 2 < last ) {
buff + = Utf8Encode ( buff , SCC_SETXY ) ;
* buff + + = * str + + ;
* buff + + = * str + + ;
}
2004-08-09 19:04:08 +02:00
break ;
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_REVISION : // {REV}
2006-10-22 01:31:34 +02:00
buff = strecpy ( buff , _openttd_revision , last ) ;
2004-08-09 19:04:08 +02:00
break ;
2006-11-16 23:05:33 +01:00
2011-11-19 23:15:44 +01:00
case SCC_STRING_ID : // {STRINL}
buff = GetStringWithArgs ( buff , Utf8Consume ( & str ) , args , last ) ;
break ;
2006-03-26 23:50:57 +02:00
2011-11-19 23:15:44 +01:00
case SCC_RAW_STRING_POINTER : { // {RAW_STRING}
2011-12-10 17:03:12 +01:00
const char * str = ( const char * ) ( size_t ) args - > GetInt64 ( SCC_RAW_STRING_POINTER ) ;
2011-11-19 23:15:44 +01:00
buff = FormatString ( buff , str , args , last ) ;
break ;
}
2006-03-26 23:50:57 +02:00
2011-11-19 23:15:44 +01:00
case SCC_STRING : { // {STRING}
StringID str = args - > GetInt32 ( SCC_STRING ) ;
/* WARNING. It's prohibited for the included string to consume any arguments.
* For included strings that consume argument , you should use STRING1 , STRING2 etc .
* To debug stuff you can set argv to NULL and it will tell you */
StringParameters tmp_params ( args - > GetDataPointer ( ) , args - > num_param - args - > offset , NULL ) ;
buff = GetStringWithArgs ( buff , str , & tmp_params , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2010-08-01 20:53:30 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_STRING1 : { // {STRING1}
2007-04-04 03:35:16 +02:00
/* String that consumes ONE argument */
2011-10-20 18:51:54 +02:00
StringID str = args - > GetInt32 ( SCC_STRING1 ) ;
2011-03-18 14:34:52 +01:00
StringParameters sub_args ( * args , 1 ) ;
2011-10-20 18:51:54 +02:00
buff = GetStringWithArgs ( buff , str , & sub_args , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2005-07-15 16:53:44 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_STRING2 : { // {STRING2}
2007-04-04 03:35:16 +02:00
/* String that consumes TWO arguments */
2011-10-20 18:51:54 +02:00
StringID str = args - > GetInt32 ( SCC_STRING2 ) ;
2011-03-18 14:34:52 +01:00
StringParameters sub_args ( * args , 2 ) ;
2011-10-20 18:51:54 +02:00
buff = GetStringWithArgs ( buff , str , & sub_args , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2005-07-15 16:53:44 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_STRING3 : { // {STRING3}
2007-04-04 03:35:16 +02:00
/* String that consumes THREE arguments */
2011-10-20 18:51:54 +02:00
StringID str = args - > GetInt32 ( SCC_STRING3 ) ;
2011-03-18 14:34:52 +01:00
StringParameters sub_args ( * args , 3 ) ;
2011-10-20 18:51:54 +02:00
buff = GetStringWithArgs ( buff , str , & sub_args , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2005-07-15 16:53:44 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_STRING4 : { // {STRING4}
2007-04-04 03:35:16 +02:00
/* String that consumes FOUR arguments */
2011-10-20 18:51:54 +02:00
StringID str = args - > GetInt32 ( SCC_STRING4 ) ;
2011-03-18 14:34:52 +01:00
StringParameters sub_args ( * args , 4 ) ;
2011-10-20 18:51:54 +02:00
buff = GetStringWithArgs ( buff , str , & sub_args , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2005-07-15 16:53:44 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
2008-11-24 21:51:55 +01:00
case SCC_STRING5 : { // {STRING5}
2007-04-04 03:35:16 +02:00
/* String that consumes FIVE arguments */
2011-10-20 18:51:54 +02:00
StringID str = args - > GetInt32 ( SCC_STRING5 ) ;
2011-03-18 14:34:52 +01:00
StringParameters sub_args ( * args , 5 ) ;
2011-10-20 18:51:54 +02:00
buff = GetStringWithArgs ( buff , str , & sub_args , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
2005-07-15 16:53:44 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_COMMA : // {COMMA}
buff = FormatCommaNumber ( buff , args - > GetInt64 ( SCC_COMMA ) , last ) ;
2005-07-15 16:53:44 +02:00
break ;
2005-07-15 20:30:13 +02:00
2011-11-19 23:15:44 +01:00
case SCC_DECIMAL : { // {DECIMAL}
int64 number = args - > GetInt64 ( SCC_DECIMAL ) ;
int digits = args - > GetInt32 ( SCC_DECIMAL ) ;
buff = FormatCommaNumber ( buff , number , last , digits ) ;
break ;
}
2005-07-15 20:30:13 +02:00
2011-11-19 23:15:44 +01:00
case SCC_NUM : // {NUM}
buff = FormatNoCommaNumber ( buff , args - > GetInt64 ( SCC_NUM ) , last ) ;
break ;
2011-03-22 23:13:20 +01:00
2011-11-19 23:15:44 +01:00
case SCC_ZEROFILL_NUM : { // {ZEROFILL_NUM}
int64 num = args - > GetInt64 ( ) ;
buff = FormatZerofillNumber ( buff , num , args - > GetInt64 ( ) , last ) ;
2005-07-15 20:30:13 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_HEX : // {HEX}
buff = FormatHexNumber ( buff , ( uint64 ) args - > GetInt64 ( SCC_HEX ) , last ) ;
2005-07-16 22:58:04 +02:00
break ;
2011-11-19 23:15:44 +01:00
case SCC_BYTES : // {BYTES}
buff = FormatBytes ( buff , args - > GetInt64 ( ) , last ) ;
break ;
2010-12-09 22:01:50 +01:00
2011-11-23 17:04:28 +01:00
case SCC_CARGO_TINY : { // {CARGO_TINY}
/* Tiny description of cargotypes. Layout:
* param 1 : cargo type
* param 2 : cargo count */
2011-12-19 21:44:39 +01:00
CargoID cargo = args - > GetInt32 ( SCC_CARGO_TINY ) ;
if ( cargo > = CargoSpec : : GetArraySize ( ) ) break ;
StringID cargo_str = CargoSpec : : Get ( cargo ) - > units_volume ;
2011-11-23 17:04:28 +01:00
int64 amount = 0 ;
switch ( cargo_str ) {
case STR_TONS :
amount = _units [ _settings_game . locale . units ] . c_weight . ToDisplay ( args - > GetInt64 ( ) ) ;
break ;
case STR_LITERS :
amount = _units [ _settings_game . locale . units ] . c_volume . ToDisplay ( args - > GetInt64 ( ) ) ;
break ;
default : {
amount = args - > GetInt64 ( ) ;
break ;
}
}
buff = FormatCommaNumber ( buff , amount , last ) ;
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_CARGO_SHORT : { // {CARGO_SHORT}
/* Short description of cargotypes. Layout:
* param 1 : cargo type
* param 2 : cargo count */
2011-12-19 21:44:39 +01:00
CargoID cargo = args - > GetInt32 ( SCC_CARGO_SHORT ) ;
if ( cargo > = CargoSpec : : GetArraySize ( ) ) break ;
StringID cargo_str = CargoSpec : : Get ( cargo ) - > units_volume ;
2011-11-19 23:15:44 +01:00
switch ( cargo_str ) {
case STR_TONS : {
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
int64 args_array [ ] = { _units [ _settings_game . locale . units ] . c_weight . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . l_weight ) , & tmp_params , last ) ;
break ;
}
2010-12-09 22:01:50 +01:00
2011-11-19 23:15:44 +01:00
case STR_LITERS : {
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
int64 args_array [ ] = { _units [ _settings_game . locale . units ] . c_volume . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . l_volume ) , & tmp_params , last ) ;
break ;
}
default : {
StringParameters tmp_params ( * args , 1 ) ;
buff = GetStringWithArgs ( buff , cargo_str , & tmp_params , last ) ;
break ;
}
2010-12-09 22:01:50 +01:00
}
2005-07-16 22:58:04 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_CARGO_LONG : { // {CARGO_LONG}
/* First parameter is cargo type, second parameter is cargo count */
CargoID cargo = args - > GetInt32 ( SCC_CARGO_LONG ) ;
2011-12-19 21:44:39 +01:00
if ( cargo ! = CT_INVALID & & cargo > = CargoSpec : : GetArraySize ( ) ) break ;
2011-11-19 23:15:44 +01:00
StringID cargo_str = ( cargo = = CT_INVALID ) ? STR_QUANTITY_N_A : CargoSpec : : Get ( cargo ) - > quantifier ;
StringParameters tmp_args ( * args , 1 ) ;
buff = GetStringWithArgs ( buff , cargo_str , & tmp_args , last ) ;
2009-01-03 01:56:16 +01:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_CURRENCY_SHORT : // {CURRENCY_SHORT}
buff = FormatGenericCurrency ( buff , _currency , args - > GetInt64 ( ) , true , last ) ;
break ;
case SCC_CURRENCY_LONG : // {CURRENCY_LONG}
buff = FormatGenericCurrency ( buff , _currency , args - > GetInt64 ( SCC_CURRENCY_LONG ) , false , last ) ;
break ;
case SCC_DATE_TINY : // {DATE_TINY}
buff = FormatTinyOrISODate ( buff , args - > GetInt32 ( SCC_DATE_TINY ) , STR_FORMAT_DATE_TINY , last ) ;
break ;
case SCC_DATE_SHORT : // {DATE_SHORT}
buff = FormatMonthAndYear ( buff , args - > GetInt32 ( SCC_DATE_SHORT ) , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
break ;
case SCC_DATE_LONG : // {DATE_LONG}
buff = FormatYmdString ( buff , args - > GetInt32 ( SCC_DATE_LONG ) , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
break ;
case SCC_DATE_ISO : // {DATE_ISO}
2011-03-18 14:34:52 +01:00
buff = FormatTinyOrISODate ( buff , args - > GetInt32 ( ) , STR_FORMAT_DATE_ISO , last ) ;
2005-07-17 12:18:23 +02:00
break ;
2011-11-19 23:15:44 +01:00
case SCC_FORCE : { // {FORCE}
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_force . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . force ) , & tmp_params , last ) ;
break ;
}
case SCC_HEIGHT : { // {HEIGHT}
int64 args_array [ ] = { _units [ _settings_game . locale . units ] . c_height . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . height ) , & tmp_params , last ) ;
2005-07-17 12:18:23 +02:00
break ;
}
2006-11-16 23:05:33 +01:00
case SCC_POWER : { // {POWER}
2011-02-09 14:05:04 +01:00
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
2011-03-22 23:13:20 +01:00
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_power . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
2011-10-20 18:53:34 +02:00
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . power ) , & tmp_params , last ) ;
2006-03-26 23:50:57 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_VELOCITY : { // {VELOCITY}
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
int64 args_array [ ] = { ConvertSpeedToDisplaySpeed ( args - > GetInt64 ( SCC_VELOCITY ) * 10 / 16 ) } ;
StringParameters tmp_params ( args_array ) ;
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . velocity ) , & tmp_params , last ) ;
break ;
}
case SCC_VOLUME_SHORT : { // {VOLUME_SHORT}
2011-02-09 14:05:04 +01:00
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
2011-03-22 23:13:20 +01:00
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_volume . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
2011-10-20 18:53:34 +02:00
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . s_volume ) , & tmp_params , last ) ;
2006-03-26 23:50:57 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_VOLUME_LONG : { // {VOLUME_LONG}
2011-02-09 14:05:04 +01:00
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
2011-11-19 23:15:44 +01:00
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_volume . ToDisplay ( args - > GetInt64 ( SCC_VOLUME_LONG ) ) } ;
2011-03-22 23:13:20 +01:00
StringParameters tmp_params ( args_array ) ;
2011-11-19 23:15:44 +01:00
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . l_volume ) , & tmp_params , last ) ;
2006-03-26 23:50:57 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_WEIGHT_SHORT : { // {WEIGHT_SHORT}
2011-02-09 14:05:04 +01:00
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
2011-03-22 23:13:20 +01:00
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_weight . ToDisplay ( args - > GetInt64 ( ) ) } ;
StringParameters tmp_params ( args_array ) ;
2011-10-20 18:53:34 +02:00
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . s_weight ) , & tmp_params , last ) ;
2006-03-26 23:50:57 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_WEIGHT_LONG : { // {WEIGHT_LONG}
2011-02-09 14:05:04 +01:00
assert ( _settings_game . locale . units < lengthof ( _units ) ) ;
2011-11-19 23:15:44 +01:00
int64 args_array [ 1 ] = { _units [ _settings_game . locale . units ] . c_weight . ToDisplay ( args - > GetInt64 ( SCC_WEIGHT_LONG ) ) } ;
2011-03-22 23:13:20 +01:00
StringParameters tmp_params ( args_array ) ;
2011-11-19 23:15:44 +01:00
buff = FormatString ( buff , GetStringPtr ( _units [ _settings_game . locale . units ] . l_weight ) , & tmp_params , last ) ;
2006-04-09 20:25:31 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_COMPANY_NAME : { // {COMPANY}
2011-12-19 21:44:39 +01:00
const Company * c = Company : : GetIfValid ( args - > GetInt32 ( ) ) ;
if ( c = = NULL ) break ;
2011-11-19 23:15:44 +01:00
if ( c - > name ! = NULL ) {
buff = strecpy ( buff , c - > name , last ) ;
2010-02-21 00:42:11 +01:00
} else {
2011-11-19 23:15:44 +01:00
int64 args_array [ ] = { c - > name_2 } ;
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , c - > name_1 , & tmp_params , last ) ;
2010-02-21 00:42:11 +01:00
}
2006-11-16 23:05:33 +01:00
break ;
}
2004-08-09 19:04:08 +02:00
2011-11-19 23:15:44 +01:00
case SCC_COMPANY_NUM : { // {COMPANY_NUM}
CompanyID company = ( CompanyID ) args - > GetInt32 ( ) ;
2005-09-10 17:14:35 +02:00
2011-11-19 23:15:44 +01:00
/* Nothing is added for AI or inactive companies */
if ( Company : : IsValidHumanID ( company ) ) {
int64 args_array [ ] = { company + 1 } ;
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , STR_FORMAT_COMPANY_NUM , & tmp_params , last ) ;
}
2011-04-09 22:26:14 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_DEPOT_NAME : { // {DEPOT}
VehicleType vt = ( VehicleType ) args - > GetInt32 ( SCC_DEPOT_NAME ) ;
if ( vt = = VEH_AIRCRAFT ) {
int64 args_array [ ] = { args - > GetInt32 ( ) } ;
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , STR_FORMAT_DEPOT_NAME_AIRCRAFT , & tmp_params , last ) ;
break ;
}
const Depot * d = Depot : : Get ( args - > GetInt32 ( ) ) ;
if ( d - > name ! = NULL ) {
buff = strecpy ( buff , d - > name , last ) ;
} else {
int64 args_array [ ] = { d - > town - > index , d - > town_cn + 1 } ;
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , STR_FORMAT_DEPOT_NAME_TRAIN + 2 * vt + ( d - > town_cn = = 0 ? 0 : 1 ) , & tmp_params , last ) ;
}
2006-11-16 23:05:33 +01:00
break ;
2010-12-21 11:04:40 +01:00
}
2005-09-10 17:14:35 +02:00
2011-11-19 23:15:44 +01:00
case SCC_ENGINE_NAME : { // {ENGINE}
2011-12-19 21:44:39 +01:00
const Engine * e = Engine : : GetIfValid ( args - > GetInt32 ( SCC_ENGINE_NAME ) ) ;
if ( e = = NULL ) break ;
2011-11-19 23:15:44 +01:00
if ( e - > name ! = NULL & & e - > IsEnabled ( ) ) {
buff = strecpy ( buff , e - > name , last ) ;
} else {
StringParameters tmp_params ( NULL , 0 , NULL ) ;
buff = GetStringWithArgs ( buff , e - > info . string_id , & tmp_params , last ) ;
}
2006-11-16 23:05:33 +01:00
break ;
}
2005-09-10 17:14:35 +02:00
2011-11-19 23:15:44 +01:00
case SCC_GROUP_NAME : { // {GROUP}
2011-12-19 21:44:39 +01:00
const Group * g = Group : : GetIfValid ( args - > GetInt32 ( ) ) ;
if ( g = = NULL ) break ;
2011-11-19 23:15:44 +01:00
if ( g - > name ! = NULL ) {
buff = strecpy ( buff , g - > name , last ) ;
} else {
int64 args_array [ ] = { g - > index } ;
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , STR_FORMAT_GROUP_NAME , & tmp_params , last ) ;
}
2010-08-01 20:53:30 +02:00
break ;
}
2009-11-19 14:53:54 +01:00
2011-11-19 23:15:44 +01:00
case SCC_INDUSTRY_NAME : { // {INDUSTRY}
2011-12-19 21:44:39 +01:00
const Industry * i = Industry : : GetIfValid ( args - > GetInt32 ( SCC_INDUSTRY_NAME ) ) ;
if ( i = = NULL ) break ;
2009-01-16 13:59:47 +01:00
2011-11-19 23:15:44 +01:00
/* First print the town name and the industry type name. */
int64 args_array [ 2 ] = { i - > town - > index , GetIndustrySpec ( i - > type ) - > name } ;
StringParameters tmp_params ( args_array ) ;
2005-09-10 17:14:35 +02:00
2011-11-19 23:15:44 +01:00
buff = FormatString ( buff , GetStringPtr ( STR_FORMAT_INDUSTRY_NAME ) , & tmp_params , last , next_substr_case_index ) ;
next_substr_case_index = 0 ;
break ;
}
2008-01-12 20:58:06 +01:00
2011-11-19 23:15:44 +01:00
case SCC_PRESIDENT_NAME : { // {PRESIDENT_NAME}
2011-12-19 21:44:39 +01:00
const Company * c = Company : : GetIfValid ( args - > GetInt32 ( SCC_PRESIDENT_NAME ) ) ;
if ( c = = NULL ) break ;
2008-09-12 19:11:28 +02:00
2011-11-19 23:15:44 +01:00
if ( c - > president_name ! = NULL ) {
buff = strecpy ( buff , c - > president_name , last ) ;
2006-11-16 23:05:33 +01:00
} else {
2011-11-19 23:15:44 +01:00
int64 args_array [ ] = { c - > president_name_2 } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args_array ) ;
2011-11-19 23:15:44 +01:00
buff = GetStringWithArgs ( buff , c - > president_name_1 , & tmp_params , last ) ;
2006-11-16 23:05:33 +01:00
}
break ;
2005-07-17 12:18:23 +02:00
}
2004-08-09 19:04:08 +02:00
2006-11-16 23:05:33 +01:00
case SCC_STATION_NAME : { // {STATION}
2011-03-18 14:34:52 +01:00
StationID sid = args - > GetInt32 ( SCC_STATION_NAME ) ;
2009-05-18 18:21:28 +02:00
const Station * st = Station : : GetIfValid ( sid ) ;
2005-07-15 16:53:44 +02:00
2009-05-18 18:21:28 +02:00
if ( st = = NULL ) {
2008-09-12 19:14:07 +02:00
/* The station doesn't exist anymore. The only place where we might
* be " drawing " an invalid station is in the case of cargo that is
* in transit . */
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( NULL , 0 , NULL ) ;
buff = GetStringWithArgs ( buff , STR_UNKNOWN_STATION , & tmp_params , last ) ;
2008-09-12 19:14:07 +02:00
break ;
}
if ( st - > name ! = NULL ) {
2008-01-12 20:58:06 +01:00
buff = strecpy ( buff , st - > name , last ) ;
2006-11-16 23:05:33 +01:00
} else {
2008-11-20 00:55:34 +01:00
StringID str = st - > string_id ;
if ( st - > indtype ! = IT_INVALID ) {
/* Special case where the industry provides the name for the station */
const IndustrySpec * indsp = GetIndustrySpec ( st - > indtype ) ;
/* Industry GRFs can change which might remove the station name and
* thus cause very strange things . Here we check for that before we
* actually set the station name . */
if ( indsp - > station_name ! = STR_NULL & & indsp - > station_name ! = STR_UNDEFINED ) {
str = indsp - > station_name ;
}
}
2011-03-22 23:13:20 +01:00
int64 args_array [ ] = { STR_TOWN_NAME , st - > town - > index , st - > index } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args_array ) ;
buff = GetStringWithArgs ( buff , str , & tmp_params , last ) ;
2006-11-16 23:05:33 +01:00
}
break ;
2004-08-16 15:54:37 +02:00
}
2005-11-14 20:48:04 +01:00
2006-11-16 23:05:33 +01:00
case SCC_TOWN_NAME : { // {TOWN}
2011-12-19 21:44:39 +01:00
const Town * t = Town : : GetIfValid ( args - > GetInt32 ( SCC_TOWN_NAME ) ) ;
if ( t = = NULL ) break ;
2004-08-09 19:04:08 +02:00
2008-01-12 20:58:06 +01:00
if ( t - > name ! = NULL ) {
buff = strecpy ( buff , t - > name , last ) ;
2007-06-19 01:00:55 +02:00
} else {
2009-09-22 15:54:54 +02:00
buff = GetTownName ( buff , t , last ) ;
2007-06-19 01:00:55 +02:00
}
2006-11-16 23:05:33 +01:00
break ;
}
2004-08-09 19:04:08 +02:00
2011-11-19 23:15:44 +01:00
case SCC_WAYPOINT_NAME : { // {WAYPOINT}
2011-12-19 21:44:39 +01:00
Waypoint * wp = Waypoint : : GetIfValid ( args - > GetInt32 ( SCC_WAYPOINT_NAME ) ) ;
if ( wp = = NULL ) break ;
2007-05-19 11:40:18 +02:00
2011-11-19 23:15:44 +01:00
if ( wp - > name ! = NULL ) {
buff = strecpy ( buff , wp - > name , last ) ;
2008-01-12 20:58:06 +01:00
} else {
2011-11-19 23:15:44 +01:00
int64 args_array [ ] = { wp - > town - > index , wp - > town_cn + 1 } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args_array ) ;
2011-11-19 23:15:44 +01:00
StringID str = ( ( wp - > string_id = = STR_SV_STNAME_BUOY ) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME ) ;
if ( wp - > town_cn ! = 0 ) str + + ;
buff = GetStringWithArgs ( buff , str , & tmp_params , last ) ;
2008-01-12 20:58:06 +01:00
}
2007-06-25 16:46:32 +02:00
break ;
}
2007-06-25 00:42:11 +02:00
case SCC_VEHICLE_NAME : { // {VEHICLE}
2011-12-19 21:44:39 +01:00
const Vehicle * v = Vehicle : : GetIfValid ( args - > GetInt32 ( SCC_VEHICLE_NAME ) ) ;
if ( v = = NULL ) break ;
2009-05-22 17:13:50 +02:00
2008-01-12 20:58:06 +01:00
if ( v - > name ! = NULL ) {
buff = strecpy ( buff , v - > name , last ) ;
} else {
2011-03-22 23:13:20 +01:00
int64 args_array [ ] = { v - > unitnumber } ;
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( args_array ) ;
2008-01-12 20:58:06 +01:00
StringID str ;
switch ( v - > type ) {
default : NOT_REACHED ( ) ;
case VEH_TRAIN : str = STR_SV_TRAIN_NAME ; break ;
2009-07-23 00:44:56 +02:00
case VEH_ROAD : str = STR_SV_ROAD_VEHICLE_NAME ; break ;
2008-01-12 20:58:06 +01:00
case VEH_SHIP : str = STR_SV_SHIP_NAME ; break ;
case VEH_AIRCRAFT : str = STR_SV_AIRCRAFT_NAME ; break ;
}
2007-06-25 00:42:11 +02:00
2011-03-18 14:34:52 +01:00
buff = GetStringWithArgs ( buff , str , & tmp_params , last ) ;
2008-01-12 20:58:06 +01:00
}
2007-06-25 00:42:11 +02:00
break ;
}
2007-06-25 12:40:56 +02:00
case SCC_SIGN_NAME : { // {SIGN}
2011-12-19 21:44:39 +01:00
const Sign * si = Sign : : GetIfValid ( args - > GetInt32 ( ) ) ;
if ( si = = NULL ) break ;
2008-01-12 20:58:06 +01:00
if ( si - > name ! = NULL ) {
buff = strecpy ( buff , si - > name , last ) ;
} else {
2011-03-18 14:34:52 +01:00
StringParameters tmp_params ( NULL , 0 , NULL ) ;
buff = GetStringWithArgs ( buff , STR_DEFAULT_SIGN_NAME , & tmp_params , last ) ;
2008-01-12 20:58:06 +01:00
}
2007-06-25 12:40:56 +02:00
break ;
}
2011-11-19 23:15:44 +01:00
case SCC_STATION_FEATURES : { // {STATIONFEATURES}
buff = StationGetSpecialString ( buff , args - > GetInt32 ( SCC_STATION_FEATURES ) , last ) ;
2006-11-16 23:05:33 +01:00
break ;
2005-07-17 12:18:23 +02:00
}
2005-01-23 00:13:20 +01:00
2006-11-16 23:05:33 +01:00
default :
if ( buff + Utf8CharLen ( b ) < last ) buff + = Utf8Encode ( buff , b ) ;
break ;
2004-08-09 19:04:08 +02:00
}
}
2005-02-06 12:23:41 +01:00
* buff = ' \0 ' ;
2004-08-09 19:04:08 +02:00
return buff ;
}
2008-11-24 21:51:55 +01:00
static char * StationGetSpecialString ( char * buff , int x , const char * last )
2004-08-09 19:04:08 +02:00
{
2008-04-19 20:03:24 +02:00
if ( ( x & FACIL_TRAIN ) & & ( buff + Utf8CharLen ( SCC_TRAIN ) < last ) ) buff + = Utf8Encode ( buff , SCC_TRAIN ) ;
if ( ( x & FACIL_TRUCK_STOP ) & & ( buff + Utf8CharLen ( SCC_LORRY ) < last ) ) buff + = Utf8Encode ( buff , SCC_LORRY ) ;
if ( ( x & FACIL_BUS_STOP ) & & ( buff + Utf8CharLen ( SCC_BUS ) < last ) ) buff + = Utf8Encode ( buff , SCC_BUS ) ;
if ( ( x & FACIL_DOCK ) & & ( buff + Utf8CharLen ( SCC_SHIP ) < last ) ) buff + = Utf8Encode ( buff , SCC_SHIP ) ;
2010-06-19 18:40:31 +02:00
if ( ( x & FACIL_AIRPORT ) & & ( buff + Utf8CharLen ( SCC_PLANE ) < last ) ) buff + = Utf8Encode ( buff , SCC_PLANE ) ;
2006-11-16 23:05:33 +01:00
* buff = ' \0 ' ;
2004-08-09 19:04:08 +02:00
return buff ;
}
2008-11-24 21:51:55 +01:00
static char * GetSpecialTownNameString ( char * buff , int ind , uint32 seed , const char * last )
2005-02-06 12:23:41 +01:00
{
2009-03-16 11:51:49 +01:00
return GenerateTownNameString ( buff , last , ind , seed ) ;
2004-08-09 19:04:08 +02:00
}
2008-11-24 21:51:55 +01:00
static const char * const _silly_company_names [ ] = {
2004-08-09 19:04:08 +02:00
" Bloggs Brothers " ,
" Tiny Transport Ltd. " ,
" Express Travel " ,
" Comfy-Coach & Co. " ,
" Crush & Bump Ltd. " ,
" Broken & Late Ltd. " ,
" Sam Speedy & Son " ,
" Supersonic Travel " ,
" Mike's Motors " ,
" Lightning International " ,
" Pannik & Loozit Ltd. " ,
" Inter-City Transport " ,
2005-11-16 12:17:52 +01:00
" Getout & Pushit Ltd. "
2004-08-09 19:04:08 +02:00
} ;
2008-11-24 21:51:55 +01:00
static const char * const _surname_list [ ] = {
2004-08-09 19:04:08 +02:00
" Adams " ,
" Allan " ,
" Baker " ,
" Bigwig " ,
" Black " ,
" Bloggs " ,
" Brown " ,
" Campbell " ,
" Gordon " ,
" Hamilton " ,
" Hawthorn " ,
" Higgins " ,
" Green " ,
" Gribble " ,
" Jones " ,
" McAlpine " ,
" MacDonald " ,
" McIntosh " ,
" Muir " ,
" Murphy " ,
" Nelson " ,
" O'Donnell " ,
" Parker " ,
" Phillips " ,
" Pilkington " ,
" Quigley " ,
" Sharkey " ,
" Thomson " ,
2005-11-16 12:17:52 +01:00
" Watkins "
} ;
2008-11-24 21:51:55 +01:00
static const char * const _silly_surname_list [ ] = {
2004-08-09 19:04:08 +02:00
" Grumpy " ,
" Dozy " ,
" Speedy " ,
" Nosey " ,
" Dribble " ,
" Mushroom " ,
" Cabbage " ,
" Sniffle " ,
" Fishy " ,
" Swindle " ,
" Sneaky " ,
2005-11-16 12:17:52 +01:00
" Nutkins "
2004-08-09 19:04:08 +02:00
} ;
2005-02-06 10:52:06 +01:00
static const char _initial_name_letters [ ] = {
2005-02-06 17:56:04 +01:00
' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' , ' G ' , ' H ' , ' I ' , ' J ' ,
' K ' , ' L ' , ' M ' , ' N ' , ' P ' , ' R ' , ' S ' , ' T ' , ' W ' ,
2004-08-09 19:04:08 +02:00
} ;
2008-11-24 21:51:55 +01:00
static char * GenAndCoName ( char * buff , uint32 arg , const char * last )
2004-08-09 19:04:08 +02:00
{
2008-11-24 21:51:55 +01:00
const char * const * base ;
2005-11-16 12:17:52 +01:00
uint num ;
2004-08-09 19:04:08 +02:00
2008-05-29 17:13:28 +02:00
if ( _settings_game . game_creation . landscape = = LT_TOYLAND ) {
2005-11-16 12:17:52 +01:00
base = _silly_surname_list ;
num = lengthof ( _silly_surname_list ) ;
} else {
base = _surname_list ;
num = lengthof ( _surname_list ) ;
2004-08-09 19:04:08 +02:00
}
2006-10-22 01:31:34 +02:00
buff = strecpy ( buff , base [ num * GB ( arg , 16 , 8 ) > > 8 ] , last ) ;
buff = strecpy ( buff , " & Co. " , last ) ;
2004-08-09 19:04:08 +02:00
return buff ;
}
2008-11-24 21:51:55 +01:00
static char * GenPresidentName ( char * buff , uint32 x , const char * last )
2004-08-09 19:04:08 +02:00
{
2006-10-22 01:31:34 +02:00
char initial [ ] = " ?. " ;
2008-11-24 21:51:55 +01:00
const char * const * base ;
2005-11-16 12:17:52 +01:00
uint num ;
uint i ;
2004-08-09 19:04:08 +02:00
2006-10-22 01:31:34 +02:00
initial [ 0 ] = _initial_name_letters [ sizeof ( _initial_name_letters ) * GB ( x , 0 , 8 ) > > 8 ] ;
buff = strecpy ( buff , initial , last ) ;
2004-08-09 19:04:08 +02:00
2005-07-21 08:31:02 +02:00
i = ( sizeof ( _initial_name_letters ) + 35 ) * GB ( x , 8 , 8 ) > > 8 ;
2004-08-09 19:04:08 +02:00
if ( i < sizeof ( _initial_name_letters ) ) {
2006-10-22 01:31:34 +02:00
initial [ 0 ] = _initial_name_letters [ i ] ;
buff = strecpy ( buff , initial , last ) ;
2004-08-09 19:04:08 +02:00
}
2008-05-29 17:13:28 +02:00
if ( _settings_game . game_creation . landscape = = LT_TOYLAND ) {
2005-11-16 12:17:52 +01:00
base = _silly_surname_list ;
num = lengthof ( _silly_surname_list ) ;
} else {
base = _surname_list ;
num = lengthof ( _surname_list ) ;
2004-08-09 19:04:08 +02:00
}
2006-10-22 01:31:34 +02:00
buff = strecpy ( buff , base [ num * GB ( x , 16 , 8 ) > > 8 ] , last ) ;
2004-08-09 19:04:08 +02:00
return buff ;
}
2011-03-18 14:34:52 +01:00
static char * GetSpecialNameString ( char * buff , int ind , StringParameters * args , const char * last )
2004-08-09 19:04:08 +02:00
{
2005-02-06 12:23:41 +01:00
switch ( ind ) {
2005-02-06 17:56:04 +01:00
case 1 : // not used
2011-09-23 21:25:48 +02:00
return strecpy ( buff , _silly_company_names [ min ( args - > GetInt32 ( ) & 0xFFFF , lengthof ( _silly_company_names ) - 1 ) ] , last ) ;
2004-08-09 19:04:08 +02:00
2005-02-06 17:56:04 +01:00
case 2 : // used for Foobar & Co company names
2011-03-18 14:34:52 +01:00
return GenAndCoName ( buff , args - > GetInt32 ( ) , last ) ;
2004-08-09 19:04:08 +02:00
2005-02-06 17:56:04 +01:00
case 3 : // President name
2011-03-18 14:34:52 +01:00
return GenPresidentName ( buff , args - > GetInt32 ( ) , last ) ;
2004-08-09 19:04:08 +02:00
}
2007-04-04 03:35:16 +02:00
/* town name? */
2008-04-18 06:37:06 +02:00
if ( IsInsideMM ( ind - 6 , 0 , SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1 ) ) {
2011-03-18 14:34:52 +01:00
buff = GetSpecialTownNameString ( buff , ind - 6 , args - > GetInt32 ( ) , last ) ;
2006-10-22 01:31:34 +02:00
return strecpy ( buff , " Transport " , last ) ;
2004-08-09 19:04:08 +02:00
}
2007-04-04 03:35:16 +02:00
/* language name? */
2007-11-24 11:38:43 +01:00
if ( IsInsideMM ( ind , ( SPECSTR_LANGUAGE_START - 0x70E4 ) , ( SPECSTR_LANGUAGE_END - 0x70E4 ) + 1 ) ) {
2004-08-09 19:04:08 +02:00
int i = ind - ( SPECSTR_LANGUAGE_START - 0x70E4 ) ;
2005-05-22 09:43:18 +02:00
return strecpy ( buff ,
2010-11-13 13:09:30 +01:00
& _languages [ i ] = = _current_language ? _current_language - > own_name : _languages [ i ] . name , last ) ;
2004-08-09 19:04:08 +02:00
}
2007-04-04 03:35:16 +02:00
/* resolution size? */
2007-11-24 11:38:43 +01:00
if ( IsInsideMM ( ind , ( SPECSTR_RESOLUTION_START - 0x70E4 ) , ( SPECSTR_RESOLUTION_END - 0x70E4 ) + 1 ) ) {
2004-08-09 19:04:08 +02:00
int i = ind - ( SPECSTR_RESOLUTION_START - 0x70E4 ) ;
2008-10-28 15:42:31 +01:00
buff + = seprintf (
2009-06-27 22:53:45 +02:00
buff , last , " %ux%u " , _resolutions [ i ] . width , _resolutions [ i ] . height
2006-10-22 01:31:34 +02:00
) ;
return buff ;
2004-08-09 19:04:08 +02:00
}
2007-04-04 03:35:16 +02:00
/* screenshot format name? */
2007-11-24 11:38:43 +01:00
if ( IsInsideMM ( ind , ( SPECSTR_SCREENSHOT_START - 0x70E4 ) , ( SPECSTR_SCREENSHOT_END - 0x70E4 ) + 1 ) ) {
2004-08-09 19:04:08 +02:00
int i = ind - ( SPECSTR_SCREENSHOT_START - 0x70E4 ) ;
2006-10-22 01:31:34 +02:00
return strecpy ( buff , GetScreenshotFormatDesc ( i ) , last ) ;
2004-08-09 19:04:08 +02:00
}
2009-07-31 15:25:20 +02:00
NOT_REACHED ( ) ;
2004-08-09 19:04:08 +02:00
}
2007-06-13 19:34:05 +02:00
# ifdef ENABLE_NETWORK
2007-06-04 18:07:22 +02:00
extern void SortNetworkLanguages ( ) ;
2007-06-13 19:34:05 +02:00
# else /* ENABLE_NETWORK */
static inline void SortNetworkLanguages ( ) { }
# endif /* ENABLE_NETWORK */
2007-06-04 18:07:22 +02:00
2011-01-18 23:31:06 +01:00
/**
* Check whether the header is a valid header for OpenTTD .
* @ return true iff the header is deemed valid .
*/
2010-11-10 18:48:31 +01:00
bool LanguagePackHeader : : IsValid ( ) const
{
2010-11-20 16:44:24 +01:00
return this - > ident = = TO_LE32 ( LanguagePackHeader : : IDENT ) & &
this - > version = = TO_LE32 ( LANGUAGE_PACK_VERSION ) & &
this - > plural_form < LANGUAGE_MAX_PLURAL & &
this - > text_dir < = 1 & &
this - > newgrflangid < MAX_LANG & &
this - > num_genders < MAX_NUM_GENDERS & &
this - > num_cases < MAX_NUM_CASES & &
StrValid ( this - > name , lastof ( this - > name ) ) & &
StrValid ( this - > own_name , lastof ( this - > own_name ) ) & &
StrValid ( this - > isocode , lastof ( this - > isocode ) ) & &
StrValid ( this - > digit_group_separator , lastof ( this - > digit_group_separator ) ) & &
StrValid ( this - > digit_group_separator_currency , lastof ( this - > digit_group_separator_currency ) ) & &
StrValid ( this - > digit_decimal_separator , lastof ( this - > digit_decimal_separator ) ) ;
2010-11-10 18:48:31 +01:00
}
2011-05-02 19:42:12 +02:00
/**
* Read a particular language .
* @ param lang The metadata about the language .
* @ return Whether the loading went okay or not .
*/
2010-11-13 12:38:01 +01:00
bool ReadLanguagePack ( const LanguageMetadata * lang )
2005-02-06 12:23:41 +01:00
{
2010-02-13 00:39:44 +01:00
/* Current language pack */
2004-08-09 19:04:08 +02:00
size_t len ;
2011-02-27 07:36:14 +01:00
LanguagePack * lang_pack = ( LanguagePack * ) ReadFileToMem ( lang - > file , & len , 1U < < 20 ) ;
2010-02-13 00:39:44 +01:00
if ( lang_pack = = NULL ) return false ;
2004-08-09 19:04:08 +02:00
2010-02-13 00:39:44 +01:00
/* End of read data (+ terminating zero added in ReadFileToMem()) */
const char * end = ( char * ) lang_pack + len + 1 ;
2007-03-17 23:21:05 +01:00
2010-02-13 00:39:44 +01:00
/* We need at least one byte of lang_pack->data */
2010-11-10 18:48:31 +01:00
if ( end < = lang_pack - > data | | ! lang_pack - > IsValid ( ) ) {
2004-08-09 19:04:08 +02:00
free ( lang_pack ) ;
return false ;
}
2004-09-10 21:02:27 +02:00
2008-06-17 21:38:00 +02:00
# if TTD_ENDIAN == TTD_BIG_ENDIAN
2011-12-18 00:16:16 +01:00
for ( uint i = 0 ; i < TAB_COUNT ; i + + ) {
2006-02-03 22:51:42 +01:00
lang_pack - > offsets [ i ] = ReadLE16Aligned ( & lang_pack - > offsets [ i ] ) ;
2004-08-09 19:04:08 +02:00
}
2008-06-17 21:38:00 +02:00
# endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
2004-08-09 19:04:08 +02:00
2010-02-13 00:39:44 +01:00
uint count = 0 ;
2011-12-18 00:16:16 +01:00
for ( uint i = 0 ; i < TAB_COUNT ; i + + ) {
2005-02-06 15:47:56 +01:00
uint num = lang_pack - > offsets [ i ] ;
2010-02-13 00:39:44 +01:00
_langtab_start [ i ] = count ;
2004-08-09 19:04:08 +02:00
_langtab_num [ i ] = num ;
2010-02-13 00:39:44 +01:00
count + = num ;
2004-08-09 19:04:08 +02:00
}
2007-04-04 03:35:16 +02:00
/* Allocate offsets */
2010-02-13 00:39:44 +01:00
char * * langpack_offs = MallocT < char * > ( count ) ;
2004-08-09 19:04:08 +02:00
2007-04-04 03:35:16 +02:00
/* Fill offsets */
2010-02-13 00:39:44 +01:00
char * s = lang_pack - > data ;
len = ( byte ) * s + + ;
for ( uint i = 0 ; i < count ; i + + ) {
if ( s + len > = end ) {
free ( lang_pack ) ;
free ( langpack_offs ) ;
return false ;
}
if ( len > = 0xC0 ) {
len = ( ( len & 0x3F ) < < 8 ) + ( byte ) * s + + ;
if ( s + len > = end ) {
free ( lang_pack ) ;
free ( langpack_offs ) ;
return false ;
}
}
2004-08-09 19:04:08 +02:00
langpack_offs [ i ] = s ;
s + = len ;
2010-02-13 00:39:44 +01:00
len = ( byte ) * s ;
* s + + = ' \0 ' ; // zero terminate the string
2004-08-09 19:04:08 +02:00
}
2005-02-06 17:56:04 +01:00
free ( _langpack ) ;
2004-08-09 19:04:08 +02:00
_langpack = lang_pack ;
2005-02-06 17:56:04 +01:00
free ( _langpack_offs ) ;
2004-08-09 19:04:08 +02:00
_langpack_offs = langpack_offs ;
2010-11-13 12:38:01 +01:00
_current_language = lang ;
2010-11-13 12:25:58 +01:00
_current_text_dir = ( TextDirection ) _current_language - > text_dir ;
const char * c_file = strrchr ( _current_language - > file , PATHSEPCHAR ) + 1 ;
2010-11-13 12:28:55 +01:00
strecpy ( _config_language_file , c_file , lastof ( _config_language_file ) ) ;
2010-11-13 12:25:58 +01:00
SetCurrentGrfLangID ( _current_language - > newgrflangid ) ;
2004-08-09 19:04:08 +02:00
2010-11-27 23:47:29 +01:00
# ifdef WITH_ICU
/* Delete previous collator. */
if ( _current_collator ! = NULL ) {
delete _current_collator ;
_current_collator = NULL ;
}
/* Create a collator instance for our current locale. */
UErrorCode status = U_ZERO_ERROR ;
_current_collator = Collator : : createInstance ( Locale ( _current_language - > isocode ) , status ) ;
/* Sort number substrings by their numerical value. */
if ( _current_collator ! = NULL ) _current_collator - > setAttribute ( UCOL_NUMERIC_COLLATION , UCOL_ON , status ) ;
/* Avoid using the collator if it is not correctly set. */
if ( U_FAILURE ( status ) ) {
delete _current_collator ;
_current_collator = NULL ;
}
# endif /* WITH_ICU */
2010-11-27 23:43:58 +01:00
/* Some lists need to be sorted again after a language change. */
2010-04-01 21:48:28 +02:00
InitializeSortedCargoSpecs ( ) ;
2010-03-28 14:25:09 +02:00
SortIndustryTypes ( ) ;
BuildIndustriesLegend ( ) ;
2007-06-04 18:07:22 +02:00
SortNetworkLanguages ( ) ;
2010-11-27 23:43:58 +01:00
InvalidateWindowClassesData ( WC_BUILD_VEHICLE ) ; // Build vehicle window.
InvalidateWindowClassesData ( WC_TRAINS_LIST ) ; // Train group window.
InvalidateWindowClassesData ( WC_ROADVEH_LIST ) ; // Road vehicle group window.
InvalidateWindowClassesData ( WC_SHIPS_LIST ) ; // Ship group window.
InvalidateWindowClassesData ( WC_AIRCRAFT_LIST ) ; // Aircraft group window.
InvalidateWindowClassesData ( WC_INDUSTRY_DIRECTORY ) ; // Industry directory window.
InvalidateWindowClassesData ( WC_STATION_LIST ) ; // Station list window.
2004-08-09 19:04:08 +02:00
return true ;
}
2009-03-15 01:32:18 +01:00
/* Win32 implementation in win32.cpp.
* OS X implementation in os / macosx / macos . mm . */
2007-08-11 17:52:34 +02:00
# if !(defined(WIN32) || defined(__APPLE__))
2010-08-01 21:22:34 +02:00
/**
* Determine the current charset based on the environment
2006-03-25 10:22:10 +01:00
* First check some default values , after this one we passed ourselves
* and if none exist return the value for $ LANG
2007-04-04 03:35:16 +02:00
* @ param param environment variable to check conditionally if default ones are not
2006-03-25 10:22:10 +01:00
* set . Pass NULL if you don ' t want additional checks .
2010-08-01 21:44:49 +02:00
* @ return return string containing current charset , or NULL if not - determinable
*/
2006-03-25 10:22:10 +01:00
const char * GetCurrentLocale ( const char * param )
{
const char * env ;
env = getenv ( " LANGUAGE " ) ;
if ( env ! = NULL ) return env ;
env = getenv ( " LC_ALL " ) ;
if ( env ! = NULL ) return env ;
if ( param ! = NULL ) {
env = getenv ( param ) ;
if ( env ! = NULL ) return env ;
}
return getenv ( " LANG " ) ;
}
2008-04-19 15:04:29 +02:00
# else
const char * GetCurrentLocale ( const char * param ) ;
2007-08-11 17:52:34 +02:00
# endif /* !(defined(WIN32) || defined(__APPLE__)) */
2006-03-25 10:22:10 +01:00
2009-09-13 19:38:07 +02:00
int CDECL StringIDSorter ( const StringID * a , const StringID * b )
2007-06-04 18:07:22 +02:00
{
char stra [ 512 ] ;
char strb [ 512 ] ;
2009-09-13 19:38:07 +02:00
GetString ( stra , * a , lastof ( stra ) ) ;
GetString ( strb , * b , lastof ( strb ) ) ;
2007-06-04 18:07:22 +02:00
return strcmp ( stra , strb ) ;
}
2007-03-17 23:21:05 +01:00
/**
2010-11-13 20:10:30 +01:00
* Get the language with the given NewGRF language ID .
* @ param newgrflangid NewGRF languages ID to check .
* @ return The language ' s metadata , or NULL if it is not known .
2007-03-17 23:21:05 +01:00
*/
2010-11-13 20:10:30 +01:00
const LanguageMetadata * GetLanguage ( byte newgrflangid )
2007-03-17 23:21:05 +01:00
{
2010-11-13 13:09:30 +01:00
for ( const LanguageMetadata * lang = _languages . Begin ( ) ; lang ! = _languages . End ( ) ; lang + + ) {
2010-11-13 20:10:30 +01:00
if ( newgrflangid = = lang - > newgrflangid ) return lang ;
2007-03-17 23:21:05 +01:00
}
2010-11-13 20:10:30 +01:00
return NULL ;
2007-03-17 23:21:05 +01:00
}
/**
* Reads the language file header and checks compatability .
* @ param file the file to read
* @ param hdr the place to write the header information to
* @ return true if and only if the language file is of a compatible version
*/
2010-11-13 12:11:02 +01:00
static bool GetLanguageFileHeader ( const char * file , LanguagePackHeader * hdr )
2007-03-17 23:21:05 +01:00
{
FILE * f = fopen ( file , " rb " ) ;
if ( f = = NULL ) return false ;
size_t read = fread ( hdr , sizeof ( * hdr ) , 1 , f ) ;
fclose ( f ) ;
2010-11-10 18:48:31 +01:00
bool ret = read = = 1 & & hdr - > IsValid ( ) ;
2008-11-24 19:53:17 +01:00
/* Convert endianness for the windows language ID */
2011-08-21 21:21:38 +02:00
if ( ret ) {
hdr - > missing = FROM_LE16 ( hdr - > missing ) ;
hdr - > winlangid = FROM_LE16 ( hdr - > winlangid ) ;
}
2008-11-24 19:53:17 +01:00
return ret ;
2006-08-05 01:45:20 +02:00
}
2007-03-17 23:21:05 +01:00
/**
* Gets a list of languages from the given directory .
* @ param path the base directory to search in
*/
2010-11-13 13:09:30 +01:00
static void GetLanguageList ( const char * path )
2006-08-05 01:45:20 +02:00
{
2007-03-17 23:21:05 +01:00
DIR * dir = ttd_opendir ( path ) ;
2006-08-05 01:45:20 +02:00
if ( dir ! = NULL ) {
2007-03-17 23:21:05 +01:00
struct dirent * dirent ;
2010-11-13 13:09:30 +01:00
while ( ( dirent = readdir ( dir ) ) ! = NULL ) {
2007-03-17 23:21:05 +01:00
const char * d_name = FS2OTTD ( dirent - > d_name ) ;
const char * extension = strrchr ( d_name , ' . ' ) ;
2006-08-05 01:45:20 +02:00
2007-03-17 23:21:05 +01:00
/* Not a language file */
if ( extension = = NULL | | strcmp ( extension , " .lng " ) ! = 0 ) continue ;
2010-11-13 13:09:30 +01:00
LanguageMetadata lmd ;
seprintf ( lmd . file , lastof ( lmd . file ) , " %s%s " , path , d_name ) ;
2007-03-17 23:21:05 +01:00
/* Check whether the file is of the correct version */
2010-11-13 13:09:30 +01:00
if ( ! GetLanguageFileHeader ( lmd . file , & lmd ) ) {
DEBUG ( misc , 3 , " %s is not a valid language file " , lmd . file ) ;
2010-11-13 20:10:30 +01:00
} else if ( GetLanguage ( lmd . newgrflangid ) ! = NULL ) {
2010-11-13 13:09:30 +01:00
DEBUG ( misc , 3 , " %s's language ID is already known " , lmd . file ) ;
} else {
* _languages . Append ( ) = lmd ;
2006-08-05 01:45:20 +02:00
}
}
closedir ( dir ) ;
}
}
2007-03-17 23:21:05 +01:00
/**
* Make a list of the available language packs . put the data in
* _dynlang struct .
*/
2007-03-07 12:47:46 +01:00
void InitializeLanguagePacks ( )
2004-08-09 19:04:08 +02:00
{
2007-06-17 17:48:57 +02:00
Searchpath sp ;
FOR_ALL_SEARCHPATHS ( sp ) {
char path [ MAX_PATH ] ;
FioAppendDirectory ( path , lengthof ( path ) , sp , LANG_DIR ) ;
2010-11-13 13:09:30 +01:00
GetLanguageList ( path ) ;
2007-06-17 17:48:57 +02:00
}
2010-11-13 13:09:30 +01:00
if ( _languages . Length ( ) = = 0 ) usererror ( " No available language packs (invalid versions?) " ) ;
2004-08-09 19:04:08 +02:00
2007-03-17 23:21:05 +01:00
/* Acquire the locale of the current system */
const char * lang = GetCurrentLocale ( " LC_MESSAGES " ) ;
if ( lang = = NULL ) lang = " en_GB " ;
2004-09-10 21:02:27 +02:00
2010-11-13 12:38:01 +01:00
const LanguageMetadata * chosen_language = NULL ; ///< Matching the language in the configuartion file or the current locale
const LanguageMetadata * language_fallback = NULL ; ///< Using pt_PT for pt_BR locale when pt_BR is not available
2010-11-13 13:09:30 +01:00
const LanguageMetadata * en_GB_fallback = _languages . Begin ( ) ; ///< Fallback when no locale-matching language has been found
2004-08-09 19:04:08 +02:00
2010-11-13 13:09:30 +01:00
/* Find a proper language. */
for ( const LanguageMetadata * lng = _languages . Begin ( ) ; lng ! = _languages . End ( ) ; lng + + ) {
2007-03-17 23:21:05 +01:00
/* We are trying to find a default language. The priority is by
* configuration file , local environment and last , if nothing found ,
2010-11-13 12:38:01 +01:00
* english . */
2010-11-13 13:09:30 +01:00
const char * lang_file = strrchr ( lng - > file , PATHSEPCHAR ) + 1 ;
2010-11-13 12:38:01 +01:00
if ( strcmp ( lang_file , _config_language_file ) = = 0 ) {
chosen_language = lng ;
break ;
2007-03-17 23:21:05 +01:00
}
2010-11-13 12:38:01 +01:00
if ( strcmp ( lng - > isocode , " en_GB " ) = = 0 ) en_GB_fallback = lng ;
if ( strncmp ( lng - > isocode , lang , 5 ) = = 0 ) chosen_language = lng ;
if ( strncmp ( lng - > isocode , lang , 2 ) = = 0 ) language_fallback = lng ;
2007-03-17 23:21:05 +01:00
}
2004-09-10 21:02:27 +02:00
2007-03-17 23:21:05 +01:00
/* We haven't found the language in the config nor the one in the locale.
* Now we set it to one of the fallback languages */
2010-11-13 12:38:01 +01:00
if ( chosen_language = = NULL ) {
chosen_language = ( language_fallback ! = NULL ) ? language_fallback : en_GB_fallback ;
2007-03-17 23:21:05 +01:00
}
2004-08-09 19:04:08 +02:00
2010-11-13 12:38:01 +01:00
if ( ! ReadLanguagePack ( chosen_language ) ) usererror ( " Can't read language pack '%s' " , chosen_language - > file ) ;
2005-07-15 23:28:26 +02:00
}
2007-12-16 19:38:19 +01:00
2009-10-17 22:34:09 +02:00
/**
* Get the ISO language code of the currently loaded language .
* @ return the ISO code .
*/
const char * GetCurrentLanguageIsoCode ( )
{
return _langpack - > isocode ;
}
2010-08-02 14:10:48 +02:00
/**
* Check whether there are glyphs missing in the current language .
* @ param Pointer to an address for storing the text pointer .
* @ return If glyphs are missing , return \ c true , else return \ false .
2010-11-23 18:59:50 +01:00
* @ post If \ c true is returned and str is not NULL , * str points to a string that is found to contain at least one missing glyph .
2010-08-02 14:10:48 +02:00
*/
2011-11-19 19:43:00 +01:00
bool MissingGlyphSearcher : : FindMissingGlyphs ( const char * * str )
2010-08-02 14:10:48 +02:00
{
2011-11-20 13:01:42 +01:00
InitFreeType ( this - > Monospace ( ) ) ;
2010-11-25 16:20:10 +01:00
const Sprite * question_mark [ FS_END ] ;
2011-12-10 13:56:37 +01:00
for ( FontSize size = this - > Monospace ( ) ? FS_MONO : FS_BEGIN ; size < ( this - > Monospace ( ) ? FS_END : FS_MONO ) ; size + + ) {
2010-11-25 16:20:10 +01:00
question_mark [ size ] = GetGlyph ( size , ' ? ' ) ;
}
2011-11-19 19:43:00 +01:00
this - > Reset ( ) ;
for ( const char * text = this - > NextString ( ) ; text ! = NULL ; text = this - > NextString ( ) ) {
FontSize size = this - > DefaultSize ( ) ;
if ( str ! = NULL ) * str = text ;
for ( WChar c = Utf8Consume ( & text ) ; c ! = ' \0 ' ; c = Utf8Consume ( & text ) ) {
if ( c = = SCC_SETX ) {
/* SetX is, together with SetXY as special character that
* uses the next ( two ) characters as data points . We have
* to skip those , otherwise the UTF8 reading will go haywire . */
text + + ;
} else if ( c = = SCC_SETXY ) {
text + = 2 ;
} else if ( c = = SCC_TINYFONT ) {
size = FS_SMALL ;
} else if ( c = = SCC_BIGFONT ) {
size = FS_LARGE ;
} else if ( ! IsInsideMM ( c , SCC_SPRITE_START , SCC_SPRITE_END ) & & IsPrintable ( c ) & & ! IsTextDirectionChar ( c ) & & c ! = ' ? ' & & GetGlyph ( size , c ) = = question_mark [ size ] ) {
/* The character is printable, but not in the normal font. This is the case we were testing for. */
return true ;
2010-08-02 14:10:48 +02:00
}
}
}
return false ;
}
2011-11-19 19:43:00 +01:00
/** Helper for searching through the language pack. */
class LanguagePackGlyphSearcher : public MissingGlyphSearcher {
uint i ; ///< Iterator for the primary language tables.
uint j ; ///< Iterator for the secondary language tables.
/* virtual */ void Reset ( )
{
this - > i = 0 ;
this - > j = 0 ;
}
2011-11-19 22:02:37 +01:00
/* virtual */ FontSize DefaultSize ( )
2011-11-19 19:43:00 +01:00
{
return FS_NORMAL ;
}
2011-11-19 22:02:37 +01:00
/* virtual */ const char * NextString ( )
2011-11-19 19:43:00 +01:00
{
2011-12-18 00:16:16 +01:00
if ( this - > i > = TAB_COUNT ) return NULL ;
2011-11-19 19:43:00 +01:00
const char * ret = _langpack_offs [ _langtab_start [ i ] + j ] ;
this - > j + + ;
2011-12-18 00:16:16 +01:00
while ( this - > j > = _langtab_num [ this - > i ] & & this - > i < TAB_COUNT ) {
2011-11-19 19:43:00 +01:00
i + + ;
j = 0 ;
}
return ret ;
}
2011-11-19 22:02:37 +01:00
2011-11-20 13:01:42 +01:00
/* virtual */ bool Monospace ( )
{
return false ;
}
2011-11-19 22:02:37 +01:00
/* virtual */ void SetFontNames ( FreeTypeSettings * settings , const char * font_name )
{
2011-11-19 23:27:53 +01:00
# ifdef WITH_FREETYPE
2011-11-19 22:02:37 +01:00
strecpy ( settings - > small_font , font_name , lastof ( settings - > small_font ) ) ;
strecpy ( settings - > medium_font , font_name , lastof ( settings - > medium_font ) ) ;
strecpy ( settings - > large_font , font_name , lastof ( settings - > large_font ) ) ;
2011-11-19 23:27:53 +01:00
# endif /* WITH_FREETYPE */
2011-11-19 22:02:37 +01:00
}
2011-11-19 19:43:00 +01:00
} ;
2007-12-16 19:38:19 +01:00
/**
* Check whether the currently loaded language pack
* uses characters that the currently loaded font
* does not support . If this is the case an error
* message will be shown in English . The error
* message will not be localized because that would
* mean it might use characters that are not in the
* font , which is the whole reason this check has
* been added .
2011-11-17 22:11:34 +01:00
* @ param base_font Whether to look at the base font as well .
2011-11-20 12:52:11 +01:00
* @ param searcher The methods to use to search for strings to check .
* If NULL the loaded language pack searcher is used .
2007-12-16 19:38:19 +01:00
*/
2011-11-20 12:52:11 +01:00
void CheckForMissingGlyphs ( bool base_font , MissingGlyphSearcher * searcher )
2007-12-16 19:38:19 +01:00
{
2011-11-20 12:52:11 +01:00
static LanguagePackGlyphSearcher pack_searcher ;
if ( searcher = = NULL ) searcher = & pack_searcher ;
bool bad_font = ! base_font | | searcher - > FindMissingGlyphs ( NULL ) ;
2008-11-24 19:53:17 +01:00
# ifdef WITH_FREETYPE
2010-08-02 14:10:48 +02:00
if ( bad_font ) {
/* We found an unprintable character... lets try whether we can find
* a fallback font that can print the characters in the current language . */
FreeTypeSettings backup ;
memcpy ( & backup , & _freetype , sizeof ( backup ) ) ;
2011-11-20 12:52:11 +01:00
bad_font = ! SetFallbackFont ( & _freetype , _langpack - > isocode , _langpack - > winlangid , searcher ) ;
2008-11-24 19:53:17 +01:00
2010-08-02 14:10:48 +02:00
memcpy ( & _freetype , & backup , sizeof ( backup ) ) ;
2008-11-24 19:53:17 +01:00
2011-11-17 22:11:34 +01:00
if ( bad_font & & base_font ) {
2010-11-23 18:59:50 +01:00
/* Our fallback font does miss characters too, so keep the
2011-01-21 02:38:30 +01:00
* user chosen font as that is more likely to be any good than
* the wild guess we made */
2011-11-20 13:01:42 +01:00
InitFreeType ( searcher - > Monospace ( ) ) ;
2007-12-16 19:38:19 +01:00
}
2010-08-02 14:10:48 +02:00
}
# endif
if ( bad_font ) {
/* All attempts have failed. Display an error. As we do not want the string to be translated by
* the translators , we ' force ' it into the binary and ' load ' it via a BindCString . To do this
* properly we have to set the colour of the string , otherwise we end up with a lot of artefacts .
* The colour ' character ' might change in the future , so for safety we just Utf8 Encode it into
* the string , which takes exactly three characters , so it replaces the " XXX " with the colour marker . */
static char * err_str = strdup ( " 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. " ) ;
Utf8Encode ( err_str , SCC_YELLOW ) ;
SetDParamStr ( 0 , err_str ) ;
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_WARNING ) ;
/* Reset the font width */
2011-12-10 13:56:37 +01:00
LoadStringWidthTable ( searcher - > Monospace ( ) ) ;
2010-08-02 14:10:48 +02:00
return ;
2007-12-16 19:38:19 +01:00
}
2008-10-17 19:42:51 +02:00
2009-02-23 19:14:18 +01:00
/* Update the font with cache */
2011-12-10 13:56:37 +01:00
LoadStringWidthTable ( searcher - > Monospace ( ) ) ;
2009-02-23 19:14:18 +01:00
2008-10-17 19:42:51 +02:00
# if !defined(WITH_ICU)
/*
* For right - to - left languages we need the ICU library . If
* we do not have support for that library we warn the user
* about it with a message . As we do not want the string to
* be translated by the translators , we ' force ' it into the
* binary and ' load ' it via a BindCString . To do this
2009-02-09 03:57:15 +01:00
* properly we have to set the colour of the string ,
* otherwise we end up with a lot of artefacts . The colour
2008-10-17 19:42:51 +02:00
* ' character ' might change in the future , so for safety
* we just Utf8 Encode it into the string , which takes
* exactly three characters , so it replaces the " XXX " with
2009-02-09 03:57:15 +01:00
* the colour marker .
2008-10-17 19:42:51 +02:00
*/
2010-11-13 10:56:25 +01:00
if ( _current_text_dir ! = TD_LTR ) {
2008-10-17 19:42:51 +02:00
static char * err_str = strdup ( " XXXThis version of OpenTTD does not support right-to-left languages. Recompile with icu enabled. " ) ;
Utf8Encode ( err_str , SCC_YELLOW ) ;
SetDParamStr ( 0 , err_str ) ;
2010-02-24 15:46:15 +01:00
ShowErrorMessage ( STR_JUST_RAW_STRING , INVALID_STRING_ID , WL_ERROR ) ;
2008-10-17 19:42:51 +02:00
}
# endif
2007-12-16 19:38:19 +01:00
}