[vlc-devel] commit: Added FromCharset helper. (Laurent Aimar )
git version control
git at videolan.org
Mon Jan 25 23:12:10 CET 2010
vlc | branch: master | Laurent Aimar <fenrir at videolan.org> | Mon Jan 25 23:05:53 2010 +0100| [599c47de9d9b5c4d8c8c406422c42f844c6e1d4f] | committer: Laurent Aimar
Added FromCharset helper.
It allows to easily convert from any charset supported by
vlc_iconv to UTF-8 (but with a performance penalty).
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=599c47de9d9b5c4d8c8c406422c42f844c6e1d4f
---
include/vlc_charset.h | 2 ++
src/text/unicode.c | 38 ++++++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/include/vlc_charset.h b/include/vlc_charset.h
index ab6d529..81af0c1 100644
--- a/include/vlc_charset.h
+++ b/include/vlc_charset.h
@@ -109,6 +109,8 @@ static inline char *FromLatin1 (const char *latin)
return utf8 ? utf8 : str;
}
+VLC_EXPORT( char *, FromCharset, ( const char *charset, const void *data, size_t data_size ) LIBVLC_USED );
+
VLC_EXPORT( const char *, GetFallbackEncoding, ( void ) LIBVLC_USED );
VLC_EXPORT( double, us_strtod, ( const char *, char ** ) LIBVLC_USED );
diff --git a/src/text/unicode.c b/src/text/unicode.c
index 1fa90e2..8a86417 100644
--- a/src/text/unicode.c
+++ b/src/text/unicode.c
@@ -40,6 +40,7 @@
#ifdef UNDER_CE
# include <tchar.h>
#endif
+#include <errno.h>
#if defined (__APPLE__) || defined (HAVE_MAEMO)
/* Define this if the OS always use UTF-8 internally */
@@ -365,3 +366,40 @@ const char *IsUTF8( const char *str )
{
return CheckUTF8( (char *)str, 0 );
}
+
+/**
+ * Converts a string from the given character encoding to utf-8.
+ *
+ * @return a nul-terminated utf-8 string, or null in case of error.
+ * The result must be freed using free().
+ */
+static char *FromCharset(const char *charset, const void *data, size_t data_size)
+{
+ vlc_iconv_t handle = vlc_iconv_open ("UTF-8", charset);
+ if (handle == (vlc_iconv_t)(-1))
+ return NULL;
+
+ char *out = NULL;
+ for(unsigned mul = 4; mul < 8; mul++ )
+ {
+ size_t in_size = data_size;
+ const char *in = data;
+ size_t out_max = mul * data_size;
+ char *tmp = out = malloc (1 + out_max);
+ if (!out)
+ break;
+
+ if (vlc_iconv (handle, &in, &in_size, &tmp, &out_max) != (size_t)(-1)) {
+ *tmp = '\0';
+ break;
+ }
+ free(out);
+ out = NULL;
+
+ if (errno != E2BIG)
+ break;
+ }
+ vlc_iconv_close(handle);
+ return out;
+}
+
More information about the vlc-devel
mailing list