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
;
243 /***********************************************************************
244 * wvsnprintf16 (Not a Windows API)
246 INT16 WINAPI
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
,
249 WPRINTF_FORMAT format
;
255 while (*spec
&& (maxlen
> 1))
257 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
259 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
260 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
263 case WPR_WCHAR
: /* No Unicode in Win16 */
265 cur_arg
= (DWORD
)VA_ARG16( args
, CHAR
);
267 case WPR_WSTRING
: /* No Unicode in Win16 */
269 cur_arg
= (DWORD
)VA_ARG16( args
, SEGPTR
);
270 if (IsBadReadPtr16( (SEGPTR
)cur_arg
, 1 )) cur_arg
= (DWORD
)"";
271 else cur_arg
= (DWORD
)PTR_SEG_TO_LIN( (SEGPTR
)cur_arg
);
274 if (!(format
.flags
& WPRINTF_LONG
))
276 cur_arg
= (DWORD
)(INT32
)VA_ARG16( args
, INT16
);
282 if (format
.flags
& WPRINTF_LONG
)
283 cur_arg
= (DWORD
)VA_ARG16( args
, UINT32
);
285 cur_arg
= (DWORD
)VA_ARG16( args
, UINT16
);
290 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
291 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
292 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
298 if ((*p
= (CHAR
)cur_arg
)) p
++;
299 else if (format
.width
> 1) *p
++ = ' ';
304 if (len
) memcpy( p
, (LPCSTR
)cur_arg
, len
);
308 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
311 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
314 format
.precision
-= 2;
320 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
321 if (len
) memcpy( p
, number
, len
);
327 if (format
.flags
& WPRINTF_LEFTALIGN
)
328 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
333 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
337 /***********************************************************************
338 * wvsnprintf32A (Not a Windows API)
340 INT32 WINAPI
wvsnprintf32A( LPSTR buffer
, UINT32 maxlen
, LPCSTR spec
,
343 WPRINTF_FORMAT format
;
348 while (*spec
&& (maxlen
> 1))
350 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
352 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
353 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
354 len
= WPRINTF_GetLen( &format
, args
, number
, maxlen
- 1 );
355 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
356 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
361 if ((*p
= (CHAR
)va_arg( args
, WCHAR
))) p
++;
362 else if (format
.width
> 1) *p
++ = ' ';
366 if ((*p
= va_arg( args
, CHAR
))) p
++;
367 else if (format
.width
> 1) *p
++ = ' ';
371 memcpy( p
, va_arg( args
, LPCSTR
), len
);
376 LPCWSTR ptr
= va_arg( args
, LPCWSTR
);
377 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
381 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
384 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
387 format
.precision
-= 2;
393 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
394 memcpy( p
, number
, len
);
396 (void)va_arg( args
, INT32
); /* Go to the next arg */
401 if (format
.flags
& WPRINTF_LEFTALIGN
)
402 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
407 TRACE(string
,"%s\n",buffer
);
408 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
412 /***********************************************************************
413 * wvsnprintf32W (Not a Windows API)
415 INT32 WINAPI
wvsnprintf32W( LPWSTR buffer
, UINT32 maxlen
, LPCWSTR spec
,
418 WPRINTF_FORMAT format
;
423 while (*spec
&& (maxlen
> 1))
425 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
427 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
428 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
429 len
= WPRINTF_GetLen( &format
, args
, number
, maxlen
- 1 );
430 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
431 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
436 if ((*p
= va_arg( args
, WCHAR
))) p
++;
437 else if (format
.width
> 1) *p
++ = ' ';
441 if ((*p
= (WCHAR
)va_arg( args
, CHAR
))) p
++;
442 else if (format
.width
> 1) *p
++ = ' ';
447 LPCSTR ptr
= va_arg( args
, LPCSTR
);
448 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
452 if (len
) memcpy( p
, va_arg( args
, LPCWSTR
), len
* sizeof(WCHAR
) );
456 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
459 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
462 format
.precision
-= 2;
468 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
469 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
470 (void)va_arg( args
, INT32
); /* Go to the next arg */
475 if (format
.flags
& WPRINTF_LEFTALIGN
)
476 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
481 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
485 /***********************************************************************
486 * wvsprintf16 (USER.421)
488 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
490 TRACE(string
,"for %p got:\n",buffer
);
491 return wvsnprintf16( buffer
, 0xffff, spec
, args
);
495 /***********************************************************************
496 * wvsprintf32A (USER32.587)
498 INT32 WINAPI
wvsprintf32A( LPSTR buffer
, LPCSTR spec
, va_list args
)
500 TRACE(string
,"for %p got:\n",buffer
);
501 return wvsnprintf32A( buffer
, 0xffffffff, spec
, args
);
505 /***********************************************************************
506 * wvsprintf32W (USER32.588)
508 INT32 WINAPI
wvsprintf32W( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
510 TRACE(string
,"for %p got:\n",buffer
);
511 return wvsnprintf32W( buffer
, 0xffffffff, spec
, args
);
515 /***********************************************************************
516 * wsprintf16 (USER.420)
518 /* Winelib version */
519 INT16 WINAPIV
wsprintf16( LPSTR buffer
, LPCSTR spec
, ... )
524 TRACE(string
,"for %p got:\n",buffer
);
525 va_start( valist
, spec
);
526 /* Note: we call the 32-bit version, because the args are 32-bit */
527 res
= (INT16
)wvsnprintf32A( buffer
, 0xffffffff, spec
, valist
);
532 /* Emulator version */
533 INT16 WINAPIV
WIN16_wsprintf16(void)
539 VA_START16( valist
);
540 buffer
= VA_ARG16( valist
, SEGPTR
);
541 spec
= VA_ARG16( valist
, SEGPTR
);
542 TRACE(string
,"got:\n");
543 res
= wvsnprintf16( (LPSTR
)PTR_SEG_TO_LIN(buffer
), 0xffff,
544 (LPCSTR
)PTR_SEG_TO_LIN(spec
), valist
);
550 /***********************************************************************
551 * wsprintf32A (USER32.585)
553 INT32 WINAPIV
wsprintf32A( LPSTR buffer
, LPCSTR spec
, ... )
558 TRACE(string
,"for %p got:\n",buffer
);
559 va_start( valist
, spec
);
560 res
= wvsnprintf32A( buffer
, 0xffffffff, spec
, valist
);
566 /***********************************************************************
567 * wsprintf32W (USER32.586)
569 INT32 WINAPIV
wsprintf32W( LPWSTR buffer
, LPCWSTR spec
, ... )
574 TRACE(string
,"wsprintf32W for %p\n",buffer
);
575 va_start( valist
, spec
);
576 res
= wvsnprintf32W( buffer
, 0xffffffff, spec
, valist
);
582 /***********************************************************************
583 * wsnprintf16 (Not a Windows API)
585 INT16 WINAPIV
wsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
, ... )
590 va_start( valist
, spec
);
591 res
= wvsnprintf16( buffer
, maxlen
, spec
, valist
);
597 /***********************************************************************
598 * wsnprintf32A (Not a Windows API)
600 INT32 WINAPIV
wsnprintf32A( LPSTR buffer
, UINT32 maxlen
, LPCSTR spec
, ... )
605 va_start( valist
, spec
);
606 res
= wvsnprintf32A( buffer
, maxlen
, spec
, valist
);
612 /***********************************************************************
613 * wsnprintf32W (Not a Windows API)
615 INT32 WINAPIV
wsnprintf32W( LPWSTR buffer
, UINT32 maxlen
, LPCWSTR spec
, ... )
620 va_start( valist
, spec
);
621 res
= wvsnprintf32W( buffer
, maxlen
, spec
, valist
);