|
1 /* |
|
2 ****************************************************************************** |
|
3 * |
|
4 * Copyright (C) 2001-2008, International Business Machines |
|
5 * Corporation and others. All Rights Reserved. |
|
6 * |
|
7 ****************************************************************************** |
|
8 * |
|
9 * File sprintf.c |
|
10 * |
|
11 * Modification History: |
|
12 * |
|
13 * Date Name Description |
|
14 * 02/08/2001 george Creation. Copied from uprintf.c |
|
15 * 03/27/2002 Mark Schneckloth Many fixes regarding alignment, null termination |
|
16 * (mschneckloth@atomz.com) and other various problems. |
|
17 * 08/07/2003 george Reunify printf implementations |
|
18 ******************************************************************************* |
|
19 */ |
|
20 |
|
21 #include "unicode/utypes.h" |
|
22 |
|
23 #if !UCONFIG_NO_FORMATTING |
|
24 |
|
25 #include "unicode/ustdio.h" |
|
26 #include "unicode/ustring.h" |
|
27 #include "unicode/putil.h" |
|
28 |
|
29 #include "uprintf.h" |
|
30 #include "locbund.h" |
|
31 |
|
32 #include "cmemory.h" |
|
33 #include <ctype.h> |
|
34 |
|
35 /* u_minstrncpy copies the minimum number of code units of (count or output->available) */ |
|
36 static int32_t |
|
37 u_sprintf_write(void *context, |
|
38 const UChar *str, |
|
39 int32_t count) |
|
40 { |
|
41 u_localized_print_string *output = (u_localized_print_string *)context; |
|
42 int32_t size = ufmt_min(count, output->available); |
|
43 |
|
44 u_strncpy(output->str + (output->len - output->available), str, size); |
|
45 output->available -= size; |
|
46 return size; |
|
47 } |
|
48 |
|
49 static int32_t |
|
50 u_sprintf_pad_and_justify(void *context, |
|
51 const u_printf_spec_info *info, |
|
52 const UChar *result, |
|
53 int32_t resultLen) |
|
54 { |
|
55 u_localized_print_string *output = (u_localized_print_string *)context; |
|
56 int32_t written = 0; |
|
57 int32_t lengthOfResult = resultLen; |
|
58 |
|
59 resultLen = ufmt_min(resultLen, output->available); |
|
60 |
|
61 /* pad and justify, if needed */ |
|
62 if(info->fWidth != -1 && resultLen < info->fWidth) { |
|
63 int32_t paddingLeft = info->fWidth - resultLen; |
|
64 int32_t outputPos = output->len - output->available; |
|
65 |
|
66 if (paddingLeft + resultLen > output->available) { |
|
67 paddingLeft = output->available - resultLen; |
|
68 if (paddingLeft < 0) { |
|
69 paddingLeft = 0; |
|
70 } |
|
71 /* paddingLeft = output->available - resultLen;*/ |
|
72 } |
|
73 written += paddingLeft; |
|
74 |
|
75 /* left justify */ |
|
76 if(info->fLeft) { |
|
77 written += u_sprintf_write(output, result, resultLen); |
|
78 u_memset(&output->str[outputPos + resultLen], info->fPadChar, paddingLeft); |
|
79 output->available -= paddingLeft; |
|
80 } |
|
81 /* right justify */ |
|
82 else { |
|
83 u_memset(&output->str[outputPos], info->fPadChar, paddingLeft); |
|
84 output->available -= paddingLeft; |
|
85 written += u_sprintf_write(output, result, resultLen); |
|
86 } |
|
87 } |
|
88 /* just write the formatted output */ |
|
89 else { |
|
90 written = u_sprintf_write(output, result, resultLen); |
|
91 } |
|
92 |
|
93 if (written >= 0 && lengthOfResult > written) { |
|
94 return lengthOfResult; |
|
95 } |
|
96 |
|
97 return written; |
|
98 } |
|
99 |
|
100 U_CAPI int32_t U_EXPORT2 |
|
101 u_sprintf(UChar *buffer, |
|
102 const char *patternSpecification, |
|
103 ... ) |
|
104 { |
|
105 va_list ap; |
|
106 int32_t written; |
|
107 |
|
108 va_start(ap, patternSpecification); |
|
109 written = u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); |
|
110 va_end(ap); |
|
111 |
|
112 return written; |
|
113 } |
|
114 |
|
115 U_CAPI int32_t U_EXPORT2 |
|
116 u_sprintf_u(UChar *buffer, |
|
117 const UChar *patternSpecification, |
|
118 ... ) |
|
119 { |
|
120 va_list ap; |
|
121 int32_t written; |
|
122 |
|
123 va_start(ap, patternSpecification); |
|
124 written = u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); |
|
125 va_end(ap); |
|
126 |
|
127 return written; |
|
128 } |
|
129 |
|
130 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ |
|
131 u_vsprintf(UChar *buffer, |
|
132 const char *patternSpecification, |
|
133 va_list ap) |
|
134 { |
|
135 return u_vsnprintf(buffer, INT32_MAX, patternSpecification, ap); |
|
136 } |
|
137 |
|
138 U_CAPI int32_t U_EXPORT2 |
|
139 u_snprintf(UChar *buffer, |
|
140 int32_t count, |
|
141 const char *patternSpecification, |
|
142 ... ) |
|
143 { |
|
144 va_list ap; |
|
145 int32_t written; |
|
146 |
|
147 va_start(ap, patternSpecification); |
|
148 written = u_vsnprintf(buffer, count, patternSpecification, ap); |
|
149 va_end(ap); |
|
150 |
|
151 return written; |
|
152 } |
|
153 |
|
154 U_CAPI int32_t U_EXPORT2 |
|
155 u_snprintf_u(UChar *buffer, |
|
156 int32_t count, |
|
157 const UChar *patternSpecification, |
|
158 ... ) |
|
159 { |
|
160 va_list ap; |
|
161 int32_t written; |
|
162 |
|
163 va_start(ap, patternSpecification); |
|
164 written = u_vsnprintf_u(buffer, count, patternSpecification, ap); |
|
165 va_end(ap); |
|
166 |
|
167 return written; |
|
168 } |
|
169 |
|
170 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ |
|
171 u_vsnprintf(UChar *buffer, |
|
172 int32_t count, |
|
173 const char *patternSpecification, |
|
174 va_list ap) |
|
175 { |
|
176 int32_t written; |
|
177 UChar *pattern; |
|
178 UChar patBuffer[UFMT_DEFAULT_BUFFER_SIZE]; |
|
179 int32_t size = (int32_t)strlen(patternSpecification) + 1; |
|
180 |
|
181 /* convert from the default codepage to Unicode */ |
|
182 if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) { |
|
183 pattern = (UChar *)uprv_malloc(size * sizeof(UChar)); |
|
184 if(pattern == 0) { |
|
185 return 0; |
|
186 } |
|
187 } |
|
188 else { |
|
189 pattern = patBuffer; |
|
190 } |
|
191 u_charsToUChars(patternSpecification, pattern, size); |
|
192 |
|
193 /* do the work */ |
|
194 written = u_vsnprintf_u(buffer, count, pattern, ap); |
|
195 |
|
196 /* clean up */ |
|
197 if (pattern != patBuffer) { |
|
198 uprv_free(pattern); |
|
199 } |
|
200 |
|
201 return written; |
|
202 } |
|
203 |
|
204 U_CAPI int32_t U_EXPORT2 |
|
205 u_vsprintf_u(UChar *buffer, |
|
206 const UChar *patternSpecification, |
|
207 va_list ap) |
|
208 { |
|
209 return u_vsnprintf_u(buffer, INT32_MAX, patternSpecification, ap); |
|
210 } |
|
211 |
|
212 static const u_printf_stream_handler g_sprintf_stream_handler = { |
|
213 u_sprintf_write, |
|
214 u_sprintf_pad_and_justify |
|
215 }; |
|
216 |
|
217 U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ |
|
218 u_vsnprintf_u(UChar *buffer, |
|
219 int32_t count, |
|
220 const UChar *patternSpecification, |
|
221 va_list ap) |
|
222 { |
|
223 int32_t written = 0; /* haven't written anything yet */ |
|
224 int32_t result = 0; /* test the return value of u_printf_parse */ |
|
225 |
|
226 u_localized_print_string outStr; |
|
227 |
|
228 if (count < 0) { |
|
229 count = INT32_MAX; |
|
230 } |
|
231 |
|
232 outStr.str = buffer; |
|
233 outStr.len = count; |
|
234 outStr.available = count; |
|
235 |
|
236 if(u_locbund_init(&outStr.fBundle, "en_US_POSIX") == 0) { |
|
237 return 0; |
|
238 } |
|
239 |
|
240 /* parse and print the whole format string */ |
|
241 result = u_printf_parse(&g_sprintf_stream_handler, patternSpecification, &outStr, &outStr, &outStr.fBundle, &written, ap); |
|
242 |
|
243 /* Terminate the buffer, if there's room. */ |
|
244 if (outStr.available > 0) { |
|
245 buffer[outStr.len - outStr.available] = 0x0000; |
|
246 } |
|
247 |
|
248 /* Release the cloned bundle, if we cloned it. */ |
|
249 u_locbund_close(&outStr.fBundle); |
|
250 |
|
251 /* parsing error */ |
|
252 if (result < 0) { |
|
253 return result; |
|
254 } |
|
255 /* return # of UChars written */ |
|
256 return written; |
|
257 } |
|
258 |
|
259 #endif /* #if !UCONFIG_NO_FORMATTING */ |
|
260 |