4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * This code is duplicated in user32. If you change something here make sure
22 * to change it in user32 too.
31 #define NO_SHLWAPI_REG
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(string
);
39 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
40 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
41 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
42 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
43 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
44 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
45 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
75 static const CHAR null_stringA
[] = "(null)";
76 static const WCHAR null_stringW
[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
78 /***********************************************************************
79 * WPRINTF_ParseFormatA
81 * Parse a format specification. A format specification has the form:
83 * [-][#][0][width][.precision]type
85 * Return value is the length of the format specification in characters.
87 static INT
WPRINTF_ParseFormatA( LPCSTR format
, WPRINTF_FORMAT
*res
)
94 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
95 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
96 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
97 while ((*p
>= '0') && (*p
<= '9')) /* width field */
99 res
->width
= res
->width
* 10 + *p
- '0';
102 if (*p
== '.') /* precision field */
105 while ((*p
>= '0') && (*p
<= '9'))
107 res
->precision
= res
->precision
* 10 + *p
- '0';
111 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
112 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
113 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
117 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
120 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
124 res
->type
= WPR_SIGNED
;
127 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
130 res
->type
= (res
->flags
& (WPRINTF_SHORT
|WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
133 res
->type
= WPR_UNSIGNED
;
136 res
->flags
|= WPRINTF_UPPER_HEX
;
139 res
->type
= WPR_HEXA
;
141 default: /* unknown format char */
142 res
->type
= WPR_UNKNOWN
;
143 p
--; /* print format as normal char */
146 return (INT
)(p
- format
) + 1;
150 /***********************************************************************
151 * WPRINTF_ParseFormatW
153 * Parse a format specification. A format specification has the form:
155 * [-][#][0][width][.precision]type
157 * Return value is the length of the format specification in characters.
159 static INT
WPRINTF_ParseFormatW( LPCWSTR format
, WPRINTF_FORMAT
*res
)
166 if (*p
== '-') { res
->flags
|= WPRINTF_LEFTALIGN
; p
++; }
167 if (*p
== '#') { res
->flags
|= WPRINTF_PREFIX_HEX
; p
++; }
168 if (*p
== '0') { res
->flags
|= WPRINTF_ZEROPAD
; p
++; }
169 while ((*p
>= '0') && (*p
<= '9')) /* width field */
171 res
->width
= res
->width
* 10 + *p
- '0';
174 if (*p
== '.') /* precision field */
177 while ((*p
>= '0') && (*p
<= '9'))
179 res
->precision
= res
->precision
* 10 + *p
- '0';
183 if (*p
== 'l') { res
->flags
|= WPRINTF_LONG
; p
++; }
184 else if (*p
== 'h') { res
->flags
|= WPRINTF_SHORT
; p
++; }
185 else if (*p
== 'w') { res
->flags
|= WPRINTF_WIDE
; p
++; }
189 res
->type
= (res
->flags
& WPRINTF_SHORT
) ? WPR_CHAR
: WPR_WCHAR
;
192 res
->type
= (res
->flags
& WPRINTF_LONG
) ? WPR_WCHAR
: WPR_CHAR
;
196 res
->type
= WPR_SIGNED
;
199 res
->type
= ((res
->flags
& WPRINTF_SHORT
) && !(res
->flags
& WPRINTF_WIDE
)) ? WPR_STRING
: WPR_WSTRING
;
202 res
->type
= (res
->flags
& (WPRINTF_LONG
|WPRINTF_WIDE
)) ? WPR_WSTRING
: WPR_STRING
;
205 res
->type
= WPR_UNSIGNED
;
208 res
->flags
|= WPRINTF_UPPER_HEX
;
211 res
->type
= WPR_HEXA
;
214 res
->type
= WPR_UNKNOWN
;
215 p
--; /* print format as normal char */
218 return (INT
)(p
- format
) + 1;
222 /***********************************************************************
225 static UINT
WPRINTF_GetLen( WPRINTF_FORMAT
*format
, WPRINTF_DATA
*arg
,
226 LPSTR number
, UINT maxlen
)
230 if (format
->flags
& WPRINTF_LEFTALIGN
) format
->flags
&= ~WPRINTF_ZEROPAD
;
231 if (format
->width
> maxlen
) format
->width
= maxlen
;
236 return (format
->precision
= 1);
238 if (!arg
->lpcstr_view
) arg
->lpcstr_view
= null_stringA
;
239 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
240 if (!*(arg
->lpcstr_view
+ len
)) break;
241 if (len
> maxlen
) len
= maxlen
;
242 return (format
->precision
= len
);
244 if (!arg
->lpcwstr_view
) arg
->lpcwstr_view
= null_stringW
;
245 for (len
= 0; !format
->precision
|| (len
< format
->precision
); len
++)
246 if (!*(arg
->lpcwstr_view
+ len
)) break;
247 if (len
> maxlen
) len
= maxlen
;
248 return (format
->precision
= len
);
250 len
= sprintf( number
, "%d", arg
->int_view
);
253 len
= sprintf( number
, "%u", (UINT
)arg
->int_view
);
256 len
= sprintf( number
,
257 (format
->flags
& WPRINTF_UPPER_HEX
) ? "%X" : "%x",
258 (UINT
)arg
->int_view
);
263 if (len
> maxlen
) len
= maxlen
;
264 if (format
->precision
< len
) format
->precision
= len
;
265 if (format
->precision
> maxlen
) format
->precision
= maxlen
;
266 if ((format
->flags
& WPRINTF_ZEROPAD
) && (format
->width
> format
->precision
))
267 format
->precision
= format
->width
;
268 if (format
->flags
& WPRINTF_PREFIX_HEX
) len
+= 2;
273 /***********************************************************************
274 * wvnsprintfA (SHLWAPI.@)
276 * Print formatted output to a string, up to a maximum number of chars.
279 * buffer [O] Destination for output string
280 * maxlen [I] Maximum number of characters to write
281 * spec [I] Format string
284 * Success: The number of characters written.
287 INT WINAPI
wvnsprintfA( LPSTR buffer
, INT maxlen
, LPCSTR spec
, va_list args
)
289 WPRINTF_FORMAT format
;
293 WPRINTF_DATA argData
;
295 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_a(spec
));
297 while (*spec
&& (maxlen
> 1))
299 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
301 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
302 spec
+= WPRINTF_ParseFormatA( spec
, &format
);
307 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
310 argData
.char_view
= (CHAR
)va_arg( args
, int );
313 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
316 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
321 argData
.int_view
= va_arg( args
, INT
);
324 argData
.wchar_view
= 0;
328 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
330 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
331 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
336 *p
++ = argData
.wchar_view
;
339 *p
++ = argData
.char_view
;
342 memcpy( p
, argData
.lpcstr_view
, len
);
347 LPCWSTR ptr
= argData
.lpcwstr_view
;
348 for (i
= 0; i
< len
; i
++) *p
++ = (CHAR
)*ptr
++;
352 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
355 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
361 /* Transfer the sign now, just in case it will be zero-padded*/
362 if (number
[0] == '-')
369 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
370 memcpy( p
, number
+ sign
, len
- sign
);
376 if (format
.flags
& WPRINTF_LEFTALIGN
)
377 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
382 TRACE("%s\n",debugstr_a(buffer
));
383 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
387 /***********************************************************************
388 * wvnsprintfW (SHLWAPI.@)
392 INT WINAPI
wvnsprintfW( LPWSTR buffer
, INT maxlen
, LPCWSTR spec
, va_list args
)
394 WPRINTF_FORMAT format
;
398 WPRINTF_DATA argData
;
400 TRACE("%p %u %s\n", buffer
, maxlen
, debugstr_w(spec
));
402 while (*spec
&& (maxlen
> 1))
404 if (*spec
!= '%') { *p
++ = *spec
++; maxlen
--; continue; }
406 if (*spec
== '%') { *p
++ = *spec
++; maxlen
--; continue; }
407 spec
+= WPRINTF_ParseFormatW( spec
, &format
);
412 argData
.wchar_view
= (WCHAR
)va_arg( args
, int );
415 argData
.char_view
= (CHAR
)va_arg( args
, int );
418 argData
.lpcstr_view
= va_arg( args
, LPCSTR
);
421 argData
.lpcwstr_view
= va_arg( args
, LPCWSTR
);
426 argData
.int_view
= va_arg( args
, INT
);
429 argData
.wchar_view
= 0;
433 len
= WPRINTF_GetLen( &format
, &argData
, number
, maxlen
- 1 );
435 if (!(format
.flags
& WPRINTF_LEFTALIGN
))
436 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
441 *p
++ = argData
.wchar_view
;
444 *p
++ = argData
.char_view
;
448 LPCSTR ptr
= argData
.lpcstr_view
;
449 for (i
= 0; i
< len
; i
++) *p
++ = (WCHAR
)*ptr
++;
453 if (len
) memcpy( p
, argData
.lpcwstr_view
, len
* sizeof(WCHAR
) );
457 if ((format
.flags
& WPRINTF_PREFIX_HEX
) && (maxlen
> 3))
460 *p
++ = (format
.flags
& WPRINTF_UPPER_HEX
) ? 'X' : 'x';
466 /* Transfer the sign now, just in case it will be zero-padded*/
467 if (number
[0] == '-')
474 for (i
= len
; i
< format
.precision
; i
++, maxlen
--) *p
++ = '0';
475 for (i
= sign
; i
< len
; i
++) *p
++ = (WCHAR
)number
[i
];
480 if (format
.flags
& WPRINTF_LEFTALIGN
)
481 for (i
= format
.precision
; i
< format
.width
; i
++, maxlen
--)
486 TRACE("%s\n",debugstr_w(buffer
));
487 return (maxlen
> 1) ? (INT
)(p
- buffer
) : -1;
491 /*************************************************************************
492 * wnsprintfA (SHLWAPI.@)
494 * Print formatted output to a string, up to a maximum number of chars.
497 * lpOut [O] Destination for output string
498 * cchLimitIn [I] Maximum number of characters to write
499 * lpFmt [I] Format string
502 * Success: The number of characters written.
505 int WINAPIV
wnsprintfA(LPSTR lpOut
, int cchLimitIn
, LPCSTR lpFmt
, ...)
510 va_start( valist
, lpFmt
);
511 res
= wvnsprintfA( lpOut
, cchLimitIn
, lpFmt
, valist
);
517 /*************************************************************************
518 * wnsprintfW (SHLWAPI.@)
522 int WINAPIV
wnsprintfW(LPWSTR lpOut
, int cchLimitIn
, LPCWSTR lpFmt
, ...)
527 va_start( valist
, lpFmt
);
528 res
= wvnsprintfW( lpOut
, cchLimitIn
, lpFmt
, valist
);