uiautomationcore/tests: Add tests for cached value helper functions in the COM API.
[wine.git] / dlls / user32 / wsprintf.c
blob1b396f0269c780f637973297c6cc9f1e29628d7e
1 /*
2 * wsprintf functions
4 * Copyright 1996 Alexandre Julliard
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.
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.
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
20 * NOTE:
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(string);
40 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
41 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
42 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
43 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
44 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
45 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
46 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
47 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
48 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
50 typedef enum
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;
62 typedef struct
64 UINT flags;
65 UINT width;
66 UINT precision;
67 WPRINTF_TYPE type;
68 } WPRINTF_FORMAT;
70 typedef union {
71 WCHAR wchar_view;
72 CHAR char_view;
73 LPCSTR lpcstr_view;
74 LPCWSTR lpcwstr_view;
75 LONGLONG int_view;
76 } WPRINTF_DATA;
78 /***********************************************************************
79 * WPRINTF_ParseFormatA
81 * Parse a format specification. A format specification has the form:
83 * [-][#][0][width][.precision]type
85 * Return value is the length of the format specification in characters.
87 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
89 LPCSTR p = format;
91 res->flags = 0;
92 res->width = 0;
93 res->precision = 0;
94 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
95 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
96 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
97 while ((*p >= '0') && (*p <= '9')) /* width field */
99 res->width = res->width * 10 + *p - '0';
100 p++;
102 if (*p == '.') /* precision field */
104 p++;
105 while ((*p >= '0') && (*p <= '9'))
107 res->precision = res->precision * 10 + *p - '0';
108 p++;
111 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
112 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
113 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
114 else if (*p == 'I')
116 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
117 else if (p[1] == '3' && p[2] == '2') p += 3;
118 else { res->flags |= WPRINTF_INTPTR; p++; }
120 switch(*p)
122 case 'c':
123 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
124 break;
125 case 'C':
126 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
127 break;
128 case 'd':
129 case 'i':
130 res->type = WPR_SIGNED;
131 break;
132 case 's':
133 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
134 break;
135 case 'S':
136 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
137 break;
138 case 'u':
139 res->type = WPR_UNSIGNED;
140 break;
141 case 'p':
142 res->width = 2 * sizeof(void *);
143 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
144 /* fall through */
145 case 'X':
146 res->flags |= WPRINTF_UPPER_HEX;
147 /* fall through */
148 case 'x':
149 res->type = WPR_HEXA;
150 break;
151 default: /* unknown format char */
152 res->type = WPR_UNKNOWN;
153 p--; /* print format as normal char */
154 break;
156 return (INT)(p - format) + 1;
160 /***********************************************************************
161 * WPRINTF_ParseFormatW
163 * Parse a format specification. A format specification has the form:
165 * [-][#][0][width][.precision]type
167 * Return value is the length of the format specification in characters.
169 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
171 LPCWSTR p = format;
173 res->flags = 0;
174 res->width = 0;
175 res->precision = 0;
176 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
177 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
178 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
179 while ((*p >= '0') && (*p <= '9')) /* width field */
181 res->width = res->width * 10 + *p - '0';
182 p++;
184 if (*p == '.') /* precision field */
186 p++;
187 while ((*p >= '0') && (*p <= '9'))
189 res->precision = res->precision * 10 + *p - '0';
190 p++;
193 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
194 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
195 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
196 else if (*p == 'I')
198 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
199 else if (p[1] == '3' && p[2] == '2') p += 3;
200 else { res->flags |= WPRINTF_INTPTR; p++; }
202 switch(*p)
204 case 'c':
205 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
206 break;
207 case 'C':
208 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
209 break;
210 case 'd':
211 case 'i':
212 res->type = WPR_SIGNED;
213 break;
214 case 's':
215 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
216 break;
217 case 'S':
218 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
219 break;
220 case 'u':
221 res->type = WPR_UNSIGNED;
222 break;
223 case 'p':
224 res->width = 2 * sizeof(void *);
225 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
226 /* fall through */
227 case 'X':
228 res->flags |= WPRINTF_UPPER_HEX;
229 /* fall through */
230 case 'x':
231 res->type = WPR_HEXA;
232 break;
233 default:
234 res->type = WPR_UNKNOWN;
235 p--; /* print format as normal char */
236 break;
238 return (INT)(p - format) + 1;
242 /***********************************************************************
243 * WPRINTF_GetLen
245 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
246 LPSTR number, UINT maxlen, BOOL dst_is_wide )
248 UINT len;
250 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
251 if (format->width > maxlen) format->width = maxlen;
252 switch(format->type)
254 case WPR_CHAR:
255 return (format->precision = 1);
256 case WPR_WCHAR:
257 if (dst_is_wide) len = 1;
258 else len = WideCharToMultiByte( CP_ACP, 0, &arg->wchar_view, 1, NULL, 0, NULL, NULL );
259 return (format->precision = len);
260 case WPR_STRING:
261 if (!arg->lpcstr_view) arg->lpcstr_view = "(null)";
262 if (dst_is_wide)
264 LPCSTR p = arg->lpcstr_view;
265 for (len = 0; (!format->precision || len < format->precision) && *p; p++)
267 /* This isn't applicable for UTF-8 and UTF-7 */
268 if (IsDBCSLeadByte( *p )) p++;
269 len++;
270 if (!*p) break;
273 else
275 for (len = 0; !format->precision || (len < format->precision); len++)
276 if (!*(arg->lpcstr_view + len)) break;
278 if (len > maxlen) len = maxlen;
279 return (format->precision = len);
280 case WPR_WSTRING:
281 if (!arg->lpcwstr_view) arg->lpcwstr_view = L"(null)";
282 if (dst_is_wide)
284 for (len = 0; !format->precision || (len < format->precision); len++)
285 if (!*(arg->lpcwstr_view + len)) break;
287 else
289 LPCWSTR p = arg->lpcwstr_view;
290 for (len = 0; (!format->precision || len < format->precision) && *p; p++)
291 len += WideCharToMultiByte( CP_ACP, 0, p, 1, NULL, 0, NULL, NULL );
292 if (format->precision && len > format->precision) len = format->precision;
294 if (len > maxlen) len = maxlen;
295 return (format->precision = len);
296 case WPR_SIGNED:
297 case WPR_UNSIGNED:
298 case WPR_HEXA:
300 const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
301 ULONGLONG num = arg->int_view;
302 int base = format->type == WPR_HEXA ? 16 : 10;
303 char buffer[20], *p = buffer, *dst = number;
305 if (format->type == WPR_SIGNED && arg->int_view < 0)
307 *dst++ = '-';
308 num = -arg->int_view;
310 if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
311 else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
315 *p++ = digits[num % base];
316 num /= base;
317 } while (num);
318 while (p > buffer) *dst++ = *(--p);
319 *dst = 0;
320 len = dst - number;
321 break;
323 default:
324 return 0;
326 if (len > maxlen) len = maxlen;
327 if (format->precision < len) format->precision = len;
328 if (format->precision > maxlen) format->precision = maxlen;
329 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
330 format->precision = format->width;
331 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
332 return len;
336 /***********************************************************************
337 * wvsnprintfA (internal)
339 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
341 WPRINTF_FORMAT format;
342 LPSTR p = buffer;
343 UINT i, len, sign;
344 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
345 WPRINTF_DATA argData;
347 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
349 while (*spec && (maxlen > 1))
351 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
352 spec++;
353 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
354 spec += WPRINTF_ParseFormatA( spec, &format );
356 switch(format.type)
358 case WPR_WCHAR:
359 argData.wchar_view = (WCHAR)va_arg( args, int );
360 break;
361 case WPR_CHAR:
362 argData.char_view = (CHAR)va_arg( args, int );
363 break;
364 case WPR_STRING:
365 argData.lpcstr_view = va_arg( args, LPCSTR );
366 break;
367 case WPR_WSTRING:
368 argData.lpcwstr_view = va_arg( args, LPCWSTR );
369 break;
370 case WPR_HEXA:
371 case WPR_SIGNED:
372 case WPR_UNSIGNED:
373 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
374 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
375 else argData.int_view = va_arg(args, INT);
376 break;
377 default:
378 argData.wchar_view = 0;
379 break;
382 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1, FALSE );
383 sign = 0;
384 if (!(format.flags & WPRINTF_LEFTALIGN))
385 for (i = format.precision; i < format.width; i++, maxlen--)
386 *p++ = ' ';
387 switch(format.type)
389 case WPR_WCHAR:
391 CHAR mb[5];
392 if (WideCharToMultiByte( CP_ACP, 0, &argData.wchar_view, 1, mb, sizeof(mb), NULL, NULL ))
394 memcpy( p, mb, len );
395 p += len;
398 break;
399 case WPR_CHAR:
400 *p++ = argData.char_view;
401 break;
402 case WPR_STRING:
403 memcpy( p, argData.lpcstr_view, len );
404 p += len;
405 break;
406 case WPR_WSTRING:
408 LPCWSTR ptr = argData.lpcwstr_view;
409 for (i = 0; i < len; ptr++)
411 CHAR mb[5]; /* 5 is MB_LEN_MAX */
412 int ret = WideCharToMultiByte( CP_ACP, 0, ptr, 1, mb, sizeof(mb), NULL, NULL );
413 if (ret > len - i) ret = len - i;
414 i += ret;
415 memcpy( p, mb, ret );
416 p += ret;
419 break;
420 case WPR_HEXA:
421 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
423 *p++ = '0';
424 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
425 maxlen -= 2;
426 len -= 2;
428 /* fall through */
429 case WPR_SIGNED:
430 /* Transfer the sign now, just in case it will be zero-padded*/
431 if (number[0] == '-')
433 *p++ = '-';
434 sign = 1;
436 /* fall through */
437 case WPR_UNSIGNED:
438 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
439 memcpy( p, number + sign, len - sign );
440 p += len - sign;
441 break;
442 case WPR_UNKNOWN:
443 continue;
445 if (format.flags & WPRINTF_LEFTALIGN)
446 for (i = format.precision; i < format.width; i++, maxlen--)
447 *p++ = ' ';
448 maxlen -= len;
450 *p = 0;
451 TRACE("%s\n",debugstr_a(buffer));
452 return (maxlen > 1) ? (INT)(p - buffer) : -1;
456 /***********************************************************************
457 * wvsnprintfW (internal)
459 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
461 WPRINTF_FORMAT format;
462 LPWSTR p = buffer;
463 UINT i, len, sign;
464 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
465 WPRINTF_DATA argData;
467 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
469 while (*spec && (maxlen > 1))
471 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
472 spec++;
473 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
474 spec += WPRINTF_ParseFormatW( spec, &format );
476 switch(format.type)
478 case WPR_WCHAR:
479 argData.wchar_view = (WCHAR)va_arg( args, int );
480 break;
481 case WPR_CHAR:
482 argData.char_view = (CHAR)va_arg( args, int );
483 break;
484 case WPR_STRING:
485 argData.lpcstr_view = va_arg( args, LPCSTR );
486 break;
487 case WPR_WSTRING:
488 argData.lpcwstr_view = va_arg( args, LPCWSTR );
489 break;
490 case WPR_HEXA:
491 case WPR_SIGNED:
492 case WPR_UNSIGNED:
493 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
494 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
495 else argData.int_view = va_arg(args, INT);
496 break;
497 default:
498 argData.wchar_view = 0;
499 break;
502 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1, TRUE );
503 sign = 0;
504 if (!(format.flags & WPRINTF_LEFTALIGN))
505 for (i = format.precision; i < format.width; i++, maxlen--)
506 *p++ = ' ';
507 switch(format.type)
509 case WPR_WCHAR:
510 *p++ = argData.wchar_view;
511 break;
512 case WPR_CHAR:
514 WCHAR wc;
515 if (!IsDBCSLeadByte( (BYTE)argData.char_view )
516 && MultiByteToWideChar( CP_ACP, 0, &argData.char_view, 1, &wc, 1 ) > 0)
517 *p++ = wc;
518 else
519 *p++ = 0;
520 break;
522 case WPR_STRING:
524 LPCSTR ptr = argData.lpcstr_view;
525 for (i = 0; i < len; i++)
527 WCHAR buf[2]; /* for LeadByte + NUL case, we need 2 WCHARs. */
528 int ret, mb_len = IsDBCSLeadByte( *ptr ) ? 2 : 1;
529 ret = MultiByteToWideChar( CP_ACP, 0, ptr, mb_len, buf, ARRAY_SIZE( buf ));
530 *p++ = buf[ret - 1];
531 ptr += mb_len;
534 break;
535 case WPR_WSTRING:
536 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
537 p += len;
538 break;
539 case WPR_HEXA:
540 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
542 *p++ = '0';
543 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
544 maxlen -= 2;
545 len -= 2;
547 /* fall through */
548 case WPR_SIGNED:
549 /* Transfer the sign now, just in case it will be zero-padded*/
550 if (number[0] == '-')
552 *p++ = '-';
553 sign = 1;
555 /* fall through */
556 case WPR_UNSIGNED:
557 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
558 for (i = sign; i < len; i++) *p++ = (BYTE)number[i];
559 break;
560 case WPR_UNKNOWN:
561 continue;
563 if (format.flags & WPRINTF_LEFTALIGN)
564 for (i = format.precision; i < format.width; i++, maxlen--)
565 *p++ = ' ';
566 maxlen -= len;
568 *p = 0;
569 TRACE("%s\n",debugstr_w(buffer));
570 return (maxlen > 1) ? (INT)(p - buffer) : -1;
574 /***********************************************************************
575 * wvsprintfA (USER32.@)
577 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
579 INT res = wvsnprintfA( buffer, 1024, spec, args );
580 return ( res == -1 ) ? 1024 : res;
584 /***********************************************************************
585 * wvsprintfW (USER32.@)
587 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
589 INT res = wvsnprintfW( buffer, 1024, spec, args );
590 return ( res == -1 ) ? 1024 : res;
594 /***********************************************************************
595 * wsprintfA (USER32.@)
597 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
599 va_list valist;
600 INT res;
602 va_start( valist, spec );
603 res = wvsnprintfA( buffer, 1024, spec, valist );
604 va_end( valist );
605 return ( res == -1 ) ? 1024 : res;
609 /***********************************************************************
610 * wsprintfW (USER32.@)
612 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
614 va_list valist;
615 INT res;
617 va_start( valist, spec );
618 res = wvsnprintfW( buffer, 1024, spec, valist );
619 va_end( valist );
620 return ( res == -1 ) ? 1024 : res;