summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-11-19 13:53:54 +0000
committerrubidium <rubidium@openttd.org>2009-11-19 13:53:54 +0000
commit2f0b1ef5f136bd32fd7be8253c034048e59a490e (patch)
tree1e00d989610c6967ef7a271db370bf4a826fa7e3
parent4987d97810a74dc1b070e44fd15a6943fb7dfaab (diff)
downloadopenttd-2f0b1ef5f136bd32fd7be8253c034048e59a490e.tar.xz
(svn r18181) -Add: the concept of zero filled numbers to strgen
-rw-r--r--src/strings.cpp27
-rw-r--r--src/table/control_codes.h1
-rw-r--r--src/table/strgen_tables.h1
3 files changed, 18 insertions, 11 deletions
diff --git a/src/strings.cpp b/src/strings.cpp
index 43fefadc2..e54edd36b 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -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 *buff, int64 number, const char *last)
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, const char *str, int64 *argv, uint casei,
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;
diff --git a/src/table/control_codes.h b/src/table/control_codes.h
index dea4ff822..cf533847a 100644
--- a/src/table/control_codes.h
+++ b/src/table/control_codes.h
@@ -72,6 +72,7 @@ enum StringControlCode {
SCC_STRING,
SCC_COMMA,
SCC_NUM,
+ SCC_ZEROFILL_NUM,
SCC_HEX,
SCC_BYTES,
diff --git a/src/table/strgen_tables.h b/src/table/strgen_tables.h
index 8aae5cf3e..5c12557c0 100644
--- a/src/table/strgen_tables.h
+++ b/src/table/strgen_tables.h
@@ -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},