36,6 → 36,7 |
#include <printf/printf_core.h> |
#include <string.h> |
#include <memstr.h> |
#include <errno.h> |
|
typedef struct { |
size_t size; /* Total size of the buffer (in bytes) */ |
43,7 → 44,7 |
char *dst; /* Destination */ |
} vsnprintf_data_t; |
|
/** Write UTF-8 string to given buffer. |
/** Write string to given buffer. |
* |
* Write at most data->size plain characters including trailing zero. |
* According to C99, snprintf() has to return number of characters that |
51,16 → 52,16 |
* the return value is not the number of actually printed characters |
* but size of the input string. |
* |
* @param str Source UTF-8 string to print. |
* @param str Source string to print. |
* @param size Number of plain characters in str. |
* @param data Structure describing destination string, counter |
* of used space and total string size. |
* |
* @return Number of UTF-8 characters to print (not characters actually |
* @return Number of characters to print (not characters actually |
* printed). |
* |
*/ |
static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data) |
static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data) |
{ |
size_t left = data->size - data->len; |
|
77,7 → 78,7 |
} |
|
if (left <= size) { |
/* We have not enought space for whole string |
/* We do not have enough space for the whole string |
* with the trailing zero => print only a part |
* of string |
*/ |
84,13 → 85,10 |
index_t index = 0; |
|
while (index < size) { |
wchar_t uc = utf8_decode(str, &index, size - 1); |
wchar_t uc = str_decode(str, &index, size); |
|
if (!utf8_encode(uc, data->dst, &data->len, data->size - 2)) |
if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK) |
break; |
|
data->len++; |
index++; |
} |
|
/* Put trailing zero at end, but not count it |
113,7 → 111,7 |
return ((int) size); |
} |
|
/** Write UTF-32 string to given buffer. |
/** Write wide string to given buffer. |
* |
* Write at most data->size plain characters including trailing zero. |
* According to C99, snprintf() has to return number of characters that |
121,16 → 119,16 |
* the return value is not the number of actually printed characters |
* but size of the input string. |
* |
* @param str Source UTF-32 string to print. |
* @param str Source wide string to print. |
* @param size Number of bytes in str. |
* @param data Structure describing destination string, counter |
* of used space and total string size. |
* |
* @return Number of UTF-8 characters to print (not characters actually |
* @return Number of wide characters to print (not characters actually |
* printed). |
* |
*/ |
static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
{ |
index_t index = 0; |
|
149,10 → 147,9 |
return ((int) size); |
} |
|
if (!utf8_encode(str[index], data->dst, &data->len, data->size - 2)) |
if (chr_encode(str[index], data->dst, &data->len, data->size - 1) != EOK) |
break; |
|
data->len++; |
index++; |
} |
|
172,8 → 169,8 |
str |
}; |
printf_spec_t ps = { |
(int(*) (const char *, size_t, void *)) vsnprintf_write_utf8, |
(int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32, |
(int(*) (const char *, size_t, void *)) vsnprintf_str_write, |
(int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write, |
&data |
}; |
|