summaryrefslogtreecommitdiff
path: root/src/strings.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-02-26 00:36:57 +0000
committerrubidium <rubidium@openttd.org>2007-02-26 00:36:57 +0000
commit34154aa89e5bff1054876f09d1e233983f103e97 (patch)
tree59eac9e4a920d8fa6394c91b53e89a6e09bbcda3 /src/strings.cpp
parent8df0b8a685f3c79daad4a3d8c045c2b7abd367e4 (diff)
downloadopenttd-34154aa89e5bff1054876f09d1e233983f103e97.tar.xz
(svn r8906) -Feature: translation dependant formatting of dates.
Diffstat (limited to 'src/strings.cpp')
-rw-r--r--src/strings.cpp44
1 files changed, 23 insertions, 21 deletions
diff --git a/src/strings.cpp b/src/strings.cpp
index 9b49b8929..b1abb55b9 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -179,9 +179,17 @@ char *InlineString(char *buf, StringID string)
}
-// This function takes a C-string and allocates a temporary string ID.
-// The duration of the bound string is valid only until the next GetString,
-// so be careful.
+/**
+ * This function takes a C-string and allocates a temporary string ID.
+ * The StringID of the bound string is valid until BindCString is called
+ * another NUM_BOUND_STRINGS times. So be careful when using it.
+ *
+ * @note formatting a DATE_TINY calls BindCString twice, thus reduces the
+ * amount of 'user' bound strings by 2.
+ * @todo rewrite the BindCString system to make the limit flexible and
+ * non-round-robin. For example by using smart pointers that free
+ * the allocated StringID when they go out-of-scope/are freed.
+ */
StringID BindCString(const char *str)
{
int idx = (++_bind_index) & (NUM_BOUND_STRINGS - 1);
@@ -285,40 +293,34 @@ static char *FormatNoCommaNumber(char *buff, int32 number, const char* last)
static char *FormatYmdString(char *buff, Date date, const char* last)
{
YearMonthDay ymd;
-
ConvertDateToYMD(date, &ymd);
- buff = strecpy(buff, GetStringPtr(ymd.day + STR_01AC_1ST - 1), last);
- buff = strecpy(buff, " ", last);
- buff = strecpy(buff, GetStringPtr(STR_0162_JAN + ymd.month), last);
- buff = strecpy(buff, " ", last);
-
- return FormatNoCommaNumber(buff, ymd.year, last);
+ int32 args[3] = { ymd.day + STR_01AC_1ST - 1, STR_0162_JAN + ymd.month, ymd.year };
+ return FormatString(buff, GetStringPtr(STR_DATE_LONG), args, 0, last);
}
static char *FormatMonthAndYear(char *buff, Date date, const char* last)
{
YearMonthDay ymd;
-
ConvertDateToYMD(date, &ymd);
- buff = strecpy(buff, GetStringPtr(STR_MONTH_JAN + ymd.month), last);
- buff = strecpy(buff, " ", last);
-
- return FormatNoCommaNumber(buff, ymd.year, last);
+ int32 args[2] = { STR_MONTH_JAN + ymd.month, ymd.year };
+ return FormatString(buff, GetStringPtr(STR_DATE_SHORT), args, 0, last);
}
static char *FormatTinyDate(char *buff, Date date, const char* last)
{
YearMonthDay ymd;
-
ConvertDateToYMD(date, &ymd);
- buff += snprintf(
- buff, last - buff + 1,
- " %02i-%02i-%04i", ymd.day, ymd.month + 1, ymd.year
- );
- return buff;
+ 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);
+
+ int32 args[3] = { BindCString(day), BindCString(month), ymd.year };
+ return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last);
}
static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 number, bool compact, const char* last)