4 * Copyright 1996 Alexandre Julliard
7 #define NO_TRANSITION_TYPES /* This file is Win32-clean */
13 #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) */
42 /***********************************************************************
43 * WPRINTF_ParseFormatA
45 * Parse a format specification. A format specification has the form:
47 * [-][#][0][width][.precision]type
49 * Return value is the length of the format specification in characters.
51 static INT32
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
58 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
59 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
60 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
61 while ((*p
>= '0') && (*p
<= '9')) /* width field */
63 res
->width
= res
->width
* 10 + *p
- '0';
66 if (*p
== '.') /* precision field */
69 while ((*p
>= '0') && (*p
<= '9'))
71 res
->precision
= res
->precision
* 10 + *p
- '0';
75 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
76 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
80 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
83 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
87 res
->type
= WPR_SIGNED
;
90 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WSTRING
: WPR_STRING
;
93 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_STRING
: WPR_WSTRING
;
96 res
->type
= WPR_UNSIGNED
;
99 res
->flags
|= WPRINTF_UPPER_HEX
;
102 res
->type
= WPR_HEXA
;
105 fprintf( stderr
, "wvsprintf32A: unknown format '%c'\n", *p
);
108 return (INT32
)(p
- format
) + 1;
112 /***********************************************************************
113 * WPRINTF_ParseFormatW
115 * Parse a format specification. A format specification has the form:
117 * [-][#][0][width][.precision]type
119 * Return value is the length of the format specification in characters.
121 static INT32
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
128 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
129 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
130 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
131 while ((*p
>= '0') && (*p
<= '9')) /* width field */
133 res
->width
= res
->width
* 10 + *p
- '0';
136 if (*p
== '.') /* precision field */
139 while ((*p
>= '0') && (*p
<= '9'))
141 res
->precision
= res
->precision
* 10 + *p
- '0';
145 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
146 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
150 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
153 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
157 res
->type
= WPR_SIGNED
;
160 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_STRING
: WPR_WSTRING
;
163 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WSTRING
: WPR_STRING
;
166 res
->type
= WPR_UNSIGNED
;
169 res
->flags
|= WPRINTF_UPPER_HEX
;
172 res
->type
= WPR_HEXA
;
175 fprintf( stderr
, "wvsprintf32W: unknown format '%c'\n", (CHAR
)*p
);
178 return (INT32
)(p
- format
) + 1;
182 /***********************************************************************
185 static UINT32
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, LPCVOID arg
,
186 LPSTR number
, UINT32 maxlen
)
190 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
191 if (format
->width
> maxlen
) format
->width
= maxlen
;
196 return (format
->precision
= 1);
198 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
199 if (!*(*(LPCSTR
*)arg
+ len
)) break;
200 if (len
> maxlen
) len
= maxlen
;
201 return (format
->precision
= len
);
203 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
204 if (!*(*(LPCWSTR
*)arg
+ len
)) break;
205 if (len
> maxlen
) len
= maxlen
;
206 return (format
->precision
= len
);
208 len
= sprintf( number
, "%d", *(INT32
*)arg
);
211 len
= sprintf( number
, "%u", *(UINT32
*)arg
);
214 len
= sprintf( number
,
215 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
217 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
222 if (len
> maxlen
) len
= maxlen
;
223 if (format
->precision
< len
) format
->precision
= len
;
224 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
225 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
226 format
->precision
= format
->width
;
231 /***********************************************************************
232 * wvsnprintf16 (Not a Windows API)
234 INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
, LPCVOID args
)
236 WPRINTF_FORMAT format
;
242 while (*spec
&& (maxlen
> 1))
244 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
246 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
247 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
250 case WPR_WCHAR
: /* No Unicode in Win16 */
252 cur_arg
= (DWORD
)*(CHAR
*)args
;
253 args
= (WORD
*)args
+ 1;
255 case WPR_WSTRING
: /* No Unicode in Win16 */
257 if (IsBadReadPtr16( *(SEGPTR
*)args
, 1 )) cur_arg
= (DWORD
)"";
258 else cur_arg
= (DWORD
)PTR_SEG_TO_LIN( *(SEGPTR
*)args
);
259 args
= (SEGPTR
*)args
+ 1;
264 if (format
.flags
& WPRINTF_LONG
)
266 cur_arg
= (DWORD
)*(UINT32
*)args
;
267 args
= (UINT32
*)args
+ 1;
271 cur_arg
= (DWORD
)*(UINT16
*)args
;
272 args
= (UINT16
*)args
+ 1;
276 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
277 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
278 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
283 if ((*p
= (CHAR
)cur_arg
)) p
++;
284 else if (format
.width
> 1) *p
++ = ' ';
288 if (len
) memcpy( p
, (LPCSTR
)cur_arg
, len
);
292 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
295 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
298 format
.precision
-= 2;
304 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
305 if (len
) memcpy( p
, number
, len
);
310 fprintf( stderr
, "Unicode not supported in wsprintf16\n" );
313 if (format
.flags
& WPRINTF_LEFTALIGN
)
314 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
319 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
323 /***********************************************************************
324 * wvsnprintf32A (Not a Windows API)
326 INT32
wvsnprintf32A( LPSTR buffer
, UINT32 maxlen
, LPCSTR spec
, LPCVOID args
)
328 WPRINTF_FORMAT format
;
333 while (*spec
&& (maxlen
> 1))
335 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
337 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
338 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
339 len
= WPRINTF_GetLen( &format
, args
, number
, maxlen
- 1 );
340 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
341 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
346 if ((*p
= (CHAR
)*(WCHAR
*)args
)) p
++;
347 else if (format
.width
> 1) *p
++ = ' ';
351 if ((*p
= *(CHAR
*)args
)) p
++;
352 else if (format
.width
> 1) *p
++ = ' ';
356 if (len
) memcpy( p
, *(LPCSTR
*)args
, len
);
360 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*(*(LPCWSTR
*)args
+ i
);
363 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
366 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
369 format
.precision
-= 2;
375 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
376 if (len
) memcpy( p
, number
, len
);
380 if (format
.flags
& WPRINTF_LEFTALIGN
)
381 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
383 args
= (INT32
*)args
+ 1;
387 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
391 /***********************************************************************
392 * wvsnprintf32W (Not a Windows API)
394 INT32
wvsnprintf32W( LPWSTR buffer
, UINT32 maxlen
, LPCWSTR spec
, LPCVOID args
)
396 WPRINTF_FORMAT format
;
401 while (*spec
&& (maxlen
> 1))
403 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
405 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
406 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
407 len
= WPRINTF_GetLen( &format
, args
, number
, maxlen
- 1 );
408 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
409 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
414 if ((*p
= *(WCHAR
*)args
)) p
++;
415 else if (format
.width
> 1) *p
++ = ' ';
419 if ((*p
= (WCHAR
)*(CHAR
*)args
)) p
++;
420 else if (format
.width
> 1) *p
++ = ' ';
424 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*(*(LPCSTR
*)args
+ i
);
427 if (len
) memcpy( p
, *(LPCWSTR
*)args
, len
* sizeof(WCHAR
) );
431 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
434 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
437 format
.precision
-= 2;
443 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
444 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
447 if (format
.flags
& WPRINTF_LEFTALIGN
)
448 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
450 args
= (INT32
*)args
+ 1;
454 return (maxlen
> 1) ? (INT32
)(p
- buffer
) : -1;
458 /***********************************************************************
459 * wvsprintf16 (USER.421)
461 INT16
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
463 return wvsnprintf16( buffer
, 0xffff, spec
, args
);
467 /***********************************************************************
468 * wvsprintf32A (USER32.586)
470 INT32
wvsprintf32A( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
472 return wvsnprintf32A( buffer
, 0xffffffff, spec
, args
);
476 /***********************************************************************
477 * wvsprintf32W (USER32.587)
479 INT32
wvsprintf32W( LPWSTR buffer
, LPCWSTR spec
, LPCVOID args
)
481 return wvsnprintf32W( buffer
, 0xffffffff, spec
, args
);
485 /***********************************************************************
486 * wsprintf16 (USER.420)
488 /* Winelib version */
489 INT16
wsprintf16( LPSTR buffer
, LPCSTR spec
, ... )
494 va_start( valist
, spec
);
495 /* Note: we call the 32-bit version, because the args are 32-bit */
496 res
= (INT16
)wvsprintf32A( buffer
, spec
, (LPCVOID
)valist
);
501 /* Emulator version */
502 INT16
WIN16_wsprintf16(void)
504 SEGPTR
*win_stack
= (SEGPTR
*)CURRENT_STACK16
->args
;
505 LPSTR buffer
= (LPSTR
)PTR_SEG_TO_LIN(win_stack
[0]);
506 LPCSTR spec
= (LPCSTR
)PTR_SEG_TO_LIN(win_stack
[1]);
507 return wvsprintf16( buffer
, spec
, &win_stack
[2] );
512 /***********************************************************************
513 * wsprintf32A (USER32.584)
515 /* Winelib version */
516 INT32
wsprintf32A( LPSTR buffer
, LPCSTR spec
, ... )
521 va_start( valist
, spec
);
522 res
= wvsprintf32A( buffer
, spec
, (LPCVOID
)valist
);
527 /* Emulator version */
528 INT32
WIN32_wsprintf32A( int *args
)
530 return wvsprintf32A( (LPSTR
)args
[0], (LPCSTR
)args
[1], (LPCVOID
)&args
[2] );
534 /***********************************************************************
535 * wsprintf32W (USER32.585)
537 /* Winelib version */
538 INT32
wsprintf32W( LPWSTR buffer
, LPCWSTR spec
, ... )
543 va_start( valist
, spec
);
544 res
= wvsprintf32W( buffer
, spec
, (LPCVOID
)valist
);
549 /* Emulator version */
550 INT32
WIN32_wsprintf32W( int *args
)
552 return wvsprintf32W( (LPWSTR
)args
[0], (LPCWSTR
)args
[1], (LPCVOID
)&args
[2]);