4 * Copyright 1996 Alexandre Julliard
12 #include "stackframe.h"
16 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
17 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
18 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
19 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
20 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
21 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
22 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
43 static const CHAR null_stringA[] = "(null)";
44 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
46 /***********************************************************************
47 * WPRINTF_ParseFormatA
49 * Parse a format specification. A format specification has the form:
51 * [-][#][0][width][.precision]type
53 * Return value is the length of the format specification in characters.
55 static INT32 WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
62 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
63 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
64 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
65 while ((*p >= '0') && (*p <= '9')) /* width field */
67 res->width = res->width * 10 + *p - '0';
70 if (*p == '.') /* precision field */
73 while ((*p >= '0') && (*p <= '9'))
75 res->precision = res->precision * 10 + *p - '0';
79 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
80 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
81 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
85 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
88 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
92 res->type = WPR_SIGNED;
95 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE))
96 ? WPR_WSTRING : WPR_STRING;
99 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE))
100 ? WPR_STRING : WPR_WSTRING;
103 res->type = WPR_UNSIGNED;
106 res->flags |= WPRINTF_UPPER_HEX;
109 res->type = WPR_HEXA;
112 fprintf( stderr, "wvsprintf32A: unknown format '%c'\n", *p );
115 return (INT32)(p - format) + 1;
119 /***********************************************************************
120 * WPRINTF_ParseFormatW
122 * Parse a format specification. A format specification has the form:
124 * [-][#][0][width][.precision]type
126 * Return value is the length of the format specification in characters.
128 static INT32 WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
135 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
136 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
137 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
138 while ((*p >= '0') && (*p <= '9')) /* width field */
140 res->width = res->width * 10 + *p - '0';
143 if (*p == '.') /* precision field */
146 while ((*p >= '0') && (*p <= '9'))
148 res->precision = res->precision * 10 + *p - '0';
152 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
153 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
154 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
158 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
161 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
165 res->type = WPR_SIGNED;
168 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
171 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
174 res->type = WPR_UNSIGNED;
177 res->flags |= WPRINTF_UPPER_HEX;
180 res->type = WPR_HEXA;
183 fprintf( stderr, "wvsprintf32W: unknown format '%c'\n", (CHAR)*p );
186 return (INT32)(p - format) + 1;
190 /***********************************************************************
193 static UINT32 WPRINTF_GetLen( WPRINTF_FORMAT *format, LPCVOID arg,
194 LPSTR number, UINT32 maxlen )
198 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
199 if (format->width > maxlen) format->width = maxlen;
204 return (format->precision = 1);
206 if (!*(LPCSTR *)arg) *(LPCSTR *)arg = null_stringA;
207 for (len = 0; !format->precision || (len < format->precision); len++)
208 if (!*(*(LPCSTR *)arg + len)) break;
209 if (len > maxlen) len = maxlen;
210 return (format->precision = len);
212 if (!*(LPCWSTR *)arg) *(LPCWSTR *)arg = null_stringW;
213 for (len = 0; !format->precision || (len < format->precision); len++)
214 if (!*(*(LPCWSTR *)arg + len)) break;
215 if (len > maxlen) len = maxlen;
216 return (format->precision = len);
218 len = sprintf( number, "%d", *(INT32 *)arg );
221 len = sprintf( number, "%u", *(UINT32 *)arg );
224 len = sprintf( number,
225 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
227 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
232 if (len > maxlen) len = maxlen;
233 if (format->precision < len) format->precision = len;
234 if (format->precision > maxlen) format->precision = maxlen;
235 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
236 format->precision = format->width;
241 /***********************************************************************
242 * wvsnprintf16 (Not a Windows API)
244 INT16 WINAPI wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
247 WPRINTF_FORMAT format;
253 while (*spec && (maxlen > 1))
255 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
257 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
258 spec += WPRINTF_ParseFormatA( spec, &format );
261 case WPR_WCHAR: /* No Unicode in Win16 */
263 cur_arg = (DWORD)VA_ARG16( args, CHAR );
265 case WPR_WSTRING: /* No Unicode in Win16 */
267 cur_arg = (DWORD)VA_ARG16( args, SEGPTR );
268 if (IsBadReadPtr16( (SEGPTR)cur_arg, 1 )) cur_arg = (DWORD)"";
269 else cur_arg = (DWORD)PTR_SEG_TO_LIN( (SEGPTR)cur_arg );
272 if (!(format.flags & WPRINTF_LONG))
274 cur_arg = (DWORD)(INT32)VA_ARG16( args, INT16 );
280 if (format.flags & WPRINTF_LONG)
281 cur_arg = (DWORD)VA_ARG16( args, UINT32 );
283 cur_arg = (DWORD)VA_ARG16( args, UINT16 );
286 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
287 if (!(format.flags & WPRINTF_LEFTALIGN))
288 for (i = format.precision; i < format.width; i++, maxlen--)
294 if ((*p = (CHAR)cur_arg)) p++;
295 else if (format.width > 1) *p++ = ' ';
300 if (len) memcpy( p, (LPCSTR)cur_arg, len );
304 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
307 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
310 format.precision -= 2;
316 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
317 if (len) memcpy( p, number, len );
321 if (format.flags & WPRINTF_LEFTALIGN)
322 for (i = format.precision; i < format.width; i++, maxlen--)
327 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
331 /***********************************************************************
332 * wvsnprintf32A (Not a Windows API)
334 INT32 WINAPI wvsnprintf32A( LPSTR buffer, UINT32 maxlen, LPCSTR spec,
337 WPRINTF_FORMAT format;
342 while (*spec && (maxlen > 1))
344 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
346 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
347 spec += WPRINTF_ParseFormatA( spec, &format );
348 len = WPRINTF_GetLen( &format, args, number, maxlen - 1 );
349 if (!(format.flags & WPRINTF_LEFTALIGN))
350 for (i = format.precision; i < format.width; i++, maxlen--)
355 if ((*p = (CHAR)va_arg( args, WCHAR ))) p++;
356 else if (format.width > 1) *p++ = ' ';
360 if ((*p = va_arg( args, CHAR ))) p++;
361 else if (format.width > 1) *p++ = ' ';
365 memcpy( p, va_arg( args, LPCSTR ), len );
370 LPCWSTR ptr = va_arg( args, LPCWSTR );
371 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
375 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
378 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
381 format.precision -= 2;
387 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
388 memcpy( p, number, len );
390 (void)va_arg( args, INT32 ); /* Go to the next arg */
393 if (format.flags & WPRINTF_LEFTALIGN)
394 for (i = format.precision; i < format.width; i++, maxlen--)
399 dprintf_info(string,"%s\n",buffer);
400 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
404 /***********************************************************************
405 * wvsnprintf32W (Not a Windows API)
407 INT32 WINAPI wvsnprintf32W( LPWSTR buffer, UINT32 maxlen, LPCWSTR spec,
410 WPRINTF_FORMAT format;
415 while (*spec && (maxlen > 1))
417 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
419 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
420 spec += WPRINTF_ParseFormatW( spec, &format );
421 len = WPRINTF_GetLen( &format, args, number, maxlen - 1 );
422 if (!(format.flags & WPRINTF_LEFTALIGN))
423 for (i = format.precision; i < format.width; i++, maxlen--)
428 if ((*p = va_arg( args, WCHAR ))) p++;
429 else if (format.width > 1) *p++ = ' ';
433 if ((*p = (WCHAR)va_arg( args, CHAR ))) p++;
434 else if (format.width > 1) *p++ = ' ';
439 LPCSTR ptr = va_arg( args, LPCSTR );
440 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
444 if (len) memcpy( p, va_arg( args, LPCWSTR ), len * sizeof(WCHAR) );
448 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
451 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
454 format.precision -= 2;
460 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
461 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
462 (void)va_arg( args, INT32 ); /* Go to the next arg */
465 if (format.flags & WPRINTF_LEFTALIGN)
466 for (i = format.precision; i < format.width; i++, maxlen--)
471 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
475 /***********************************************************************
476 * wvsprintf16 (USER.421)
478 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
480 dprintf_info(string,"wvsprintf16 for %p got:\n",buffer);
481 return wvsnprintf16( buffer, 0xffff, spec, args );
485 /***********************************************************************
486 * wvsprintf32A (USER32.586)
488 INT32 WINAPI wvsprintf32A( LPSTR buffer, LPCSTR spec, va_list args )
490 dprintf_info(string,"wvsprintf32A for %p got:\n",buffer);
491 return wvsnprintf32A( buffer, 0xffffffff, spec, args );
495 /***********************************************************************
496 * wvsprintf32W (USER32.587)
498 INT32 WINAPI wvsprintf32W( LPWSTR buffer, LPCWSTR spec, va_list args )
500 dprintf_info(string,"wvsprintf32W for %p got:\n",buffer);
501 return wvsnprintf32W( buffer, 0xffffffff, spec, args );
505 /***********************************************************************
506 * wsprintf16 (USER.420)
508 /* Winelib version */
509 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, ... )
514 dprintf_info(string,"wsprintf16 for %p got:\n",buffer);
515 va_start( valist, spec );
516 /* Note: we call the 32-bit version, because the args are 32-bit */
517 res = (INT16)wvsnprintf32A( buffer, 0xffffffff, spec, valist );
522 /* Emulator version */
523 INT16 WINAPIV WIN16_wsprintf16(void)
529 VA_START16( valist );
530 buffer = VA_ARG16( valist, SEGPTR );
531 spec = VA_ARG16( valist, SEGPTR );
532 dprintf_info(string,"WIN16_wsprintf16 got:\n");
533 res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 0xffff,
534 (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
540 /***********************************************************************
541 * wsprintf32A (USER32.585)
543 INT32 WINAPIV wsprintf32A( LPSTR buffer, LPCSTR spec, ... )
548 dprintf_info(string,"wsprintf32A for %p got:\n",buffer);
549 va_start( valist, spec );
550 res = wvsnprintf32A( buffer, 0xffffffff, spec, valist );
556 /***********************************************************************
557 * wsprintf32W (USER32.586)
559 INT32 WINAPIV wsprintf32W( LPWSTR buffer, LPCWSTR spec, ... )
564 dprintf_info(string,"wsprintf32W for %p\n",buffer);
565 va_start( valist, spec );
566 res = wvsnprintf32W( buffer, 0xffffffff, spec, valist );
572 /***********************************************************************
573 * wsnprintf16 (Not a Windows API)
575 INT16 WINAPIV wsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec, ... )
580 va_start( valist, spec );
581 res = wvsnprintf16( buffer, maxlen, spec, valist );
587 /***********************************************************************
588 * wsnprintf32A (Not a Windows API)
590 INT32 WINAPIV wsnprintf32A( LPSTR buffer, UINT32 maxlen, LPCSTR spec, ... )
595 va_start( valist, spec );
596 res = wvsnprintf32A( buffer, maxlen, spec, valist );
602 /***********************************************************************
603 * wsnprintf32W (Not a Windows API)
605 INT32 WINAPIV wsnprintf32W( LPWSTR buffer, UINT32 maxlen, LPCWSTR spec, ... )
610 va_start( valist, spec );
611 res = wvsnprintf32W( buffer, maxlen, spec, valist );