src/strings.cpp
branchnoai
changeset 9722 ebf0ece7d8f6
parent 9703 d2a6acdbd665
child 9723 eee46cb39750
equal deleted inserted replaced
9721:9a27928bcd5e 9722:ebf0ece7d8f6
   114  * @param last
   114  * @param last
   115  * @return a formatted string of char
   115  * @return a formatted string of char
   116  */
   116  */
   117 static char *GetStringWithArgs(char *buffr, uint string, const int64 *argv, const char* last)
   117 static char *GetStringWithArgs(char *buffr, uint string, const int64 *argv, const char* last)
   118 {
   118 {
       
   119 	if (GB(string, 0, 16) == 0) return GetStringWithArgs(buffr, STR_UNDEFINED, argv, last);
       
   120 
   119 	uint index = GB(string,  0, 11);
   121 	uint index = GB(string,  0, 11);
   120 	uint tab   = GB(string, 11,  5);
   122 	uint tab   = GB(string, 11,  5);
   121 	char buff[512];
   123 	char buff[512];
   122 
       
   123 	if (GB(string, 0, 16) == 0) error("!invalid string id 0 in GetString");
       
   124 
   124 
   125 	switch (tab) {
   125 	switch (tab) {
   126 		case 4:
   126 		case 4:
   127 			if (index >= 0xC0)
   127 			if (index >= 0xC0)
   128 				return GetSpecialTownNameString(buffr, index - 0xC0, GetInt32(&argv), last);
   128 				return GetSpecialTownNameString(buffr, index - 0xC0, GetInt32(&argv), last);
   137 			/* User defined name */
   137 			/* User defined name */
   138 			return GetName(buffr, index, last);
   138 			return GetName(buffr, index, last);
   139 
   139 
   140 		case 26:
   140 		case 26:
   141 			/* Include string within newgrf text (format code 81) */
   141 			/* Include string within newgrf text (format code 81) */
   142 			if (HASBIT(index, 10)) {
   142 			if (HasBit(index, 10)) {
   143 				StringID string = GetGRFStringID(0, 0xD000 + GB(index, 0, 10));
   143 				StringID string = GetGRFStringID(0, 0xD000 + GB(index, 0, 10));
   144 				return GetStringWithArgs(buffr, string, argv, last);
   144 				return GetStringWithArgs(buffr, string, argv, last);
   145 			}
   145 			}
   146 			break;
   146 			break;
   147 
   147 
   329 	return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last);
   329 	return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last);
   330 }
   330 }
   331 
   331 
   332 static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, Money number, bool compact, const char* last)
   332 static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, Money number, bool compact, const char* last)
   333 {
   333 {
   334 	const char* multiplier = "";
   334 	/* We are going to make number absolute for printing, so
       
   335 	 * keep this piece of data as we need it later on */
       
   336 	bool negative = number < 0;
       
   337 	const char *multiplier = "";
   335 	char buf[40];
   338 	char buf[40];
   336 	char* p;
   339 	char *p;
   337 	int j;
   340 	int j;
   338 
   341 
   339 	/* Multiply by exchange rate, but do it safely. */
   342 	number *= spec->rate;
   340 	CommandCost cs(number);
       
   341 	cs.MultiplyCost(spec->rate);
       
   342 	number = cs.GetCost();
       
   343 
   343 
   344 	/* convert from negative */
   344 	/* convert from negative */
   345 	if (number < 0) {
   345 	if (number < 0) {
   346 		if (buff + Utf8CharLen(SCC_RED) > last) return buff;
   346 		if (buff + Utf8CharLen(SCC_RED) > last) return buff;
   347 		buff += Utf8Encode(buff, SCC_RED);
   347 		buff += Utf8Encode(buff, SCC_RED);
   372 	do {
   372 	do {
   373 		if (--j == 0) {
   373 		if (--j == 0) {
   374 			*--p = spec->separator;
   374 			*--p = spec->separator;
   375 			j = 3;
   375 			j = 3;
   376 		}
   376 		}
   377 		*--p = '0' + number % 10;
   377 		*--p = '0' + (char)(number % 10);
   378 	} while ((number /= 10) != 0);
   378 	} while ((number /= 10) != 0);
   379 	buff = strecpy(buff, p, last);
   379 	buff = strecpy(buff, p, last);
   380 
   380 
   381 	buff = strecpy(buff, multiplier, last);
   381 	buff = strecpy(buff, multiplier, last);
   382 
   382 
   383 	/* Add suffix part, folowing symbol_pos specification.
   383 	/* Add suffix part, folowing symbol_pos specification.
   384 	 * Here, it can can be either 1 (suffix) or 2 (both prefix anf suffix).
   384 	 * Here, it can can be either 1 (suffix) or 2 (both prefix anf suffix).
   385 	 * The only remaining value is 1 (prefix), so everything that is not 0 */
   385 	 * The only remaining value is 1 (prefix), so everything that is not 0 */
   386 	if (spec->symbol_pos != 0) buff = strecpy(buff, spec->suffix, last);
   386 	if (spec->symbol_pos != 0) buff = strecpy(buff, spec->suffix, last);
   387 
   387 
   388 	if (cs.GetCost() < 0) {
   388 	if (negative) {
   389 		if (buff + Utf8CharLen(SCC_PREVIOUS_COLOUR) > last) return buff;
   389 		if (buff + Utf8CharLen(SCC_PREVIOUS_COLOUR) > last) return buff;
   390 		buff += Utf8Encode(buff, SCC_PREVIOUS_COLOUR);
   390 		buff += Utf8Encode(buff, SCC_PREVIOUS_COLOUR);
   391 		*buff = '\0';
   391 		*buff = '\0';
   392 	}
   392 	}
   393 
   393 
   689 				modifier = 0;
   689 				modifier = 0;
   690 				break;
   690 				break;
   691 			}
   691 			}
   692 
   692 
   693 			case SCC_GENDER_LIST: { // {G 0 Der Die Das}
   693 			case SCC_GENDER_LIST: { // {G 0 Der Die Das}
   694 				const char* s = GetStringPtr(argv_orig[(byte)*str++]); // contains the string that determines gender.
   694 				char buffr[512];
       
   695 				const char *s = GetStringWithArgs(buffr, argv_orig[(byte)*str++], argv, last); // contains the string that determines gender.
   695 				int len;
   696 				int len;
   696 				int gender = 0;
   697 				int gender = 0;
   697 				if (s != NULL) {
   698 				if (s != NULL) {
   698 					wchar_t c = Utf8Consume(&s);
   699 					wchar_t c = Utf8Consume(&s);
   699 					/* Switch case is always put before genders, so remove those bits */
   700 					/* Switch case is always put before genders, so remove those bits */