mirror of
https://github.com/reactos/reactos.git
synced 2025-07-28 12:01:55 +00:00
[CRT]
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:
parent
916ec376d6
commit
92487266d8
11 changed files with 110 additions and 220 deletions
|
@ -6,34 +6,7 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf _snprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 1
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "_sxprintf.c"
|
||||||
|
|
|
@ -6,37 +6,8 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf _snwprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 1
|
||||||
|
#define _UNICODE
|
||||||
|
|
||||||
int _cdecl wstreamout(FILE *stream, const wchar_t *format, va_list argptr);
|
#include "_sxprintf.c"
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
68
reactos/lib/sdk/crt/printf/_sxprintf.c
Normal file
68
reactos/lib/sdk/crt/printf/_sxprintf.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,32 +6,8 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf _vsnprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 1
|
||||||
|
#define USE_VARARGS 1
|
||||||
|
|
||||||
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
|
#include "_sxprintf.c"
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,32 +6,9 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf _vsnwprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 1
|
||||||
|
#define USE_VARARGS 1
|
||||||
|
#define _UNICODE
|
||||||
|
|
||||||
int _cdecl wstreamout(FILE *stream, const wchar_t *format, va_list argptr);
|
#include "_sxprintf.c"
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -22,6 +22,7 @@ printf(const char *format, ...)
|
||||||
va_start(argptr, format);
|
va_start(argptr, format);
|
||||||
result = streamout(stdout, format, argptr);
|
result = streamout(stdout, format, argptr);
|
||||||
va_end(argptr);
|
va_end(argptr);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,33 +6,7 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf sprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 0
|
||||||
#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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#include "_sxprintf.c"
|
||||||
|
|
|
@ -199,10 +199,14 @@ static
|
||||||
int
|
int
|
||||||
streamout_char(FILE *stream, int chr)
|
streamout_char(FILE *stream, int chr)
|
||||||
{
|
{
|
||||||
/* Flush the buffer if neccessary */
|
/* Check if the buffer is full */
|
||||||
if (stream->_cnt < sizeof(TCHAR))
|
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;
|
*(TCHAR*)stream->_ptr = chr;
|
||||||
|
@ -587,7 +591,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
|
||||||
if (prefix)
|
if (prefix)
|
||||||
{
|
{
|
||||||
written = streamout_string(stream, prefix, prefixlen);
|
written = streamout_string(stream, prefix, prefixlen);
|
||||||
if (written == -1) return -3;
|
if (written == -1) return -1;
|
||||||
written_all += written;
|
written_all += written;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -604,7 +608,7 @@ streamout(FILE *stream, const TCHAR *format, va_list argptr)
|
||||||
written = streamout_wstring(stream, (wchar_t*)string, len);
|
written = streamout_wstring(stream, (wchar_t*)string, len);
|
||||||
else
|
else
|
||||||
written = streamout_astring(stream, (char*)string, len);
|
written = streamout_astring(stream, (char*)string, len);
|
||||||
if (written == -1) return -5;
|
if (written == -1) return -1;
|
||||||
written_all += written;
|
written_all += written;
|
||||||
|
|
||||||
#if 0 && SUPPORT_FLOAT
|
#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;
|
return written_all;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,34 +6,8 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf swprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 0
|
||||||
#include <limits.h>
|
#define _UNICODE
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "_sxprintf.c"
|
||||||
|
|
|
@ -6,32 +6,8 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf vsprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 0
|
||||||
#include <limits.h>
|
#define USE_VARARGS 1
|
||||||
|
|
||||||
int _cdecl streamout(FILE *stream, const char *format, va_list argptr);
|
#include "_sxprintf.c"
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,13 +6,9 @@
|
||||||
* PROGRAMMER: Timo Kreuzer
|
* PROGRAMMER: Timo Kreuzer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#define _sxprintf vswprintf
|
||||||
#include <stdarg.h>
|
#define USE_COUNT 0
|
||||||
#include <limits.h>
|
#define USE_VARARGS 1
|
||||||
|
#define _UNICODE
|
||||||
|
|
||||||
int
|
#include "_sxprintf.c"
|
||||||
__cdecl
|
|
||||||
vswprintf(wchar_t *buffer, const wchar_t *format, va_list argptr)
|
|
||||||
{
|
|
||||||
return _vsnwprintf(buffer, INT_MAX, format, argptr);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue