Rev 4180 | Rev 4199 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4180 | Rev 4184 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Josef Cejka |
2 | * Copyright (c) 2006 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup generic |
29 | /** @addtogroup generic |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
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 | 39 | ||
40 | typedef struct { |
40 | typedef struct { |
41 | size_t size; /* Total size of the buffer (in bytes) */ |
41 | size_t size; /* Total size of the buffer (in bytes) */ |
42 | size_t len; /* Number of already used bytes */ |
42 | size_t len; /* Number of already used bytes */ |
43 | char *dst; /* Destination */ |
43 | char *dst; /* Destination */ |
44 | } vsnprintf_data_t; |
44 | } vsnprintf_data_t; |
45 | 45 | ||
46 | /** Write UTF-8 string to given buffer. |
46 | /** Write UTF-8 string to given buffer. |
47 | * |
47 | * |
48 | * Write at most data->size plain characters including trailing zero. |
48 | * Write at most data->size plain characters including trailing zero. |
49 | * According to C99, snprintf() has to return number of characters that |
49 | * According to C99, snprintf() has to return number of characters that |
50 | * would have been written if enough space had been available. Hence |
50 | * would have been written if enough space had been available. Hence |
51 | * the return value is not the number of actually printed characters |
51 | * the return value is not the number of actually printed characters |
52 | * but size of the input string. |
52 | * but size of the input string. |
53 | * |
53 | * |
54 | * @param str Source UTF-8 string to print. |
54 | * @param str Source UTF-8 string to print. |
55 | * @param size Number of plain characters in str. |
55 | * @param size Number of plain characters in str. |
56 | * @param data Structure describing destination string, counter |
56 | * @param data Structure describing destination string, counter |
57 | * of used space and total string size. |
57 | * of used space and total string size. |
58 | * |
58 | * |
59 | * @return Number of UTF-8 characters to print (not characters actually |
59 | * @return Number of UTF-8 characters to print (not characters actually |
60 | * printed). |
60 | * printed). |
61 | * |
61 | * |
62 | */ |
62 | */ |
63 | static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data) |
63 | static int vsnprintf_write_utf8(const char *str, size_t size, vsnprintf_data_t *data) |
64 | { |
64 | { |
65 | size_t left = data->size - data->len; |
65 | size_t left = data->size - data->len; |
66 | 66 | ||
67 | if (left == 0) |
67 | if (left == 0) |
68 | return ((int) size); |
68 | return ((int) size); |
69 | 69 | ||
70 | if (left == 1) { |
70 | if (left == 1) { |
71 | /* We have only one free byte left in buffer |
71 | /* We have only one free byte left in buffer |
72 | * -> store trailing zero |
72 | * -> store trailing zero |
73 | */ |
73 | */ |
74 | data->dst[data->size - 1] = 0; |
74 | data->dst[data->size - 1] = 0; |
75 | data->len = data->size; |
75 | data->len = data->size; |
76 | return ((int) size); |
76 | return ((int) size); |
77 | } |
77 | } |
78 | 78 | ||
79 | if (left <= size) { |
79 | if (left <= size) { |
80 | /* We have not enought space for whole string |
80 | /* We have not enought space for whole string |
81 | * with the trailing zero => print only a part |
81 | * with the trailing zero => print only a part |
82 | * of string |
82 | * of string |
83 | */ |
83 | */ |
84 | index_t index = 0; |
84 | index_t index = 0; |
85 | 85 | ||
86 | while (index < size) { |
86 | while (index < size) { |
87 | wchar_t uc = utf8_decode(str, &index, size - 1); |
87 | wchar_t uc = utf8_decode(str, &index, size - 1); |
88 | 88 | ||
89 | if (!utf8_encode(uc, data->dst, &data->len, data->size - 1)) |
89 | if (!utf8_encode(uc, data->dst, &data->len, data->size - 2)) |
90 | break; |
90 | break; |
91 | 91 | ||
92 | data->len++; |
92 | data->len++; |
93 | index++; |
93 | index++; |
94 | } |
94 | } |
95 | 95 | ||
96 | /* Put trailing zero at end, but not count it |
96 | /* Put trailing zero at end, but not count it |
97 | * into data->len so it could be rewritten next time |
97 | * into data->len so it could be rewritten next time |
98 | */ |
98 | */ |
99 | data->dst[data->len] = 0; |
99 | data->dst[data->len] = 0; |
100 | 100 | ||
101 | return ((int) size); |
101 | return ((int) size); |
102 | } |
102 | } |
103 | 103 | ||
104 | /* Buffer is big enought to print the whole string */ |
104 | /* Buffer is big enought to print the whole string */ |
105 | memcpy((void *)(data->dst + data->len), (void *) str, size); |
105 | memcpy((void *)(data->dst + data->len), (void *) str, size); |
106 | data->len += size; |
106 | data->len += size; |
107 | 107 | ||
108 | /* Put trailing zero at end, but not count it |
108 | /* Put trailing zero at end, but not count it |
109 | * into data->len so it could be rewritten next time |
109 | * into data->len so it could be rewritten next time |
110 | */ |
110 | */ |
111 | data->dst[data->len] = 0; |
111 | data->dst[data->len] = 0; |
112 | 112 | ||
113 | return ((int) size); |
113 | return ((int) size); |
114 | } |
114 | } |
115 | 115 | ||
116 | /** Write UTF-32 string to given buffer. |
116 | /** Write UTF-32 string to given buffer. |
117 | * |
117 | * |
118 | * Write at most data->size plain characters including trailing zero. |
118 | * Write at most data->size plain characters including trailing zero. |
119 | * According to C99, snprintf() has to return number of characters that |
119 | * According to C99, snprintf() has to return number of characters that |
120 | * would have been written if enough space had been available. Hence |
120 | * would have been written if enough space had been available. Hence |
121 | * the return value is not the number of actually printed characters |
121 | * the return value is not the number of actually printed characters |
122 | * but size of the input string. |
122 | * but size of the input string. |
123 | * |
123 | * |
124 | * @param str Source UTF-32 string to print. |
124 | * @param str Source UTF-32 string to print. |
125 | * @param size Number of bytes in str. |
125 | * @param size Number of bytes in str. |
126 | * @param data Structure describing destination string, counter |
126 | * @param data Structure describing destination string, counter |
127 | * of used space and total string size. |
127 | * of used space and total string size. |
128 | * |
128 | * |
129 | * @return Number of UTF-8 characters to print (not characters actually |
129 | * @return Number of UTF-8 characters to print (not characters actually |
130 | * printed). |
130 | * printed). |
131 | * |
131 | * |
132 | */ |
132 | */ |
133 | static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
133 | static int vsnprintf_write_utf32(const wchar_t *str, size_t size, vsnprintf_data_t *data) |
134 | { |
134 | { |
135 | index_t index = 0; |
135 | index_t index = 0; |
136 | 136 | ||
137 | while (index < (size / sizeof(wchar_t))) { |
137 | while (index < (size / sizeof(wchar_t))) { |
138 | size_t left = data->size - data->len; |
138 | size_t left = data->size - data->len; |
139 | 139 | ||
140 | if (left == 0) |
140 | if (left == 0) |
141 | return ((int) size); |
141 | return ((int) size); |
142 | 142 | ||
143 | if (left == 1) { |
143 | if (left == 1) { |
144 | /* We have only one free byte left in buffer |
144 | /* We have only one free byte left in buffer |
145 | * -> store trailing zero |
145 | * -> store trailing zero |
146 | */ |
146 | */ |
147 | data->dst[data->size - 1] = 0; |
147 | data->dst[data->size - 1] = 0; |
148 | data->len = data->size; |
148 | data->len = data->size; |
149 | return ((int) size); |
149 | return ((int) size); |
150 | } |
150 | } |
151 | 151 | ||
152 | if (!utf8_encode(str[index], data->dst, &data->len, data->size - 1)) |
152 | if (!utf8_encode(str[index], data->dst, &data->len, data->size - 2)) |
153 | break; |
153 | break; |
154 | 154 | ||
155 | data->len++; |
155 | data->len++; |
156 | index++; |
156 | index++; |
157 | } |
157 | } |
158 | 158 | ||
159 | /* Put trailing zero at end, but not count it |
159 | /* Put trailing zero at end, but not count it |
160 | * into data->len so it could be rewritten next time |
160 | * into data->len so it could be rewritten next time |
161 | */ |
161 | */ |
162 | data->dst[data->len] = 0; |
162 | data->dst[data->len] = 0; |
163 | 163 | ||
164 | return ((int) size); |
164 | return ((int) size); |
165 | } |
165 | } |
166 | 166 | ||
167 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
167 | int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) |
168 | { |
168 | { |
169 | vsnprintf_data_t data = { |
169 | vsnprintf_data_t data = { |
170 | size, |
170 | size, |
171 | 0, |
171 | 0, |
172 | str |
172 | str |
173 | }; |
173 | }; |
174 | printf_spec_t ps = { |
174 | printf_spec_t ps = { |
175 | (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8, |
175 | (int(*) (const char *, size_t, void *)) vsnprintf_write_utf8, |
176 | (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32, |
176 | (int(*) (const wchar_t *, size_t, void *)) vsnprintf_write_utf32, |
177 | &data |
177 | &data |
178 | }; |
178 | }; |
179 | 179 | ||
180 | /* Print 0 at end of string - fix the case that nothing will be printed */ |
180 | /* Print 0 at end of string - fix the case that nothing will be printed */ |
181 | if (size > 0) |
181 | if (size > 0) |
182 | str[0] = 0; |
182 | str[0] = 0; |
183 | 183 | ||
184 | /* vsnprintf_write ensures that str will be terminated by zero. */ |
184 | /* vsnprintf_write ensures that str will be terminated by zero. */ |
185 | return printf_core(fmt, &ps, ap); |
185 | return printf_core(fmt, &ps, ap); |
186 | } |
186 | } |
187 | 187 | ||
188 | /** @} |
188 | /** @} |
189 | */ |
189 | */ |
190 | 190 |