]> git.sesse.net Git - vlc/commitdiff
A few utf8 *printf wrappers (refs #556)
authorRémi Denis-Courmont <rem@videolan.org>
Tue, 21 Feb 2006 11:53:49 +0000 (11:53 +0000)
committerRémi Denis-Courmont <rem@videolan.org>
Tue, 21 Feb 2006 11:53:49 +0000 (11:53 +0000)
src/misc/unicode.c

index 30e48fab24b10e91812fea45b49dfee9bdac9863..91f9809ea4d65f122d453d279fb574a799465524 100644 (file)
@@ -30,6 +30,7 @@
 #include <assert.h>
 
 #include <stdio.h>
+#include <stdarg.h>
 #include <errno.h>
 #include <sys/types.h>
 #ifdef HAVE_DIRENT_H
@@ -266,6 +267,19 @@ char *ToLocale( const char *utf8 )
 #endif
 }
 
+char *ToLocaleDup( const char *utf8 )
+{
+#if defined (ASSUME_UTF8)
+    return strdup( utf8 );
+#else
+# ifdef USE_ICONV
+    if (to_locale.hd == (vlc_iconv_t)(-1))
+        return strdup( utf8 );
+# endif
+    return ToLocale( utf8 );
+#endif
+}
+
 void LocaleFree( const char *str )
 {
 #ifdef USE_ICONV
@@ -440,6 +454,44 @@ int utf8_lstat( const char *filename, void *buf)
     return utf8_statEx( filename, buf, VLC_FALSE );
 }
 
+/*****************************************************************************
+ * utf8_*printf: *printf with conversion from UTF-8 to local encoding
+ *****************************************************************************/
+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;
+}
+
+int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap )
+{
+       char *str;
+       int res = utf8_vasprintf( &str, fmt, ap );
+       if( res == -1 )
+               return -1;
+
+       fputs( str, stream );
+       free( str );
+       return res;
+}
+
+int utf8_fprintf( FILE *stream, const char *fmt, ... )
+{
+       va_list ap;
+       int res;
+
+       va_start( ap, fmt );
+       res = utf8_vfprintf( stream, fmt, ap );
+       va_end( ap );
+       return res;
+}
+
 /*****************************************************************************
  * EnsureUTF8: replaces invalid/overlong UTF-8 sequences with question marks
  *****************************************************************************