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 shlwapi. If you change something here make sure
22 * to change it in shlwapi too.
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;
137 res->flags |= WPRINTF_ZEROPAD;
140 res->flags |= WPRINTF_UPPER_HEX;
143 res->type = WPR_HEXA;
145 default: /* unknown format char */
146 res->type = WPR_UNKNOWN;
147 p--; /* print format as normal char */
150 return (INT)(p - format) + 1;
154 /***********************************************************************
155 * WPRINTF_ParseFormatW
157 * Parse a format specification. A format specification has the form:
159 * [-][#][0][width][.precision]type
161 * Return value is the length of the format specification in characters.
163 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
170 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
171 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
172 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
173 while ((*p >= '0') && (*p <= '9')) /* width field */
175 res->width = res->width * 10 + *p - '0';
178 if (*p == '.') /* precision field */
181 while ((*p >= '0') && (*p <= '9'))
183 res->precision = res->precision * 10 + *p - '0';
187 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
188 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
189 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
193 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
196 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
200 res->type = WPR_SIGNED;
203 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
206 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
209 res->type = WPR_UNSIGNED;
213 res->flags |= WPRINTF_ZEROPAD;
216 res->flags |= WPRINTF_UPPER_HEX;
219 res->type = WPR_HEXA;
222 res->type = WPR_UNKNOWN;
223 p--; /* print format as normal char */
226 return (INT)(p - format) + 1;
230 /***********************************************************************
233 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
234 LPSTR number, UINT maxlen )
238 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
239 if (format->width > maxlen) format->width = maxlen;
244 return (format->precision = 1);
246 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
247 for (len = 0; !format->precision || (len < format->precision); len++)
248 if (!*(arg->lpcstr_view + len)) break;
249 if (len > maxlen) len = maxlen;
250 return (format->precision = len);
252 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
253 for (len = 0; !format->precision || (len < format->precision); len++)
254 if (!*(arg->lpcwstr_view + len)) break;
255 if (len > maxlen) len = maxlen;
256 return (format->precision = len);
258 len = sprintf( number, "%d", arg->int_view );
261 len = sprintf( number, "%u", (UINT)arg->int_view );
264 len = sprintf( number,
265 (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
266 (UINT)arg->int_view);
271 if (len > maxlen) len = maxlen;
272 if (format->precision < len) format->precision = len;
273 if (format->precision > maxlen) format->precision = maxlen;
274 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
275 format->precision = format->width;
276 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
281 /***********************************************************************
282 * wvsnprintfA (internal)
284 static INT wvsnprintfA( LPSTR buffer, UINT maxlen, LPCSTR spec, __ms_va_list args )
286 WPRINTF_FORMAT format;
290 WPRINTF_DATA argData;
292 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
294 while (*spec && (maxlen > 1))
296 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
298 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
299 spec += WPRINTF_ParseFormatA( spec, &format );
304 argData.wchar_view = (WCHAR)va_arg( args, int );
307 argData.char_view = (CHAR)va_arg( args, int );
310 argData.lpcstr_view = va_arg( args, LPCSTR );
313 argData.lpcwstr_view = va_arg( args, LPCWSTR );
318 argData.int_view = va_arg( args, INT );
321 argData.wchar_view = 0;
325 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
327 if (!(format.flags & WPRINTF_LEFTALIGN))
328 for (i = format.precision; i < format.width; i++, maxlen--)
333 *p++ = argData.wchar_view;
336 *p++ = argData.char_view;
339 memcpy( p, argData.lpcstr_view, len );
344 LPCWSTR ptr = argData.lpcwstr_view;
345 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
349 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
352 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
358 /* Transfer the sign now, just in case it will be zero-padded*/
359 if (number[0] == '-')
366 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
367 memcpy( p, number + sign, len - sign );
373 if (format.flags & WPRINTF_LEFTALIGN)
374 for (i = format.precision; i < format.width; i++, maxlen--)
379 TRACE("%s\n",debugstr_a(buffer));
380 return (maxlen > 1) ? (INT)(p - buffer) : -1;
384 /***********************************************************************
385 * wvsnprintfW (internal)
387 static INT wvsnprintfW( LPWSTR buffer, UINT maxlen, LPCWSTR spec, __ms_va_list args )
389 WPRINTF_FORMAT format;
393 WPRINTF_DATA argData;
395 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
397 while (*spec && (maxlen > 1))
399 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
401 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
402 spec += WPRINTF_ParseFormatW( spec, &format );
407 argData.wchar_view = (WCHAR)va_arg( args, int );
410 argData.char_view = (CHAR)va_arg( args, int );
413 argData.lpcstr_view = va_arg( args, LPCSTR );
416 argData.lpcwstr_view = va_arg( args, LPCWSTR );
421 argData.int_view = va_arg( args, INT );
424 argData.wchar_view = 0;
428 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
430 if (!(format.flags & WPRINTF_LEFTALIGN))
431 for (i = format.precision; i < format.width; i++, maxlen--)
436 *p++ = argData.wchar_view;
439 *p++ = argData.char_view;
443 LPCSTR ptr = argData.lpcstr_view;
444 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
448 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
452 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
455 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
461 /* Transfer the sign now, just in case it will be zero-padded*/
462 if (number[0] == '-')
469 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
470 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
475 if (format.flags & WPRINTF_LEFTALIGN)
476 for (i = format.precision; i < format.width; i++, maxlen--)
481 TRACE("%s\n",debugstr_w(buffer));
482 return (maxlen > 1) ? (INT)(p - buffer) : -1;
486 /***********************************************************************
487 * wvsprintfA (USER32.@)
489 INT WINAPI wvsprintfA( LPSTR buffer, LPCSTR spec, __ms_va_list args )
491 INT res = wvsnprintfA( buffer, 1024, spec, args );
492 return ( res == -1 ) ? 1024 : res;
496 /***********************************************************************
497 * wvsprintfW (USER32.@)
499 INT WINAPI wvsprintfW( LPWSTR buffer, LPCWSTR spec, __ms_va_list args )
501 INT res = wvsnprintfW( buffer, 1024, spec, args );
502 return ( res == -1 ) ? 1024 : res;
506 /***********************************************************************
507 * wsprintfA (USER32.@)
509 INT WINAPIV wsprintfA( LPSTR buffer, LPCSTR spec, ... )
514 __ms_va_start( valist, spec );
515 res = wvsnprintfA( buffer, 1024, spec, valist );
516 __ms_va_end( valist );
517 return ( res == -1 ) ? 1024 : res;
521 /***********************************************************************
522 * wsprintfW (USER32.@)
524 INT WINAPIV wsprintfW( LPWSTR buffer, LPCWSTR spec, ... )
529 __ms_va_start( valist, spec );
530 res = wvsnprintfW( buffer, 1024, spec, valist );
531 __ms_va_end( valist );
532 return ( res == -1 ) ? 1024 : res;