Merge from amd64-branch:
[reactos.git] / reactos / dll / win32 / user32 / misc / wsprintf.c
1 /*
2 * wsprintf functions
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * NOTE:
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
23 */
24 /*
25 * COPYRIGHT: See COPYING in the top level directory
26 * PROJECT: ReactOS User32
27 * PURPOSE: [w]sprintf functions
28 * FILE: lib/user32/wsprintf.c
29 * PROGRAMER: Steven Edwards
30 * REVISION HISTORY: 2003/07/13 Merged from wine user/wsprintf.c
31 * NOTES: Adapted from Wine
32 */
33
34 #include <user32.h>
35
36 #define WINE_NO_TRACE_MSGS
37 #include <wine/debug.h>
38
39 WINE_DEFAULT_DEBUG_CHANNEL(string);
40
41
42 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
43 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
44 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
45 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
46 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
47 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
48 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
49
50 typedef enum
51 {
52 WPR_UNKNOWN,
53 WPR_CHAR,
54 WPR_WCHAR,
55 WPR_STRING,
56 WPR_WSTRING,
57 WPR_SIGNED,
58 WPR_UNSIGNED,
59 WPR_HEXA
60 } WPRINTF_TYPE;
61
62 typedef struct
63 {
64 UINT flags;
65 UINT width;
66 UINT precision;
67 WPRINTF_TYPE type;
68 } WPRINTF_FORMAT;
69
70 typedef union {
71 WCHAR wchar_view;
72 CHAR char_view;
73 LPCSTR lpcstr_view;
74 LPCWSTR lpcwstr_view;
75 INT int_view;
76 } WPRINTF_DATA;
77
78 static const CHAR null_stringA[] = "(null)";
79 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80
81 /***********************************************************************
82 * WPRINTF_ParseFormatA
83 *
84 * Parse a format specification. A format specification has the form:
85 *
86 * [-][#][0][width][.precision]type
87 *
88 * Return value is the length of the format specification in characters.
89 */
90 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
91 {
92 LPCSTR p = format;
93
94 res->flags = 0;
95 res->width = 0;
96 res->precision = 0;
97 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
98 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
99 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
100 while ((*p >= '0') && (*p <= '9')) /* width field */
101 {
102 res->width = res->width * 10 + *p - '0';
103 p++;
104 }
105 if (*p == '.') /* precision field */
106 {
107 p++;
108 while ((*p >= '0') && (*p <= '9'))
109 {
110 res->precision = res->precision * 10 + *p - '0';
111 p++;
112 }
113 }
114 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
115 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
116 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
117 switch(*p)
118 {
119 case 'c':
120 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
121 break;
122 case 'C':
123 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
124 break;
125 case 'd':
126 case 'i':
127 res->type = WPR_SIGNED;
128 break;
129 case 's':
130 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
131 break;
132 case 'S':
133 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
134 break;
135 case 'u':
136 res->type = WPR_UNSIGNED;
137 break;
138 case 'p':
139 res->width = 8;
140 res->flags |= WPRINTF_ZEROPAD;
141 /* fall through */
142 case 'X':
143 res->flags |= WPRINTF_UPPER_HEX;
144 /* fall through */
145 case 'x':
146 res->type = WPR_HEXA;
147 break;
148 default: /* unknown format char */
149 res->type = WPR_UNKNOWN;
150 p--; /* print format as normal char */
151 break;
152 }
153 return (INT)(p - format) + 1;
154 }
155
156
157 /***********************************************************************
158 * WPRINTF_ParseFormatW
159 *
160 * Parse a format specification. A format specification has the form:
161 *
162 * [-][#][0][width][.precision]type
163 *
164 * Return value is the length of the format specification in characters.
165 */
166 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
167 {
168 LPCWSTR p = format;
169
170 res->flags = 0;
171 res->width = 0;
172 res->precision = 0;
173 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
174 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
175 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
176 while ((*p >= '0') && (*p <= '9')) /* width field */
177 {
178 res->width = res->width * 10 + *p - '0';
179 p++;
180 }
181 if (*p == '.') /* precision field */
182 {
183 p++;
184 while ((*p >= '0') && (*p <= '9'))
185 {
186 res->precision = res->precision * 10 + *p - '0';
187 p++;
188 }
189 }
190 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
191 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
192 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
193 switch((CHAR)*p)
194 {
195 case 'c':
196 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
197 break;
198 case 'C':
199 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
200 break;
201 case 'd':
202 case 'i':
203 res->type = WPR_SIGNED;
204 break;
205 case 's':
206 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
207 break;
208 case 'S':
209 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
210 break;
211 case 'u':
212 res->type = WPR_UNSIGNED;
213 break;
214 case 'p':
215 res->width = 8;
216 res->flags |= WPRINTF_ZEROPAD;
217 /* fall through */
218 case 'X':
219 res->flags |= WPRINTF_UPPER_HEX;
220 /* fall through */
221 case 'x':
222 res->type = WPR_HEXA;
223 break;
224 default:
225 res->type = WPR_UNKNOWN;
226 p--; /* print format as normal char */
227 break;
228 }
229 return (INT)(p - format) + 1;
230 }
231
232
233 /***********************************************************************
234 * WPRINTF_GetLen
235 */
236 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
237 LPSTR number, UINT maxlen )
238 {
239 UINT len;
240
241 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
242 if (format->width > maxlen) format->width = maxlen;
243 switch(format->type)
244 {
245 case WPR_CHAR:
246 case WPR_WCHAR:
247 return (format->precision = 1);
248 case WPR_STRING:
249 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
250 for (len = 0; !format->precision || (len < format->precision); len++)
251 if (!*(arg->lpcstr_view + len)) break;
252 if (len > maxlen) len = maxlen;
253 return (format->precision = len);
254 case WPR_WSTRING:
255 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
256 for (len = 0; !format->precision || (len < format->precision); len++)
257 if (!*(arg->lpcwstr_view + len)) break;
258 if (len > maxlen) len = maxlen;
259 return (format->precision = len);
260 case WPR_SIGNED:
261 len = sprintf( number, "%d", arg->int_view );
262 break;
263 case WPR_UNSIGNED:
264 len = sprintf( number, "%u", (UINT)arg->int_view );
265 break;
266 case WPR_HEXA:
267 len = sprintf( number,
268 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
269 (UINT)arg->int_view);
270 break;
271 default:
272 return 0;
273 }
274 if (len > maxlen) len = maxlen;
275 if (format->precision < len) format->precision = len;
276 if (format->precision > maxlen) format->precision = maxlen;
277 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
278 format->precision = format->width;
279 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
280 return len;
281 }
282
283 /***********************************************************************
284 * wvsnprintfA (internal)
285 */
286 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
287 {
288 WPRINTF_FORMAT format;
289 LPSTR p = buffer;
290 UINT i, len, sign;
291 CHAR number[20];
292 WPRINTF_DATA argData;
293
294 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
295
296 while (*spec && (maxlen > 1))
297 {
298 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
299 spec++;
300 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
301 spec += WPRINTF_ParseFormatA( spec, &format );
302
303 switch(format.type)
304 {
305 case WPR_WCHAR:
306 argData.wchar_view = (WCHAR)va_arg( args, int );
307 break;
308 case WPR_CHAR:
309 argData.char_view = (CHAR)va_arg( args, int );
310 break;
311 case WPR_STRING:
312 argData.lpcstr_view = va_arg( args, LPCSTR );
313 break;
314 case WPR_WSTRING:
315 argData.lpcwstr_view = va_arg( args, LPCWSTR );
316 break;
317 case WPR_HEXA:
318 case WPR_SIGNED:
319 case WPR_UNSIGNED:
320 argData.int_view = va_arg( args, INT );
321 break;
322 default:
323 argData.wchar_view = 0;
324 break;
325 }
326
327 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
328 sign = 0;
329 if (!(format.flags & WPRINTF_LEFTALIGN))
330 for (i = format.precision; i < format.width; i++, maxlen--)
331 *p++ = ' ';
332 switch(format.type)
333 {
334 case WPR_WCHAR:
335 *p++ = argData.wchar_view;
336 break;
337 case WPR_CHAR:
338 *p++ = argData.char_view;
339 break;
340 case WPR_STRING:
341 memcpy( p, argData.lpcstr_view, len );
342 p += len;
343 break;
344 case WPR_WSTRING:
345 {
346 LPCWSTR ptr = argData.lpcwstr_view;
347 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
348 }
349 break;
350 case WPR_HEXA:
351 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
352 {
353 *p++ = '0';
354 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
355 maxlen -= 2;
356 len -= 2;
357 }
358 /* fall through */
359 case WPR_SIGNED:
360 /* Transfer the sign now, just in case it will be zero-padded*/
361 if (number[0] == '-')
362 {
363 *p++ = '-';
364 sign = 1;
365 }
366 /* fall through */
367 case WPR_UNSIGNED:
368 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
369 memcpy( p, number + sign, len - sign );
370 p += len - sign;
371 break;
372 case WPR_UNKNOWN:
373 continue;
374 }
375 if (format.flags & WPRINTF_LEFTALIGN)
376 for (i = format.precision; i < format.width; i++, maxlen--)
377 *p++ = ' ';
378 maxlen -= len;
379 }
380 *p = 0;
381 TRACE("%s\n",debugstr_a(buffer));
382 return (maxlen > 1) ? (INT)(p - buffer) : -1;
383 }
384
385
386 /***********************************************************************
387 * wvsnprintfW (internal)
388 */
389 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
390 {
391 WPRINTF_FORMAT format;
392 LPWSTR p = buffer;
393 UINT i, len, sign;
394 CHAR number[20];
395 WPRINTF_DATA argData;
396
397 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
398
399 while (*spec && (maxlen > 1))
400 {
401 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
402 spec++;
403 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
404 spec += WPRINTF_ParseFormatW( spec, &format );
405
406 switch(format.type)
407 {
408 case WPR_WCHAR:
409 argData.wchar_view = (WCHAR)va_arg( args, int );
410 break;
411 case WPR_CHAR:
412 argData.char_view = (CHAR)va_arg( args, int );
413 break;
414 case WPR_STRING:
415 argData.lpcstr_view = va_arg( args, LPCSTR );
416 break;
417 case WPR_WSTRING:
418 argData.lpcwstr_view = va_arg( args, LPCWSTR );
419 break;
420 case WPR_HEXA:
421 case WPR_SIGNED:
422 case WPR_UNSIGNED:
423 argData.int_view = va_arg( args, INT );
424 break;
425 default:
426 argData.wchar_view = 0;
427 break;
428 }
429
430 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
431 sign = 0;
432 if (!(format.flags & WPRINTF_LEFTALIGN))
433 for (i = format.precision; i < format.width; i++, maxlen--)
434 *p++ = ' ';
435 switch(format.type)
436 {
437 case WPR_WCHAR:
438 *p++ = argData.wchar_view;
439 break;
440 case WPR_CHAR:
441 *p++ = argData.char_view;
442 break;
443 case WPR_STRING:
444 {
445 LPCSTR ptr = argData.lpcstr_view;
446 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
447 }
448 break;
449 case WPR_WSTRING:
450 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
451 p += len;
452 break;
453 case WPR_HEXA:
454 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
455 {
456 *p++ = '0';
457 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
458 maxlen -= 2;
459 len -= 2;
460 }
461 /* fall through */
462 case WPR_SIGNED:
463 /* Transfer the sign now, just in case it will be zero-padded*/
464 if (number[0] == '-')
465 {
466 *p++ = '-';
467 sign = 1;
468 }
469 /* fall through */
470 case WPR_UNSIGNED:
471 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
472 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
473 break;
474 case WPR_UNKNOWN:
475 continue;
476 }
477 if (format.flags & WPRINTF_LEFTALIGN)
478 for (i = format.precision; i < format.width; i++, maxlen--)
479 *p++ = ' ';
480 maxlen -= len;
481 }
482 *p = 0;
483 TRACE("%s\n",debugstr_w(buffer));
484 return (maxlen > 1) ? (INT)(p - buffer) : -1;
485 }
486
487 /***********************************************************************
488 * wvsprintfA (USER32.@)
489 */
490 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
491 {
492 INT res = wvsnprintfA( buffer, 1024, spec, args );
493 return ( res == -1 ) ? 1024 : res;
494 }
495
496
497 /***********************************************************************
498 * wvsprintfW (USER32.@)
499 */
500 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
501 {
502 INT res = wvsnprintfW( buffer, 1024, spec, args );
503 return ( res == -1 ) ? 1024 : res;
504 }
505
506 /***********************************************************************
507 * wsprintfA (USER32.@)
508 */
509 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
510 {
511 __ms_va_list valist;
512 INT res;
513
514 __ms_va_start( valist, spec );
515 res = wvsnprintfA( buffer, 1024, spec, valist );
516 __ms_va_end( valist );
517 return ( res == -1 ) ? 1024 : res;
518 }
519
520 /***********************************************************************
521 * wsprintfW (USER32.@)
522 */
523 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
524 {
525 __ms_va_list valist;
526 INT res;
527
528 __ms_va_start( valist, spec );
529 res = wvsnprintfW( buffer, 1024, spec, valist );
530 __ms_va_end( valist );
531 return ( res == -1 ) ? 1024 : res;
532 }