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
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
31 #include "stackframe.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(string);
38 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
39 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
40 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
41 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
42 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
43 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
44 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
74 static const CHAR null_stringA[] = "(null)";
75 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
77 /***********************************************************************
78 * WPRINTF_ParseFormatA
80 * Parse a format specification. A format specification has the form:
82 * [-][#][0][width][.precision]type
84 * Return value is the length of the format specification in characters.
86 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
93 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
94 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
95 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
96 while ((*p >= '0') && (*p <= '9')) /* width field */
98 res->width = res->width * 10 + *p - '0';
101 if (*p == '.') /* precision field */
104 while ((*p >= '0') && (*p <= '9'))
106 res->precision = res->precision * 10 + *p - '0';
110 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
111 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
112 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
116 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
119 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
123 res->type = WPR_SIGNED;
126 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
129 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
132 res->type = WPR_UNSIGNED;
135 res->flags |= WPRINTF_UPPER_HEX;
138 res->type = WPR_HEXA;
140 default: /* unknown format char */
141 res->type = WPR_UNKNOWN;
142 p--; /* print format as normal char */
145 return (INT)(p - format) + 1;
149 /***********************************************************************
150 * WPRINTF_ParseFormatW
152 * Parse a format specification. A format specification has the form:
154 * [-][#][0][width][.precision]type
156 * Return value is the length of the format specification in characters.
158 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
165 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
166 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
167 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
168 while ((*p >= '0') && (*p <= '9')) /* width field */
170 res->width = res->width * 10 + *p - '0';
173 if (*p == '.') /* precision field */
176 while ((*p >= '0') && (*p <= '9'))
178 res->precision = res->precision * 10 + *p - '0';
182 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
183 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
184 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
188 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
191 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
195 res->type = WPR_SIGNED;
198 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
201 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
204 res->type = WPR_UNSIGNED;
207 res->flags |= WPRINTF_UPPER_HEX;
210 res->type = WPR_HEXA;
213 res->type = WPR_UNKNOWN;
214 p--; /* print format as normal char */
217 return (INT)(p - format) + 1;
221 /***********************************************************************
224 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
225 LPSTR number, UINT maxlen )
229 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
230 if (format->width > maxlen) format->width = maxlen;
235 return (format->precision = 1);
237 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
238 for (len = 0; !format->precision || (len < format->precision); len++)
239 if (!*(arg->lpcstr_view + len)) break;
240 if (len > maxlen) len = maxlen;
241 return (format->precision = len);
243 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
244 for (len = 0; !format->precision || (len < format->precision); len++)
245 if (!*(arg->lpcwstr_view + len)) break;
246 if (len > maxlen) len = maxlen;
247 return (format->precision = len);
249 len = sprintf( number, "%d", arg->int_view );
252 len = sprintf( number, "%u", (UINT)arg->int_view );
255 len = sprintf( number,
256 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
257 (UINT)arg->int_view);
262 if (len > maxlen) len = maxlen;
263 if (format->precision < len) format->precision = len;
264 if (format->precision > maxlen) format->precision = maxlen;
265 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
266 format->precision = format->width;
267 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
272 /***********************************************************************
273 * wvsnprintf16 (Not a Windows API)
275 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
278 WPRINTF_FORMAT format;
282 WPRINTF_DATA cur_arg;
285 while (*spec && (maxlen > 1))
287 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
289 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
290 spec += WPRINTF_ParseFormatA( spec, &format );
293 case WPR_WCHAR: /* No Unicode in Win16 */
295 cur_arg.char_view = VA_ARG16( args, CHAR );
297 case WPR_WSTRING: /* No Unicode in Win16 */
299 seg_str = VA_ARG16( args, SEGPTR );
300 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
301 else cur_arg.lpcstr_view = MapSL( seg_str );
304 if (!(format.flags & WPRINTF_LONG))
306 cur_arg.int_view = VA_ARG16( args, INT16 );
312 if (format.flags & WPRINTF_LONG)
313 cur_arg.int_view = VA_ARG16( args, UINT );
315 cur_arg.int_view = VA_ARG16( args, UINT16 );
320 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
322 if (!(format.flags & WPRINTF_LEFTALIGN))
323 for (i = format.precision; i < format.width; i++, maxlen--)
327 case WPR_WCHAR: /* No Unicode in Win16 */
329 *p= cur_arg.char_view;
330 /* wsprintf16 (unlike wsprintf) ignores null characters */
332 else if (format.width > 1) *p++ = ' ';
335 case WPR_WSTRING: /* No Unicode in Win16 */
337 if (len) memcpy( p, cur_arg.lpcstr_view, len );
341 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
344 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
350 /* Transfer the sign now, just in case it will be zero-padded*/
351 if (number[0] == '-')
358 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
359 if (len > sign) memcpy( p, number + sign, len - sign );
365 if (format.flags & WPRINTF_LEFTALIGN)
366 for (i = format.precision; i < format.width; i++, maxlen--)
371 return (maxlen > 1) ? (INT)(p - buffer) : -1;
375 /***********************************************************************
376 * wvsnprintfA (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
378 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
380 WPRINTF_FORMAT format;
384 WPRINTF_DATA argData;
386 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
388 while (*spec && (maxlen > 1))
390 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
392 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
393 spec += WPRINTF_ParseFormatA( spec, &format );
398 argData.wchar_view = (WCHAR)va_arg( args, int );
401 argData.char_view = (CHAR)va_arg( args, int );
404 argData.lpcstr_view = va_arg( args, LPCSTR );
407 argData.lpcwstr_view = va_arg( args, LPCWSTR );
412 argData.int_view = va_arg( args, INT );
415 argData.wchar_view = 0;
419 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
421 if (!(format.flags & WPRINTF_LEFTALIGN))
422 for (i = format.precision; i < format.width; i++, maxlen--)
427 *p++ = argData.wchar_view;
430 *p++ = argData.char_view;
433 memcpy( p, argData.lpcstr_view, len );
438 LPCWSTR ptr = argData.lpcwstr_view;
439 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
443 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
446 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
452 /* Transfer the sign now, just in case it will be zero-padded*/
453 if (number[0] == '-')
460 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
461 memcpy( p, number + sign, len - sign );
467 if (format.flags & WPRINTF_LEFTALIGN)
468 for (i = format.precision; i < format.width; i++, maxlen--)
473 TRACE("%s\n",debugstr_a(buffer));
474 return (maxlen > 1) ? (INT)(p - buffer) : -1;
478 /***********************************************************************
479 * wvsnprintfW (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
481 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
483 WPRINTF_FORMAT format;
487 WPRINTF_DATA argData;
489 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
491 while (*spec && (maxlen > 1))
493 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
495 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
496 spec += WPRINTF_ParseFormatW( spec, &format );
501 argData.wchar_view = (WCHAR)va_arg( args, int );
504 argData.char_view = (CHAR)va_arg( args, int );
507 argData.lpcstr_view = va_arg( args, LPCSTR );
510 argData.lpcwstr_view = va_arg( args, LPCWSTR );
515 argData.int_view = va_arg( args, INT );
518 argData.wchar_view = 0;
522 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
524 if (!(format.flags & WPRINTF_LEFTALIGN))
525 for (i = format.precision; i < format.width; i++, maxlen--)
530 *p++ = argData.wchar_view;
533 *p++ = argData.char_view;
537 LPCSTR ptr = argData.lpcstr_view;
538 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
542 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
546 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
549 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
555 /* Transfer the sign now, just in case it will be zero-padded*/
556 if (number[0] == '-')
563 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
564 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
569 if (format.flags & WPRINTF_LEFTALIGN)
570 for (i = format.precision; i < format.width; i++, maxlen--)
575 TRACE("%s\n",debugstr_w(buffer));
576 return (maxlen > 1) ? (INT)(p - buffer) : -1;
580 /***********************************************************************
581 * wvsprintf (USER.421)
583 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
587 TRACE("for %p got:\n",buffer);
588 res = wvsnprintf16( buffer, 1024, spec, args );
589 return ( res == -1 ) ? 1024 : res;
593 /***********************************************************************
594 * wvsprintfA (USER32.@)
596 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
598 INT res = wvsnprintfA( buffer, 1024, spec, args );
599 return ( res == -1 ) ? 1024 : res;
603 /***********************************************************************
604 * wvsprintfW (USER32.@)
606 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
608 INT res = wvsnprintfW( buffer, 1024, spec, args );
609 return ( res == -1 ) ? 1024 : res;
613 /***********************************************************************
614 * _wsprintf (USER.420)
616 INT16 WINAPIV wsprintf16(void)
622 VA_START16( valist );
623 buffer = VA_ARG16( valist, SEGPTR );
624 spec = VA_ARG16( valist, SEGPTR );
625 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
627 return ( res == -1 ) ? 1024 : res;
631 /***********************************************************************
632 * wsprintfA (USER32.@)
634 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
639 va_start( valist, spec );
640 res = wvsnprintfA( buffer, 1024, spec, valist );
642 return ( res == -1 ) ? 1024 : res;
646 /***********************************************************************
647 * wsprintfW (USER32.@)
649 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
654 va_start( valist, spec );
655 res = wvsnprintfW( buffer, 1024, spec, valist );
657 return ( res == -1 ) ? 1024 : res;