4 * Copyright 1996 Alexandre Julliard
10 #include "wine/winbase16.h"
14 #include "stackframe.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(string
);
20 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
21 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
22 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
23 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
24 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
25 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
26 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
56 static const CHAR null_stringA
[] = "(null)";
57 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
59 /***********************************************************************
60 * WPRINTF_ParseFormatA
62 * Parse a format specification. A format specification has the form:
64 * [-][#][0][width][.precision]type
66 * Return value is the length of the format specification in characters.
68 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
75 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
76 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
77 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
78 while ((*p
>= '0') && (*p
<= '9')) /* width field */
80 res
->width
= res
->width
* 10 + *p
- '0';
83 if (*p
== '.') /* precision field */
86 while ((*p
>= '0') && (*p
<= '9'))
88 res
->precision
= res
->precision
* 10 + *p
- '0';
92 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
93 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
94 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
98 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
101 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
105 res
->type
= WPR_SIGNED
;
108 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
111 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
114 res
->type
= WPR_UNSIGNED
;
117 res
->flags
|= WPRINTF_UPPER_HEX
;
120 res
->type
= WPR_HEXA
;
122 default: /* unknown format char */
123 res
->type
= WPR_UNKNOWN
;
124 p
--; /* print format as normal char */
127 return (INT
)(p
- format
) + 1;
131 /***********************************************************************
132 * WPRINTF_ParseFormatW
134 * Parse a format specification. A format specification has the form:
136 * [-][#][0][width][.precision]type
138 * Return value is the length of the format specification in characters.
140 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
147 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
148 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
149 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
150 while ((*p
>= '0') && (*p
<= '9')) /* width field */
152 res
->width
= res
->width
* 10 + *p
- '0';
155 if (*p
== '.') /* precision field */
158 while ((*p
>= '0') && (*p
<= '9'))
160 res
->precision
= res
->precision
* 10 + *p
- '0';
164 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
165 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
166 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
170 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
173 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
177 res
->type
= WPR_SIGNED
;
180 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
183 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
186 res
->type
= WPR_UNSIGNED
;
189 res
->flags
|= WPRINTF_UPPER_HEX
;
192 res
->type
= WPR_HEXA
;
195 res
->type
= WPR_UNKNOWN
;
196 p
--; /* print format as normal char */
199 return (INT
)(p
- format
) + 1;
203 /***********************************************************************
206 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
207 LPSTR number
, UINT maxlen
)
211 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
212 if (format
->width
> maxlen
) format
->width
= maxlen
;
217 return (format
->precision
= 1);
219 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
220 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
221 if (!*(arg
->lpcstr_view
+ len
)) break;
222 if (len
> maxlen
) len
= maxlen
;
223 return (format
->precision
= len
);
225 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
226 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
227 if (!*(arg
->lpcwstr_view
+ len
)) break;
228 if (len
> maxlen
) len
= maxlen
;
229 return (format
->precision
= len
);
231 len
= sprintf( number
, "%d", arg
->int_view
);
234 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
237 len
= sprintf( number
,
238 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
239 (UINT
)arg
->int_view
);
244 if (len
> maxlen
) len
= maxlen
;
245 if (format
->precision
< len
) format
->precision
= len
;
246 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
247 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
248 format
->precision
= format
->width
;
249 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
254 /***********************************************************************
255 * wvsnprintf16 (Not a Windows API)
257 static INT16
wvsnprintf16( LPSTR buffer
, UINT16 maxlen
, LPCSTR spec
,
260 WPRINTF_FORMAT format
;
264 WPRINTF_DATA cur_arg
;
267 while (*spec
&& (maxlen
> 1))
269 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
271 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
272 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
275 case WPR_WCHAR
: /* No Unicode in Win16 */
277 cur_arg
.char_view
= VA_ARG16( args
, CHAR
);
279 case WPR_WSTRING
: /* No Unicode in Win16 */
281 seg_str
= VA_ARG16( args
, SEGPTR
);
282 if (IsBadReadPtr16(seg_str
, 1 )) cur_arg
.lpcstr_view
= "";
283 else cur_arg
.lpcstr_view
= MapSL( seg_str
);
286 if (!(format
.flags
& WPRINTF_LONG
))
288 cur_arg
.int_view
= VA_ARG16( args
, INT16
);
294 if (format
.flags
& WPRINTF_LONG
)
295 cur_arg
.int_view
= VA_ARG16( args
, UINT
);
297 cur_arg
.int_view
= VA_ARG16( args
, UINT16
);
302 len
= WPRINTF_GetLen( &format
, &cur_arg
, number
, maxlen
- 1 );
303 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
304 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
308 case WPR_WCHAR
: /* No Unicode in Win16 */
310 *p
= cur_arg
.char_view
;
312 else if (format
.width
> 1) *p
++ = ' ';
315 case WPR_WSTRING
: /* No Unicode in Win16 */
317 if (len
) memcpy( p
, cur_arg
.lpcstr_view
, len
);
321 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
324 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
331 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
332 if (len
) memcpy( p
, number
, len
);
338 if (format
.flags
& WPRINTF_LEFTALIGN
)
339 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
344 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
348 /***********************************************************************
349 * wvsnprintfA (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
351 INT WINAPI
wvsnprintfA( LPSTR buffer
, UINT maxlen
, LPCSTR spec
, va_list args
)
353 WPRINTF_FORMAT format
;
357 WPRINTF_DATA argData
;
359 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
361 while (*spec
&& (maxlen
> 1))
363 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
365 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
366 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
371 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
374 argData
.char_view
= (CHAR
)va_arg( args
, int );
377 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
380 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
385 argData
.int_view
= va_arg( args
, INT
);
388 argData
.wchar_view
= 0;
392 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
393 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
394 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
399 *p
= argData
.wchar_view
;
401 else if (format
.width
> 1) *p
++ = ' ';
405 *p
= argData
.char_view
;
407 else if (format
.width
> 1) *p
++ = ' ';
411 memcpy( p
, argData
.lpcstr_view
, len
);
416 LPCWSTR ptr
= argData
.lpcwstr_view
;
417 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
421 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
424 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
431 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
432 memcpy( p
, number
, len
);
438 if (format
.flags
& WPRINTF_LEFTALIGN
)
439 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
444 TRACE("%s\n",debugstr_a(buffer
));
445 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
449 /***********************************************************************
450 * wvsnprintfW (USER32.@) (Not a Windows API, but we export it from USER32 anyway)
452 INT WINAPI
wvsnprintfW( LPWSTR buffer
, UINT maxlen
, LPCWSTR spec
, va_list args
)
454 WPRINTF_FORMAT format
;
458 WPRINTF_DATA argData
;
460 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
462 while (*spec
&& (maxlen
> 1))
464 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
466 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
467 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
472 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
475 argData
.char_view
= (CHAR
)va_arg( args
, int );
478 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
481 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
486 argData
.int_view
= va_arg( args
, INT
);
489 argData
.wchar_view
= 0;
493 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
494 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
495 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
500 *p
= argData
.wchar_view
;
502 else if (format
.width
> 1) *p
++ = ' ';
506 *p
= argData
.char_view
;
508 else if (format
.width
> 1) *p
++ = ' ';
513 LPCSTR ptr
= argData
.lpcstr_view
;
514 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
518 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
522 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
525 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
532 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
533 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
538 if (format
.flags
& WPRINTF_LEFTALIGN
)
539 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
544 TRACE("%s\n",debugstr_w(buffer
));
545 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
549 /***********************************************************************
550 * wvsprintf16 (USER.421)
552 INT16 WINAPI
wvsprintf16( LPSTR buffer
, LPCSTR spec
, LPCVOID args
)
556 TRACE("for %p got:\n",buffer
);
557 res
= wvsnprintf16( buffer
, 1024, spec
, args
);
558 return ( res
== -1 ) ? 1024 : res
;
562 /***********************************************************************
563 * wvsprintfA (USER32.@)
565 INT WINAPI
wvsprintfA( LPSTR buffer
, LPCSTR spec
, va_list args
)
567 INT res
= wvsnprintfA( buffer
, 1024, spec
, args
);
568 return ( res
== -1 ) ? 1024 : res
;
572 /***********************************************************************
573 * wvsprintfW (USER32.@)
575 INT WINAPI
wvsprintfW( LPWSTR buffer
, LPCWSTR spec
, va_list args
)
577 INT res
= wvsnprintfW( buffer
, 1024, spec
, args
);
578 return ( res
== -1 ) ? 1024 : res
;
582 /***********************************************************************
583 * wsprintf16 (USER.420)
585 INT16 WINAPIV
wsprintf16(void)
591 VA_START16( valist
);
592 buffer
= VA_ARG16( valist
, SEGPTR
);
593 spec
= VA_ARG16( valist
, SEGPTR
);
594 res
= wvsnprintf16( MapSL(buffer
), 1024, MapSL(spec
), valist
);
596 return ( res
== -1 ) ? 1024 : res
;
600 /***********************************************************************
601 * wsprintfA (USER32.@)
603 INT WINAPIV
wsprintfA( LPSTR buffer
, LPCSTR spec
, ... )
608 va_start( valist
, spec
);
609 res
= wvsnprintfA( buffer
, 1024, spec
, valist
);
611 return ( res
== -1 ) ? 1024 : res
;
615 /***********************************************************************
616 * wsprintfW (USER32.@)
618 INT WINAPIV
wsprintfW( LPWSTR buffer
, LPCWSTR spec
, ... )
623 va_start( valist
, spec
);
624 res
= wvsnprintfW( buffer
, 1024, spec
, valist
);
626 return ( res
== -1 ) ? 1024 : res
;