Changeset - r13647:3cbfb3297757
[Not reviewed]
master
0 3 0
rubidium - 15 years ago 2009-11-19 13:53:54
rubidium@openttd.org
(svn r18181) -Add: the concept of zero filled numbers to strgen
3 files changed with 18 insertions and 11 deletions:
0 comments (0 inline, 0 general)
src/strings.cpp
Show inline comments
 
@@ -185,29 +185,24 @@ void InjectDParam(uint amount)
 
	memmove(_decode_parameters + amount, _decode_parameters, sizeof(_decode_parameters) - amount * sizeof(uint64));
 
}
 

	
 
static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator)
 
static char *FormatNumber(char *buff, int64 number, const char *last, const char *separator, int zerofill_from = 19)
 
{
 
	uint64 divisor = 10000000000000000000ULL;
 
	uint64 quot;
 
	int i;
 
	uint64 tot;
 
	uint64 num;
 

	
 
	if (number < 0) {
 
		buff += seprintf(buff, last, "-");
 
		number = -number;
 
	}
 

	
 
	num = number;
 

	
 
	tot = 0;
 
	for (i = 0; i < 20; i++) {
 
		quot = 0;
 
	uint64 num = number;
 
	uint64 tot = 0;
 
	for (int i = 0; i < 20; i++) {
 
		uint64 quot = 0;
 
		if (num >= divisor) {
 
			quot = num / divisor;
 
			num = num % divisor;
 
		}
 
		if (tot |= quot || i == 19) {
 
		if (tot |= quot || i >= zerofill_from) {
 
			buff += seprintf(buff, last, "%i", (int)quot);
 
			if ((i % 3) == 1 && i != 19) buff = strecpy(buff, separator, last);
 
		}
 
@@ -232,6 +227,11 @@ static char *FormatNoCommaNumber(char *b
 
	return FormatNumber(buff, number, last, "");
 
}
 

	
 
static char *FormatZerofillNumber(char *buff, int64 number, int64 count, const char *last)
 
{
 
	return FormatNumber(buff, number, last, "", 20 - count);
 
}
 

	
 
static char *FormatHexNumber(char *buff, int64 number, const char *last)
 
{
 
	return buff + seprintf(buff, last, "0x%x", (uint32)number);
 
@@ -839,6 +839,11 @@ static char *FormatString(char *buff, co
 
				buff = FormatNoCommaNumber(buff, GetInt64(&argv), last);
 
				break;
 

	
 
			case SCC_ZEROFILL_NUM: { // {ZEROFILL_NUM}
 
				int64 num = GetInt64(&argv);
 
				buff = FormatZerofillNumber(buff, num, GetInt64(&argv), last);
 
			} break;
 

	
 
			case SCC_HEX: // {HEX}
 
				buff = FormatHexNumber(buff, GetInt64(&argv), last);
 
				break;
src/table/control_codes.h
Show inline comments
 
@@ -72,6 +72,7 @@ enum StringControlCode {
 
	SCC_STRING,
 
	SCC_COMMA,
 
	SCC_NUM,
 
	SCC_ZEROFILL_NUM,
 
	SCC_HEX,
 
	SCC_BYTES,
 

	
src/table/strgen_tables.h
Show inline comments
 
@@ -99,6 +99,7 @@ static const CmdStruct _cmd_structs[] = 
 
	/* Numbers */
 
	{"COMMA",           EmitSingleChar, SCC_COMMA,              1, C_NONE}, // Number with comma
 
	{"NUM",             EmitSingleChar, SCC_NUM,                1, C_NONE}, // Signed number
 
	{"ZEROFILL_NUM",    EmitSingleChar, SCC_ZEROFILL_NUM,       2, C_NONE}, // Unsigned number with zero fill, e.g. "02". First parameter is number, second minimum length
 
	{"BYTES",           EmitSingleChar, SCC_BYTES,              1, C_NONE}, // Unsigned number with "bytes", i.e. "1.02 MiB or 123 KiB"
 

	
 
	{"CURRENCY",        EmitSingleChar, SCC_CURRENCY,           1, C_NONE},
0 comments (0 inline, 0 general)