Create this branch to work on loading of different Kernel-Debugger DLL providers...
[reactos.git] / dll / win32 / shlwapi / wsprintf.c
1 /*
2 * wsprintf functions
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 *
20 * NOTE:
21 * This code is duplicated in user32. If you change something here make sure
22 * to change it in user32 too.
23 */
24
25 #define WIN32_NO_STATUS
26
27 //#include <stdarg.h>
28 //#include <string.h>
29 //#include <stdio.h>
30
31 //#include "windef.h"
32 //#include "winbase.h"
33 #define NO_SHLWAPI_REG
34 //#include "shlwapi.h"
35
36 #include <wine/debug.h>
37
38 WINE_DEFAULT_DEBUG_CHANNEL(string);
39
40
41 #define WPRINTF_LEFTALIGN 0x0001 /* Align output on the left ('-' prefix) */
42 #define WPRINTF_PREFIX_HEX 0x0002 /* Prefix hex with 0x ('#' prefix) */
43 #define WPRINTF_ZEROPAD 0x0004 /* Pad with zeros ('0' prefix) */
44 #define WPRINTF_LONG 0x0008 /* Long arg ('l' prefix) */
45 #define WPRINTF_SHORT 0x0010 /* Short arg ('h' prefix) */
46 #define WPRINTF_UPPER_HEX 0x0020 /* Upper-case hex ('X' specifier) */
47 #define WPRINTF_WIDE 0x0040 /* Wide arg ('w' prefix) */
48 #define WPRINTF_INTPTR 0x0080 /* Pointer-size arg ('I' prefix) */
49 #define WPRINTF_I64 0x0100 /* 64-bit arg ('I64' prefix) */
50
51 typedef enum
52 {
53 WPR_UNKNOWN,
54 WPR_CHAR,
55 WPR_WCHAR,
56 WPR_STRING,
57 WPR_WSTRING,
58 WPR_SIGNED,
59 WPR_UNSIGNED,
60 WPR_HEXA
61 } WPRINTF_TYPE;
62
63 typedef struct
64 {
65 UINT flags;
66 UINT width;
67 UINT precision;
68 WPRINTF_TYPE type;
69 } WPRINTF_FORMAT;
70
71 typedef union {
72 WCHAR wchar_view;
73 CHAR char_view;
74 LPCSTR lpcstr_view;
75 LPCWSTR lpcwstr_view;
76 LONGLONG int_view;
77 } WPRINTF_DATA;
78
79 static const CHAR null_stringA[] = "(null)";
80 static const WCHAR null_stringW[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
81
82 /***********************************************************************
83 * WPRINTF_ParseFormatA
84 *
85 * Parse a format specification. A format specification has the form:
86 *
87 * [-][#][0][width][.precision]type
88 *
89 * Return value is the length of the format specification in characters.
90 */
91 static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
92 {
93 LPCSTR p = format;
94
95 res->flags = 0;
96 res->width = 0;
97 res->precision = 0;
98 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
99 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
100 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
101 while ((*p >= '0') && (*p <= '9')) /* width field */
102 {
103 res->width = res->width * 10 + *p - '0';
104 p++;
105 }
106 if (*p == '.') /* precision field */
107 {
108 p++;
109 while ((*p >= '0') && (*p <= '9'))
110 {
111 res->precision = res->precision * 10 + *p - '0';
112 p++;
113 }
114 }
115 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
116 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
117 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
118 else if (*p == 'I')
119 {
120 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
121 else if (p[1] == '3' && p[2] == '2') p += 3;
122 else { res->flags |= WPRINTF_INTPTR; p++; }
123 }
124 switch(*p)
125 {
126 case 'c':
127 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
128 break;
129 case 'C':
130 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
131 break;
132 case 'd':
133 case 'i':
134 res->type = WPR_SIGNED;
135 break;
136 case 's':
137 res->type = (res->flags & (WPRINTF_LONG |WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
138 break;
139 case 'S':
140 res->type = (res->flags & (WPRINTF_SHORT|WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
141 break;
142 case 'u':
143 res->type = WPR_UNSIGNED;
144 break;
145 case 'p':
146 res->width = 2 * sizeof(void *);
147 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
148 /* fall through */
149 case 'X':
150 res->flags |= WPRINTF_UPPER_HEX;
151 /* fall through */
152 case 'x':
153 res->type = WPR_HEXA;
154 break;
155 default: /* unknown format char */
156 res->type = WPR_UNKNOWN;
157 p--; /* print format as normal char */
158 break;
159 }
160 return (INT)(p - format) + 1;
161 }
162
163
164 /***********************************************************************
165 * WPRINTF_ParseFormatW
166 *
167 * Parse a format specification. A format specification has the form:
168 *
169 * [-][#][0][width][.precision]type
170 *
171 * Return value is the length of the format specification in characters.
172 */
173 static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
174 {
175 LPCWSTR p = format;
176
177 res->flags = 0;
178 res->width = 0;
179 res->precision = 0;
180 if (*p == '-') { res->flags |= WPRINTF_LEFTALIGN; p++; }
181 if (*p == '#') { res->flags |= WPRINTF_PREFIX_HEX; p++; }
182 if (*p == '0') { res->flags |= WPRINTF_ZEROPAD; p++; }
183 while ((*p >= '0') && (*p <= '9')) /* width field */
184 {
185 res->width = res->width * 10 + *p - '0';
186 p++;
187 }
188 if (*p == '.') /* precision field */
189 {
190 p++;
191 while ((*p >= '0') && (*p <= '9'))
192 {
193 res->precision = res->precision * 10 + *p - '0';
194 p++;
195 }
196 }
197 if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
198 else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
199 else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
200 else if (*p == 'I')
201 {
202 if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
203 else if (p[1] == '3' && p[2] == '2') p += 3;
204 else { res->flags |= WPRINTF_INTPTR; p++; }
205 }
206 switch(*p)
207 {
208 case 'c':
209 res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
210 break;
211 case 'C':
212 res->type = (res->flags & WPRINTF_LONG) ? WPR_WCHAR : WPR_CHAR;
213 break;
214 case 'd':
215 case 'i':
216 res->type = WPR_SIGNED;
217 break;
218 case 's':
219 res->type = ((res->flags & WPRINTF_SHORT) && !(res->flags & WPRINTF_WIDE)) ? WPR_STRING : WPR_WSTRING;
220 break;
221 case 'S':
222 res->type = (res->flags & (WPRINTF_LONG|WPRINTF_WIDE)) ? WPR_WSTRING : WPR_STRING;
223 break;
224 case 'u':
225 res->type = WPR_UNSIGNED;
226 break;
227 case 'p':
228 res->width = 2 * sizeof(void *);
229 res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
230 /* fall through */
231 case 'X':
232 res->flags |= WPRINTF_UPPER_HEX;
233 /* fall through */
234 case 'x':
235 res->type = WPR_HEXA;
236 break;
237 default:
238 res->type = WPR_UNKNOWN;
239 p--; /* print format as normal char */
240 break;
241 }
242 return (INT)(p - format) + 1;
243 }
244
245
246 /***********************************************************************
247 * WPRINTF_GetLen
248 */
249 static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
250 LPSTR number, UINT maxlen )
251 {
252 UINT len;
253
254 if (format->flags & WPRINTF_LEFTALIGN) format->flags &= ~WPRINTF_ZEROPAD;
255 if (format->width > maxlen) format->width = maxlen;
256 switch(format->type)
257 {
258 case WPR_CHAR:
259 case WPR_WCHAR:
260 return (format->precision = 1);
261 case WPR_STRING:
262 if (!arg->lpcstr_view) arg->lpcstr_view = null_stringA;
263 for (len = 0; !format->precision || (len < format->precision); len++)
264 if (!*(arg->lpcstr_view + len)) break;
265 if (len > maxlen) len = maxlen;
266 return (format->precision = len);
267 case WPR_WSTRING:
268 if (!arg->lpcwstr_view) arg->lpcwstr_view = null_stringW;
269 for (len = 0; !format->precision || (len < format->precision); len++)
270 if (!*(arg->lpcwstr_view + len)) break;
271 if (len > maxlen) len = maxlen;
272 return (format->precision = len);
273 case WPR_SIGNED:
274 case WPR_UNSIGNED:
275 case WPR_HEXA:
276 {
277 const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
278 ULONGLONG num = arg->int_view;
279 int base = format->type == WPR_HEXA ? 16 : 10;
280 char buffer[20], *p = buffer, *dst = number;
281
282 if (format->type == WPR_SIGNED && arg->int_view < 0)
283 {
284 *dst++ = '-';
285 num = -arg->int_view;
286 }
287 if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
288 else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
289
290 do
291 {
292 *p++ = digits[num % base];
293 num /= base;
294 } while (num);
295 while (p > buffer) *dst++ = *(--p);
296 *dst = 0;
297 len = dst - number;
298 break;
299 }
300 default:
301 return 0;
302 }
303 if (len > maxlen) len = maxlen;
304 if (format->precision < len) format->precision = len;
305 if (format->precision > maxlen) format->precision = maxlen;
306 if ((format->flags & WPRINTF_ZEROPAD) && (format->width > format->precision))
307 format->precision = format->width;
308 if (format->flags & WPRINTF_PREFIX_HEX) len += 2;
309 return len;
310 }
311
312
313 /***********************************************************************
314 * wvnsprintfA (SHLWAPI.@)
315 *
316 * Print formatted output to a string, up to a maximum number of chars.
317 *
318 * PARAMS
319 * buffer [O] Destination for output string
320 * maxlen [I] Maximum number of characters to write
321 * spec [I] Format string
322 *
323 * RETURNS
324 * Success: The number of characters written.
325 * Failure: -1.
326 */
327 INT WINAPI wvnsprintfA( LPSTR buffer, INT maxlen, LPCSTR spec, __ms_va_list args )
328 {
329 WPRINTF_FORMAT format;
330 LPSTR p = buffer;
331 UINT i, len, sign;
332 CHAR number[20];
333 WPRINTF_DATA argData;
334
335 TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
336
337 while (*spec && (maxlen > 1))
338 {
339 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
340 spec++;
341 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
342 spec += WPRINTF_ParseFormatA( spec, &format );
343
344 switch(format.type)
345 {
346 case WPR_WCHAR:
347 argData.wchar_view = (WCHAR)va_arg( args, int );
348 break;
349 case WPR_CHAR:
350 argData.char_view = (CHAR)va_arg( args, int );
351 break;
352 case WPR_STRING:
353 argData.lpcstr_view = va_arg( args, LPCSTR );
354 break;
355 case WPR_WSTRING:
356 argData.lpcwstr_view = va_arg( args, LPCWSTR );
357 break;
358 case WPR_HEXA:
359 case WPR_SIGNED:
360 case WPR_UNSIGNED:
361 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
362 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
363 else argData.int_view = va_arg(args, INT);
364 break;
365 default:
366 argData.wchar_view = 0;
367 break;
368 }
369
370 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
371 sign = 0;
372 if (!(format.flags & WPRINTF_LEFTALIGN))
373 for (i = format.precision; i < format.width; i++, maxlen--)
374 *p++ = ' ';
375 switch(format.type)
376 {
377 case WPR_WCHAR:
378 *p++ = argData.wchar_view;
379 break;
380 case WPR_CHAR:
381 *p++ = argData.char_view;
382 break;
383 case WPR_STRING:
384 memcpy( p, argData.lpcstr_view, len );
385 p += len;
386 break;
387 case WPR_WSTRING:
388 {
389 LPCWSTR ptr = argData.lpcwstr_view;
390 for (i = 0; i < len; i++) *p++ = (CHAR)*ptr++;
391 }
392 break;
393 case WPR_HEXA:
394 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
395 {
396 *p++ = '0';
397 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
398 maxlen -= 2;
399 len -= 2;
400 }
401 /* fall through */
402 case WPR_SIGNED:
403 /* Transfer the sign now, just in case it will be zero-padded*/
404 if (number[0] == '-')
405 {
406 *p++ = '-';
407 sign = 1;
408 }
409 /* fall through */
410 case WPR_UNSIGNED:
411 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
412 memcpy( p, number + sign, len - sign );
413 p += len - sign;
414 break;
415 case WPR_UNKNOWN:
416 continue;
417 }
418 if (format.flags & WPRINTF_LEFTALIGN)
419 for (i = format.precision; i < format.width; i++, maxlen--)
420 *p++ = ' ';
421 maxlen -= len;
422 }
423 *p = 0;
424 TRACE("%s\n",debugstr_a(buffer));
425 return (maxlen > 1) ? (INT)(p - buffer) : -1;
426 }
427
428
429 /***********************************************************************
430 * wvnsprintfW (SHLWAPI.@)
431 *
432 * See wvnsprintfA.
433 */
434 INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list args )
435 {
436 WPRINTF_FORMAT format;
437 LPWSTR p = buffer;
438 UINT i, len, sign;
439 CHAR number[20];
440 WPRINTF_DATA argData;
441
442 TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
443
444 while (*spec && (maxlen > 1))
445 {
446 if (*spec != '%') { *p++ = *spec++; maxlen--; continue; }
447 spec++;
448 if (*spec == '%') { *p++ = *spec++; maxlen--; continue; }
449 spec += WPRINTF_ParseFormatW( spec, &format );
450
451 switch(format.type)
452 {
453 case WPR_WCHAR:
454 argData.wchar_view = (WCHAR)va_arg( args, int );
455 break;
456 case WPR_CHAR:
457 argData.char_view = (CHAR)va_arg( args, int );
458 break;
459 case WPR_STRING:
460 argData.lpcstr_view = va_arg( args, LPCSTR );
461 break;
462 case WPR_WSTRING:
463 argData.lpcwstr_view = va_arg( args, LPCWSTR );
464 break;
465 case WPR_HEXA:
466 case WPR_SIGNED:
467 case WPR_UNSIGNED:
468 if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
469 else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
470 else argData.int_view = va_arg(args, INT);
471 break;
472 default:
473 argData.wchar_view = 0;
474 break;
475 }
476
477 len = WPRINTF_GetLen( &format, &argData, number, maxlen - 1 );
478 sign = 0;
479 if (!(format.flags & WPRINTF_LEFTALIGN))
480 for (i = format.precision; i < format.width; i++, maxlen--)
481 *p++ = ' ';
482 switch(format.type)
483 {
484 case WPR_WCHAR:
485 *p++ = argData.wchar_view;
486 break;
487 case WPR_CHAR:
488 *p++ = argData.char_view;
489 break;
490 case WPR_STRING:
491 {
492 LPCSTR ptr = argData.lpcstr_view;
493 for (i = 0; i < len; i++) *p++ = (BYTE)*ptr++;
494 }
495 break;
496 case WPR_WSTRING:
497 if (len) memcpy( p, argData.lpcwstr_view, len * sizeof(WCHAR) );
498 p += len;
499 break;
500 case WPR_HEXA:
501 if ((format.flags & WPRINTF_PREFIX_HEX) && (maxlen > 3))
502 {
503 *p++ = '0';
504 *p++ = (format.flags & WPRINTF_UPPER_HEX) ? 'X' : 'x';
505 maxlen -= 2;
506 len -= 2;
507 }
508 /* fall through */
509 case WPR_SIGNED:
510 /* Transfer the sign now, just in case it will be zero-padded*/
511 if (number[0] == '-')
512 {
513 *p++ = '-';
514 sign = 1;
515 }
516 /* fall through */
517 case WPR_UNSIGNED:
518 for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
519 for (i = sign; i < len; i++) *p++ = (BYTE)number[i];
520 break;
521 case WPR_UNKNOWN:
522 continue;
523 }
524 if (format.flags & WPRINTF_LEFTALIGN)
525 for (i = format.precision; i < format.width; i++, maxlen--)
526 *p++ = ' ';
527 maxlen -= len;
528 }
529 *p = 0;
530 TRACE("%s\n",debugstr_w(buffer));
531 return (maxlen > 1) ? (INT)(p - buffer) : -1;
532 }
533
534
535 /*************************************************************************
536 * wnsprintfA (SHLWAPI.@)
537 *
538 * Print formatted output to a string, up to a maximum number of chars.
539 *
540 * PARAMS
541 * lpOut [O] Destination for output string
542 * cchLimitIn [I] Maximum number of characters to write
543 * lpFmt [I] Format string
544 *
545 * RETURNS
546 * Success: The number of characters written.
547 * Failure: -1.
548 */
549 int WINAPIV wnsprintfA(LPSTR lpOut, int cchLimitIn, LPCSTR lpFmt, ...)
550 {
551 __ms_va_list valist;
552 INT res;
553
554 __ms_va_start( valist, lpFmt );
555 res = wvnsprintfA( lpOut, cchLimitIn, lpFmt, valist );
556 __ms_va_end( valist );
557 return res;
558 }
559
560
561 /*************************************************************************
562 * wnsprintfW (SHLWAPI.@)
563 *
564 * See wnsprintfA.
565 */
566 int WINAPIV wnsprintfW(LPWSTR lpOut, int cchLimitIn, LPCWSTR lpFmt, ...)
567 {
568 __ms_va_list valist;
569 INT res;
570
571 __ms_va_start( valist, lpFmt );
572 res = wvnsprintfW( lpOut, cchLimitIn, lpFmt, valist );
573 __ms_va_end( valist );
574 return res;
575 }