Author: tkreuzer
Date: Mon Jan 3 01:07:54 2011
New Revision: 50273
URL:
http://svn.reactos.org/svn/reactos?rev=50273&view=rev
Log:
[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
Added:
trunk/reactos/lib/sdk/crt/printf/_sxprintf.c (with props)
Modified:
trunk/reactos/lib/sdk/crt/printf/_snprintf.c
trunk/reactos/lib/sdk/crt/printf/_snwprintf.c
trunk/reactos/lib/sdk/crt/printf/_vsnprintf.c
trunk/reactos/lib/sdk/crt/printf/_vsnwprintf.c
trunk/reactos/lib/sdk/crt/printf/printf.c
trunk/reactos/lib/sdk/crt/printf/sprintf.c
trunk/reactos/lib/sdk/crt/printf/streamout.c
trunk/reactos/lib/sdk/crt/printf/swprintf.c
trunk/reactos/lib/sdk/crt/printf/vsprintf.c
trunk/reactos/lib/sdk/crt/printf/vswprintf.c
Modified: trunk/reactos/lib/sdk/crt/printf/_snprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/_snprin…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/_snprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/_snprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -6,34 +6,7 @@
* PROGRAMMER: Timo Kreuzer
*/
-#include <stdio.h>
-#include <stdarg.h>
-#include <tchar.h>
+#define _sxprintf _snprintf
+#define USE_COUNT 1
-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"
Modified: trunk/reactos/lib/sdk/crt/printf/_snwprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/_snwpri…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/_snwprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/_snwprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -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"
Added: trunk/reactos/lib/sdk/crt/printf/_sxprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/_sxprin…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/_sxprintf.c (added)
+++ trunk/reactos/lib/sdk/crt/printf/_sxprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -1,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;
+}
+
+
Propchange: trunk/reactos/lib/sdk/crt/printf/_sxprintf.c
------------------------------------------------------------------------------
svn:eol-style = native
Modified: trunk/reactos/lib/sdk/crt/printf/_vsnprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/_vsnpri…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/_vsnprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/_vsnprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -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"
Modified: trunk/reactos/lib/sdk/crt/printf/_vsnwprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/_vsnwpr…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/_vsnwprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/_vsnwprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -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"
Modified: trunk/reactos/lib/sdk/crt/printf/printf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/printf.…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/printf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/printf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -22,6 +22,7 @@
va_start(argptr, format);
result = streamout(stdout, format, argptr);
va_end(argptr);
+
return result;
}
Modified: trunk/reactos/lib/sdk/crt/printf/sprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/sprintf…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/sprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/sprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -6,33 +6,7 @@
* PROGRAMMER: Timo Kreuzer
*/
-#include <stdio.h>
-#include <stdarg.h>
-#include <limits.h>
+#define _sxprintf sprintf
+#define USE_COUNT 0
-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"
Modified: trunk/reactos/lib/sdk/crt/printf/streamout.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/streamo…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/streamout.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/streamout.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -199,10 +199,14 @@
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 @@
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 @@
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 @@
}
- if (written == -1) return -8;
+ if (written == -1) return -1;
return written_all;
}
Modified: trunk/reactos/lib/sdk/crt/printf/swprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/swprint…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/swprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/swprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -6,34 +6,8 @@
* PROGRAMMER: Timo Kreuzer
*/
-#include <stdio.h>
-#include <stdarg.h>
-#include <limits.h>
+#define _sxprintf swprintf
+#define USE_COUNT 0
+#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"
Modified: trunk/reactos/lib/sdk/crt/printf/vsprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/vsprint…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/vsprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/vsprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -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"
Modified: trunk/reactos/lib/sdk/crt/printf/vswprintf.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/printf/vswprin…
==============================================================================
--- trunk/reactos/lib/sdk/crt/printf/vswprintf.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/printf/vswprintf.c [iso-8859-1] Mon Jan 3 01:07:54 2011
@@ -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"