Improve new printf implementation:
- remove duplicated code and implement a generic function for all (v)s(w)(n)printf
- don't call _flsbuf for string streams, while this works on windows, it doesn't work correctly on reactos (bug!)
- Fix return error codes

svn path=/trunk/; revision=50273
This commit is contained in:
Timo Kreuzer 2011-01-03 01:07:54 +00:00
parent 916ec376d6
commit 92487266d8
11 changed files with 110 additions and 220 deletions

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,68 @@
/*
* 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
_cdecl
_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

@ -199,10 +199,14 @@ 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;
/* Strings are done now */
if (stream->_flag & _IOSTRG) return _TEOF;
/* Flush buffer for files */
return _flsbuf(chr, stream) != _TEOF;
}
*(TCHAR*)stream->_ptr = chr;
@ -587,7 +591,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 +608,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 +633,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"