Prepare POSIX wprintf family support.


git-svn-id: svn+ssh://svn.code.sf.net/p/mingw-w64/code/trunk@3972 4407c894-4637-0410-b4f5-ada5f102cad1
diff --git a/mingw-w64-crt/stdio/mingw_fprintf.c b/mingw-w64-crt/stdio/mingw_fprintf.c
index 2b449fe..011a634 100644
--- a/mingw-w64-crt/stdio/mingw_fprintf.c
+++ b/mingw-w64-crt/stdio/mingw_fprintf.c
@@ -44,9 +44,9 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __fprintf (FILE *, const char *, ...) __MINGW_NOTHROW;
+int __cdecl __fprintf (FILE *, const APICHAR *, ...) __MINGW_NOTHROW;
 
-int __cdecl __fprintf( FILE *stream, const char *fmt, ... )
+int __cdecl __fprintf(FILE *stream, const APICHAR *fmt, ...)
 {
   register int retval;
   va_list argv; va_start( argv, fmt );
@@ -54,4 +54,3 @@
   va_end( argv );
   return retval;
 }
-
diff --git a/mingw-w64-crt/stdio/mingw_pformat.c b/mingw-w64-crt/stdio/mingw_pformat.c
index 5f29b76..34a1df0 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -274,12 +274,12 @@
       /*
        * This is single character output to a FILE stream...
        */
-      fputc( c, (FILE *)(stream->dest) );
+      __fputc(c, (FILE *)(stream->dest));
 
     else
       /* Whereas, this is to an internal memory buffer...
        */
-      ((char *)(stream->dest))[stream->count] = c;
+      ((APICHAR *)(stream->dest))[stream->count] = c;
   }
   ++stream->count;
 }
@@ -330,11 +330,36 @@
 
   /* Emit the data...
    */
+#ifdef __BUILD_WIDEAPI
+  {
+    /* mbrtowc */
+    size_t l;
+    wchar_t w[12], *p;
+    while( count > 0 )
+    {
+      mbstate_t ps;
+      memset(&ps, 0, sizeof(ps) );
+      --count;
+      p = &w[0];
+      l = mbrtowc (p, s, strlen (s), &ps);
+      if (!l)
+        break;
+      if ((ssize_t)l < 0)
+      {
+        l = 1;
+        w[0] = (wchar_t) *s;
+      }
+      s += l;
+      __pformat_putc((int)w[0], stream);
+    }
+  }
+#else
   while( count-- )
     /*
      * copying the requisite number of characters from the input.
      */
     __pformat_putc( *s++, stream );
+#endif
 
   /* If we still haven't consumed the entire specified field width,
    * we must be doing flush left justification; any residual width
@@ -373,7 +398,9 @@
    * by byte, through `__pformat_putc()', to ensure that any specified
    * output quota is honoured.
    */
-  char buf[16]; mbstate_t state; int len = wcrtomb( buf, L'\0', &state );
+  char buf[16];
+  mbstate_t state;
+  int len = wcrtomb(buf, L'\0', &state);
 
   if( (stream->precision >= 0) && (count > stream->precision) )
     /*
@@ -408,13 +435,19 @@
   /* Emit the data, converting each character from the wide
    * to the multibyte domain as we go...
    */
+#ifdef __BUILD_WIDEAPI
+  while(count-- > 0 && *s != 0)
+  {
+      __pformat_putc(*s++, stream);
+  }
+#else
   while( (count-- > 0) && ((len = wcrtomb( buf, *s++, &state )) > 0) )
   {
     char *p = buf;
     while( len-- > 0 )
       __pformat_putc( *p++, stream );
   }
-
+#endif
   /* If we still haven't consumed the entire specified field width,
    * we must be doing flush left justification; any residual width
    * must be filled with blanks, to the right of the output value.
@@ -1729,7 +1762,8 @@
   }
 }
 
-int __pformat( int flags, void *dest, int max, const char *fmt, va_list argv )
+int
+__pformat (int flags, void *dest, int max, const APICHAR *fmt, va_list argv)
 {
   int c;
 
@@ -1767,7 +1801,7 @@
       /* Save the current format scan position, so that we can backtrack
        * in the event of encountering an invalid format specification...
        */
-      const char *backtrack = fmt;
+      const APICHAR *backtrack = fmt;
 
       /* Restart capture for dynamic field width and precision specs...
        */
@@ -1835,7 +1869,6 @@
 	      wchar_t argval = (wchar_t)(va_arg( argv, int ));
 	      __pformat_wputchars( &argval, 1, &stream );
 	    }
-
 	    else
 	    { /* while anything else is simply taken as `char', (which
 	       * is also promoted to an `int' argument)...
@@ -1854,20 +1887,18 @@
 
 	  case 's':
 	    if( (length == PFORMAT_LENGTH_LONG)
-	    ||  (length == PFORMAT_LENGTH_LLONG)  )
+	         || (length == PFORMAT_LENGTH_LLONG))
 	    {
 	      /* considering any `long' type modifier as a reference to
 	       * a `wchar_t' string...
 	       */
 	      __pformat_wcputs( va_arg( argv, wchar_t * ), &stream );
 	    }
-
 	    else
 	      /* This is normal string output;
 	       * we simply invoke the appropriate handler...
 	       */
 	      __pformat_puts( va_arg( argv, char * ), &stream );
-
 	    goto format_scan;
 
 	  case 'o':
diff --git a/mingw-w64-crt/stdio/mingw_pformat.h b/mingw-w64-crt/stdio/mingw_pformat.h
index 5e498c5..898abad 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.h
+++ b/mingw-w64-crt/stdio/mingw_pformat.h
@@ -50,6 +50,12 @@
 # endif
 #endif
 
+#ifdef __BUILD_WIDEAPI
+#define APICHAR	wchar_t
+#else
+#define APICHAR char
+#endif
+
 /* The following are the declarations specific to the `pformat' API...
  */
 #define PFORMAT_TO_FILE     0x1000
@@ -60,7 +66,22 @@
   * Map MinGW specific function names, for use in place of the generic
   * implementation defined equivalent function names.
   */
+#ifdef __BUILD_WIDEAPI
+# define __pformat        __mingw_wpformat
+#define __fputc(X,STR) fputwc((wchar_t) (X), (STR))
+
+# define __printf         __mingw_wprintf
+# define __fprintf        __mingw_fwprintf
+# define __sprintf        __mingw_swprintf
+# define __snprintf       __mingw_snwprintf
+
+# define __vprintf        __mingw_vwprintf
+# define __vfprintf       __mingw_vfwprintf
+# define __vsprintf       __mingw_vswprintf
+# define __vsnprintf      __mingw_vsnwprintf
+#else
 # define __pformat        __mingw_pformat
+#define __fputc(X,STR) fputc((X), (STR))
 
 # define __printf         __mingw_printf
 # define __fprintf        __mingw_fprintf
@@ -71,9 +92,8 @@
 # define __vfprintf       __mingw_vfprintf
 # define __vsprintf       __mingw_vsprintf
 # define __vsnprintf      __mingw_vsnprintf
-
+#endif /* __BUILD_WIDEAPI */
 #endif
 
-int __cdecl __pformat( int, void *, int, const char *, va_list ) __MINGW_NOTHROW;
-
+int __cdecl __pformat(int, void *, int, const APICHAR *, va_list) __MINGW_NOTHROW;
 #endif /* !defined PFORMAT_H */
diff --git a/mingw-w64-crt/stdio/mingw_printf.c b/mingw-w64-crt/stdio/mingw_printf.c
index 515d721..5d23b02 100644
--- a/mingw-w64-crt/stdio/mingw_printf.c
+++ b/mingw-w64-crt/stdio/mingw_printf.c
@@ -44,9 +44,9 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __printf( const char *, ... ) __MINGW_NOTHROW;
+int __cdecl __printf(const APICHAR *, ...) __MINGW_NOTHROW;
 
-int __cdecl __printf( const char *fmt, ... )
+int __cdecl __printf(const APICHAR *fmt, ...)
 {
   register int retval;
   va_list argv; va_start( argv, fmt );
diff --git a/mingw-w64-crt/stdio/mingw_snprintf.c b/mingw-w64-crt/stdio/mingw_snprintf.c
index 4618860..0c0d762 100644
--- a/mingw-w64-crt/stdio/mingw_snprintf.c
+++ b/mingw-w64-crt/stdio/mingw_snprintf.c
@@ -28,14 +28,13 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __snprintf (char *, size_t, const char *fmt, ...) __MINGW_NOTHROW;
-int __cdecl __vsnprintf (char *, size_t, const char *fmt, va_list) __MINGW_NOTHROW;
+int __cdecl __snprintf (APICHAR *, size_t, const APICHAR *fmt, ...) __MINGW_NOTHROW;
+int __cdecl __vsnprintf (APICHAR *, size_t, const APICHAR *fmt, va_list) __MINGW_NOTHROW;
 
-int __cdecl __snprintf( char *buf, size_t length, const char *fmt, ... )
+int __cdecl __snprintf(APICHAR *buf, size_t length, const APICHAR *fmt, ...)
 {
   va_list argv; va_start( argv, fmt );
   register int retval = __vsnprintf( buf, length, fmt, argv );
   va_end( argv );
   return retval;
 }
-
diff --git a/mingw-w64-crt/stdio/mingw_sprintf.c b/mingw-w64-crt/stdio/mingw_sprintf.c
index be8cfb9..4b46eb5 100644
--- a/mingw-w64-crt/stdio/mingw_sprintf.c
+++ b/mingw-w64-crt/stdio/mingw_sprintf.c
@@ -44,9 +44,9 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __sprintf (char *, const char *, ...) __MINGW_NOTHROW;
+int __cdecl __sprintf (APICHAR *, const APICHAR *, ...) __MINGW_NOTHROW;
 
-int __cdecl __sprintf( char *buf, const char *fmt, ... )
+int __cdecl __sprintf(APICHAR *buf, const APICHAR *fmt, ...)
 {
   register int retval;
   va_list argv; va_start( argv, fmt );
@@ -54,4 +54,3 @@
   va_end( argv );
   return retval;
 }
-
diff --git a/mingw-w64-crt/stdio/mingw_vfprintf.c b/mingw-w64-crt/stdio/mingw_vfprintf.c
index c6f4b07..68a0008 100644
--- a/mingw-w64-crt/stdio/mingw_vfprintf.c
+++ b/mingw-w64-crt/stdio/mingw_vfprintf.c
@@ -44,10 +44,9 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __vfprintf (FILE *, const char *, va_list) __MINGW_NOTHROW;
+int __cdecl __vfprintf (FILE *, const APICHAR *, va_list) __MINGW_NOTHROW;
 
-int __cdecl __vfprintf( FILE *stream, const char *fmt, va_list argv )
+int __cdecl __vfprintf(FILE *stream, const APICHAR *fmt, va_list argv)
 {
   return __pformat( PFORMAT_TO_FILE | PFORMAT_NOLIMIT, stream, 0, fmt, argv );
 }
-
diff --git a/mingw-w64-crt/stdio/mingw_vprintf.c b/mingw-w64-crt/stdio/mingw_vprintf.c
index 1d5d2a5..c60431a 100644
--- a/mingw-w64-crt/stdio/mingw_vprintf.c
+++ b/mingw-w64-crt/stdio/mingw_vprintf.c
@@ -44,10 +44,9 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __vprintf (const char *, va_list) __MINGW_NOTHROW;
+int __cdecl __vprintf (const APICHAR *, va_list) __MINGW_NOTHROW;
 
-int __cdecl __vprintf( const char *fmt, va_list argv )
+int __cdecl __vprintf(const APICHAR *fmt, va_list argv)
 {
   return __pformat( PFORMAT_TO_FILE | PFORMAT_NOLIMIT, stdout, 0, fmt, argv );
 }
-
diff --git a/mingw-w64-crt/stdio/mingw_vsnprintf.c b/mingw-w64-crt/stdio/mingw_vsnprintf.c
index 0d40887..b477300 100644
--- a/mingw-w64-crt/stdio/mingw_vsnprintf.c
+++ b/mingw-w64-crt/stdio/mingw_vsnprintf.c
@@ -28,8 +28,8 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __vsnprintf (char *, size_t, const char *fmt, va_list) __MINGW_NOTHROW;
-int __cdecl __vsnprintf( char *buf, size_t length, const char *fmt, va_list argv )
+int __cdecl __vsnprintf (APICHAR *, size_t, const APICHAR *fmt, va_list) __MINGW_NOTHROW;
+int __cdecl __vsnprintf(APICHAR *buf, size_t length, const APICHAR *fmt, va_list argv )
 {
   register int retval;
 
@@ -38,7 +38,7 @@
      * No buffer; simply compute and return the size required,
      * without actually emitting any data.
      */
-    return __pformat( 0, buf, 0, fmt, argv );
+    return __pformat( 0, buf, 0, fmt, argv);
 
   /* If we get to here, then we have a buffer...
    * Emit data up to the limit of buffer length less one,
diff --git a/mingw-w64-crt/stdio/mingw_vsprintf.c b/mingw-w64-crt/stdio/mingw_vsprintf.c
index 13ee7a0..af9793d 100644
--- a/mingw-w64-crt/stdio/mingw_vsprintf.c
+++ b/mingw-w64-crt/stdio/mingw_vsprintf.c
@@ -44,12 +44,11 @@
 
 #include "mingw_pformat.h"
 
-int __cdecl __vsprintf (char *, const char *, va_list) __MINGW_NOTHROW;
+int __cdecl __vsprintf (APICHAR *, const APICHAR *, va_list) __MINGW_NOTHROW;
 
-int __cdecl __vsprintf( char *buf, const char *fmt, va_list argv )
+int __cdecl __vsprintf(APICHAR *buf, const APICHAR *fmt, va_list argv)
 {
   register int retval;
   buf[retval = __pformat( PFORMAT_NOLIMIT, buf, 0, fmt, argv )] = '\0';
   return retval;
 }
-