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;
139 res->flags |= WPRINTF_UPPER_HEX;
142 res->type = WPR_HEXA;
144 default: /* unknown format char */
145 res->type = WPR_UNKNOWN;
146 p--; /* print format as normal char */
149 return (INT)(p - format) + 1;
153 /***********************************************************************
154 * WPRINTF_ParseFormatW
156 * Parse a format specification. A format specification has the form:
158 * [-][#][0][width][.precision]type
160 * Return value is the length of the format specification in characters.
162 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
169 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
170 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
171 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
172 while ((*p >= '0') && (*p <= '9')) /* width field */
174 res->width = res->width * 10 + *p - '0';
177 if (*p == '.') /* precision field */
180 while ((*p >= '0') && (*p <= '9'))
182 res->precision = res->precision * 10 + *p - '0';
186 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
187 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
188 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
192 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
195 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
199 res->type = WPR_SIGNED;
202 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
205 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
208 res->type = WPR_UNSIGNED;
211 res->flags |= WPRINTF_UPPER_HEX;
214 res->type = WPR_HEXA;
217 res->type = WPR_UNKNOWN;
218 p--; /* print format as normal char */
221 return (INT)(p - format) + 1;
225 /***********************************************************************
228 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
229 LPSTR number, UINT maxlen )
233 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
234 if (format->width > maxlen) format->width = maxlen;
239 return (format->precision = 1);
241 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
242 for (len = 0; !format->precision || (len < format->precision); len++)
243 if (!*(arg->lpcstr_view + len)) break;
244 if (len > maxlen) len = maxlen;
245 return (format->precision = len);
247 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
248 for (len = 0; !format->precision || (len < format->precision); len++)
249 if (!*(arg->lpcwstr_view + len)) break;
250 if (len > maxlen) len = maxlen;
251 return (format->precision = len);
253 len = sprintf( number, "%d", arg->int_view );
256 len = sprintf( number, "%u", (UINT)arg->int_view );
259 len = sprintf( number,
260 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
261 (UINT)arg->int_view);
266 if (len > maxlen) len = maxlen;
267 if (format->precision < len) format->precision = len;
268 if (format->precision > maxlen) format->precision = maxlen;
269 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
270 format->precision = format->width;
271 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
276 /***********************************************************************
277 * wvsnprintf16 (Not a Windows API)
279 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
282 WPRINTF_FORMAT format;
286 WPRINTF_DATA cur_arg;
289 while (*spec && (maxlen > 1))
291 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
293 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
294 spec += WPRINTF_ParseFormatA( spec, &format );
297 case WPR_WCHAR: /* No Unicode in Win16 */
299 cur_arg.char_view = VA_ARG16( args, CHAR );
301 case WPR_WSTRING: /* No Unicode in Win16 */
303 seg_str = VA_ARG16( args, SEGPTR );
304 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
305 else cur_arg.lpcstr_view = MapSL( seg_str );
308 if (!(format.flags & WPRINTF_LONG))
310 cur_arg.int_view = VA_ARG16( args, INT16 );
316 if (format.flags & WPRINTF_LONG)
317 cur_arg.int_view = VA_ARG16( args, UINT );
319 cur_arg.int_view = VA_ARG16( args, UINT16 );
324 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
326 if (!(format.flags & WPRINTF_LEFTALIGN))
327 for (i = format.precision; i < format.width; i++, maxlen--)
331 case WPR_WCHAR: /* No Unicode in Win16 */
333 *p= cur_arg.char_view;
334 /* wsprintf16 (unlike wsprintf) ignores null characters */
336 else if (format.width > 1) *p++ = ' ';
339 case WPR_WSTRING: /* No Unicode in Win16 */
341 if (len) memcpy( p, cur_arg.lpcstr_view, len );
345 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
348 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
354 /* Transfer the sign now, just in case it will be zero-padded*/
355 if (number[0] == '-')
362 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
363 if (len > sign) memcpy( p, number + sign, len - sign );
369 if (format.flags & WPRINTF_LEFTALIGN)
370 for (i = format.precision; i < format.width; i++, maxlen--)
375 return (maxlen > 1) ? (INT)(p - buffer) : -1;
379 /***********************************************************************
380 * wvsnprintfA (internal)
382 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
384 WPRINTF_FORMAT format;
388 WPRINTF_DATA argData;
390 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
392 while (*spec && (maxlen > 1))
394 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
396 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
397 spec += WPRINTF_ParseFormatA( spec, &format );
402 argData.wchar_view = (WCHAR)va_arg( args, int );
405 argData.char_view = (CHAR)va_arg( args, int );
408 argData.lpcstr_view = va_arg( args, LPCSTR );
411 argData.lpcwstr_view = va_arg( args, LPCWSTR );
416 argData.int_view = va_arg( args, INT );
419 argData.wchar_view = 0;
423 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
425 if (!(format.flags & WPRINTF_LEFTALIGN))
426 for (i = format.precision; i < format.width; i++, maxlen--)
431 *p++ = argData.wchar_view;
434 *p++ = argData.char_view;
437 memcpy( p, argData.lpcstr_view, len );
442 LPCWSTR ptr = argData.lpcwstr_view;
443 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
447 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
450 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
456 /* Transfer the sign now, just in case it will be zero-padded*/
457 if (number[0] == '-')
464 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
465 memcpy( p, number + sign, len - sign );
471 if (format.flags & WPRINTF_LEFTALIGN)
472 for (i = format.precision; i < format.width; i++, maxlen--)
477 TRACE("%s\n",debugstr_a(buffer));
478 return (maxlen > 1) ? (INT)(p - buffer) : -1;
482 /***********************************************************************
483 * wvsnprintfW (internal)
485 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
487 WPRINTF_FORMAT format;
491 WPRINTF_DATA argData;
493 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
495 while (*spec && (maxlen > 1))
497 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
499 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
500 spec += WPRINTF_ParseFormatW( spec, &format );
505 argData.wchar_view = (WCHAR)va_arg( args, int );
508 argData.char_view = (CHAR)va_arg( args, int );
511 argData.lpcstr_view = va_arg( args, LPCSTR );
514 argData.lpcwstr_view = va_arg( args, LPCWSTR );
519 argData.int_view = va_arg( args, INT );
522 argData.wchar_view = 0;
526 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
528 if (!(format.flags & WPRINTF_LEFTALIGN))
529 for (i = format.precision; i < format.width; i++, maxlen--)
534 *p++ = argData.wchar_view;
537 *p++ = argData.char_view;
541 LPCSTR ptr = argData.lpcstr_view;
542 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
546 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
550 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
553 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
559 /* Transfer the sign now, just in case it will be zero-padded*/
560 if (number[0] == '-')
567 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
568 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
573 if (format.flags & WPRINTF_LEFTALIGN)
574 for (i = format.precision; i < format.width; i++, maxlen--)
579 TRACE("%s\n",debugstr_w(buffer));
580 return (maxlen > 1) ? (INT)(p - buffer) : -1;
584 /***********************************************************************
585 * wvsprintf (USER.421)
587 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
591 TRACE("for %p got:\n",buffer);
592 res = wvsnprintf16( buffer, 1024, spec, args );
593 return ( res == -1 ) ? 1024 : res;
597 /***********************************************************************
598 * wvsprintfA (USER32.@)
600 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
602 INT res = wvsnprintfA( buffer, 1024, spec, args );
603 return ( res == -1 ) ? 1024 : res;
607 /***********************************************************************
608 * wvsprintfW (USER32.@)
610 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
612 INT res = wvsnprintfW( buffer, 1024, spec, args );
613 return ( res == -1 ) ? 1024 : res;
617 /***********************************************************************
618 * _wsprintf (USER.420)
620 INT16 WINAPIV wsprintf16(void)
626 VA_START16( valist );
627 buffer = VA_ARG16( valist, SEGPTR );
628 spec = VA_ARG16( valist, SEGPTR );
629 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
631 return ( res == -1 ) ? 1024 : res;
635 /***********************************************************************
636 * wsprintfA (USER32.@)
638 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
643 va_start( valist, spec );
644 res = wvsnprintfA( buffer, 1024, spec, valist );
646 return ( res == -1 ) ? 1024 : res;
650 /***********************************************************************
651 * wsprintfW (USER32.@)
653 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
658 va_start( valist, spec );
659 res = wvsnprintfW( buffer, 1024, spec, valist );
661 return ( res == -1 ) ? 1024 : res;