Sync trunk up to r50477.

svn path=/branches/cmake-bringup/; revision=50504
This commit is contained in:
Sylvain Petreolle 2011-01-26 22:19:12 +00:00
commit e2b7eacdd5
162 changed files with 4613 additions and 6228 deletions

View file

@ -50,9 +50,7 @@ list(APPEND SOURCE
security.c
slist.c
sid.c
sprintf.c
srw.c
swprintf.c
splaytree.c
thread.c
time.c

View file

@ -238,7 +238,7 @@ RtlpInsertFreeBlockHelper(PHEAP Heap,
/* Check if PreviousSize of the next entry matches ours */
if (!(FreeEntry->Flags & HEAP_ENTRY_LAST_ENTRY))
{
ASSERT(((PHEAP_ENTRY)FreeEntry + BlockSize)->PreviousSize = BlockSize);
ASSERT(((PHEAP_ENTRY)FreeEntry + BlockSize)->PreviousSize == BlockSize);
}
/* Insert it either into dedicated or non-dedicated list */

View file

@ -1,747 +0,0 @@
#ifndef USE_NEW_SPRINTF
/*
* PROGRAMMERS: David Welch
* Eric Kohl
*
* TODO:
* - Verify the implementation of '%Z'.
*/
/*
* linux/lib/vsprintf.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
#include <rtl.h>
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
#define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
typedef struct {
unsigned int mantissal:32;
unsigned int mantissah:20;
unsigned int exponent:11;
unsigned int sign:1;
} double_t;
static
__inline
int
_isinf(double __x)
{
union
{
double* __x;
double_t* x;
} x;
x.__x = &__x;
return ( x.x->exponent == 0x7ff && ( x.x->mantissah == 0 && x.x->mantissal == 0 ));
}
static
__inline
int
_isnan(double __x)
{
union
{
double* __x;
double_t* x;
} x;
x.__x = &__x;
return ( x.x->exponent == 0x7ff && ( x.x->mantissah != 0 || x.x->mantissal != 0 ));
}
static
__inline
int
do_div(long long *n, int base)
{
int a;
a = ((unsigned long long) *n) % (unsigned) base;
*n = ((unsigned long long) *n) / (unsigned) base;
return a;
}
static int skip_atoi(const char **s)
{
int i=0;
while (isdigit(**s))
i = i*10 + *((*s)++) - '0';
return i;
}
static char *
number(char * buf, char * end, long long num, int base, int size, int precision, int type)
{
char c,sign,tmp[66];
const char *digits;
const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
digits = (type & LARGE) ? large_digits : small_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base == 16)
size -= 2;
}
i = 0;
if ((num == 0) && (precision !=0))
tmp[i++] = '0';
else while (num != 0)
tmp[i++] = digits[do_div(&num,base)];
if (i > precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT))) {
while(size-->0) {
if (buf <= end)
*buf = ' ';
++buf;
}
}
if (sign) {
if (buf <= end)
*buf = sign;
++buf;
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base==16) {
if (buf <= end)
*buf = '0';
++buf;
if (buf <= end)
*buf = digits[33];
++buf;
}
}
if (!(type & LEFT)) {
while (size-- > 0) {
if (buf <= end)
*buf = c;
++buf;
}
}
while (i < precision--) {
if (buf <= end)
*buf = '0';
++buf;
}
while (i-- > 0) {
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0) {
if (buf <= end)
*buf = ' ';
++buf;
}
return buf;
}
static char *
numberf(char * buf, char * end, double num, int base, int size, int precision, int type)
{
char c,sign,tmp[66];
const char *digits;
const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
long long x;
/* FIXME
the float version of number is direcly copy of number
*/
digits = (type & LARGE) ? large_digits : small_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
tmp[i++] = '0';
else while (num != 0)
{
x = num;
tmp[i++] = digits[do_div(&x,base)];
#ifndef _M_ARM // Missing __floatdidf in CeGCC 0.55 -- GCC 4.4
num=x;
#endif
}
if (i > precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT))) {
while(size-->0) {
if (buf <= end)
*buf = ' ';
++buf;
}
}
if (sign) {
if (buf <= end)
*buf = sign;
++buf;
}
if (type & SPECIAL) {
if (base==8) {
if (buf <= end)
*buf = '0';
++buf;
} else if (base==16) {
if (buf <= end)
*buf = '0';
++buf;
if (buf <= end)
*buf = digits[33];
++buf;
}
}
if (!(type & LEFT)) {
while (size-- > 0) {
if (buf <= end)
*buf = c;
++buf;
}
}
while (i < precision--) {
if (buf <= end)
*buf = '0';
++buf;
}
while (i-- > 0) {
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0) {
if (buf <= end)
*buf = ' ';
++buf;
}
return buf;
}
static char*
string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
{
int i;
char c;
c = (flags & ZEROPAD) ? '0' : ' ';
if (s == NULL)
{
s = "<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && s[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
++buf;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = *s++;
++buf;
}
while (len < field_width--)
{
if (buf <= end)
*buf = ' ';
++buf;
}
return buf;
}
static char*
stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
{
int i;
char c;
c = (flags & ZEROPAD) ? '0' : ' ';
if (sw == NULL)
{
sw = L"<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && sw[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
buf++;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = (unsigned char)(*sw++);
buf++;
}
while (len < field_width--)
{
if (buf <= end)
*buf = ' ';
buf++;
}
return buf;
}
/*
* @implemented
*/
int __cdecl _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
{
int len;
unsigned long long num;
double _double;
int base;
char *str, *end;
const char *s;
const wchar_t *sw;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
/* clear the string buffer with zero so we do not need NULL terment it at end */
str = buf;
end = buf + cnt - 1;
if (end < buf - 1) {
end = ((char *) -1);
cnt = end - buf + 1;
}
for ( ; *fmt ; ++fmt) {
if (*fmt != '%') {
if (str <= end)
*str = *fmt;
++str;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-': flags |= LEFT; goto repeat;
case '+': flags |= PLUS; goto repeat;
case ' ': flags |= SPACE; goto repeat;
case '#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat;
}
/* get field width */
field_width = -1;
if (isdigit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
field_width = va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (isdigit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
precision = va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'l' && *(fmt+1) == 'l') {
qualifier = 'I';
fmt += 2;
} else if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
qualifier = *fmt;
++fmt;
} else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
qualifier = *fmt;
fmt += 3;
} else if (*fmt == 'I' && *(fmt+1) == '3' && *(fmt+2) == '2') {
qualifier = 'l';
fmt += 3;
} else if (*fmt == 'F' && *(fmt+1) == 'p') {
fmt += 1;
flags |= REMOVEHEX;
}
/* default base */
base = 10;
switch (*fmt) {
case 'c': /* finished */
if (qualifier == 'l' || qualifier == 'w') {
wchar_t sw1[2];
/* print unicode string */
sw1[0] = (wchar_t) va_arg(args, int);
sw1[1] = 0;
str = stringw(str, end, (wchar_t *)&sw1, -1, field_width, precision, flags);
} else {
char s1[2];
/* print ascii string */
s1[0] = ( unsigned char) va_arg(args, int);
s1[1] = 0;
str = string(str, end, (char *)&s1, -1, field_width, precision, flags);
}
continue;
case 'C': /* finished */
if (!(flags & LEFT))
while (--field_width > 0) {
if (str <= end)
*str = ' ';
++str;
}
if (qualifier == 'h') {
if (str <= end)
*str = (unsigned char) va_arg(args, int);
++str;
} else {
if (str <= end)
*str = (unsigned char)(wchar_t) va_arg(args, int);
++str;
}
while (--field_width > 0) {
if (str <= end)
*str = ' ';
++str;
}
continue;
case 's': /* finished */
if (qualifier == 'l' || qualifier == 'w') {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
} else {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
}
continue;
case 'S':
if (qualifier == 'h') {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
} else {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
}
continue;
case 'Z':
if (qualifier == 'w') {
/* print counted unicode string */
PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
sw = NULL;
len = -1;
} else {
sw = pus->Buffer;
len = pus->Length / sizeof(WCHAR);
}
str = stringw(str, end, sw, len, field_width, precision, flags);
} else {
/* print counted ascii string */
PANSI_STRING pus = va_arg(args, PANSI_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
s = NULL;
len = -1;
} else {
s = pus->Buffer;
len = pus->Length;
}
str = string(str, end, s, len, field_width, precision, flags);
}
continue;
case 'p':
if ((flags & LARGE) == 0)
flags |= LARGE;
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
str = number(str, end,
(ULONG_PTR) va_arg(args, void *), 16,
field_width, precision, flags);
continue;
case 'n':
/* FIXME: What does C99 say about the overflow case here? */
if (qualifier == 'l') {
long * ip = va_arg(args, long *);
*ip = (str - buf);
} else {
int * ip = va_arg(args, int *);
*ip = (str - buf);
}
continue;
/* float number formats - set up the flags and "break" */
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
_double = (double)va_arg(args, double);
if ( _isnan(_double) ) {
s = "Nan";
len = 3;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else if ( _isinf(_double) < 0 ) {
s = "-Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else if ( _isinf(_double) > 0 ) {
s = "+Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else {
if ( precision == -1 )
precision = 6;
str = numberf(str, end, (int)_double, base, field_width, precision, flags);
}
continue;
/* integer number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'b':
base = 2;
break;
case 'X':
flags |= LARGE;
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
if (*fmt) {
if (str <= end)
*str = *fmt;
++str;
} else
--fmt;
continue;
}
if (qualifier == 'I')
num = va_arg(args, unsigned long long);
else if (qualifier == 'l') {
if (flags & SIGN)
num = va_arg(args, long);
else
num = va_arg(args, unsigned long);
}
else if (qualifier == 'h') {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
else {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
str = number(str, end, num, base, field_width, precision, flags);
}
if (str <= end)
*str = '\0';
else if (cnt > 0)
/* don't write out a null byte if the buf size is zero */
*end = '\0';
return str-buf;
}
/*
* @implemented
*/
int sprintf(char * buf, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=_vsnprintf(buf,MAXLONG,fmt,args);
va_end(args);
return i;
}
/*
* @implemented
*/
int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=_vsnprintf(buf,cnt,fmt,args);
va_end(args);
return i;
}
/*
* @implemented
*/
int __cdecl vsprintf(char *buf, const char *fmt, va_list args)
{
return _vsnprintf(buf,MAXLONG,fmt,args);
}
/* EOF */
#endif

View file

@ -131,7 +131,7 @@ RtlpReleaseWaitBlockLockExclusive(IN OUT PRTL_SRWLOCK SRWLock,
}
}
(void)InterlockedExchange((PLONG)&SRWLock->Ptr, NewValue);
(void)_InterlockedExchangePointer(&SRWLock->Ptr, (PVOID)NewValue);
if (FirstWaitBlock->Exclusive)
{
@ -186,7 +186,7 @@ RtlpReleaseWaitBlockLockLastShared(IN OUT PRTL_SRWLOCK SRWLock,
NewValue = RTL_SRWLOCK_OWNED;
}
(void)InterlockedExchange((PLONG)&SRWLock->Ptr, NewValue);
(void)_InterlockedExchangePointer(&SRWLock->Ptr, (PVOID)NewValue);
(void)InterlockedOr(&FirstWaitBlock->Wake,
TRUE);
@ -420,9 +420,9 @@ RtlAcquireSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
NewValue = (CurrentValue >> RTL_SRWLOCK_BITS) + 1;
NewValue = (NewValue << RTL_SRWLOCK_BITS) | (CurrentValue & RTL_SRWLOCK_MASK);
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
/* Successfully incremented the shared count, we acquired the lock */
break;
@ -499,9 +499,9 @@ RtlAcquireSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
ASSERT_SRW_WAITBLOCK(&StackWaitBlock);
NewValue = (ULONG_PTR)&StackWaitBlock | RTL_SRWLOCK_OWNED | RTL_SRWLOCK_CONTENDED;
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
RtlpAcquireSRWLockSharedWait(SRWLock,
&StackWaitBlock,
@ -521,9 +521,9 @@ RtlAcquireSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
RTL_SRWLOCK_SHARED nor the RTL_SRWLOCK_OWNED bit is set */
ASSERT(!(CurrentValue & RTL_SRWLOCK_CONTENDED));
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
/* Successfully set the shared count, we acquired the lock */
break;
@ -580,9 +580,9 @@ RtlReleaseSRWLockShared(IN OUT PRTL_SRWLOCK SRWLock)
NewValue = (NewValue << RTL_SRWLOCK_BITS) | RTL_SRWLOCK_SHARED | RTL_SRWLOCK_OWNED;
}
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
/* Successfully released the lock */
break;
@ -639,9 +639,9 @@ RtlAcquireSRWLockExclusive(IN OUT PRTL_SRWLOCK SRWLock)
NewValue = (ULONG_PTR)&StackWaitBlock | RTL_SRWLOCK_SHARED | RTL_SRWLOCK_CONTENDED | RTL_SRWLOCK_OWNED;
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
RtlpAcquireSRWLockExclusiveWait(SRWLock,
&StackWaitBlock);
@ -697,9 +697,9 @@ AddWaitBlock:
ASSERT_SRW_WAITBLOCK(&StackWaitBlock);
NewValue = (ULONG_PTR)&StackWaitBlock | RTL_SRWLOCK_OWNED | RTL_SRWLOCK_CONTENDED;
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
RtlpAcquireSRWLockExclusiveWait(SRWLock,
&StackWaitBlock);
@ -767,9 +767,9 @@ RtlReleaseSRWLockExclusive(IN OUT PRTL_SRWLOCK SRWLock)
ASSERT(!(CurrentValue & ~RTL_SRWLOCK_OWNED));
NewValue = 0;
if (InterlockedCompareExchange((PLONG)&SRWLock->Ptr,
NewValue,
CurrentValue) == CurrentValue)
if ((LONG_PTR)_InterlockedCompareExchangePointer(&SRWLock->Ptr,
(PVOID)NewValue,
(PVOID)CurrentValue) == CurrentValue)
{
/* We released the lock */
break;

View file

@ -1,744 +0,0 @@
#ifndef USE_NEW_SPRINTF
/*
* PROGRAMMERS: David Welch
* Eric Kohl
*
* TODO:
* - Verify the implementation of '%Z'.
*/
/*
* linux/lib/vsprintf.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
#include <rtl.h>
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed long */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
#define REMOVEHEX 256 /* use 256 as remve 0x frim BASE 16 */
typedef struct {
unsigned int mantissal:32;
unsigned int mantissah:20;
unsigned int exponent:11;
unsigned int sign:1;
} double_t;
static
__inline
int
_isinf(double __x)
{
union
{
double* __x;
double_t* x;
} x;
x.__x = &__x;
return ( x.x->exponent == 0x7ff && ( x.x->mantissah == 0 && x.x->mantissal == 0 ));
}
static
__inline
int
_isnan(double __x)
{
union
{
double* __x;
double_t* x;
} x;
x.__x = &__x;
return ( x.x->exponent == 0x7ff && ( x.x->mantissah != 0 || x.x->mantissal != 0 ));
}
static
__inline
int
do_div(long long *n, int base)
{
int a;
a = ((unsigned long long) *n) % (unsigned) base;
*n = ((unsigned long long) *n) / (unsigned) base;
return a;
}
static int skip_atoi(const wchar_t **s)
{
int i=0;
while (iswdigit(**s))
i = i*10 + *((*s)++) - L'0';
return i;
}
static wchar_t *
number(wchar_t * buf, wchar_t * end, long long num, int base, int size, int precision, int type)
{
wchar_t c, sign, tmp[66];
const wchar_t *digits;
const wchar_t *small_digits = L"0123456789abcdefghijklmnopqrstuvwxyz";
const wchar_t *large_digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
digits = (type & LARGE) ? large_digits : small_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? L'0' : L' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = L'-';
num = -num;
size--;
} else if (type & PLUS) {
sign = L'+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base == 16)
size -= 2;
}
i = 0;
if ((num == 0) && (precision !=0))
tmp[i++] = L'0';
else while (num != 0)
tmp[i++] = digits[do_div(&num,base)];
if (i > precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT))) {
while(size-->0) {
if (buf <= end)
*buf = L' ';
++buf;
}
}
if (sign) {
if (buf <= end)
*buf = sign;
++buf;
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base==16) {
if (buf <= end)
*buf = L'0';
++buf;
if (buf <= end)
*buf = digits[33];
++buf;
}
}
if (!(type & LEFT)) {
while (size-- > 0) {
if (buf <= end)
*buf = c;
++buf;
}
}
while (i < precision--) {
if (buf <= end)
*buf = L'0';
++buf;
}
while (i-- > 0) {
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0) {
if (buf <= end)
*buf = L' ';
++buf;
}
return buf;
}
static wchar_t *
numberf(wchar_t * buf, wchar_t * end, double num, int base, int size, int precision, int type)
{
wchar_t c, sign, tmp[66];
const wchar_t *digits;
const wchar_t *small_digits = L"0123456789abcdefghijklmnopqrstuvwxyz";
const wchar_t *large_digits = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
long long x;
/* FIXME
the float version of number is direcly copy of number
*/
digits = (type & LARGE) ? large_digits : small_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? L'0' : L' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = L'-';
num = -num;
size--;
} else if (type & PLUS) {
sign = L'+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if (type & SPECIAL) {
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}
i = 0;
if (num == 0)
tmp[i++] = L'0';
else while (num != 0)
{
x = num;
tmp[i++] = digits[do_div(&x,base)];
#ifndef _M_ARM // Missing __floatdidf in CeGCC 0.55 -- GCC 4.4
num = x;
#endif
}
if (i > precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT))) {
while(size-->0) {
if (buf <= end)
*buf = L' ';
++buf;
}
}
if (sign) {
if (buf <= end)
*buf = sign;
++buf;
}
if (type & SPECIAL) {
if (base==8) {
if (buf <= end)
*buf = L'0';
++buf;
} else if (base==16) {
if (buf <= end)
*buf = L'0';
++buf;
if (buf <= end)
*buf = digits[33];
++buf;
}
}
if (!(type & LEFT)) {
while (size-- > 0) {
if (buf <= end)
*buf = c;
++buf;
}
}
while (i < precision--) {
if (buf <= end)
*buf = L'0';
++buf;
}
while (i-- > 0) {
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0) {
if (buf <= end)
*buf = L' ';
++buf;
}
return buf;
}
static wchar_t*
string(wchar_t* buf, wchar_t* end, const char* s, int len, int field_width, int precision, int flags)
{
int i;
wchar_t c;
c = (flags & ZEROPAD) ? L'0' : L' ';
if (s == NULL)
{
s = "<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && s[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
++buf;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = *s++;
++buf;
}
while (len < field_width--)
{
if (buf <= end)
*buf = L' ';
++buf;
}
return buf;
}
static wchar_t*
stringw(wchar_t* buf, wchar_t* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
{
int i;
wchar_t c;
c = (flags & ZEROPAD) ? L'0' : L' ';
if (sw == NULL)
{
sw = L"<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && sw[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
buf++;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = *sw++;
buf++;
}
while (len < field_width--)
{
if (buf <= end)
*buf = L' ';
buf++;
}
return buf;
}
/*
* @implemented
*/
int __cdecl _vsnwprintf(wchar_t *buf, size_t cnt, const wchar_t *fmt, va_list args)
{
int len;
unsigned long long num;
int base;
wchar_t * str, * end;
const char *s;
const wchar_t *sw;
const wchar_t *ss;
double _double;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h', 'l', 'L', 'w' or 'I' for integer fields */
str = buf;
end = buf + cnt - 1;
if (end < buf - 1) {
end = ((wchar_t *) -1);
cnt = end - buf + 1;
}
for ( ; *fmt ; ++fmt) {
if (*fmt != L'%') {
if (str <= end)
*str = *fmt;
++str;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case L'-': flags |= LEFT; goto repeat;
case L'+': flags |= PLUS; goto repeat;
case L' ': flags |= SPACE; goto repeat;
case L'#': flags |= SPECIAL; goto repeat;
case L'0': flags |= ZEROPAD; goto repeat;
}
/* get field width */
field_width = -1;
if (iswdigit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == L'*') {
++fmt;
/* it's the next argument */
field_width = va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == L'.') {
++fmt;
if (iswdigit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == L'*') {
++fmt;
/* it's the next argument */
precision = va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == L'l' && *(fmt+1) == L'l') {
qualifier = L'I';
fmt += 2;
} else if (*fmt == L'h' || *fmt == L'l' || *fmt == L'L' || *fmt == L'w') {
qualifier = *fmt;
++fmt;
} else if (*fmt == L'I' && *(fmt+1) == L'6' && *(fmt+2) == L'4') {
qualifier = *fmt;
fmt += 3;
} else if (*fmt == L'I' && *(fmt+1) == L'3' && *(fmt+2) == L'2') {
qualifier = L'l';
fmt += 3;
} else if (*fmt == L'F' && *(fmt+1) == L'p') {
fmt += 1;
flags |= REMOVEHEX;
}
/* default base */
base = 10;
switch (*fmt) {
case L'c':
if (qualifier == 'h' || qualifier == 'w') {
wchar_t sw1[2];
/* print unicode string */
sw1[0] = (wchar_t) va_arg(args, int);
sw1[1] = 0;
str = stringw(str, end, (wchar_t *)&sw1, -1, field_width, precision, flags);
} else {
char s1[2];
/* print ascii string */
s1[0] = ( unsigned char) va_arg(args, int);
s1[1] = 0;
str = string(str, end, (char *)&s1, -1, field_width, precision, flags);
}
continue;
case L'C':
if (!(flags & LEFT))
while (--field_width > 0) {
if (str <= end)
*str = L' ';
++str;
}
if (qualifier == 'l' || qualifier == 'w') {
if (str <= end)
*str = (wchar_t) va_arg(args, int);
++str;
} else {
if (str <= end)
*str = (wchar_t) va_arg(args, int);
++str;
}
while (--field_width > 0) {
if (str <= end)
*str = L' ';
++str;
}
continue;
case L's':
if (qualifier == 'h') {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
} else {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
}
continue;
case L'S':
if (qualifier == 'l' || qualifier == 'w') {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
} else {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
}
continue;
case L'Z':
if (qualifier == 'h') {
/* print counted ascii string */
PANSI_STRING pus = va_arg(args, PANSI_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
s = NULL;
len = -1;
} else {
s = pus->Buffer;
len = pus->Length;
}
str = string(str, end, s, len, field_width, precision, flags);
} else {
/* print counted unicode string */
PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
sw = NULL;
len = -1;
} else {
sw = pus->Buffer;
len = pus->Length / sizeof(WCHAR);
}
str = stringw(str, end, sw, len, field_width, precision, flags);
}
continue;
case L'p':
if ((flags & LARGE) == 0)
flags |= LARGE;
if (field_width == -1) {
field_width = 2*sizeof(void *);
flags |= ZEROPAD;
}
str = number(str, end,
(ULONG_PTR) va_arg(args, void *), 16,
field_width, precision, flags);
continue;
case L'n':
/* FIXME: What does C99 say about the overflow case here? */
if (qualifier == 'l') {
long * ip = va_arg(args, long *);
*ip = (str - buf);
} else {
int * ip = va_arg(args, int *);
*ip = (str - buf);
}
continue;
/* float number formats - set up the flags and "break" */
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
_double = (double)va_arg(args, double);
if ( _isnan(_double) ) {
ss = L"Nan";
len = 3;
while ( len > 0 ) {
if (str <= end)
*str = *ss++;
++str;
len --;
}
} else if ( _isinf(_double) < 0 ) {
ss = L"-Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *ss++;
++str;
len --;
}
} else if ( _isinf(_double) > 0 ) {
ss = L"+Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *ss++;
++str;
len --;
}
} else {
if ( precision == -1 )
precision = 6;
str = numberf(str, end, _double, base, field_width, precision, flags);
}
continue;
/* integer number formats - set up the flags and "break" */
case L'o':
base = 8;
break;
case L'b':
base = 2;
break;
case L'X':
flags |= LARGE;
case L'x':
base = 16;
break;
case L'd':
case L'i':
flags |= SIGN;
case L'u':
break;
default:
if (*fmt) {
if (str <= end)
*str = *fmt;
++str;
} else
--fmt;
continue;
}
if (qualifier == L'I')
num = va_arg(args, unsigned long long);
else if (qualifier == L'l') {
if (flags & SIGN)
num = va_arg(args, long);
else
num = va_arg(args, unsigned long);
}
else if (qualifier == L'h') {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
else {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
str = number(str, end, num, base, field_width, precision, flags);
}
if (str <= end)
*str = L'\0';
else if (cnt > 0)
/* don't write out a null byte if the buf size is zero */
*end = L'\0';
return str-buf;
}
/*
* @implemented
*/
int swprintf(wchar_t *buf, const wchar_t *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=_vsnwprintf(buf,MAXLONG,fmt,args);
va_end(args);
return i;
}
/*
* @implemented
*/
int __cdecl _snwprintf(wchar_t *buf, size_t cnt, const wchar_t *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=_vsnwprintf(buf,cnt,fmt,args);
va_end(args);
return i;
}
/*
* @implemented
*/
int __cdecl vswprintf(wchar_t *buf, const wchar_t *fmt, va_list args)
{
return _vsnwprintf(buf,MAXLONG,fmt,args);
}
/* EOF */
#endif

View file

@ -5,7 +5,6 @@ add_definitions(-D_CRTBLD)
list(APPEND CRT_SOURCE
conio/cgets.c
conio/cprintf.c
conio/cputs.c
conio/getch.c
conio/getche.c
@ -54,6 +53,7 @@ list(APPEND CRT_SOURCE
math/ldiv.c
math/logf.c
math/modf.c
math/powf.c
math/rand.c
math/sqrtf.c
math/s_modf.c
@ -128,6 +128,26 @@ list(APPEND CRT_SOURCE
misc/purecall.c
misc/stubs.c
misc/tls.c
printf/_cprintf.c
printf/_snprintf.c
printf/_snwprintf.c
printf/_vcprintf.c
printf/_vsnprintf.c
printf/_vsnwprintf.c
printf/fprintf.c
printf/fwprintf.c
printf/printf.c
printf/sprintf.c
printf/streamout.c
printf/swprintf.c
printf/vfprintf.c
printf/vfwprintf.c
printf/vprintf.c
printf/vsprintf.c
printf/vswprintf.c
printf/vwprintf.c
printf/wprintf.c
printf/wstreamout.c
process/_cwait.c
process/_system.c
process/dll.c
@ -142,13 +162,15 @@ list(APPEND CRT_SOURCE
search/lsearch.c
signal/signal.c
signal/xcptinfo.c
stdio/_flsbuf.c
stdio/_flswbuf.c
stdio/access.c
stdio/file.c
stdio/find.c
stdio/find64.c
stdio/findi64.c
stdio/fmode.c
stdio/lnx_sprintf.c
stdio/lock_file.c
stdio/perror.c
stdio/popen.c
stdio/stat.c
@ -427,6 +449,18 @@ list(APPEND LIBCNTPR_SOURCE
mem/memccpy.c
mem/memcmp.c
mem/memicmp.c
printf/_snprintf.c
printf/_snwprintf.c
printf/_vcprintf.c
printf/_vsnprintf.c
printf/_vsnwprintf.c
printf/sprintf.c
printf/streamout.c
printf/swprintf.c
printf/vprintf.c
printf/vsprintf.c
printf/vswprintf.c
printf/wstreamout.c
search/bsearch.c
search/lfind.c
stdlib/qsort.c
@ -588,3 +622,16 @@ if(MSVC)
add_library(msvcsup ${MSVCSUP_SOURCE})
add_dependencies(msvcsup psdk buildno_header asm)
endif()
add_definitions(-D_USER32_WSPRINTF)
add_library(user32_wsprintf
printf/streamout.c
printf/wstreamout.c
printf/wsprintfA.c
printf/wsprintfW.c
printf/wvsprintfA.c
printf/wvsprintfW.c
printf/wvsnprintfA.c
printf/wvsnprintfW.c
string/mbstowcs_nt.c
string/wcstombs_nt.c)

View file

@ -1,43 +0,0 @@
/*
* COPYRIGHT: Winehq
* PROJECT: wine
* FILE: msvcrt/conio/cprintf.c
* PURPOSE: C Runtime
* PROGRAMMER: Magnus Olsen (Imported from wine cvs 2006-05-23)
*/
#ifndef USE_NEW_SPRINTF
#include <precomp.h>
/*
* @implemented
*/
int
_cprintf(const char *fmt, ...)
{
char buf[2048], *mem = buf;
int written, resize = sizeof(buf), retval;
va_list valist;
va_start( valist, fmt );
while ((written = _vsnprintf( mem, resize, fmt, valist )) == -1 ||
written > resize)
{
resize = (written == -1 ? resize * 2 : written + 1);
if (mem != buf)
free (mem);
if (!(mem = (char *)malloc(resize)))
return EOF;
va_end ( valist );
va_start( valist, fmt );
}
va_end ( valist );
retval = _cputs( mem );
if (mem != buf)
free (mem);
return retval;
}
#endif

View file

@ -6,34 +6,7 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <tchar.h>
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
int
_cdecl
_snprintf(char *buffer, size_t count, const char *format, ...)
{
va_list argptr;
int result;
FILE stream;
stream._base = buffer;
stream._ptr = stream._base;
stream._charbuf = 0;
stream._bufsiz = count;
stream._cnt = stream._bufsiz;
stream._flag = 0;
stream._tmpfname = 0;
va_start(argptr, format);
result = streamout(&stream, format, argptr);
va_end(argptr);
*stream._ptr = '\0';
return result;
}
#define _sxprintf _snprintf
#define USE_COUNT 1
#include "_sxprintf.c"

View file

@ -6,37 +6,8 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#define _sxprintf _snwprintf
#define USE_COUNT 1
#define _UNICODE
int _cdecl wstreamout(FILE *stream, const wchar_t *format, va_list argptr);
int
__cdecl
_snwprintf(
wchar_t *buffer,
size_t count,
const wchar_t *format,
...)
{
va_list argptr;
int result;
FILE stream;
stream._base = (char*)buffer;
stream._ptr = stream._base;
stream._bufsiz = count * sizeof(wchar_t);
stream._cnt = stream._bufsiz;
stream._flag = _IOSTRG | _IOWRT;
stream._tmpfname = 0;
stream._charbuf = 0;
va_start(argptr, format);
result = wstreamout(&stream, format, argptr);
va_end(argptr);
/* Only zero terminate if there is enough space left */
if (stream._cnt >= sizeof(wchar_t)) *(wchar_t*)stream._ptr = L'\0';
return result;
}
#include "_sxprintf.c"

View file

@ -0,0 +1,72 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/swprintf.c
* PURPOSE: Implementation of swprintf
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <tchar.h>
#ifdef _UNICODE
#define _tstreamout wstreamout
#else
#define _tstreamout streamout
#endif
int _cdecl _tstreamout(FILE *stream, const TCHAR *format, va_list argptr);
int
#if defined(USER32_WSPRINTF) && defined(_M_IX86)
_stdcall
#else
_cdecl
#endif
_sxprintf(
TCHAR *buffer,
#if USE_COUNT
size_t count,
#endif
const TCHAR *format,
#if USE_VARARGS
va_list argptr)
#else
...)
#endif
{
#if !USE_VARARGS
va_list argptr;
#endif
int result;
FILE stream;
stream._base = (char*)buffer;
stream._ptr = stream._base;
stream._charbuf = 0;
#if USE_COUNT
stream._cnt = count * sizeof(TCHAR);
#else
stream._cnt = INT_MAX;
#endif
stream._bufsiz = 0;
stream._flag = _IOSTRG | _IOWRT;
stream._tmpfname = 0;
#if !USE_VARARGS
va_start(argptr, format);
#endif
result = _tstreamout(&stream, format, argptr);
#if !USE_VARARGS
va_end(argptr);
#endif
/* Only zero terminate if there is enough space left */
if (stream._cnt >= sizeof(TCHAR)) *(TCHAR*)stream._ptr = _T('\0');
return result;
}

View file

@ -6,32 +6,8 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#define _sxprintf _vsnprintf
#define USE_COUNT 1
#define USE_VARARGS 1
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
int
__cdecl
_vsnprintf(
char *buffer,
size_t count,
const char *format,
va_list argptr)
{
int result;
FILE stream;
stream._base = buffer;
stream._ptr = stream._base;
stream._bufsiz = count;
stream._cnt = stream._bufsiz;
stream._flag = _IOSTRG | _IOWRT;
stream._tmpfname = 0;
stream._charbuf = 0;
result = streamout(&stream, format, argptr);
*stream._ptr = '\0';
return result;
}
#include "_sxprintf.c"

View file

@ -6,32 +6,9 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#define _sxprintf _vsnwprintf
#define USE_COUNT 1
#define USE_VARARGS 1
#define _UNICODE
int _cdecl wstreamout(FILE *stream, const wchar_t *format, va_list argptr);
int
__cdecl
_vsnwprintf(
wchar_t *buffer,
size_t count,
const wchar_t *format,
va_list argptr)
{
int result;
FILE stream;
stream._base = (char*)buffer;
stream._ptr = stream._base;
stream._bufsiz = count * sizeof(wchar_t);
stream._cnt = stream._bufsiz;
stream._flag = _IOSTRG | _IOWRT;
stream._tmpfname = 0;
stream._charbuf = 0;
result = wstreamout(&stream, format, argptr);
*(wchar_t*)stream._ptr = L'\0';
return result;
}
#include "_sxprintf.c"

View file

@ -22,6 +22,7 @@ printf(const char *format, ...)
va_start(argptr, format);
result = streamout(stdout, format, argptr);
va_end(argptr);
return result;
}

View file

@ -6,33 +6,7 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
int
_cdecl
sprintf(char *buffer, const char *format, ...)
{
va_list argptr;
int result;
FILE stream;
stream._base = buffer;
stream._ptr = stream._base;
stream._charbuf = 0;
stream._bufsiz = INT_MAX;
stream._cnt = stream._bufsiz;
stream._flag = 0;
stream._tmpfname = 0;
va_start(argptr, format);
result = streamout(&stream, format, argptr);
va_end(argptr);
*stream._ptr = '\0';
return result;
}
#define _sxprintf sprintf
#define USE_COUNT 0
#include "_sxprintf.c"

View file

@ -14,8 +14,15 @@
#include <float.h>
#ifdef _UNICODE
#define streamout wstreamout
#define format_float format_floatw
# define streamout wstreamout
# define format_float format_floatw
# define _flsbuf _flswbuf
int __cdecl _flswbuf(int ch, FILE *stream);
#endif
#ifdef _LIBCNT_
# undef _flsbuf
# define _flsbuf(chr, stream) _TEOF
#endif
#define MB_CUR_MAX 10
@ -67,11 +74,10 @@ enum
(flags & FLAG_LONGDOUBLE) ? va_arg(argptr, long double) : \
va_arg(argptr, double)
#ifdef _LIBCNT_
# define _flsbuf(chr, stream) 0
#endif
#define get_exp(f) floor(f == 0 ? 0 : (f >= 0 ? log10(f) : log10(-f)))
#define round(x) floor((x) + 0.5)
#define get_exp(f) floor(f > 0 ? log10(f) : log10(-f))
#ifndef _USER32_WSPRINTF
void
#ifdef _LIBCNT
@ -92,30 +98,56 @@ format_float(
static const TCHAR _nan[] = _T("#QNAN");
static const TCHAR _infinity[] = _T("#INF");
const TCHAR *digits = digits_l;
int exponent = 0;
long double fpval;
int num_digits, val32, base = 10;
__int64 val64;
int exponent = 0, sign;
long double fpval, fpval2;
int padding = 0, num_digits, val32, base = 10;
/* Normalize the precision */
if (precision < 0) precision = 6;
else if (precision > 512) precision = 512;
else if (precision > 17)
{
padding = precision - 17;
precision = 17;
}
/* Get the float value and calculate the exponent */
fpval = va_arg_ffp(*argptr, flags);
exponent = get_exp(fpval);
sign = fpval < 0 ? -1 : 1;
switch (chr)
{
case _T('G'):
digits = digits_u;
case _T('g'):
if (precision > 0) precision--;
if (exponent < -4 || exponent >= precision) goto case_e;
/* Shift the decimal point and round */
fpval2 = round(sign * fpval * pow(10., precision));
/* Skip trailing zeroes */
while (precision && (unsigned __int64)fpval2 % 10 == 0)
{
precision--;
fpval2 /= 10;
}
break;
case _T('E'):
digits = digits_u;
case _T('e'):
case_e:
fpval /= pow(10., exponent);
/* Shift the decimal point and round */
fpval2 = round(sign * fpval * pow(10., precision - exponent));
/* Compensate for changed exponent through rounding */
if (fpval2 >= (unsigned __int64)pow(10., precision + 1))
{
exponent++;
fpval2 = round(sign * fpval * pow(10., precision - exponent));
}
val32 = exponent >= 0 ? exponent : -exponent;
// FIXME: handle length of exponent field:
@ -128,7 +160,7 @@ format_float(
}
/* Sign for the exponent */
*--(*string) = exponent > 0 ? _T('+') : _T('-');
*--(*string) = exponent >= 0 ? _T('+') : _T('-');
/* Add 'e' or 'E' separator */
*--(*string) = digits[0xe];
@ -141,16 +173,15 @@ format_float(
// FIXME: TODO
case _T('f'):
default:
/* Shift the decimal point and round */
fpval2 = round(sign * fpval * pow(10., precision));
break;
}
/* CHECKME: Windows seems to handle a max of 17 digits(?) */
num_digits = precision <= 17 ? precision: 17;
/* Handle sign */
if (fpval < 0)
{
fpval = -fpval;
*prefix = _T("-");
}
else if (flags & FLAG_FORCE_SIGN)
@ -163,46 +194,58 @@ format_float(
{
(*string) -= sizeof(_nan) / sizeof(TCHAR) - 1;
_tcscpy((*string), _nan);
val64 = 1;
fpval2 = 1;
}
else if (!_finite(fpval))
{
(*string) -= sizeof(_infinity) / sizeof(TCHAR) - 1;
_tcscpy((*string), _infinity);
val64 = 1;
fpval2 = 1;
}
else
{
fpval *= pow(10., precision);
val64 = (__int64)(fpval + 0.5);
/* Zero padding */
while (padding-- > 0) *--(*string) = _T('0');
/* Digits after the decimal point */
num_digits = precision;
while (num_digits-- > 0)
{
*--(*string) = digits[val64 % 10];
val64 /= 10;
*--(*string) = digits[(unsigned __int64)fpval2 % 10];
fpval2 /= base;
}
}
if (precision > 0 || flags & FLAG_SPECIAL)
*--(*string) = _T('.');
/* Digits before the decimal point */
do
{
*--(*string) = digits[val64 % base];
val64 /= base;
*--(*string) = digits[(unsigned __int64)fpval2 % base];
fpval2 /= base;
}
while (val64);
while ((unsigned __int64)fpval2);
}
#endif
static
int
streamout_char(FILE *stream, int chr)
{
/* Flush the buffer if neccessary */
/* Check if the buffer is full */
if (stream->_cnt < sizeof(TCHAR))
{
return _flsbuf(chr, stream) != EOF;
#ifdef _USER32_WSPRINTF
return _TEOF;
#else
/* Strings are done now */
if (stream->_flag & _IOSTRG) return _TEOF;
/* Flush buffer for files */
return _flsbuf(chr, stream) != _TEOF;
#endif
}
*(TCHAR*)stream->_ptr = chr;
@ -270,6 +313,11 @@ streamout_wstring(FILE *stream, const wchar_t *string, int count)
#define streamout_string streamout_astring
#endif
#ifdef _USER32_WSPRINTF
# define USE_MULTISIZE 0
#else
# define USE_MULTISIZE 1
#endif
int
_cdecl
@ -363,22 +411,18 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
else precision = -1;
/* Handle argument size prefix */
while (1)
do
{
if (chr == _T('h')) flags |= FLAG_SHORT;
else if (chr == _T('w')) flags |= FLAG_WIDECHAR;
else if (chr == _T('L')) flags |= 0; // FIXME: long double
else if (chr == _T('F')) flags |= 0; // FIXME: what is that?
else if (chr == _T('l'))
{
flags |= FLAG_LONG;
#if SUPPORT_LL
if (format[0] == _T('l'))
{
format++;
flags |= FLAG_INT64;
/* Check if this is the 2nd 'l' in a row */
if (format[-2] == 'l') flags |= FLAG_INT64;
else flags |= FLAG_LONG;
}
#endif
}
else if (chr == _T('I'))
{
if (format[0] == _T('3') && format[1] == _T('2'))
@ -401,6 +445,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
else break;
chr = *format++;
}
while (USE_MULTISIZE);
/* Handle the format specifier */
digits = digits_l;
@ -479,8 +524,10 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
else
len = strlen((char*)string);
if (precision >= 0 && len > precision) len = precision;
precision = 0;
break;
#ifndef _USER32_WSPRINTF
case _T('G'):
case _T('E'):
case _T('A'):
@ -498,6 +545,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
len = _tcslen(string);
precision = 0;
break;
#endif
case _T('d'):
case _T('i'):
@ -517,9 +565,12 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
case _T('o'):
base = 8;
if (flags & FLAG_SPECIAL) prefix = _T("0");
if (flags & FLAG_SPECIAL)
{
prefix = _T("0");
if (precision > 0) precision--;
}
goto case_unsigned;
/* Fall through */
case _T('p'):
precision = 2 * sizeof(void*);
@ -587,7 +638,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
if (prefix)
{
written = streamout_string(stream, prefix, prefixlen);
if (written == -1) return -3;
if (written == -1) return -1;
written_all += written;
}
@ -604,7 +655,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
written = streamout_wstring(stream, (wchar_t*)string, len);
else
written = streamout_astring(stream, (char*)string, len);
if (written == -1) return -5;
if (written == -1) return -1;
written_all += written;
#if 0 && SUPPORT_FLOAT
@ -629,7 +680,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
}
if (written == -1) return -8;
if (written == -1) return -1;
return written_all;
}

View file

@ -6,34 +6,8 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
int _cdecl wstreamout(FILE *stream, const wchar_t *format, va_list argptr);
int
_cdecl
swprintf(wchar_t *buffer, const wchar_t *format, ...)
{
va_list argptr;
int result;
FILE stream;
stream._base = (char*)buffer;
stream._ptr = stream._base;
stream._charbuf = 0;
stream._bufsiz = INT_MAX;
stream._cnt = stream._bufsiz;
stream._flag = 0;
stream._tmpfname = 0;
va_start(argptr, format);
result = wstreamout(&stream, format, argptr);
va_end(argptr);
*(wchar_t*)stream._ptr = '\0';
return result;
}
#define _sxprintf swprintf
#define USE_COUNT 0
#define _UNICODE
#include "_sxprintf.c"

View file

@ -6,32 +6,8 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#define _sxprintf vsprintf
#define USE_COUNT 0
#define USE_VARARGS 1
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
int
__cdecl
vsprintf(
char *buffer,
const char *format,
va_list argptr)
{
int result;
FILE stream;
stream._base = buffer;
stream._ptr = stream._base;
stream._charbuf = 0;
stream._bufsiz = INT_MAX;
stream._cnt = stream._bufsiz;
stream._flag = _IOSTRG|_IOWRT|_IOMYBUF;
stream._tmpfname = 0;
result = streamout(&stream, format, argptr);
*stream._ptr = '\0';
return result;
}
#include "_sxprintf.c"

View file

@ -6,13 +6,9 @@
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#define _sxprintf vswprintf
#define USE_COUNT 0
#define USE_VARARGS 1
#define _UNICODE
int
__cdecl
vswprintf(wchar_t *buffer, const wchar_t *format, va_list argptr)
{
return _vsnwprintf(buffer, INT_MAX, format, argptr);
}
#include "_sxprintf.c"

View file

@ -0,0 +1,13 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wsprintfA.c
* PURPOSE: Implementation of wsprintfA
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wsprintfA
#define USE_COUNT 0
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,14 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wsprintfW.c
* PURPOSE: Implementation of wsprintfW
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wsprintfW
#define USE_COUNT 0
#define _UNICODE
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,14 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wvsnprintfA.c
* PURPOSE: Implementation of wvsnprintfA
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wvsnprintfA
#define USE_COUNT 1
#define USE_VARARGS 1
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,14 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wvsnprintfW.c
* PURPOSE: Implementation of wvsnprintfW
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wvsnprintfW
#define USE_COUNT 1
#define USE_VARARGS 1
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,14 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wvsprintfA.c
* PURPOSE: Implementation of wvsprintfA
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wvsprintfA
#define USE_COUNT 0
#define USE_VARARGS 1
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,14 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/printf/wvsprintfW.c
* PURPOSE: Implementation of wvsprintfW
* PROGRAMMER: Timo Kreuzer
*/
#define _sxprintf wvsprintfW
#define USE_COUNT 0
#define USE_VARARGS 1
#define USER32_WSPRINTF
#include "_sxprintf.c"

View file

@ -0,0 +1,82 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/stdio/_flsbuf.c
* PURPOSE: Implementation of _flsbuf / _flswbuf
* PROGRAMMER: Timo Kreuzer
*/
#include <stdio.h>
#include <io.h>
#include <tchar.h>
void __cdecl alloc_buffer(FILE *stream);
int __cdecl
_flsbuf(int ch, FILE *stream)
{
int count, written;
/* Check if the stream supports flushing */
if ((stream->_flag & _IOSTRG) || !(stream->_flag & (_IORW|_IOWRT)))
{
stream->_flag |= _IOERR;
return EOF;
}
/* Is this was a read buffer */
if (stream->_flag & _IOREAD)
{
/* Must be at the end of the file */
if (!(stream->_flag & _IOEOF))
{
stream->_flag |= _IOERR;
stream->_cnt = 0;
return EOF;
}
/* Reset buffer */
stream->_ptr = stream->_base;
}
/* Fixup flags */
stream->_flag &= ~(_IOREAD|_IOEOF);
stream->_flag |= _IOWRT;
/* If we have no buffer, try to allocate one */
if (!stream->_base && stream != stdout && stream != stderr)
{
alloc_buffer(stream);
}
/* Check if we have a buffer now */
if (stream->_base)
{
/* We have one, check if there is something to write */
count = stream->_ptr - stream->_base;
if (count > 0)
written = _write(stream->_file, stream->_base, count);
else
written = 0;
/* Reset buffer and put the char into it */
stream->_ptr = stream->_base + sizeof(TCHAR);
stream->_cnt = stream->_bufsiz - sizeof(TCHAR);
*(TCHAR*)stream->_base = ch;
}
else
{
/* There is no buffer, write the char directly */
count = sizeof(TCHAR);
written = _write(stream->_file, &ch, sizeof(TCHAR));
}
/* Check for failure */
if (written != count)
{
stream->_flag |= _IOERR;
return EOF;
}
return ch & (sizeof(TCHAR) > sizeof(char) ? 0xffff : 0xff);
}

View file

@ -0,0 +1,11 @@
/*
* COPYRIGHT: GNU GPL, see COPYING in the top level directory
* PROJECT: ReactOS crt library
* FILE: lib/sdk/crt/stdio/_flswbuf
* PURPOSE: Implementation of _flswbuf
* PROGRAMMER: Timo Kreuzer
*/
#define _UNICODE
#define _flsbuf _flswbuf
#include "_flsbuf.c"

View file

@ -88,7 +88,7 @@ int *__p___mb_cur_max(void);
typedef struct {
HANDLE handle;
unsigned char wxflag;
DWORD unkn[7]; /* critical section and init flag */
DWORD unkn[7]; /* critical section and init flag */
} ioinfo;
ioinfo fdesc[MAX_FILES];
@ -292,12 +292,12 @@ unsigned create_io_inherit_block(WORD *size, BYTE **block)
*handle_ptr = INVALID_HANDLE_VALUE;
}
wxflag_ptr++; handle_ptr++;
}
}
return TRUE;
}
/* INTERNAL: Set up all file descriptors,
* as well as default streams (stdin, stderr and stdout)
/* INTERNAL: Set up all file descriptors,
* as well as default streams (stdin, stderr and stdout)
*/
void msvcrt_init_io(void)
{
@ -342,7 +342,7 @@ void msvcrt_init_io(void)
{
#ifndef __REACTOS__
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
GetCurrentProcess(), &fdesc[0].handle, 0, TRUE,
GetCurrentProcess(), &fdesc[0].handle, 0, TRUE,
DUPLICATE_SAME_ACCESS);
#else
fdesc[0].handle = GetStdHandle(STD_INPUT_HANDLE);
@ -355,7 +355,7 @@ void msvcrt_init_io(void)
{
#ifndef __REACTOS__
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_OUTPUT_HANDLE),
GetCurrentProcess(), &fdesc[1].handle, 0, TRUE,
GetCurrentProcess(), &fdesc[1].handle, 0, TRUE,
DUPLICATE_SAME_ACCESS);
#else
fdesc[1].handle = GetStdHandle(STD_OUTPUT_HANDLE);
@ -368,7 +368,7 @@ void msvcrt_init_io(void)
{
#ifndef __REACTOS__
DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE),
GetCurrentProcess(), &fdesc[2].handle, 0, TRUE,
GetCurrentProcess(), &fdesc[2].handle, 0, TRUE,
DUPLICATE_SAME_ACCESS);
#else
fdesc[2].handle = GetStdHandle(STD_ERROR_HANDLE);
@ -409,7 +409,7 @@ static int flush_buffer(FILE* file)
}
/* INTERNAL: Allocate stdio file buffer */
static void alloc_buffer(FILE* file)
void alloc_buffer(FILE* file)
{
file->_base = calloc(BUFSIZ,1);
if(file->_base) {
@ -722,7 +722,7 @@ int CDECL _dup2(int od, int nd)
int CDECL _dup(int od)
{
int fd, ret;
LOCK_FILES();
fd = fdstart;
if (_dup2(od, fd) == 0)
@ -1393,7 +1393,7 @@ int CDECL _sopen( const char *path, int oflags, int shflags, ... )
else
creation = OPEN_EXISTING;
}
switch( shflags )
{
case _SH_DENYRW:
@ -2030,12 +2030,12 @@ wint_t CDECL fgetwc(FILE* file)
wcp = (char *)&wc;
for(i=0; i<sizeof(wc); i++)
{
if (file->_cnt>0)
if (file->_cnt>0)
{
file->_cnt--;
chp = file->_ptr++;
wcp[i] = *chp;
}
}
else
{
j = _filbuf(file);
@ -2050,7 +2050,7 @@ wint_t CDECL fgetwc(FILE* file)
}
return wc;
}
c = fgetc(file);
if ((*__p___mb_cur_max() > 1) && isleadbyte(c))
{
@ -2290,37 +2290,6 @@ int CDECL fputc(int c, FILE* file)
}
}
/*********************************************************************
* _flsbuf (MSVCRT.@)
*/
int CDECL _flsbuf(int c, FILE* file)
{
/* Flush output buffer */
if(file->_bufsiz == 0 && !(file->_flag & _IONBF)) {
alloc_buffer(file);
}
if(!(file->_flag & _IOWRT)) {
if(file->_flag & _IORW) {
file->_flag |= _IOWRT;
} else {
return EOF;
}
}
if(file->_bufsiz) {
int res=flush_buffer(file);
return res?res : fputc(c, file);
} else {
unsigned char cc=c;
int len;
/* set _cnt to 0 for unbuffered FILEs */
file->_cnt = 0;
len = _write(file->_file, &cc, 1);
if (len == 1) return c & 0xff;
file->_flag |= _IOERR;
return EOF;
}
}
/*********************************************************************
* _fputchar (MSVCRT.@)
*/
@ -2487,7 +2456,7 @@ int CDECL fsetpos(FILE* file, const fpos_t *pos)
/* Discard buffered input */
file->_cnt = 0;
file->_ptr = file->_base;
/* Reset direction of i/o */
if(file->_flag & _IORW) {
file->_flag &= ~(_IOREAD|_IOWRT);
@ -2560,7 +2529,7 @@ int CDECL fputs(const char *s, FILE* file)
if (!(fdesc[file->_file].wxflag & WX_TEXT))
return fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
for (i=0; i<len; i++)
if (fputc(s[i], file) == EOF)
if (fputc(s[i], file) == EOF)
return EOF;
return 0;
}
@ -2578,7 +2547,7 @@ int CDECL fputws(const wchar_t *s, FILE* file)
if ((s[i] == '\n') && (fputc('\r', file) == EOF))
return WEOF;
if (fputwc(s[i], file) == WEOF)
return WEOF;
return WEOF;
}
return 0;
}
@ -2814,142 +2783,6 @@ FILE* CDECL tmpfile(void)
return file;
}
#ifndef USE_NEW_SPRINTF
/*********************************************************************
* vfprintf (MSVCRT.@)
*/
int CDECL vfprintf(FILE* file, const char *format, va_list valist)
{
char buf[2048], *mem = buf;
int written, resize = sizeof(buf), retval;
/* There are two conventions for vsnprintf failing:
* Return -1 if we truncated, or
* Return the number of bytes that would have been written
* The code below handles both cases
*/
while ((written = _vsnprintf(mem, resize, format, valist)) == -1 ||
written > resize)
{
resize = (written == -1 ? resize * 2 : written + 1);
if (mem != buf)
free (mem);
if (!(mem = malloc(resize)))
return EOF;
}
retval = fwrite(mem, sizeof(*mem), written, file);
if (mem != buf)
free (mem);
return retval;
}
/*********************************************************************
* vfwprintf (MSVCRT.@)
* FIXME:
* Is final char included in written (then resize is too big) or not
* (then we must test for equality too)?
*/
int CDECL vfwprintf(FILE* file, const wchar_t *format, va_list valist)
{
wchar_t buf[2048], *mem = buf;
int written, resize = sizeof(buf) / sizeof(wchar_t), retval;
/* See vfprintf comments */
while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 ||
written > resize)
{
resize = (written == -1 ? resize * 2 : written + sizeof(wchar_t));
if (mem != buf)
free (mem);
if (!(mem = malloc(resize*sizeof(*mem))))
return EOF;
}
/* Check if outputting to a text-file */
if (fdesc[file->_file].wxflag & WX_TEXT)
{
/* Convert each character and stop at the first invalid character. Behavior verified by tests under WinXP SP2 */
char chMultiByte[MB_LEN_MAX];
int nReturn;
wchar_t *p;
retval = 0;
for (p = mem; *p; p++)
{
nReturn = wctomb(chMultiByte, *p);
if(nReturn == -1)
break;
retval += fwrite(chMultiByte, 1, nReturn, file);
}
}
else
{
retval = fwrite(mem, sizeof(*mem), written, file);
}
if (mem != buf)
free (mem);
return retval;
}
/*********************************************************************
* vprintf (MSVCRT.@)
*/
int CDECL vprintf(const char *format, va_list valist)
{
return vfprintf(stdout,format,valist);
}
/*********************************************************************
* vwprintf (MSVCRT.@)
*/
int CDECL vwprintf(const wchar_t *format, va_list valist)
{
return vfwprintf(stdout,format,valist);
}
/*********************************************************************
* fprintf (MSVCRT.@)
*/
int CDECL fprintf(FILE* file, const char *format, ...)
{
va_list valist;
int res;
va_start(valist, format);
res = vfprintf(file, format, valist);
va_end(valist);
return res;
}
/*********************************************************************
* fwprintf (MSVCRT.@)
*/
int CDECL fwprintf(FILE* file, const wchar_t *format, ...)
{
va_list valist;
int res;
va_start(valist, format);
res = vfwprintf(file, format, valist);
va_end(valist);
return res;
}
/*********************************************************************
* printf (MSVCRT.@)
*/
int CDECL printf(const char *format, ...)
{
va_list valist;
int res;
va_start(valist, format);
res = vfprintf(stdout, format, valist);
va_end(valist);
return res;
}
#endif
/*********************************************************************
* ungetc (MSVCRT.@)
*/
@ -2986,21 +2819,6 @@ wint_t CDECL ungetwc(wint_t wc, FILE * file)
return mwc;
}
#ifndef USE_NEW_SPRINTF
/*********************************************************************
* wprintf (MSVCRT.@)
*/
int CDECL wprintf(const wchar_t *format, ...)
{
va_list valist;
int res;
va_start(valist, format);
res = vwprintf(format, valist);
va_end(valist);
return res;
}
#endif
/*********************************************************************
* _getmaxstdio (MSVCRT.@)
*/

View file

@ -1,882 +0,0 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PURPOSE: CRT's vsnprintf
* FILE: lib/sdk/crt/stdio/lnx_printf.c
* PROGRAMERS: David Welch
Eric Kohl
Gregor Schneider
* TODO:
* - Verify the implementation of '%Z'.
*/
/*
* Parts from linux/lib/vsprintf.c
* Lars Wirzenius & Linus Torvalds
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
#ifndef USE_NEW_SPRINTF
#include <precomp.h>
#include <wchar.h>
#include <tchar.h>
#define ZEROPAD 1 /* pad with zero */
#define SIGN 2 /* unsigned/signed */
#define PLUS 4 /* show plus */
#define SPACE 8 /* space if plus */
#define LEFT 16 /* left justified */
#define SPECIAL 32 /* 0x */
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
#define ZEROTRUNC 128 /* truncate zero's */
#define REMOVEHEX 256 /* remove 0x from BASE 16 */
static
__inline
int
do_div(long long *n, int base)
{
int a;
a = ((unsigned long long) *n) % (unsigned) base;
*n = ((unsigned long long) *n) / (unsigned) base;
return a;
}
static int skip_atoi(const char **s)
{
int i=0;
while (isdigit(**s))
i = i*10 + *((*s)++) - '0';
return i;
}
static char *
number(char * buf, char * end, long long num, int base, int size, int precision, int type)
{
char c,sign,tmp[66];
const char *digits;
const char *small_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
const char *large_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
digits = (type & LARGE) ? large_digits : small_digits;
if (type & LEFT)
type &= ~ZEROPAD;
if (base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN) {
if (num < 0) {
sign = '-';
num = -num;
size--;
} else if (type & PLUS) {
sign = '+';
size--;
} else if (type & SPACE) {
sign = ' ';
size--;
}
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base == 16)
size -= 2;
}
i = 0;
if ((num == 0) && (precision !=0))
tmp[i++] = '0';
else while (num != 0)
tmp[i++] = digits[do_div(&num,base)];
if (i > precision)
precision = i;
size -= precision;
if (!(type&(ZEROPAD+LEFT))) {
while(size-->0) {
if (buf <= end)
*buf = ' ';
++buf;
}
}
if (sign) {
if (buf <= end)
*buf = sign;
++buf;
}
if ((type & SPECIAL) && ((type & REMOVEHEX) == 0)) {
if (base==16) {
if (buf <= end)
*buf = '0';
++buf;
if (buf <= end)
*buf = digits[33];
++buf;
}
}
if (!(type & LEFT)) {
while (size-- > 0) {
if (buf <= end)
*buf = c;
++buf;
}
}
while (i < precision--) {
if (buf <= end)
*buf = '0';
++buf;
}
while (i-- > 0) {
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0) {
if (buf <= end)
*buf = ' ';
++buf;
}
return buf;
}
typedef struct {
unsigned int mantissal:32;
unsigned int mantissah:20;
unsigned int exponent:11;
unsigned int sign:1;
} ieee_double_t;
static __inline void fracrnd(double *number, int prec)
{
/* Shifts fractional digits to decimal places and compares to round table */
/* Only suitable to determine the exponent with more precision, not for normal rounding */
/* Incoming numbers are expected to range from approx -10.0 to 10.0 */
int lpos = 1, ubound, sign = 1;
long decimal = abs((long)*number);
double frac = (*number - decimal) * 10;
long rt[] =
{
0,
9,
99,
999,
9999,
99999,
999999,
9999999,
99999999,
999999999
};
if (*number < 0)
{
sign = -1;
}
ubound = min(prec, sizeof(rt)/sizeof(*rt) - 1);
while ((long)frac % 10 != 0 && lpos < ubound)
{
frac *= 10;
lpos++;
}
if (abs((long)frac) == rt[lpos])
{
*number = sign * (decimal + 1);
}
}
static char *
numberf(char * buf, char * end, double num, char exp_sign, int size, int precision, int type)
{
double exponent = 0.0;
double e = 0.0;
long ie;
int i = 0;
int j = 0;
int ro = 0;
int isize;
double num2, frac, intr;
double p;
char c, sign, digits[66];
char *tmp;
union
{
double* __n;
ieee_double_t* n;
} n;
n.__n = &num;
if ( exp_sign == 'g' || exp_sign == 'G' || exp_sign == 'e' || exp_sign == 'E' )
{
ie = ((unsigned int)n.n->exponent - (unsigned int)0x3ff);
if (num != 0.0)
{
exponent = ie/3.321928;
}
}
if ( exp_sign == 'g' || exp_sign == 'G' )
{
type |= ZEROTRUNC;
if ( exponent < -4 || fabs(exponent) >= precision )
exp_sign -= 2; // g -> e and G -> E
else
exp_sign = 'f';
if (type & SPECIAL) precision--;
}
if ( exp_sign == 'e' || exp_sign == 'E' )
{
if (num != 0.0)
{
/* Find a suitable exponent */
frac = modf(exponent, &e);
num2 = num/pow(10.0L, (long double)e);
/* Check if rounding is possible */
fracrnd(&num2, precision);
if (num2 < 1.0 && num2 > -1.0)
{
e--;
}
else if (num2 <= -10.0 || num2 >= 10.0)
{
e++;
}
}
/* size-5 because "e+abc" is going to follow */
buf = numberf(buf, end, num/pow(10.0L,(long double)e), 'f', size-5, precision, type);
isize = 4;
while(*(buf-1) == ' ')
{
isize++;
--buf;
}
if (buf <= end)
*buf = exp_sign;
++buf;
size--;
ie = (long)e;
type = LEFT | SIGN | PLUS;
buf = number(buf, end, ie, 10, isize, 3, type);
return buf;
}
if ( exp_sign == 'f' )
{
if (type & LEFT)
type &= ~ZEROPAD;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (num < 0)
{
sign = '-';
num = fabs(num);
size--;
}
else if (type & PLUS)
{
sign = '+';
size--;
}
else if (type & SPACE)
{
sign = ' ';
size--;
}
frac = modf(num,&intr);
// # flags forces a . and prevents truncation of trailing zero's
if ( precision > 0 )
{
i = precision-1;
while ( i >= 0 )
{
frac*=10.0L;
frac = modf(frac, &p);
digits[i] = (int)p + '0';
i--;
}
i = precision;
size -= precision;
}
if ( precision >= 1 || type & SPECIAL)
{
digits[i++] = '.';
size--;
}
ro = 0;
if ( frac > 0.5 )
{
ro = 1;
}
if ( intr == 0.0 )
{
digits[i++] = '0';
size--;
}
else
{
while ( intr > 0.0 )
{
p = intr;
intr/=10.0L;
modf(intr, &intr);
p -= 10.0*intr;
digits[i++] = (int)p + '0';
size--;
}
}
j = 0;
while ( j < i && ro == 1)
{
if ( digits[j] >= '0' && digits[j] <= '8' )
{
digits[j]++;
ro = 0;
}
else if ( digits[j] == '9' )
{
digits[j] = '0';
}
j++;
}
if ( ro == 1 )
digits[i++] = '1';
digits[i] = 0;
if (!(type & (ZEROPAD+LEFT)))
{
while(size-->0)
{
if (buf <= end)
*buf = ' ';
++buf;
}
}
if (sign)
{
if (buf <= end)
*buf = sign;
++buf;
}
if (!(type & (ZEROPAD+LEFT)))
{
while(size-->0)
{
if (buf <= end)
*buf = ' ';
++buf;
}
}
if (!(type & LEFT))
{
while (size-- > 0)
{
if (buf <= end)
*buf = c;
++buf;
}
}
tmp = digits;
if ( type & ZEROTRUNC && ((type & SPECIAL) != SPECIAL) )
{
j = 0;
while ( j < i && ( *tmp == '0' || *tmp == '.' ))
{
tmp++;
i--;
}
}
while (i-- > 0)
{
if (buf <= end)
*buf = tmp[i];
++buf;
}
while (size-- > 0)
{
if (buf <= end)
*buf = ' ';
buf++;
}
}
return buf;
}
static char*
string(char* buf, char* end, const char* s, int len, int field_width, int precision, int flags)
{
int i;
char c;
c = (flags & ZEROPAD) ? '0' : ' ';
if (s == NULL)
{
s = "<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && s[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
++buf;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = *s++;
++buf;
}
while (len < field_width--)
{
if (buf <= end)
*buf = ' ';
++buf;
}
return buf;
}
static char*
stringw(char* buf, char* end, const wchar_t* sw, int len, int field_width, int precision, int flags)
{
int i;
char c;
c = (flags & ZEROPAD) ? '0' : ' ';
if (sw == NULL)
{
sw = L"<NULL>";
len = 6;
}
else
{
if (len == -1)
{
len = 0;
while ((unsigned int)len < (unsigned int)precision && sw[len])
len++;
}
else
{
if ((unsigned int)len > (unsigned int)precision)
len = precision;
}
}
if (!(flags & LEFT))
while (len < field_width--)
{
if (buf <= end)
*buf = c;
buf++;
}
for (i = 0; i < len; ++i)
{
if (buf <= end)
*buf = (unsigned char)(*sw++);
buf++;
}
while (len < field_width--)
{
if (buf <= end)
*buf = ' ';
buf++;
}
return buf;
}
/*
* @implemented
*/
int __cdecl lnx_vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
{
int len;
unsigned long long num;
double _double;
int base;
char *str, *end;
const char *s;
const wchar_t *sw;
int flags; /* flags to number() */
int field_width; /* width of output field */
int precision; /* min. # of digits for integers; max
number of chars for from string */
int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
str = buf;
end = buf + cnt - 1;
if (end < buf - 1) {
end = ((char *) -1);
cnt = end - buf + 1;
}
for ( ; *fmt ; ++fmt) {
if (*fmt != '%') {
if (str <= end)
*str = *fmt;
++str;
continue;
}
/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-': flags |= LEFT; goto repeat;
case '+': flags |= PLUS; goto repeat;
case ' ': flags |= SPACE; goto repeat;
case '#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat;
}
/* get field width */
field_width = -1;
if (isdigit(*fmt))
field_width = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
field_width = va_arg(args, int);
if (field_width < 0) {
field_width = -field_width;
flags |= LEFT;
}
}
/* get the precision */
precision = -1;
if (*fmt == '.') {
++fmt;
if (isdigit(*fmt))
precision = skip_atoi(&fmt);
else if (*fmt == '*') {
++fmt;
/* it's the next argument */
precision = va_arg(args, int);
}
if (precision < 0)
precision = 0;
}
/* get the conversion qualifier */
qualifier = -1;
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
qualifier = *fmt;
++fmt;
} else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
qualifier = *fmt;
fmt += 3;
} else if (*fmt == 'I' && *(fmt+1) == '3' && *(fmt+2) == '2') {
qualifier = 'l';
fmt += 3;
} else if (*fmt == 'F' && *(fmt+1) == 'p') {
fmt += 1;
flags |= REMOVEHEX;
}
/* default base */
base = 10;
switch (*fmt) {
case 'c': /* finished */
if (qualifier == 'l' || qualifier == 'w') {
wchar_t sw1[2];
/* print unicode string */
sw1[0] = (wchar_t) va_arg(args, int);
sw1[1] = 0;
str = stringw(str, end, (wchar_t *)&sw1, -1, field_width, precision, flags);
} else {
char s1[2];
/* print ascii string */
s1[0] = ( unsigned char) va_arg(args, int);
s1[1] = 0;
str = string(str, end, (char *)&s1, -1, field_width, precision, flags);
}
continue;
case 'C': /* finished */
if (!(flags & LEFT))
while (--field_width > 0) {
if (str <= end)
*str = ' ';
++str;
}
if (qualifier == 'h') {
if (str <= end)
*str = (unsigned char) va_arg(args, int);
++str;
} else {
if (str <= end)
*str = (unsigned char)(wchar_t) va_arg(args, int);
++str;
}
while (--field_width > 0) {
if (str <= end)
*str = ' ';
++str;
}
continue;
case 's': /* finished */
if (qualifier == 'l' || qualifier == 'w') {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
} else {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
}
continue;
case 'S':
if (qualifier == 'h') {
/* print ascii string */
s = va_arg(args, char *);
str = string(str, end, s, -1, field_width, precision, flags);
} else {
/* print unicode string */
sw = va_arg(args, wchar_t *);
str = stringw(str, end, sw, -1, field_width, precision, flags);
}
continue;
case 'Z':
if (qualifier == 'w') {
/* print counted unicode string */
PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
sw = NULL;
len = -1;
} else {
sw = pus->Buffer;
len = pus->Length / sizeof(WCHAR);
}
str = stringw(str, end, sw, len, field_width, precision, flags);
} else {
/* print counted ascii string */
PANSI_STRING pus = va_arg(args, PANSI_STRING);
if ((pus == NULL) || (pus->Buffer == NULL)) {
s = NULL;
len = -1;
} else {
s = pus->Buffer;
len = pus->Length;
}
str = string(str, end, s, len, field_width, precision, flags);
}
continue;
case 'p':
if ((flags & LARGE) == 0)
flags |= LARGE;
if (field_width == -1) {
field_width = 2 * sizeof(void *);
flags |= ZEROPAD;
}
str = number(str, end,
(uintptr_t) va_arg(args, void *), 16,
field_width, precision, flags);
continue;
case 'n':
/* FIXME: What does C99 say about the overflow case here? */
if (qualifier == 'l') {
long * ip = va_arg(args, long *);
*ip = (str - buf);
} else {
int * ip = va_arg(args, int *);
*ip = (str - buf);
}
continue;
/* float number formats - set up the flags and "break" */
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
_double = (double)va_arg(args, double);
if ( _isnan(_double) ) {
s = "Nan";
len = 3;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else if ( _isinf(_double) < 0 ) {
s = "-Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else if ( _isinf(_double) > 0 ) {
s = "+Inf";
len = 4;
while ( len > 0 ) {
if (str <= end)
*str = *s++;
++str;
len --;
}
} else {
if ( precision == -1 )
precision = 6;
str = numberf(str, end, _double, *fmt, field_width, precision, flags);
}
continue;
/* integer number formats - set up the flags and "break" */
case 'o':
base = 8;
break;
case 'b':
base = 2;
break;
case 'X':
flags |= LARGE;
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
if (*fmt) {
if (str <= end)
*str = *fmt;
++str;
} else
--fmt;
continue;
}
if (qualifier == 'I')
num = va_arg(args, unsigned long long);
else if (qualifier == 'l') {
if (flags & SIGN)
num = va_arg(args, long);
else
num = va_arg(args, unsigned long);
}
else if (qualifier == 'h') {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
else {
if (flags & SIGN)
num = va_arg(args, int);
else
num = va_arg(args, unsigned int);
}
str = number(str, end, num, base, field_width, precision, flags);
}
if (str <= end)
*str = '\0';
else if (cnt > 0)
/* don't write out a null byte if the buf size is zero */
*end = '\0';
return str-buf;
}
/*
* @implemented
*/
int lnx_sprintf(char * buf, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=lnx_vsnprintf(buf,MAXLONG,fmt,args);
va_end(args);
return i;
}
#if 0
/*
* @implemented
*/
int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=_vsnprintf(buf,cnt,fmt,args);
va_end(args);
return i;
}
/*
* @implemented
*/
int __cdecl vsprintf(char *buf, const char *fmt, va_list args)
{
return _vsnprintf(buf,MAXLONG,fmt,args);
}
#endif
/* EOF */
#endif

View file

@ -18,6 +18,7 @@ _ecvt (double value, int ndigits, int *decpt, int *sign)
static char ecvtbuf[DBL_MAX_10_EXP + 10];
char *cvtbuf, *s, *d;
if (ndigits < 0) ndigits = 0;
s = cvtbuf = (char*)malloc(ndigits + NUMBER_EFMT);
d = ecvtbuf;

View file

@ -54,6 +54,9 @@ strtoull(const char *nptr, char **endptr, int base)
if (any < 0)
{
acc = ULLONG_MAX;
#ifndef _LIBCNT_
__set_errno(ERANGE);
#endif
}
else if (neg)
acc = -acc;

View file

@ -213,677 +213,6 @@ double CDECL wcstod(const wchar_t* lpszStr, wchar_t** end)
TRACE("returning %g\n", ret);
return ret;
}
typedef struct pf_output_t
{
int used;
int len;
BOOL unicode;
union {
LPWSTR W;
LPSTR A;
} buf;
} pf_output;
typedef struct pf_flags_t
{
char Sign, LeftAlign, Alternate, PadZero;
int FieldLength, Precision;
char IntegerLength, IntegerDouble;
char WideString;
char Format;
} pf_flags;
/*
* writes a string of characters to the output
* returns -1 if the string doesn't fit in the output buffer
* return the length of the string if all characters were written
*/
static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
{
int space = out->len - out->used;
if( len < 0 )
len = strlenW( str );
if( out->unicode )
{
LPWSTR p = out->buf.W + out->used;
if( space >= len )
{
memcpy( p, str, len*sizeof(WCHAR) );
out->used += len;
return len;
}
if( space > 0 )
memcpy( p, str, space*sizeof(WCHAR) );
out->used += len;
}
else
{
int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
LPSTR p = out->buf.A + out->used;
if( space >= n )
{
WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
out->used += n;
return len;
}
if( space > 0 )
WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
out->used += n;
}
return -1;
}
static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
{
int space = out->len - out->used;
if( len < 0 )
len = strlen( str );
if( !out->unicode )
{
LPSTR p = out->buf.A + out->used;
if( space >= len )
{
memcpy( p, str, len );
out->used += len;
return len;
}
if( space > 0 )
memcpy( p, str, space );
out->used += len;
}
else
{
int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
LPWSTR p = out->buf.W + out->used;
if( space >= n )
{
MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
out->used += n;
return len;
}
if( space > 0 )
MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
out->used += n;
}
return -1;
}
/* pf_fill: takes care of signs, alignment, zero and field padding */
static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
{
int i, r = 0;
if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
flags->Sign = 0;
if( left && flags->Sign )
{
flags->FieldLength--;
if( flags->PadZero )
r = pf_output_stringA( out, &flags->Sign, 1 );
}
if( ( !left && flags->LeftAlign ) ||
( left && !flags->LeftAlign ))
{
for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
{
if( left && flags->PadZero )
r = pf_output_stringA( out, "0", 1 );
else
r = pf_output_stringA( out, " ", 1 );
}
}
if( left && flags->Sign && !flags->PadZero )
r = pf_output_stringA( out, &flags->Sign, 1 );
return r;
}
static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
int len, pf_flags *flags )
{
int r = 0;
if( len < 0 )
len = strlenW( str );
if (flags->Precision >= 0 && flags->Precision < len)
len = flags->Precision;
r = pf_fill( out, len, flags, 1 );
if( r>=0 )
r = pf_output_stringW( out, str, len );
if( r>=0 )
r = pf_fill( out, len, flags, 0 );
return r;
}
static inline int pf_output_format_A( pf_output *out, LPCSTR str,
int len, pf_flags *flags )
{
int r = 0;
if( len < 0 )
len = strlen( str );
if (flags->Precision >= 0 && flags->Precision < len)
len = flags->Precision;
r = pf_fill( out, len, flags, 1 );
if( r>=0 )
r = pf_output_stringA( out, str, len );
if( r>=0 )
r = pf_fill( out, len, flags, 0 );
return r;
}
#ifndef USE_NEW_SPRINTF
static int pf_handle_string_format( pf_output *out, const void* str, int len,
pf_flags *flags, BOOL capital_letter)
{
if(str == NULL) /* catch NULL pointer */
return pf_output_format_A( out, "(null)", -1, flags);
/* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
if(flags->WideString || flags->IntegerLength == 'l')
return pf_output_format_W( out, str, len, flags);
if(flags->IntegerLength == 'h')
return pf_output_format_A( out, str, len, flags);
/* %s,%c -> chars in ansi functions & wchars in unicode
* %S,%C -> wchars in ansi functions & chars in unicode */
if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
return pf_output_format_A( out, str, len, flags);
else
return pf_output_format_W( out, str, len, flags);
}
static inline BOOL pf_is_integer_format( char fmt )
{
static const char float_fmts[] = "diouxX";
if (!fmt)
return FALSE;
return strchr( float_fmts, fmt ) ? TRUE : FALSE;
}
static inline BOOL pf_is_double_format( char fmt )
{
static const char float_fmts[] = "aeEfgG";
if (!fmt)
return FALSE;
return strchr( float_fmts, fmt ) ? TRUE : FALSE;
}
static inline BOOL pf_is_valid_format( char fmt )
{
static const char float_fmts[] = "acCdeEfgGinouxX";
if (!fmt)
return FALSE;
return strchr( float_fmts, fmt ) ? TRUE : FALSE;
}
static void pf_rebuild_format_string( char *p, pf_flags *flags )
{
*p++ = '%';
if( flags->Sign )
*p++ = flags->Sign;
if( flags->LeftAlign )
*p++ = flags->LeftAlign;
if( flags->Alternate )
*p++ = flags->Alternate;
if( flags->PadZero )
*p++ = flags->PadZero;
if( flags->FieldLength )
{
lnx_sprintf(p, "%d", flags->FieldLength);
p += strlen(p);
}
if( flags->Precision >= 0 )
{
lnx_sprintf(p, ".%d", flags->Precision);
p += strlen(p);
}
*p++ = flags->Format;
*p++ = 0;
}
/* pf_integer_conv: prints x to buf, including alternate formats and
additional precision digits, but not field characters or the sign */
static void pf_integer_conv( char *buf, unsigned int buf_len, pf_flags *flags,
LONGLONG x )
{
unsigned int base;
const char *digits;
int i, j, k;
char number[40], *tmp = number;
if( buf_len > sizeof number )
tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
base = 10;
if( flags->Format == 'o' )
base = 8;
else if( flags->Format == 'x' || flags->Format == 'X' )
base = 16;
if( flags->Format == 'X' )
digits = "0123456789ABCDEFX";
else
digits = "0123456789abcdefx";
if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
{
x = -x;
flags->Sign = '-';
}
/* Do conversion (backwards) */
i = 0;
if( x == 0 && flags->Precision )
tmp[i++] = '0';
else
while( x != 0 )
{
j = (ULONGLONG) x % base;
x = (ULONGLONG) x / base;
tmp[i++] = digits[j];
}
k = flags->Precision - i;
while( k-- > 0 )
tmp[i++] = '0';
if( flags->Alternate )
{
if( base == 16 )
{
tmp[i++] = digits[16];
tmp[i++] = '0';
}
else if( base == 8 && tmp[i-1] != '0' )
tmp[i++] = '0';
}
/* Reverse for buf */
j = 0;
while( i-- > 0 )
buf[j++] = tmp[i];
buf[j] = '\0';
/* Adjust precision so pf_fill won't truncate the number later */
flags->Precision = strlen( buf );
if( tmp != number )
HeapFree( GetProcessHeap(), 0, tmp );
return;
}
/*********************************************************************
* pf_vsnprintf (INTERNAL)
*
* implements both A and W vsnprintf functions
*/
static int pf_vsnprintf( pf_output *out, const WCHAR *format, va_list valist )
{
int r;
LPCWSTR q, p = format;
pf_flags flags;
TRACE("format is %s\n",debugstr_w(format));
while (*p)
{
q = strchrW( p, '%' );
/* there's no % characters left, output the rest of the string */
if( !q )
{
r = pf_output_stringW(out, p, -1);
if( r<0 )
return r;
p += r;
continue;
}
/* there's characters before the %, output them */
if( q != p )
{
r = pf_output_stringW(out, p, q - p);
if( r<0 )
return r;
p = q;
}
/* we must be at a % now, skip over it */
assert( *p == '%' );
p++;
/* output a single % character */
if( *p == '%' )
{
r = pf_output_stringW(out, p++, 1);
if( r<0 )
return r;
continue;
}
/* parse the flags */
memset( &flags, 0, sizeof flags );
while (*p)
{
if( *p == '+' || *p == ' ' )
{
if ( flags.Sign != '+' )
flags.Sign = *p;
}
else if( *p == '-' )
flags.LeftAlign = *p;
else if( *p == '0' )
flags.PadZero = *p;
else if( *p == '#' )
flags.Alternate = *p;
else
break;
p++;
}
/* deal with the field width specifier */
flags.FieldLength = 0;
if( *p == '*' )
{
flags.FieldLength = va_arg( valist, int );
if (flags.FieldLength < 0)
{
flags.LeftAlign = '-';
flags.FieldLength = -flags.FieldLength;
}
p++;
}
else while( isdigit(*p) )
{
flags.FieldLength *= 10;
flags.FieldLength += *p++ - '0';
}
/* deal with precision */
flags.Precision = -1;
if( *p == '.' )
{
flags.Precision = 0;
p++;
if( *p == '*' )
{
flags.Precision = va_arg( valist, int );
p++;
}
else while( isdigit(*p) )
{
flags.Precision *= 10;
flags.Precision += *p++ - '0';
}
}
/* deal with integer width modifier */
while( *p )
{
if( *p == 'h' || *p == 'l' || *p == 'L' )
{
flags.IntegerLength = *p;
p++;
}
else if( *p == 'I' )
{
if( *(p+1) == '6' && *(p+2) == '4' )
{
flags.IntegerDouble++;
p += 3;
}
else if( *(p+1) == '3' && *(p+2) == '2' )
p += 3;
else if( isdigit(*(p+1)) || *(p+1) == 0 )
break;
else
p++;
}
else if( *p == 'w' )
flags.WideString = *p++;
else if( *p == 'F' )
p++; /* ignore */
else
break;
}
flags.Format = *p;
r = 0;
/* output a string */
if( flags.Format == 's' || flags.Format == 'S' )
r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
&flags, (flags.Format == 'S') );
/* output a single character */
else if( flags.Format == 'c' || flags.Format == 'C' )
{
INT ch = va_arg( valist, int );
r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
}
/* output a pointer */
else if( flags.Format == 'p' )
{
char pointer[11];
flags.PadZero = 0;
if( flags.Alternate )
lnx_sprintf(pointer, "0X%08lX", va_arg(valist, long));
else
lnx_sprintf(pointer, "%08lX", va_arg(valist, long));
r = pf_output_format_A( out, pointer, -1, &flags );
}
/* deal with %n */
else if( flags.Format == 'n' )
{
int *x = va_arg(valist, int *);
*x = out->used;
}
/* deal with 64-bit integers */
else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
{
char number[40], *x = number;
/* Estimate largest possible required buffer size:
* Chooses the larger of the field or precision
* Includes extra bytes: 1 byte for null, 1 byte for sign,
4 bytes for exponent, 2 bytes for alternate formats, 1 byte
for a decimal, and 1 byte for an additional float digit. */
unsigned x_len = ((flags.FieldLength > flags.Precision) ?
flags.FieldLength : flags.Precision) + 10;
if( x_len >= sizeof number)
x = HeapAlloc( GetProcessHeap(), 0, x_len );
pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
r = pf_output_format_A( out, x, -1, &flags );
if( x != number )
HeapFree( GetProcessHeap(), 0, x );
}
/* deal with integers and floats using libc's printf */
else if( pf_is_valid_format( flags.Format ) )
{
char fmt[20], number[40], *x = number;
/* Estimate largest possible required buffer size:
* Chooses the larger of the field or precision
* Includes extra bytes: 1 byte for null, 1 byte for sign,
4 bytes for exponent, 2 bytes for alternate formats, 1 byte
for a decimal, and 1 byte for an additional float digit. */
unsigned x_len = ((flags.FieldLength > flags.Precision) ?
flags.FieldLength : flags.Precision) + 10;
if( x_len >= sizeof number)
x = HeapAlloc( GetProcessHeap(), 0, x_len );
pf_rebuild_format_string( fmt, &flags );
if( pf_is_double_format( flags.Format ) )
lnx_sprintf( x, fmt, va_arg(valist, double) );
else
lnx_sprintf( x, fmt, va_arg(valist, int) );
r = pf_output_stringA( out, x, -1 );
if( x != number )
HeapFree( GetProcessHeap(), 0, x );
}
else
continue;
if( r<0 )
return r;
p++;
}
/* check we reached the end, and null terminate the string */
assert( *p == 0 );
pf_output_stringW( out, p, 1 );
return out->used - 1;
}
/*********************************************************************
* _vsnprintf (MSVCRT.@)
*/
int CDECL _vsnprintf( char *str, size_t len,
const char *format, va_list valist )
{
DWORD sz;
LPWSTR formatW = NULL;
pf_output out;
int r;
out.unicode = FALSE;
out.buf.A = str;
out.used = 0;
out.len = len;
if( format )
{
sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
}
r = pf_vsnprintf( &out, formatW, valist );
HeapFree( GetProcessHeap(), 0, formatW );
return r;
}
/*********************************************************************
* vsprintf (MSVCRT.@)
*/
int CDECL vsprintf( char *str, const char *format, va_list valist)
{
return _vsnprintf(str, INT_MAX, format, valist);
}
/*********************************************************************
* _snprintf (MSVCRT.@)
*/
int CDECL _snprintf(char *str, size_t len, const char *format, ...)
{
int retval;
va_list valist;
va_start(valist, format);
retval = _vsnprintf(str, len, format, valist);
va_end(valist);
return retval;
}
/*********************************************************************
* _vsnwsprintf (MSVCRT.@)
*/
int CDECL _vsnwprintf( wchar_t *str, size_t len,
const wchar_t *format, va_list valist )
{
pf_output out;
out.unicode = TRUE;
out.buf.W = str;
out.used = 0;
out.len = len;
return pf_vsnprintf( &out, format, valist );
}
/*********************************************************************
* _snwprintf (MSVCRT.@)
*/
int CDECL _snwprintf( wchar_t *str, size_t len, const wchar_t *format, ...)
{
int retval;
va_list valist;
va_start(valist, format);
retval = _vsnwprintf(str, len, format, valist);
va_end(valist);
return retval;
}
/*********************************************************************
* sprintf (MSVCRT.@)
*/
int CDECL sprintf( char *str, const char *format, ... )
{
va_list ap;
int r;
va_start( ap, format );
r = _vsnprintf( str, INT_MAX, format, ap );
va_end( ap );
return r;
}
/*********************************************************************
* swprintf (MSVCRT.@)
*/
int CDECL swprintf( wchar_t *str, const wchar_t *format, ... )
{
va_list ap;
int r;
va_start( ap, format );
r = _vsnwprintf( str, INT_MAX, format, ap );
va_end( ap );
return r;
}
/*********************************************************************
* vswprintf (MSVCRT.@)
*/
int CDECL vswprintf( wchar_t* str, const wchar_t* format, va_list args )
{
return _vsnwprintf( str, INT_MAX, format, args );
}
#endif
#endif
/*********************************************************************
@ -1052,97 +381,6 @@ size_t CDECL wcstombs(char *mbstr, const wchar_t *wcstr, size_t count)
}
#endif
#ifndef __REACTOS__
/*********************************************************************
* iswalnum (MSVCRT.@)
*/
INT CDECL iswalnum( wchar_t wc )
{
return isalnumW( wc );
}
/*********************************************************************
* iswalpha (MSVCRT.@)
*/
INT CDECL iswalpha( wchar_t wc )
{
return isalphaW( wc );
}
/*********************************************************************
* iswcntrl (MSVCRT.@)
*/
INT CDECL iswcntrl( wchar_t wc )
{
return iscntrlW( wc );
}
/*********************************************************************
* iswdigit (MSVCRT.@)
*/
INT CDECL iswdigit( wchar_t wc )
{
return isdigitW( wc );
}
/*********************************************************************
* iswgraph (MSVCRT.@)
*/
INT CDECL iswgraph( wchar_t wc )
{
return isgraphW( wc );
}
/*********************************************************************
* iswlower (MSVCRT.@)
*/
INT CDECL iswlower( wchar_t wc )
{
return islowerW( wc );
}
/*********************************************************************
* iswprint (MSVCRT.@)
*/
INT CDECL iswprint( wchar_t wc )
{
return isprintW( wc );
}
/*********************************************************************
* iswpunct (MSVCRT.@)
*/
INT CDECL iswpunct( wchar_t wc )
{
return ispunctW( wc );
}
/*********************************************************************
* iswspace (MSVCRT.@)
*/
INT CDECL iswspace( wchar_t wc )
{
return isspaceW( wc );
}
/*********************************************************************
* iswupper (MSVCRT.@)
*/
INT CDECL iswupper( wchar_t wc )
{
return isupperW( wc );
}
/*********************************************************************
* iswxdigit (MSVCRT.@)
*/
INT CDECL iswxdigit( wchar_t wc )
{
return isxdigitW( wc );
}
#endif
/*********************************************************************
* wcscpy_s (MSVCRT.@)
*/