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) */
46 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
47 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
77 static const CHAR null_stringA[] = "(null)";
78 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
80 /***********************************************************************
81 * WPRINTF_ParseFormatA
83 * Parse a format specification. A format specification has the form:
85 * [-][#][0][width][.precision]type
87 * Return value is the length of the format specification in characters.
89 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
96 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
97 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
98 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
99 while ((*p >= '0') && (*p <= '9')) /* width field */
101 res->width = res->width * 10 + *p - '0';
104 if (*p == '.') /* precision field */
107 while ((*p >= '0') && (*p <= '9'))
109 res->precision = res->precision * 10 + *p - '0';
113 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
114 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
115 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
118 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
119 else if (p[1] == '3' && p[2] == '2') p += 3;
120 else { res->flags |= WPRINTF_INTPTR; p++; }
125 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
128 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
132 res->type = WPR_SIGNED;
135 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
138 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
141 res->type = WPR_UNSIGNED;
144 res->width = 2 * sizeof(void *);
145 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
148 res->flags |= WPRINTF_UPPER_HEX;
151 res->type = WPR_HEXA;
153 default: /* unknown format char */
154 res->type = WPR_UNKNOWN;
155 p--; /* print format as normal char */
158 return (INT)(p - format) + 1;
162 /***********************************************************************
163 * WPRINTF_ParseFormatW
165 * Parse a format specification. A format specification has the form:
167 * [-][#][0][width][.precision]type
169 * Return value is the length of the format specification in characters.
171 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
178 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
179 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
180 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
181 while ((*p >= '0') && (*p <= '9')) /* width field */
183 res->width = res->width * 10 + *p - '0';
186 if (*p == '.') /* precision field */
189 while ((*p >= '0') && (*p <= '9'))
191 res->precision = res->precision * 10 + *p - '0';
195 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
196 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
197 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
200 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
201 else if (p[1] == '3' && p[2] == '2') p += 3;
202 else { res->flags |= WPRINTF_INTPTR; p++; }
207 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
210 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
214 res->type = WPR_SIGNED;
217 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
220 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
223 res->type = WPR_UNSIGNED;
226 res->width = 2 * sizeof(void *);
227 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
230 res->flags |= WPRINTF_UPPER_HEX;
233 res->type = WPR_HEXA;
236 res->type = WPR_UNKNOWN;
237 p--; /* print format as normal char */
240 return (INT)(p - format) + 1;
244 /***********************************************************************
247 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
248 LPSTR number, UINT maxlen )
252 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
253 if (format->width > maxlen) format->width = maxlen;
258 return (format->precision = 1);
260 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
261 for (len = 0; !format->precision || (len < format->precision); len++)
262 if (!*(arg->lpcstr_view + len)) break;
263 if (len > maxlen) len = maxlen;
264 return (format->precision = len);
266 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
267 for (len = 0; !format->precision || (len < format->precision); len++)
268 if (!*(arg->lpcwstr_view + len)) break;
269 if (len > maxlen) len = maxlen;
270 return (format->precision = len);
275 const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
276 ULONGLONG num = arg->int_view;
277 int base = format->type == WPR_HEXA ? 16 : 10;
278 char buffer[20], *p = buffer, *dst = number;
280 if (format->type == WPR_SIGNED && arg->int_view < 0)
283 num = -arg->int_view;
285 if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
286 else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
290 *p++ = digits[num % base];
293 while (p > buffer) *dst++ = *(--p);
301 if (len > maxlen) len = maxlen;
302 if (format->precision < len) format->precision = len;
303 if (format->precision > maxlen) format->precision = maxlen;
304 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
305 format->precision = format->width;
306 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
311 /***********************************************************************
312 * wvnsprintfA (SHLWAPI.@)
314 * Print formatted output to a string, up to a maximum number of chars.
317 * buffer [O] Destination for output string
318 * maxlen [I] Maximum number of characters to write
319 * spec [I] Format string
322 * Success: The number of characters written.
325 INT WINAPI wvnsprintfA( LPSTR buffer, INT maxlen, LPCSTR spec, __ms_va_list args )
327 WPRINTF_FORMAT format;
331 WPRINTF_DATA argData;
333 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
335 while (*spec && (maxlen > 1))
337 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
339 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
340 spec += WPRINTF_ParseFormatA( spec, &format );
345 argData.wchar_view = (WCHAR)va_arg( args, int );
348 argData.char_view = (CHAR)va_arg( args, int );
351 argData.lpcstr_view = va_arg( args, LPCSTR );
354 argData.lpcwstr_view = va_arg( args, LPCWSTR );
359 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
360 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
361 else argData.int_view = va_arg(args, INT);
364 argData.wchar_view = 0;
368 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
370 if (!(format.flags & WPRINTF_LEFTALIGN))
371 for (i = format.precision; i < format.width; i++, maxlen--)
376 *p++ = argData.wchar_view;
379 *p++ = argData.char_view;
382 memcpy( p, argData.lpcstr_view, len );
387 LPCWSTR ptr = argData.lpcwstr_view;
388 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
392 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
395 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
401 /* Transfer the sign now, just in case it will be zero-padded*/
402 if (number[0] == '-')
409 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
410 memcpy( p, number + sign, len - sign );
416 if (format.flags & WPRINTF_LEFTALIGN)
417 for (i = format.precision; i < format.width; i++, maxlen--)
422 TRACE("%s\n",debugstr_a(buffer));
423 return (maxlen > 1) ? (INT)(p - buffer) : -1;
427 /***********************************************************************
428 * wvnsprintfW (SHLWAPI.@)
432 INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list args )
434 WPRINTF_FORMAT format;
438 WPRINTF_DATA argData;
440 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
442 while (*spec && (maxlen > 1))
444 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
446 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
447 spec += WPRINTF_ParseFormatW( spec, &format );
452 argData.wchar_view = (WCHAR)va_arg( args, int );
455 argData.char_view = (CHAR)va_arg( args, int );
458 argData.lpcstr_view = va_arg( args, LPCSTR );
461 argData.lpcwstr_view = va_arg( args, LPCWSTR );
466 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
467 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
468 else argData.int_view = va_arg(args, INT);
471 argData.wchar_view = 0;
475 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
477 if (!(format.flags & WPRINTF_LEFTALIGN))
478 for (i = format.precision; i < format.width; i++, maxlen--)
483 *p++ = argData.wchar_view;
486 *p++ = argData.char_view;
490 LPCSTR ptr = argData.lpcstr_view;
491 for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
495 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
499 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
502 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
508 /* Transfer the sign now, just in case it will be zero-padded*/
509 if (number[0] == '-')
516 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
517 for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
522 if (format.flags & WPRINTF_LEFTALIGN)
523 for (i = format.precision; i < format.width; i++, maxlen--)
528 TRACE("%s\n",debugstr_w(buffer));
529 return (maxlen > 1) ? (INT)(p - buffer) : -1;
533 /*************************************************************************
534 * wnsprintfA (SHLWAPI.@)
536 * Print formatted output to a string, up to a maximum number of chars.
539 * lpOut [O] Destination for output string
540 * cchLimitIn [I] Maximum number of characters to write
541 * lpFmt [I] Format string
544 * Success: The number of characters written.
547 int WINAPIV wnsprintfA(LPSTR lpOut, int cchLimitIn, LPCSTR lpFmt, ...)
552 __ms_va_start( valist, lpFmt );
553 res = wvnsprintfA( lpOut, cchLimitIn, lpFmt, valist );
554 __ms_va_end( valist );
559 /*************************************************************************
560 * wnsprintfW (SHLWAPI.@)
564 int WINAPIV wnsprintfW(LPWSTR lpOut, int cchLimitIn, LPCWSTR lpFmt, ...)
569 __ms_va_start( valist, lpFmt );
570 res = wvnsprintfW( lpOut, cchLimitIn, lpFmt, valist );
571 __ms_va_end( valist );