src/strings.cpp
branchcpp_gui
changeset 6285 187e3ef04cc9
parent 6268 4b5241e5dd10
child 6298 c30fe89622df
equal deleted inserted replaced
6284:45d0233e7d79 6285:187e3ef04cc9
   177 	buf += Utf8Encode(buf, string);
   177 	buf += Utf8Encode(buf, string);
   178 	return buf;
   178 	return buf;
   179 }
   179 }
   180 
   180 
   181 
   181 
   182 // This function takes a C-string and allocates a temporary string ID.
   182 /**
   183 // The duration of the bound string is valid only until the next GetString,
   183  * This function takes a C-string and allocates a temporary string ID.
   184 // so be careful.
   184  * The StringID of the bound string is valid until BindCString is called
       
   185  * another NUM_BOUND_STRINGS times. So be careful when using it.
       
   186  *
       
   187  * @note formatting a DATE_TINY calls BindCString twice, thus reduces the
       
   188  *       amount of 'user' bound strings by 2.
       
   189  * @todo rewrite the BindCString system to make the limit flexible and
       
   190  *       non-round-robin. For example by using smart pointers that free
       
   191  *       the allocated StringID when they go out-of-scope/are freed.
       
   192  */
   185 StringID BindCString(const char *str)
   193 StringID BindCString(const char *str)
   186 {
   194 {
   187 	int idx = (++_bind_index) & (NUM_BOUND_STRINGS - 1);
   195 	int idx = (++_bind_index) & (NUM_BOUND_STRINGS - 1);
   188 	_bound_strings[idx] = str;
   196 	_bound_strings[idx] = str;
   189 	return idx + STR_SPEC_DYNSTRING;
   197 	return idx + STR_SPEC_DYNSTRING;
   283 
   291 
   284 
   292 
   285 static char *FormatYmdString(char *buff, Date date, const char* last)
   293 static char *FormatYmdString(char *buff, Date date, const char* last)
   286 {
   294 {
   287 	YearMonthDay ymd;
   295 	YearMonthDay ymd;
   288 
       
   289 	ConvertDateToYMD(date, &ymd);
   296 	ConvertDateToYMD(date, &ymd);
   290 
   297 
   291 	buff = strecpy(buff, GetStringPtr(ymd.day + STR_01AC_1ST - 1), last);
   298 	int32 args[3] = { ymd.day + STR_01AC_1ST - 1, STR_0162_JAN + ymd.month, ymd.year };
   292 	buff = strecpy(buff, " ", last);
   299 	return FormatString(buff, GetStringPtr(STR_DATE_LONG), args, 0, last);
   293 	buff = strecpy(buff, GetStringPtr(STR_0162_JAN + ymd.month), last);
       
   294 	buff = strecpy(buff, " ", last);
       
   295 
       
   296 	return FormatNoCommaNumber(buff, ymd.year, last);
       
   297 }
   300 }
   298 
   301 
   299 static char *FormatMonthAndYear(char *buff, Date date, const char* last)
   302 static char *FormatMonthAndYear(char *buff, Date date, const char* last)
   300 {
   303 {
   301 	YearMonthDay ymd;
   304 	YearMonthDay ymd;
   302 
       
   303 	ConvertDateToYMD(date, &ymd);
   305 	ConvertDateToYMD(date, &ymd);
   304 
   306 
   305 	buff = strecpy(buff, GetStringPtr(STR_MONTH_JAN + ymd.month), last);
   307 	int32 args[2] = { STR_MONTH_JAN + ymd.month, ymd.year };
   306 	buff = strecpy(buff, " ", last);
   308 	return FormatString(buff, GetStringPtr(STR_DATE_SHORT), args, 0, last);
   307 
       
   308 	return FormatNoCommaNumber(buff, ymd.year, last);
       
   309 }
   309 }
   310 
   310 
   311 static char *FormatTinyDate(char *buff, Date date, const char* last)
   311 static char *FormatTinyDate(char *buff, Date date, const char* last)
   312 {
   312 {
   313 	YearMonthDay ymd;
   313 	YearMonthDay ymd;
   314 
       
   315 	ConvertDateToYMD(date, &ymd);
   314 	ConvertDateToYMD(date, &ymd);
   316 	buff += snprintf(
   315 
   317 		buff, last - buff + 1,
   316 	char day[3];
   318 		" %02i-%02i-%04i", ymd.day, ymd.month + 1, ymd.year
   317 	char month[3];
   319 	);
   318 	/* We want to zero-pad the days and months */
   320 
   319 	snprintf(day,   lengthof(day),   "%02i", ymd.day);
   321 	return buff;
   320 	snprintf(month, lengthof(month), "%02i", ymd.month + 1);
       
   321 
       
   322 	int32 args[3] = { BindCString(day), BindCString(month), ymd.year };
       
   323 	return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last);
   322 }
   324 }
   323 
   325 
   324 static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 number, bool compact, const char* last)
   326 static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 number, bool compact, const char* last)
   325 {
   327 {
   326 	const char* multiplier = "";
   328 	const char* multiplier = "";