4 * Copyright 1996 Alexandre Julliard
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "stackframe.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(string);
24 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
25 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
26 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
27 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
28 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
29 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
30 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
60 static const CHAR null_stringA[] = "(null)";
61 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
63 /***********************************************************************
64 * WPRINTF_ParseFormatA
66 * Parse a format specification. A format specification has the form:
68 * [-][#][0][width][.precision]type
70 * Return value is the length of the format specification in characters.
72 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
79 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
80 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
81 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
82 while ((*p >= '0') && (*p <= '9')) /* width field */
84 res->width = res->width * 10 + *p - '0';
87 if (*p == '.') /* precision field */
90 while ((*p >= '0') && (*p <= '9'))
92 res->precision = res->precision * 10 + *p - '0';
96 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
97 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
98 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
102 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
105 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
109 res->type = WPR_SIGNED;
112 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
115 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
118 res->type = WPR_UNSIGNED;
121 res->flags |= WPRINTF_UPPER_HEX;
124 res->type = WPR_HEXA;
126 default: /* unknown format char */
127 res->type = WPR_UNKNOWN;
128 p--; /* print format as normal char */
131 return (INT)(p - format) + 1;
135 /***********************************************************************
136 * WPRINTF_ParseFormatW
138 * Parse a format specification. A format specification has the form:
140 * [-][#][0][width][.precision]type
142 * Return value is the length of the format specification in characters.
144 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
151 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
152 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
153 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
154 while ((*p >= '0') && (*p <= '9')) /* width field */
156 res->width = res->width * 10 + *p - '0';
159 if (*p == '.') /* precision field */
162 while ((*p >= '0') && (*p <= '9'))
164 res->precision = res->precision * 10 + *p - '0';
168 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
169 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
170 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
174 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
177 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
181 res->type = WPR_SIGNED;
184 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
187 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
190 res->type = WPR_UNSIGNED;
193 res->flags |= WPRINTF_UPPER_HEX;
196 res->type = WPR_HEXA;
199 res->type = WPR_UNKNOWN;
200 p--; /* print format as normal char */
203 return (INT)(p - format) + 1;
207 /***********************************************************************
210 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
211 LPSTR number, UINT maxlen )
215 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
216 if (format->width > maxlen) format->width = maxlen;
221 return (format->precision = 1);
223 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
224 for (len = 0; !format->precision || (len < format->precision); len++)
225 if (!*(arg->lpcstr_view + len)) break;
226 if (len > maxlen) len = maxlen;
227 return (format->precision = len);
229 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
230 for (len = 0; !format->precision || (len < format->precision); len++)
231 if (!*(arg->lpcwstr_view + len)) break;
232 if (len > maxlen) len = maxlen;
233 return (format->precision = len);
235 len = sprintf( number, "%d", arg->int_view );
238 len = sprintf( number, "%u", (UINT)arg->int_view );
241 len = sprintf( number,
242 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
243 (UINT)arg->int_view);
248 if (len > maxlen) len = maxlen;
249 if (format->precision < len) format->precision = len;
250 if (format->precision > maxlen) format->precision = maxlen;
251 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
252 format->precision = format->width;
253 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
258 /***********************************************************************
259 * wvsnprintf16 (Not a Windows API)
261 static INT16 wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
264 WPRINTF_FORMAT format;
268 WPRINTF_DATA cur_arg;
271 while (*spec && (maxlen > 1))
273 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
275 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
276 spec += WPRINTF_ParseFormatA( spec, &format );
279 case WPR_WCHAR: /* No Unicode in Win16 */
281 cur_arg.char_view = VA_ARG16( args, CHAR );
283 case WPR_WSTRING: /* No Unicode in Win16 */
285 seg_str = VA_ARG16( args, SEGPTR );
286 if (IsBadReadPtr16(seg_str, 1 )) cur_arg.lpcstr_view = "";
287 else cur_arg.lpcstr_view = MapSL( seg_str );
290 if (!(format.flags & WPRINTF_LONG))
292 cur_arg.int_view = VA_ARG16( args, INT16 );
298 if (format.flags & WPRINTF_LONG)
299 cur_arg.int_view = VA_ARG16( args, UINT );
301 cur_arg.int_view = VA_ARG16( args, UINT16 );
306 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
307 if (!(format.flags & WPRINTF_LEFTALIGN))
308 for (i = format.precision; i < format.width; i++, maxlen--)
312 case WPR_WCHAR: /* No Unicode in Win16 */
314 *p= cur_arg.char_view;
315 /* wsprintf16 (unlike wsprintf) ignores null characters */
317 else if (format.width > 1) *p++ = ' ';
320 case WPR_WSTRING: /* No Unicode in Win16 */
322 if (len) memcpy( p, cur_arg.lpcstr_view, len );
326 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
329 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
336 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
337 if (len) memcpy( p, number, len );
343 if (format.flags & WPRINTF_LEFTALIGN)
344 for (i = format.precision; i < format.width; i++, maxlen--)
349 return (maxlen > 1) ? (INT)(p - buffer) : -1;
353 /***********************************************************************
354 * wvsnprintfA (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
356 INT WINAPI wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, va_list args )
358 WPRINTF_FORMAT format;
362 WPRINTF_DATA argData;
364 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
366 while (*spec && (maxlen > 1))
368 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
370 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
371 spec += WPRINTF_ParseFormatA( spec, &format );
376 argData.wchar_view = (WCHAR)va_arg( args, int );
379 argData.char_view = (CHAR)va_arg( args, int );
382 argData.lpcstr_view = va_arg( args, LPCSTR );
385 argData.lpcwstr_view = va_arg( args, LPCWSTR );
390 argData.int_view = va_arg( args, INT );
393 argData.wchar_view = 0;
397 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
398 if (!(format.flags & WPRINTF_LEFTALIGN))
399 for (i = format.precision; i < format.width; i++, maxlen--)
404 *p++ = argData.wchar_view;
407 *p++ = argData.char_view;
410 memcpy( p, argData.lpcstr_view, len );
415 LPCWSTR ptr = argData.lpcwstr_view;
416 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
420 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
423 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
430 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
431 memcpy( p, number, len );
437 if (format.flags & WPRINTF_LEFTALIGN)
438 for (i = format.precision; i < format.width; i++, maxlen--)
443 TRACE("%s\n",debugstr_a(buffer));
444 return (maxlen > 1) ? (INT)(p - buffer) : -1;
448 /***********************************************************************
449 * wvsnprintfW (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
451 INT WINAPI wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, va_list args )
453 WPRINTF_FORMAT format;
457 WPRINTF_DATA argData;
459 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
461 while (*spec && (maxlen > 1))
463 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
465 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
466 spec += WPRINTF_ParseFormatW( spec, &format );
471 argData.wchar_view = (WCHAR)va_arg( args, int );
474 argData.char_view = (CHAR)va_arg( args, int );
477 argData.lpcstr_view = va_arg( args, LPCSTR );
480 argData.lpcwstr_view = va_arg( args, LPCWSTR );
485 argData.int_view = va_arg( args, INT );
488 argData.wchar_view = 0;
492 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
493 if (!(format.flags & WPRINTF_LEFTALIGN))
494 for (i = format.precision; i < format.width; i++, maxlen--)
499 *p++ = argData.wchar_view;
502 *p++ = argData.char_view;
506 LPCSTR ptr = argData.lpcstr_view;
507 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
511 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
515 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
518 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
525 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
526 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
531 if (format.flags & WPRINTF_LEFTALIGN)
532 for (i = format.precision; i < format.width; i++, maxlen--)
537 TRACE("%s\n",debugstr_w(buffer));
538 return (maxlen > 1) ? (INT)(p - buffer) : -1;
542 /***********************************************************************
543 * wvsprintf (USER.421)
545 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
549 TRACE("for %p got:\n",buffer);
550 res = wvsnprintf16( buffer, 1024, spec, args );
551 return ( res == -1 ) ? 1024 : res;
555 /***********************************************************************
556 * wvsprintfA (USER32.@)
558 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, va_list args )
560 INT res = wvsnprintfA( buffer, 1024, spec, args );
561 return ( res == -1 ) ? 1024 : res;
565 /***********************************************************************
566 * wvsprintfW (USER32.@)
568 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, va_list args )
570 INT res = wvsnprintfW( buffer, 1024, spec, args );
571 return ( res == -1 ) ? 1024 : res;
575 /***********************************************************************
576 * _wsprintf (USER.420)
578 INT16 WINAPIV wsprintf16(void)
584 VA_START16( valist );
585 buffer = VA_ARG16( valist, SEGPTR );
586 spec = VA_ARG16( valist, SEGPTR );
587 res = wvsnprintf16( MapSL(buffer), 1024, MapSL(spec), valist );
589 return ( res == -1 ) ? 1024 : res;
593 /***********************************************************************
594 * wsprintfA (USER32.@)
596 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
601 va_start( valist, spec );
602 res = wvsnprintfA( buffer, 1024, spec, valist );
604 return ( res == -1 ) ? 1024 : res;
608 /***********************************************************************
609 * wsprintfW (USER32.@)
611 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
616 va_start( valist, spec );
617 res = wvsnprintfW( buffer, 1024, spec, valist );
619 return ( res == -1 ) ? 1024 : res;