4 * Copyright 1996 Alexandre Julliard
11 #include "stackframe.h"
15 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
16 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
17 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
18 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
19 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
20 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
21 #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;
111 default: /* unknown format char */
112 res->type = WPR_UNKNOWN;
113 p--; /* print format as normal char */
116 return (INT32)(p - format) + 1;
120 /***********************************************************************
121 * WPRINTF_ParseFormatW
123 * Parse a format specification. A format specification has the form:
125 * [-][#][0][width][.precision]type
127 * Return value is the length of the format specification in characters.
129 static INT32 WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
136 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
137 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
138 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
139 while ((*p >= '0') && (*p <= '9')) /* width field */
141 res->width = res->width * 10 + *p - '0';
144 if (*p == '.') /* precision field */
147 while ((*p >= '0') && (*p <= '9'))
149 res->precision = res->precision * 10 + *p - '0';
153 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
154 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
155 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
159 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
162 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
166 res->type = WPR_SIGNED;
169 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
172 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
175 res->type = WPR_UNSIGNED;
178 res->flags |= WPRINTF_UPPER_HEX;
181 res->type = WPR_HEXA;
184 res->type = WPR_UNKNOWN;
185 p--; /* print format as normal char */
188 return (INT32)(p - format) + 1;
192 /***********************************************************************
195 static UINT32 WPRINTF_GetLen( WPRINTF_FORMAT *format, LPCVOID arg,
196 LPSTR number, UINT32 maxlen )
200 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
201 if (format->width > maxlen) format->width = maxlen;
206 return (format->precision = 1);
208 if (!*(LPCSTR *)arg) *(LPCSTR *)arg = null_stringA;
209 for (len = 0; !format->precision || (len < format->precision); len++)
210 if (!*(*(LPCSTR *)arg + len)) break;
211 if (len > maxlen) len = maxlen;
212 return (format->precision = len);
214 if (!*(LPCWSTR *)arg) *(LPCWSTR *)arg = null_stringW;
215 for (len = 0; !format->precision || (len < format->precision); len++)
216 if (!*(*(LPCWSTR *)arg + len)) break;
217 if (len > maxlen) len = maxlen;
218 return (format->precision = len);
220 len = sprintf( number, "%d", *(INT32 *)arg );
223 len = sprintf( number, "%u", *(UINT32 *)arg );
226 len = sprintf( number,
227 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
229 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
234 if (len > maxlen) len = maxlen;
235 if (format->precision < len) format->precision = len;
236 if (format->precision > maxlen) format->precision = maxlen;
237 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
238 format->precision = format->width;
242 /***********************************************************************
243 * WPRINTF_ExtractVAPtr (Not a Windows API)
245 static LPVOID WPRINTF_ExtractVAPtr( WPRINTF_FORMAT *format, va_list* args )
250 return (LPVOID)va_arg( *args, WCHAR );
252 return (LPVOID)va_arg( *args, CHAR );
254 return (LPVOID)va_arg( *args, LPCSTR);
256 return (LPVOID)va_arg( *args, LPCWSTR);
260 return (LPVOID)va_arg( *args, INT32 );
266 /***********************************************************************
267 * wvsnprintf16 (Not a Windows API)
269 INT16 WINAPI wvsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec,
272 WPRINTF_FORMAT format;
278 while (*spec && (maxlen > 1))
280 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
282 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
283 spec += WPRINTF_ParseFormatA( spec, &format );
286 case WPR_WCHAR: /* No Unicode in Win16 */
288 cur_arg = (DWORD)VA_ARG16( args, CHAR );
290 case WPR_WSTRING: /* No Unicode in Win16 */
292 cur_arg = (DWORD)VA_ARG16( args, SEGPTR );
293 if (IsBadReadPtr16( (SEGPTR)cur_arg, 1 )) cur_arg = (DWORD)"";
294 else cur_arg = (DWORD)PTR_SEG_TO_LIN( (SEGPTR)cur_arg );
297 if (!(format.flags & WPRINTF_LONG))
299 cur_arg = (DWORD)(INT32)VA_ARG16( args, INT16 );
305 if (format.flags & WPRINTF_LONG)
306 cur_arg = (DWORD)VA_ARG16( args, UINT32 );
308 cur_arg = (DWORD)VA_ARG16( args, UINT16 );
313 len = WPRINTF_GetLen( &format, &cur_arg, number, maxlen - 1 );
314 if (!(format.flags & WPRINTF_LEFTALIGN))
315 for (i = format.precision; i < format.width; i++, maxlen--)
321 if ((*p = (CHAR)cur_arg)) p++;
322 else if (format.width > 1) *p++ = ' ';
327 if (len) memcpy( p, (LPCSTR)cur_arg, len );
331 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
334 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
337 format.precision -= 2;
343 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
344 if (len) memcpy( p, number, len );
350 if (format.flags & WPRINTF_LEFTALIGN)
351 for (i = format.precision; i < format.width; i++, maxlen--)
356 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
360 /***********************************************************************
361 * wvsnprintf32A (Not a Windows API)
363 INT32 WINAPI wvsnprintf32A( LPSTR buffer, UINT32 maxlen, LPCSTR spec,
366 WPRINTF_FORMAT format;
370 LPVOID argPtr = NULL;
372 while (*spec && (maxlen > 1))
374 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
376 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
377 spec += WPRINTF_ParseFormatA( spec, &format );
378 argPtr = WPRINTF_ExtractVAPtr( &format, &args );
379 len = WPRINTF_GetLen( &format, &argPtr, number, maxlen - 1 );
380 if (!(format.flags & WPRINTF_LEFTALIGN))
381 for (i = format.precision; i < format.width; i++, maxlen--)
386 if ((*p = (CHAR)argPtr)) p++;
387 else if (format.width > 1) *p++ = ' ';
391 if ((*p = (CHAR)argPtr)) p++;
392 else if (format.width > 1) *p++ = ' ';
396 memcpy( p, (LPCSTR)argPtr, len );
401 LPCWSTR ptr = (LPCWSTR)argPtr;
402 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
406 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
409 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
412 format.precision -= 2;
418 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
419 memcpy( p, number, len );
421 /* Go to the next arg */
426 if (format.flags & WPRINTF_LEFTALIGN)
427 for (i = format.precision; i < format.width; i++, maxlen--)
432 TRACE(string,"%s\n",buffer);
433 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
437 /***********************************************************************
438 * wvsnprintf32W (Not a Windows API)
440 INT32 WINAPI wvsnprintf32W( LPWSTR buffer, UINT32 maxlen, LPCWSTR spec,
443 WPRINTF_FORMAT format;
448 while (*spec && (maxlen > 1))
450 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
452 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
453 spec += WPRINTF_ParseFormatW( spec, &format );
454 len = WPRINTF_GetLen( &format, args, number, maxlen - 1 );
455 if (!(format.flags & WPRINTF_LEFTALIGN))
456 for (i = format.precision; i < format.width; i++, maxlen--)
461 if ((*p = va_arg( args, WCHAR ))) p++;
462 else if (format.width > 1) *p++ = ' ';
466 if ((*p = (WCHAR)va_arg( args, CHAR ))) p++;
467 else if (format.width > 1) *p++ = ' ';
472 LPCSTR ptr = va_arg( args, LPCSTR );
473 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
477 if (len) memcpy( p, va_arg( args, LPCWSTR ), len * sizeof(WCHAR) );
481 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
484 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
487 format.precision -= 2;
493 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
494 for (i = 0; i < len; i++) *p++ = (WCHAR)number[i];
495 (void)va_arg( args, INT32 ); /* Go to the next arg */
500 if (format.flags & WPRINTF_LEFTALIGN)
501 for (i = format.precision; i < format.width; i++, maxlen--)
506 return (maxlen > 1) ? (INT32)(p - buffer) : -1;
510 /***********************************************************************
511 * wvsprintf16 (USER.421)
513 INT16 WINAPI wvsprintf16( LPSTR buffer, LPCSTR spec, LPCVOID args )
515 TRACE(string,"for %p got:\n",buffer);
516 return wvsnprintf16( buffer, 0xffff, spec, args );
520 /***********************************************************************
521 * wvsprintf32A (USER32.587)
523 INT32 WINAPI wvsprintf32A( LPSTR buffer, LPCSTR spec, va_list args )
525 TRACE(string,"for %p got:\n",buffer);
526 return wvsnprintf32A( buffer, 0xffffffff, spec, args );
530 /***********************************************************************
531 * wvsprintf32W (USER32.588)
533 INT32 WINAPI wvsprintf32W( LPWSTR buffer, LPCWSTR spec, va_list args )
535 TRACE(string,"for %p got:\n",buffer);
536 return wvsnprintf32W( buffer, 0xffffffff, spec, args );
540 /***********************************************************************
541 * wsprintf16 (USER.420)
543 /* Winelib version */
544 INT16 WINAPIV wsprintf16( LPSTR buffer, LPCSTR spec, ... )
549 TRACE(string,"for %p got:\n",buffer);
550 va_start( valist, spec );
551 /* Note: we call the 32-bit version, because the args are 32-bit */
552 res = (INT16)wvsnprintf32A( buffer, 0xffffffff, spec, valist );
557 /* Emulator version */
558 INT16 WINAPIV WIN16_wsprintf16(void)
564 VA_START16( valist );
565 buffer = VA_ARG16( valist, SEGPTR );
566 spec = VA_ARG16( valist, SEGPTR );
567 TRACE(string,"got:\n");
568 res = wvsnprintf16( (LPSTR)PTR_SEG_TO_LIN(buffer), 0xffff,
569 (LPCSTR)PTR_SEG_TO_LIN(spec), valist );
575 /***********************************************************************
576 * wsprintf32A (USER32.585)
578 INT32 WINAPIV wsprintf32A( LPSTR buffer, LPCSTR spec, ... )
583 TRACE(string,"for %p got:\n",buffer);
584 va_start( valist, spec );
585 res = wvsnprintf32A( buffer, 0xffffffff, spec, valist );
591 /***********************************************************************
592 * wsprintf32W (USER32.586)
594 INT32 WINAPIV wsprintf32W( LPWSTR buffer, LPCWSTR spec, ... )
599 TRACE(string,"wsprintf32W for %p\n",buffer);
600 va_start( valist, spec );
601 res = wvsnprintf32W( buffer, 0xffffffff, spec, valist );
607 /***********************************************************************
608 * wsnprintf16 (Not a Windows API)
610 INT16 WINAPIV wsnprintf16( LPSTR buffer, UINT16 maxlen, LPCSTR spec, ... )
615 va_start( valist, spec );
616 res = wvsnprintf16( buffer, maxlen, spec, valist );
622 /***********************************************************************
623 * wsnprintf32A (Not a Windows API)
625 INT32 WINAPIV wsnprintf32A( LPSTR buffer, UINT32 maxlen, LPCSTR spec, ... )
630 va_start( valist, spec );
631 res = wvsnprintf32A( buffer, maxlen, spec, valist );
637 /***********************************************************************
638 * wsnprintf32W (Not a Windows API)
640 INT32 WINAPIV wsnprintf32W( LPWSTR buffer, UINT32 maxlen, LPCWSTR spec, ... )
645 va_start( valist, spec );
646 res = wvsnprintf32W( buffer, maxlen, spec, valist );