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
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(string);
39 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
40 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
41 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
42 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
43 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
44 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
45 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
46 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
47 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
77 static const CHAR null_stringA[] = "(null)";
78 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
83 * Parse a format specification. A format specification has the form:
85 * [-][#][0][width][.precision]type
87 * Return value is the length of the format specification in characters.
89 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
96 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
97 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
98 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
99 while ((*p >= '0') && (*p <= '9')) /* width field */
101 res->width = res->width * 10 + *p - '0';
104 if (*p == '.') /* precision field */
107 while ((*p >= '0') && (*p <= '9'))
109 res->precision = res->precision * 10 + *p - '0';
113 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
114 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
115 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
118 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
119 else if (p[1] == '3' && p[2] == '2') p += 3;
120 else { res->flags |= WPRINTF_INTPTR; p++; }
125 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
128 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
132 res->type = WPR_SIGNED;
135 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
138 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
141 res->type = WPR_UNSIGNED;
144 res->width = 2 * sizeof(void *);
145 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
148 res->flags |= WPRINTF_UPPER_HEX;
151 res->type = WPR_HEXA;
153 default: /* unknown format char */
154 res->type = WPR_UNKNOWN;
155 p--; /* print format as normal char */
158 return (INT)(p - format) + 1;
162 /***********************************************************************
163 * WPRINTF_ParseFormatW
165 * Parse a format specification. A format specification has the form:
167 * [-][#][0][width][.precision]type
169 * Return value is the length of the format specification in characters.
171 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
178 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
179 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
180 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
181 while ((*p >= '0') && (*p <= '9')) /* width field */
183 res->width = res->width * 10 + *p - '0';
186 if (*p == '.') /* precision field */
189 while ((*p >= '0') && (*p <= '9'))
191 res->precision = res->precision * 10 + *p - '0';
195 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
196 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
197 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
200 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
201 else if (p[1] == '3' && p[2] == '2') p += 3;
202 else { res->flags |= WPRINTF_INTPTR; p++; }
207 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
210 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
214 res->type = WPR_SIGNED;
217 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
220 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
223 res->type = WPR_UNSIGNED;
226 res->width = 2 * sizeof(void *);
227 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
230 res->flags |= WPRINTF_UPPER_HEX;
233 res->type = WPR_HEXA;
236 res->type = WPR_UNKNOWN;
237 p--; /* print format as normal char */
240 return (INT)(p - format) + 1;
244 /***********************************************************************
247 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
248 LPSTR number, UINT maxlen )
252 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
253 if (format->width > maxlen) format->width = maxlen;
258 return (format->precision = 1);
260 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
261 for (len = 0; !format->precision || (len < format->precision); len++)
262 if (!*(arg->lpcstr_view + len)) break;
263 if (len > maxlen) len = maxlen;
264 return (format->precision = len);
266 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
267 for (len = 0; !format->precision || (len < format->precision); len++)
268 if (!*(arg->lpcwstr_view + len)) break;
269 if (len > maxlen) len = maxlen;
270 return (format->precision = len);
275 const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
276 ULONGLONG num = arg->int_view;
277 int base = format->type == WPR_HEXA ? 16 : 10;
278 char buffer[20], *p = buffer, *dst = number;
280 if (format->type == WPR_SIGNED && arg->int_view < 0)
283 num = -arg->int_view;
285 if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
286 else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
290 *p++ = digits[num % base];
293 while (p > buffer) *dst++ = *(--p);
301 if (len > maxlen) len = maxlen;
302 if (format->precision < len) format->precision = len;
303 if (format->precision > maxlen) format->precision = maxlen;
304 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
305 format->precision = format->width;
306 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
311 /***********************************************************************
312 * wvsnprintfA (internal)
314 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
316 WPRINTF_FORMAT format;
319 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
320 WPRINTF_DATA argData;
322 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
324 while (*spec && (maxlen > 1))
326 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
328 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
329 spec += WPRINTF_ParseFormatA( spec, &format );
334 argData.wchar_view = (WCHAR)va_arg( args, int );
337 argData.char_view = (CHAR)va_arg( args, int );
340 argData.lpcstr_view = va_arg( args, LPCSTR );
343 argData.lpcwstr_view = va_arg( args, LPCWSTR );
348 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
349 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
350 else argData.int_view = va_arg(args, INT);
353 argData.wchar_view = 0;
357 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
359 if (!(format.flags & WPRINTF_LEFTALIGN))
360 for (i = format.precision; i < format.width; i++, maxlen--)
365 *p++ = argData.wchar_view;
368 *p++ = argData.char_view;
371 memcpy( p, argData.lpcstr_view, len );
376 LPCWSTR ptr = argData.lpcwstr_view;
377 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
381 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
384 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
390 /* Transfer the sign now, just in case it will be zero-padded*/
391 if (number[0] == '-')
398 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
399 memcpy( p, number + sign, len - sign );
405 if (format.flags & WPRINTF_LEFTALIGN)
406 for (i = format.precision; i < format.width; i++, maxlen--)
411 TRACE("%s\n",debugstr_a(buffer));
412 return (maxlen > 1) ? (INT)(p - buffer) : -1;
416 /***********************************************************************
417 * wvsnprintfW (internal)
419 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
421 WPRINTF_FORMAT format;
424 CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
425 WPRINTF_DATA argData;
427 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
429 while (*spec && (maxlen > 1))
431 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
433 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
434 spec += WPRINTF_ParseFormatW( spec, &format );
439 argData.wchar_view = (WCHAR)va_arg( args, int );
442 argData.char_view = (CHAR)va_arg( args, int );
445 argData.lpcstr_view = va_arg( args, LPCSTR );
448 argData.lpcwstr_view = va_arg( args, LPCWSTR );
453 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
454 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
455 else argData.int_view = va_arg(args, INT);
458 argData.wchar_view = 0;
462 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
464 if (!(format.flags & WPRINTF_LEFTALIGN))
465 for (i = format.precision; i < format.width; i++, maxlen--)
470 *p++ = argData.wchar_view;
473 *p++ = argData.char_view;
477 LPCSTR ptr = argData.lpcstr_view;
478 for (i = 0; i < len; i++) *p++ = (BYTE)*ptr++;
482 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
486 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
489 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
495 /* Transfer the sign now, just in case it will be zero-padded*/
496 if (number[0] == '-')
503 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
504 for (i = sign; i < len; i++) *p++ = (BYTE)number[i];
509 if (format.flags & WPRINTF_LEFTALIGN)
510 for (i = format.precision; i < format.width; i++, maxlen--)
515 TRACE("%s\n",debugstr_w(buffer));
516 return (maxlen > 1) ? (INT)(p - buffer) : -1;
520 /***********************************************************************
521 * wvsprintfA (USER32.@)
523 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
525 INT res = wvsnprintfA( buffer, 1024, spec, args );
526 return ( res == -1 ) ? 1024 : res;
530 /***********************************************************************
531 * wvsprintfW (USER32.@)
533 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
535 INT res = wvsnprintfW( buffer, 1024, spec, args );
536 return ( res == -1 ) ? 1024 : res;
540 /***********************************************************************
541 * wsprintfA (USER32.@)
543 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
548 __ms_va_start( valist, spec );
549 res = wvsnprintfA( buffer, 1024, spec, valist );
550 __ms_va_end( valist );
551 return ( res == -1 ) ? 1024 : res;
555 /***********************************************************************
556 * wsprintfW (USER32.@)
558 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
563 __ms_va_start( valist, spec );
564 res = wvsnprintfW( buffer, 1024, spec, valist );
565 __ms_va_end( valist );
566 return ( res == -1 ) ? 1024 : res;