4 * Copyright 1996 Alexandre Julliard
12 #include "stackframe.h"
14 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
15 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
16 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
17 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
18 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
19 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
41 /***********************************************************************
42 * WPRINTF_ParseFormatA
44 * Parse a format specification. A format specification has the form:
46 * [-][#][0][width][.precision]type
48 * Return value is the length of the format specification in characters.
50 static INT32
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
57 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
58 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
59 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
60 while ((*p
>= '0') && (*p
<= '9')) /* width field */
62 res
->width
= res
->width
* 10 + *p
- '0';
65 if (*p
== '.') /* precision field */
68 while ((*p
>= '0') && (*p
<= '9'))
70 res
->precision
= res
->precision
* 10 + *p
- '0';
74 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
75 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
79 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
82 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
86 res
->type
= WPR_SIGNED
;
89 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WSTRING
: WPR_STRING
;
92 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_STRING
: WPR_WSTRING
;
95 res
->type
= WPR_UNSIGNED
;
98 res
->flags
|= WPRINTF_UPPER_HEX
;
101 res
->type
= WPR_HEXA
;
104 fprintf( stderr
, "wvsprintf32A: unknown format '%c'\n", *p
);
107 return (INT32
)(p
- format
) + 1;
111 /***********************************************************************
112 * WPRINTF_ParseFormatW
114 * Parse a format specification. A format specification has the form:
116 * [-][#][0][width][.precision]type
118 * Return value is the length of the format specification in characters.
120 static INT32
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
127 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
128 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
129 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
130 while ((*p
>= '0') && (*p
<= '9')) /* width field */
132 res
->width
= res
->width
* 10 + *p
- '0';
135 if (*p
== '.') /* precision field */
138 while ((*p
>= '0') && (*p
<= '9'))
140 res
->precision
= res
->precision
* 10 + *p
- '0';
144 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
145 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
149 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
152 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
156 res
->type
= WPR_SIGNED
;
159 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_STRING
: WPR_WSTRING
;
162 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WSTRING
: WPR_STRING
;
165 res
->type
= WPR_UNSIGNED
;
168 res
->flags
|= WPRINTF_UPPER_HEX
;
171 res
->type
= WPR_HEXA
;
174 fprintf( stderr
, "wvsprintf32W: unknown format '%c'\n", (CHAR
)*p
);
177 return (INT32
)(p
- format
) + 1;
181 /***********************************************************************
184 static UINT32
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, LPCVOID arg
,
185 LPSTR number
, UINT32 maxlen
)
189 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
190 if (format
->width
> maxlen
) format
->width
= maxlen
;
195 return (format
->precision
= 1);
197 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
198 if (!*(*(LPCSTR
*)arg
+ len
)) break;
199 if (len
> maxlen
) len
= maxlen
;
200 return (format
->precision
= len
);
202 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
203 if (!*(*(LPCWSTR
*)arg
+ len
)) break;
204 if (len
> maxlen
) len
= maxlen
;
205 return (format
->precision
= len
);
207 len
= sprintf( number
, "%d", *(INT32
*)arg
);
210 len
= sprintf( number
, "%u", *(UINT32
*)arg
);
213 len
= sprintf( number
,
214 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
216 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
221 if (len
> maxlen
) len
= maxlen
;
222 if (format
->precision
< len
) format
->precision
= len
;
223 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
224 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
225 format
->precision
= format
->width
;
230 /***********************************************************************
231 * wvsnprintf16 (Not a Windows API)
233 INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
, LPCVOID args
)
235 WPRINTF_FORMAT format
;
241 while (*spec
&& (maxlen
> 1))
243 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
245 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
246 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
250 cur_arg
= (DWORD
)*(CHAR
*)args
;
251 args
= (WORD
*)args
+ 1;
254 cur_arg
= (DWORD
)PTR_SEG_TO_LIN( *(SEGPTR
*)args
);
255 args
= (SEGPTR
*)args
+ 1;
260 if (format
.flags
& WPRINTF_LONG
)
262 cur_arg
= (DWORD
)*(UINT32
*)args
;
263 args
= (UINT32
*)args
+ 1;
267 cur_arg
= (DWORD
)*(UINT16
*)args
;
268 args
= (UINT16
*)args
+ 1;
273 fprintf( stderr
, "Unicode not supported in wsprintf16\n" );
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 */
503 INT16
WIN16_wsprintf16(void)
505 SEGPTR
*win_stack
= (DWORD
*)CURRENT_STACK16
->args
;
506 LPSTR buffer
= (LPSTR
)PTR_SEG_TO_LIN(win_stack
[0]);
507 LPCSTR spec
= (LPCSTR
)PTR_SEG_TO_LIN(win_stack
[1]);
508 return wvsprintf16( buffer
, spec
, &win_stack
[2] );
514 /***********************************************************************
515 * wsprintf32A (USER32.584)
517 /* Winelib version */
518 INT32
wsprintf32A( LPSTR buffer
, LPCSTR spec
, ... )
523 va_start( valist
, spec
);
524 res
= wvsprintf32A( buffer
, spec
, (LPCVOID
)valist
);
529 /* Emulator version */
530 INT32
WIN32_wsprintf32A( int *args
)
532 return wvsprintf32A( (LPSTR
)args
[0], (LPCSTR
)args
[1], (LPCVOID
)&args
[2] );
536 /***********************************************************************
537 * wsprintf32W (USER32.585)
539 /* Winelib version */
540 INT32
wsprintf32W( LPWSTR buffer
, LPCWSTR spec
, ... )
545 va_start( valist
, spec
);
546 res
= wvsprintf32W( buffer
, spec
, (LPCVOID
)valist
);
551 /* Emulator version */
552 INT32
WIN32_wsprintf32W( int *args
)
554 return wvsprintf32W( (LPWSTR
)args
[0], (LPCWSTR
)args
[1], (LPCVOID
)&args
[2]);