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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This code is duplicated in shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
33 #include "wine/winbase16.h"
34 #include "wine/winuser16.h"
35 #include "stackframe.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(string);
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) */
78 static const CHAR null_stringA[] = "(null)";
79 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
81 /***********************************************************************
82 * WPRINTF_ParseFormatA
84 * Parse a format specification. A format specification has the form:
86 * [-][#][0][width][.precision]type
88 * Return value is the length of the format specification in characters.
90 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
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 */
102 res->width = res->width * 10 + *p - '0';
105 if (*p == '.') /* precision field */
108 while ((*p >= '0') && (*p <= '9'))
110 res->precision = res->precision * 10 + *p - '0';
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++; }
120 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
123 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
127 res->type = WPR_SIGNED;
130 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
133 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
136 res->type = WPR_UNSIGNED;
140 res->flags |= WPRINTF_ZEROPAD;
143 res->flags |= WPRINTF_UPPER_HEX;
146 res->type = WPR_HEXA;
148 default: /* unknown format char */
149 res->type = WPR_UNKNOWN;
150 p--; /* print format as normal char */
153 return (INT)(p - format) + 1;
157 /***********************************************************************
158 * WPRINTF_ParseFormatW
160 * Parse a format specification. A format specification has the form:
162 * [-][#][0][width][.precision]type
164 * Return value is the length of the format specification in characters.
166 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
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 */
178 res->width = res->width * 10 + *p - '0';
181 if (*p == '.') /* precision field */
184 while ((*p >= '0') && (*p <= '9'))
186 res->precision = res->precision * 10 + *p - '0';
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++; }
196 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
199 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
203 res->type = WPR_SIGNED;
206 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
209 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
212 res->type = WPR_UNSIGNED;
216 res->flags |= WPRINTF_ZEROPAD;
219 res->flags |= WPRINTF_UPPER_HEX;
222 res->type = WPR_HEXA;
225 res->type = WPR_UNKNOWN;
226 p--; /* print format as normal char */
229 return (INT)(p - format) + 1;
233 /***********************************************************************
236 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
237 LPSTR number, UINT maxlen )
241 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
242 if (format->width > maxlen) format->width = maxlen;
247 return (format->precision = 1);
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);
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);
261 len = sprintf( number, "%d", arg->int_view );
264 len = sprintf( number, "%u", (UINT)arg->int_view );
267 len = sprintf( number,
268 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
269 (UINT)arg->int_view);
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;
284 /***********************************************************************
285 * wvsnprintf16 (Not a Windows API)
287 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
290 WPRINTF_FORMAT format;
294 WPRINTF_DATA cur_arg;
297 while (*spec && (maxlen > 1))
299 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
301 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
302 spec += WPRINTF_ParseFormatA( spec, &format );
305 case WPR_WCHAR: /* No Unicode in Win16 */
307 cur_arg.char_view = VA_ARG16( args, CHAR );
309 case WPR_WSTRING: /* No Unicode in Win16 */
311 seg_str = VA_ARG16( args, SEGPTR );
312 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
313 else cur_arg.lpcstr_view = MapSL( seg_str );
316 if (!(format.flags & WPRINTF_LONG))
318 cur_arg.int_view = VA_ARG16( args, INT16 );
324 if (format.flags & WPRINTF_LONG)
325 cur_arg.int_view = VA_ARG16( args, UINT );
327 cur_arg.int_view = VA_ARG16( args, UINT16 );
332 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
334 if (!(format.flags & WPRINTF_LEFTALIGN))
335 for (i = format.precision; i < format.width; i++, maxlen--)
339 case WPR_WCHAR: /* No Unicode in Win16 */
341 *p= cur_arg.char_view;
342 /* wsprintf16 (unlike wsprintf) ignores null characters */
344 else if (format.width > 1) *p++ = ' ';
347 case WPR_WSTRING: /* No Unicode in Win16 */
349 if (len) memcpy( p, cur_arg.lpcstr_view, len );
353 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
356 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
362 /* Transfer the sign now, just in case it will be zero-padded*/
363 if (number[0] == '-')
370 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
371 if (len > sign) memcpy( p, number + sign, len - sign );
377 if (format.flags & WPRINTF_LEFTALIGN)
378 for (i = format.precision; i < format.width; i++, maxlen--)
383 return (maxlen > 1) ? (INT)(p - buffer) : -1;
387 /***********************************************************************
388 * wvsnprintfA (internal)
390 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
392 WPRINTF_FORMAT format;
396 WPRINTF_DATA argData;
398 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
400 while (*spec && (maxlen > 1))
402 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
404 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
405 spec += WPRINTF_ParseFormatA( spec, &format );
410 argData.wchar_view = (WCHAR)va_arg( args, int );
413 argData.char_view = (CHAR)va_arg( args, int );
416 argData.lpcstr_view = va_arg( args, LPCSTR );
419 argData.lpcwstr_view = va_arg( args, LPCWSTR );
424 argData.int_view = va_arg( args, INT );
427 argData.wchar_view = 0;
431 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
433 if (!(format.flags & WPRINTF_LEFTALIGN))
434 for (i = format.precision; i < format.width; i++, maxlen--)
439 *p++ = argData.wchar_view;
442 *p++ = argData.char_view;
445 memcpy( p, argData.lpcstr_view, len );
450 LPCWSTR ptr = argData.lpcwstr_view;
451 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
455 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
458 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
464 /* Transfer the sign now, just in case it will be zero-padded*/
465 if (number[0] == '-')
472 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
473 memcpy( p, number + sign, len - sign );
479 if (format.flags & WPRINTF_LEFTALIGN)
480 for (i = format.precision; i < format.width; i++, maxlen--)
485 TRACE("%s\n",debugstr_a(buffer));
486 return (maxlen > 1) ? (INT)(p - buffer) : -1;
490 /***********************************************************************
491 * wvsnprintfW (internal)
493 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
495 WPRINTF_FORMAT format;
499 WPRINTF_DATA argData;
501 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
503 while (*spec && (maxlen > 1))
505 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
507 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
508 spec += WPRINTF_ParseFormatW( spec, &format );
513 argData.wchar_view = (WCHAR)va_arg( args, int );
516 argData.char_view = (CHAR)va_arg( args, int );
519 argData.lpcstr_view = va_arg( args, LPCSTR );
522 argData.lpcwstr_view = va_arg( args, LPCWSTR );
527 argData.int_view = va_arg( args, INT );
530 argData.wchar_view = 0;
534 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
536 if (!(format.flags & WPRINTF_LEFTALIGN))
537 for (i = format.precision; i < format.width; i++, maxlen--)
542 *p++ = argData.wchar_view;
545 *p++ = argData.char_view;
549 LPCSTR ptr = argData.lpcstr_view;
550 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
554 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
558 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
561 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
567 /* Transfer the sign now, just in case it will be zero-padded*/
568 if (number[0] == '-')
575 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
576 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
581 if (format.flags & WPRINTF_LEFTALIGN)
582 for (i = format.precision; i < format.width; i++, maxlen--)
587 TRACE("%s\n",debugstr_w(buffer));
588 return (maxlen > 1) ? (INT)(p - buffer) : -1;
592 /***********************************************************************
593 * wvsprintf (USER.421)
595 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
599 TRACE("for %p got:\n",buffer);
600 res = wvsnprintf16( buffer, 1024, spec, args );
601 return ( res == -1 ) ? 1024 : res;
605 /***********************************************************************
606 * wvsprintfA (USER32.@)
608 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
610 INT res = wvsnprintfA( buffer, 1024, spec, args );
611 return ( res == -1 ) ? 1024 : res;
615 /***********************************************************************
616 * wvsprintfW (USER32.@)
618 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
620 INT res = wvsnprintfW( buffer, 1024, spec, args );
621 return ( res == -1 ) ? 1024 : res;
625 /***********************************************************************
626 * _wsprintf (USER.420)
628 INT16 WINAPIV wsprintf16(void)
634 VA_START16( valist );
635 buffer = VA_ARG16( valist, SEGPTR );
636 spec = VA_ARG16( valist, SEGPTR );
637 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
639 return ( res == -1 ) ? 1024 : res;
643 /***********************************************************************
644 * wsprintfA (USER32.@)
646 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
651 va_start( valist, spec );
652 res = wvsnprintfA( buffer, 1024, spec, valist );
654 return ( res == -1 ) ? 1024 : res;
658 /***********************************************************************
659 * wsprintfW (USER32.@)
661 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
666 va_start( valist, spec );
667 res = wvsnprintfW( buffer, 1024, spec, valist );
669 return ( res == -1 ) ? 1024 : res;