]> git.sesse.net Git - vlc/blobdiff - src/text/unicode.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / text / unicode.c
index 41734eddc7cc0ade4b99a5a09494321bd4f541f4..3030d47085e7fc7d238765eda619272d7d89bc7a 100644 (file)
 #endif
 #include <errno.h>
 
-#if defined (__APPLE__) || defined (HAVE_MAEMO)
-/* Define this if the OS always use UTF-8 internally */
-# define ASSUME_UTF8 1
-#endif
-
 #if defined (ASSUME_UTF8)
 /* Cool */
+
 #elif defined (WIN32) || defined (UNDER_CE)
 # define USE_MB2MB 1
-#elif defined (HAVE_ICONV)
-# define USE_ICONV 1
-#else
-# error No UTF8 charset conversion implemented on this platform!
-#endif
+# include <io.h>
 
-static char *locale_fast (const char *string, bool from)
+static char *locale_dup (const char *string, bool from)
 {
-    if( string == NULL )
+    char *out;
+    int len;
+
+    len = 1 + MultiByteToWideChar (from ? CP_ACP : CP_UTF8,
+                                   0, string, -1, NULL, 0);
+    wchar_t *wide = malloc (len * sizeof (wchar_t));
+    if (wide == NULL)
         return NULL;
 
-#if defined (USE_ICONV)
+    MultiByteToWideChar (from ? CP_ACP : CP_UTF8, 0, string, -1, wide, len);
+    len = 1 + WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1,
+                                   NULL, 0, NULL, NULL);
+    out = malloc (len);
+    if (out != NULL)
+        WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1, out, len,
+                             NULL, NULL);
+    free (wide);
+    return out;
+}
+
+#elif defined (HAVE_ICONV)
+# define USE_ICONV 1
+
+static char *locale_dup (const char *string, bool from)
+{
     vlc_iconv_t hd = vlc_iconv_open (from ? "UTF-8" : "",
                                      from ? "" : "UTF-8");
     if (hd == (vlc_iconv_t)(-1))
@@ -89,45 +102,12 @@ static char *locale_fast (const char *string, bool from)
     assert (*optr == '\0');
     assert (strlen (output) == (size_t)(optr - output));
     return strdup (output);
-#elif defined (USE_MB2MB)
-    char *out;
-    int len;
-
-    len = 1 + MultiByteToWideChar (from ? CP_ACP : CP_UTF8,
-                                   0, string, -1, NULL, 0);
-    wchar_t *wide = malloc (len * sizeof (wchar_t));
-    if (wide == NULL)
-        return NULL;
-
-    MultiByteToWideChar (from ? CP_ACP : CP_UTF8, 0, string, -1, wide, len);
-    len = 1 + WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1,
-                                   NULL, 0, NULL, NULL);
-    out = malloc (len);
-    if (out != NULL)
-        WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1, out, len,
-                             NULL, NULL);
-    free (wide);
-    return out;
-#else
-    (void)from;
-    return (char *)string;
-#endif
 }
 
-
-static inline char *locale_dup (const char *string, bool from)
-{
-    assert( string );
-
-#if defined (USE_ICONV)
-    return locale_fast (string, from);
-#elif defined (USE_MB2MB)
-    return locale_fast (string, from);
 #else
-    (void)from;
-    return strdup (string);
+# error No UTF8 charset conversion implemented on this platform!
 #endif
-}
+
 
 /**
  * Releases (if needed) a localized or uniformized string.
@@ -135,12 +115,10 @@ static inline char *locale_dup (const char *string, bool from)
  */
 void LocaleFree (const char *str)
 {
-#if defined (USE_ICONV)
-    free ((char *)str);
-#elif defined (USE_MB2MB)
-    free ((char *)str);
+#ifdef ASSUME_UTF8
+    (void) str;
 #else
-    (void)str;
+    free ((char *)str);
 #endif
 }
 
@@ -156,7 +134,11 @@ void LocaleFree (const char *str)
  */
 char *FromLocale (const char *locale)
 {
-    return locale_fast (locale, true);
+#ifdef ASSUME_UTF8
+    return (char *)locale;
+#else
+    return locale ? locale_dup (locale, true) : NULL;
+#endif
 }
 
 /**
@@ -170,7 +152,11 @@ char *FromLocale (const char *locale)
  */
 char *FromLocaleDup (const char *locale)
 {
+#ifdef ASSUME_UTF8
+    return strdup (locale);
+#else
     return locale_dup (locale, true);
+#endif
 }
 
 
@@ -185,7 +171,11 @@ char *FromLocaleDup (const char *locale)
  */
 char *ToLocale (const char *utf8)
 {
-    return locale_fast (utf8, false);
+#ifdef ASSUME_UTF8
+    return (char *)utf8;
+#else
+    return utf8 ? locale_dup (utf8, false) : NULL;
+#endif
 }
 
 
@@ -200,23 +190,11 @@ char *ToLocale (const char *utf8)
  */
 char *ToLocaleDup (const char *utf8)
 {
+#ifdef ASSUME_UTF8
+    return strdup (utf8);
+#else
     return locale_dup (utf8, false);
-}
-
-/**
- * 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;
-
-    *str = ToLocaleDup( utf8 );
-    free( utf8 );
-    return res;
+#endif
 }
 
 /**
@@ -225,14 +203,58 @@ static int utf8_vasprintf( char **str, const char *fmt, va_list ap )
  */
 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
 }
 
 /**