Localization: Make Qt's locale-aware formatting accessible from C

This commit introduces functions:
 - QString asprintf_loc(const char *cformat, ...);
 - int snprintf_loc(char *dst, size_t size, const char *cformat, ...);
 - put_format_loc(struct membuffer *, const char *fmt, ...);
and their va_arg equivalents, which use Qt's QString::arg() formatting
options to render the strings.

The snprintf_loc() function takes care not to truncate multi-byte
UTF-8 encodings. Thus, on overflow, the resulting string might be
shorter than size-1.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-02-21 22:35:50 +01:00 committed by Robert C. Helling
parent 9a8a0afa29
commit bec3e80683
4 changed files with 421 additions and 3 deletions

View file

@ -52,7 +52,7 @@ static void oom(void)
exit(1);
}
static void make_room(struct membuffer *b, unsigned int size)
void make_room(struct membuffer *b, unsigned int size)
{
unsigned int needed = b->len + size;
if (needed > b->alloc) {
@ -141,6 +141,15 @@ void put_format(struct membuffer *b, const char *fmt, ...)
va_end(args);
}
void put_format_loc(struct membuffer *b, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
put_vformat_loc(b, fmt, args);
va_end(args);
}
void put_milli(struct membuffer *b, const char *pre, int value, const char *post)
{
int i;