[CRT]
authorTimo Kreuzer <timo.kreuzer@reactos.org>
Mon, 3 Jan 2011 01:07:54 +0000 (01:07 +0000)
committerTimo Kreuzer <timo.kreuzer@reactos.org>
Mon, 3 Jan 2011 01:07:54 +0000 (01:07 +0000)
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

reactos/lib/sdk/crt/printf/_snprintf.c
reactos/lib/sdk/crt/printf/_snwprintf.c
reactos/lib/sdk/crt/printf/_sxprintf.c [new file with mode: 0644]
reactos/lib/sdk/crt/printf/_vsnprintf.c
reactos/lib/sdk/crt/printf/_vsnwprintf.c
reactos/lib/sdk/crt/printf/printf.c
reactos/lib/sdk/crt/printf/sprintf.c
reactos/lib/sdk/crt/printf/streamout.c
reactos/lib/sdk/crt/printf/swprintf.c
reactos/lib/sdk/crt/printf/vsprintf.c
reactos/lib/sdk/crt/printf/vswprintf.c

index c8af489..061865e 100644 (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"
index 5a4a951..98ceeb6 100644 (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"
diff --git a/reactos/lib/sdk/crt/printf/_sxprintf.c b/reactos/lib/sdk/crt/printf/_sxprintf.c
new file mode 100644 (file)
index 0000000..c212232
--- /dev/null
@@ -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;
+}
+
+
index c94ffc6..0c68887 100644 (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"
index 77e1230..9df26ad 100644 (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"
index a9f83f1..467a8be 100644 (file)
@@ -22,6 +22,7 @@ printf(const char *format, ...)
     va_start(argptr, format);
     result = streamout(stdout, format, argptr);
     va_end(argptr);
+
     return result;
 }
 
index 33c6092..2e515c6 100644 (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"
index ee7755d..1e1992d 100644 (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;
 }
index 115c1e6..edb8f78 100644 (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"
index 12a3d53..0f4d4b8 100644 (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"
index 4bba6ba..99502b3 100644 (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"