]> git.sesse.net Git - vlc/blobdiff - src/text/unicode.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / text / unicode.c
index bb1ec6bf5295a2e0ff7cd0c7bc46ef547a0014f4..3030d47085e7fc7d238765eda619272d7d89bc7a 100644 (file)
@@ -47,6 +47,7 @@
 
 #elif defined (WIN32) || defined (UNDER_CE)
 # define USE_MB2MB 1
+# include <io.h>
 
 static char *locale_dup (const char *string, bool from)
 {
@@ -173,7 +174,7 @@ char *ToLocale (const char *utf8)
 #ifdef ASSUME_UTF8
     return (char *)utf8;
 #else
-    return utf8 ? locale_fast (utf8, false) : NULL
+    return utf8 ? locale_dup (utf8, false) : NULL;
 #endif
 }
 
@@ -196,40 +197,64 @@ char *ToLocaleDup (const char *utf8)
 #endif
 }
 
-/**
- * Formats an UTF-8 string as vasprintf(), then print it to stdout, with
- * appropriate conversion to local encoding.
- */
-static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
-{
-    char *utf8;
-    int res = vasprintf( &utf8, fmt, ap );
-    if( res == -1 )
-        return -1;
-
-#ifdef ASSUME_UTF8
-    *str = utf8;
-#else
-    *str = ToLocaleDup( utf8 );
-    free( utf8 );
-#endif
-    return res;
-}
-
 /**
  * Formats an UTF-8 string as vfprintf(), then print it, with
  * appropriate conversion to local encoding.
  */
 int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
 {
+#ifdef ASSUME_UTF8
+    return vfprintf (stream, fmt, ap);
+#else
     char *str;
-    int res = utf8_vasprintf( &str, fmt, ap );
-    if( res == -1 )
+    int res;
+
+# if defined( WIN32 ) && !defined( UNDER_CE )
+    /* Writing to the console is a lot of fun on Microsoft Windows.
+     * If you use the standard I/O functions, you must use the OEM code page,
+     * which is different from the usual ANSI code page. Or maybe not, if the
+     * user called "chcp". Anyway, we prefer Unicode. */
+    int fd = _fileno (stream);
+    if (likely(fd != -1) && _isatty (fd))
+    {
+        res = vasprintf (&str, fmt, ap);
+        if (unlikely(res == -1))
+            return -1;
+
+        size_t wlen = 2 * (res + 1);
+        wchar_t *wide = malloc (wlen);
+        if (likely(wide != NULL))
+        {
+            wlen = MultiByteToWideChar (CP_UTF8, 0, str, res + 1, wide, wlen);
+            if (wlen > 0)
+            {
+                HANDLE h = (HANDLE)(intptr_t)_get_osfhandle (fd);
+                DWORD out;
+
+                WriteConsoleW (h, wide, wlen - 1, &out, NULL);
+            }
+            else
+                res = -1;
+            free (wide);
+        }
+        else
+            res = -1;
+        free (str);
+        return res;
+    }
+# endif
+
+    res = vasprintf (&str, fmt, ap);
+    if (unlikely(res == -1))
         return -1;
 
-    fputs( str, stream );
-    free( str );
+    char *ansi = ToLocaleDup (str);
+    free (str);
+
+    fputs (ansi, stream);
+    free (ansi);
     return res;
+#endif
 }
 
 /**