Rev 4347 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4347 | Rev 4348 | ||
|---|---|---|---|
| Line 34... | Line 34... | ||
| 34 | 34 | ||
| 35 | #include <print.h> |
35 | #include <print.h> |
| 36 | #include <printf/printf_core.h> |
36 | #include <printf/printf_core.h> |
| 37 | #include <string.h> |
37 | #include <string.h> |
| 38 | #include <memstr.h> |
38 | #include <memstr.h> |
| - | 39 | #include <errno.h> |
|
| 39 | 40 | ||
| 40 | typedef struct { |
41 | typedef struct { |
| 41 | size_t size; /* Total size of the buffer (in bytes) */ |
42 | size_t size; /* Total size of the buffer (in bytes) */ |
| 42 | size_t len; /* Number of already used bytes */ |
43 | size_t len; /* Number of already used bytes */ |
| 43 | char *dst; /* Destination */ |
44 | char *dst; /* Destination */ |
| 44 | } vsnprintf_data_t; |
45 | } vsnprintf_data_t; |
| 45 | 46 | ||
| 46 | /** Write UTF-8 string to given buffer. |
47 | /** Write string to given buffer. |
| 47 | * |
48 | * |
| 48 | * Write at most data->size plain characters including trailing zero. |
49 | * Write at most data->size plain characters including trailing zero. |
| 49 | * According to C99, snprintf() has to return number of characters that |
50 | * According to C99, snprintf() has to return number of characters that |
| 50 | * would have been written if enough space had been available. Hence |
51 | * would have been written if enough space had been available. Hence |
| 51 | * the return value is not the number of actually printed characters |
52 | * the return value is not the number of actually printed characters |
| 52 | * but size of the input string. |
53 | * but size of the input string. |
| 53 | * |
54 | * |
| 54 | * @param str Source UTF-8 string to print. |
55 | * @param str Source string to print. |
| 55 | * @param size Number of plain characters in str. |
56 | * @param size Number of plain characters in str. |
| 56 | * @param data Structure describing destination string, counter |
57 | * @param data Structure describing destination string, counter |
| 57 | * of used space and total string size. |
58 | * of used space and total string size. |
| 58 | * |
59 | * |
| 59 | * @return Number of UTF-8 characters to print (not characters actually |
60 | * @return Number of characters to print (not characters actually |
| 60 | * printed). |
61 | * printed). |
| 61 | * |
62 | * |
| 62 | */ |
63 | */ |
| 63 | static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data) |
64 | static int vsnprintf_str_write(const char *str, size_t size, vsnprintf_data_t *data) |
| 64 | { |
65 | { |
| 65 | size_t left = data->size - data->len; |
66 | size_t left = data->size - data->len; |
| 66 | 67 | ||
| 67 | if (left == 0) |
68 | if (left == 0) |
| 68 | return ((int) size); |
69 | return ((int) size); |
| Line 75... | Line 76... | ||
| 75 | data->len = data->size; |
76 | data->len = data->size; |
| 76 | return ((int) size); |
77 | return ((int) size); |
| 77 | } |
78 | } |
| 78 | 79 | ||
| 79 | if (left <= size) { |
80 | if (left <= size) { |
| 80 | /* We have not enought space for whole string |
81 | /* We do not have enough space for the whole string |
| 81 | * with the trailing zero => print only a part |
82 | * with the trailing zero => print only a part |
| 82 | * of string |
83 | * of string |
| 83 | */ |
84 | */ |
| 84 | index_t index = 0; |
85 | index_t index = 0; |
| 85 | 86 | ||
| 86 | while (index < size) { |
87 | while (index < size) { |
| 87 | wchar_t uc = utf8_decode(str, &index, size - 1); |
88 | wchar_t uc = str_decode(str, &index, size); |
| 88 | 89 | ||
| 89 | if (!utf8_encode(uc, data->dst, &data->len, data->size - 2)) |
90 | if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK) |
| 90 | break; |
91 | break; |
| 91 | - | ||
| 92 | data->len++; |
- | |
| 93 | index++; |
- | |
| 94 | } |
92 | } |
| 95 | 93 | ||
| 96 | /* Put trailing zero at end, but not count it |
94 | /* Put trailing zero at end, but not count it |
| 97 | * into data->len so it could be rewritten next time |
95 | * into data->len so it could be rewritten next time |
| 98 | */ |
96 | */ |
| Line 111... | Line 109... | ||
| 111 | data->dst[data->len] = 0; |
109 | data->dst[data->len] = 0; |
| 112 | 110 | ||
| 113 | return ((int) size); |
111 | return ((int) size); |
| 114 | } |
112 | } |
| 115 | 113 | ||
| 116 | /** Write UTF-32 string to given buffer. |
114 | /** Write wide string to given buffer. |
| 117 | * |
115 | * |
| 118 | * Write at most data->size plain characters including trailing zero. |
116 | * Write at most data->size plain characters including trailing zero. |
| 119 | * According to C99, snprintf() has to return number of characters that |
117 | * According to C99, snprintf() has to return number of characters that |
| 120 | * would have been written if enough space had been available. Hence |
118 | * would have been written if enough space had been available. Hence |
| 121 | * the return value is not the number of actually printed characters |
119 | * the return value is not the number of actually printed characters |
| 122 | * but size of the input string. |
120 | * but size of the input string. |
| 123 | * |
121 | * |
| 124 | * @param str Source UTF-32 string to print. |
122 | * @param str Source wide string to print. |
| 125 | * @param size Number of bytes in str. |
123 | * @param size Number of bytes in str. |
| 126 | * @param data Structure describing destination string, counter |
124 | * @param data Structure describing destination string, counter |
| 127 | * of used space and total string size. |
125 | * of used space and total string size. |
| 128 | * |
126 | * |
| 129 | * @return Number of UTF-8 characters to print (not characters actually |
127 | * @return Number of wide characters to print (not characters actually |
| 130 | * printed). |
128 | * printed). |
| 131 | * |
129 | * |
| 132 | */ |
130 | */ |
| 133 | static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
131 | static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
| 134 | { |
132 | { |
| 135 | index_t index = 0; |
133 | index_t index = 0; |
| 136 | 134 | ||
| 137 | while (index < (size / sizeof(wchar_t))) { |
135 | while (index < (size / sizeof(wchar_t))) { |
| 138 | size_t left = data->size - data->len; |
136 | size_t left = data->size - data->len; |
| Line 147... | Line 145... | ||
| 147 | data->dst[data->size - 1] = 0; |
145 | data->dst[data->size - 1] = 0; |
| 148 | data->len = data->size; |
146 | data->len = data->size; |
| 149 | return ((int) size); |
147 | return ((int) size); |
| 150 | } |
148 | } |
| 151 | 149 | ||
| 152 | if (!utf8_encode(str[index], data->dst, &data->len, data->size - 2)) |
150 | if (chr_encode(str[index], data->dst, &data->len, data->size - 1) != EOK) |
| 153 | break; |
151 | break; |
| 154 | 152 | ||
| 155 | data->len++; |
- | |
| 156 | index++; |
153 | index++; |
| 157 | } |
154 | } |
| 158 | 155 | ||
| 159 | /* Put trailing zero at end, but not count it |
156 | /* Put trailing zero at end, but not count it |
| 160 | * into data->len so it could be rewritten next time |
157 | * into data->len so it could be rewritten next time |
| Line 170... | Line 167... | ||
| 170 | size, |
167 | size, |
| 171 | 0, |
168 | 0, |
| 172 | str |
169 | str |
| 173 | }; |
170 | }; |
| 174 | printf_spec_t ps = { |
171 | printf_spec_t ps = { |
| 175 | (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8, |
172 | (int(*) (const char *, size_t, void *)) vsnprintf_str_write, |
| 176 | (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32, |
173 | (int(*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write, |
| 177 | &data |
174 | &data |
| 178 | }; |
175 | }; |
| 179 | 176 | ||
| 180 | /* Print 0 at end of string - fix the case that nothing will be printed */ |
177 | /* Print 0 at end of string - fix the case that nothing will be printed */ |
| 181 | if (size > 0) |
178 | if (size > 0) |