3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/swprintf.c
6 * PURPOSE: unicode sprintf functions
7 * PROGRAMMERS: David Welch
11 * - Verify the implementation of '%Z'.
15 * linux/lib/vsprintf.c
17 * Copyright (C) 1991, 1992 Linus Torvalds
20 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
22 * Wirzenius wrote this portably, Torvalds fucked it up :-)
25 #include <ddk/ntddk.h>
33 #include <internal/debug.h>
36 #define ZEROPAD 1 /* pad with zero */
37 #define SIGN 2 /* unsigned/signed long */
38 #define PLUS 4 /* show plus */
39 #define SPACE 8 /* space if plus */
40 #define LEFT 16 /* left justified */
41 #define SPECIAL 32 /* 0x */
42 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
45 #define do_div(n,base) ({ \
47 __res = ((unsigned long long) n) % (unsigned) base; \
48 n = ((unsigned long long) n) / (unsigned) base; \
52 static int skip_atoi(const wchar_t **s)
57 i = i*10 + *((*s)++) - L'0';
63 number(wchar_t * buf, wchar_t * end, long long num, int base, int size, int precision, int type)
65 wchar_t c,sign, tmp[66];
66 const wchar_t *digits;
67 const wchar_t *small_digits = L"0123456789abcdefghijklmnopqrstuvwxyz";
68 const wchar_t *large_digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
71 digits = (type & LARGE) ? large_digits : small_digits;
74 if (base < 2 || base > 36)
76 c = (type & ZEROPAD) ? L'0' : L' ';
83 } else if (type & PLUS) {
86 } else if (type & SPACE) {
100 else while (num != 0)
101 tmp[i++] = digits[do_div(num,base)];
105 if (!(type&(ZEROPAD+LEFT))) {
117 if (type & SPECIAL) {
122 } else if (base==16) {
131 if (!(type & LEFT)) {
138 while (i < precision--) {
157 string(wchar_t* buf, wchar_t* end, const char* s, int len, int field_width, int precision, int flags)
170 while (s[len] && (unsigned int)len < (unsigned int)precision)
175 if ((unsigned int)len > (unsigned int)precision)
180 while (len < field_width--)
186 for (i = 0; i < len; ++i)
192 while (len < field_width--)
202 stringw(wchar_t* buf, wchar_t* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
215 while (sw[len] && (unsigned int)len < (unsigned int)precision)
220 if ((unsigned int)len > (unsigned int)precision)
225 while (len < field_width--)
231 for (i = 0; i < len; ++i)
237 while (len < field_width--)
246 int _vsnwprintf(wchar_t *buf, size_t cnt, const wchar_t *fmt, va_list args)
249 unsigned long long num;
251 wchar_t * str, * end;
255 int flags; /* flags to number() */
257 int field_width; /* width of output field */
258 int precision; /* min. # of digits for integers; max
259 number of chars for from string */
260 int qualifier; /* 'h', 'l', 'L', 'w' or 'I' for integer fields */
269 for ( ; *fmt ; ++fmt) {
280 ++fmt; /* this also skips first '%' */
282 case L'-': flags |= LEFT; goto repeat;
283 case L'+': flags |= PLUS; goto repeat;
284 case L' ': flags |= SPACE; goto repeat;
285 case L'#': flags |= SPECIAL; goto repeat;
286 case L'0': flags |= ZEROPAD; goto repeat;
289 /* get field width */
292 field_width = skip_atoi(&fmt);
293 else if (*fmt == L'*') {
295 /* it's the next argument */
296 field_width = va_arg(args, int);
297 if (field_width < 0) {
298 field_width = -field_width;
303 /* get the precision */
308 precision = skip_atoi(&fmt);
309 else if (*fmt == L'*') {
311 /* it's the next argument */
312 precision = va_arg(args, int);
318 /* get the conversion qualifier */
320 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
323 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
334 while (--field_width > 0) {
339 if (qualifier == 'h') {
341 *str = (wchar_t) va_arg(args, int);
345 *str = (wchar_t) va_arg(args, int);
348 while (--field_width > 0) {
357 while (--field_width > 0) {
362 if (qualifier == 'l' || qualifier == 'w') {
364 *str = (wchar_t) va_arg(args, int);
368 *str = (wchar_t) va_arg(args, int);
371 while (--field_width > 0) {
379 if (qualifier == 'h') {
380 /* print ascii string */
381 s = va_arg(args, char *);
382 str = string(str, end, s, -1, field_width, precision, flags);
384 /* print unicode string */
385 sw = va_arg(args, wchar_t *);
386 str = stringw(str, end, sw, -1, field_width, precision, flags);
391 if (qualifier == 'l' || qualifier == 'w') {
392 /* print unicode string */
393 sw = va_arg(args, wchar_t *);
394 str = stringw(str, end, sw, -1, field_width, precision, flags);
396 /* print ascii string */
397 s = va_arg(args, char *);
398 str = string(str, end, s, -1, field_width, precision, flags);
403 if (qualifier == 'h') {
404 /* print counted ascii string */
405 PANSI_STRING pus = va_arg(args, PANSI_STRING);
406 if ((pus == NULL) || (pus->Buffer == NULL)) {
413 str = string(str, end, s, len, field_width, precision, flags);
415 /* print counted unicode string */
416 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
417 if ((pus == NULL) || (pus->Buffer == NULL)) {
422 len = pus->Length / sizeof(WCHAR);
424 str = stringw(str, end, sw, len, field_width, precision, flags);
429 if (field_width == -1) {
430 field_width = 2*sizeof(void *);
433 str = number(str, end,
434 (unsigned long) va_arg(args, void *), 16,
435 field_width, precision, flags);
439 /* FIXME: What does C99 say about the overflow case here? */
440 if (qualifier == 'l') {
441 long * ip = va_arg(args, long *);
444 int * ip = va_arg(args, int *);
449 /* integer number formats - set up the flags and "break" */
485 if (qualifier == 'I')
486 num = va_arg(args, unsigned long long);
487 else if (qualifier == 'l')
488 num = va_arg(args, unsigned long);
489 else if (qualifier == 'h') {
491 num = va_arg(args, int);
493 num = va_arg(args, unsigned int);
497 num = va_arg(args, int);
499 num = va_arg(args, unsigned int);
501 str = number(str, end, num, base, field_width, precision, flags);
506 /* don't write out a null byte if the buf size is zero */
515 int swprintf(wchar_t *buf, const wchar_t *fmt, ...)
521 i=_vsnwprintf(buf,INT_MAX,fmt,args);
530 int _snwprintf(wchar_t *buf, size_t cnt, const wchar_t *fmt, ...)
536 i=_vsnwprintf(buf,cnt,fmt,args);
542 int vswprintf(wchar_t *buf, const wchar_t *fmt, va_list args)
544 return _vsnwprintf(buf,INT_MAX,fmt,args);