vlc_charset.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * vlc_charset.h: Unicode UTF-8 wrappers function
00003  *****************************************************************************
00004  * Copyright (C) 2003-2005 VLC authors and VideoLAN
00005  * Copyright © 2005-2010 Rémi Denis-Courmont
00006  * $Id: df3fc876a26fd0e65ca40cef955e9339bf16898c $
00007  *
00008  * Author: Rémi Denis-Courmont <rem # videolan,org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify it
00011  * under the terms of the GNU Lesser General Public License as published by
00012  * the Free Software Foundation; either version 2.1 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018  * GNU Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public License
00021  * along with this program; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00023  *****************************************************************************/
00024 
00025 #ifndef VLC_CHARSET_H
00026 #define VLC_CHARSET_H 1
00027 
00028 /**
00029  * \file
00030  * This files handles locale conversions in vlc
00031  */
00032 
00033 /* iconv wrappers (defined in src/extras/libc.c) */
00034 typedef void *vlc_iconv_t;
00035 VLC_API vlc_iconv_t vlc_iconv_open( const char *, const char * ) VLC_USED;
00036 VLC_API size_t vlc_iconv( vlc_iconv_t, const char **, size_t *, char **, size_t * ) VLC_USED;
00037 VLC_API int vlc_iconv_close( vlc_iconv_t );
00038 
00039 #include <stdarg.h>
00040 
00041 VLC_API int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap );
00042 VLC_API int utf8_fprintf( FILE *, const char *, ... ) VLC_FORMAT( 2, 3 );
00043 VLC_API char * vlc_strcasestr(const char *, const char *) VLC_USED;
00044 
00045 VLC_API char * EnsureUTF8( char * );
00046 VLC_API const char * IsUTF8( const char * ) VLC_USED;
00047 
00048 VLC_API char * FromCharset( const char *charset, const void *data, size_t data_size ) VLC_USED;
00049 VLC_API void * ToCharset( const char *charset, const char *in, size_t *outsize ) VLC_USED;
00050 
00051 #ifndef WIN32
00052 # define FromLocale(l) (l)
00053 # define ToLocale(u)   (u)
00054 # define LocaleFree(s) ((void)(s))
00055 # define FromLocaleDup strdup
00056 # define ToLocaleDup   strdup
00057 
00058 #else
00059 VLC_USED
00060 static inline char *FromWide (const wchar_t *wide)
00061 {
00062     size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
00063     if (len == 0)
00064         return NULL;
00065 
00066     char *out = (char *)malloc (len);
00067 
00068     if (likely(out))
00069         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
00070     return out;
00071 }
00072 
00073 VLC_USED
00074 static inline wchar_t *ToWide (const char *utf8)
00075 {
00076     int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
00077     if (len == 0)
00078         return NULL;
00079 
00080     wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
00081 
00082     if (likely(out))
00083         MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
00084     return out;
00085 }
00086 
00087 VLC_USED VLC_MALLOC
00088 static inline char *ToCodePage (unsigned cp, const char *utf8)
00089 {
00090     wchar_t *wide = ToWide (utf8);
00091     if (wide == NULL)
00092         return NULL;
00093 
00094     size_t len = WideCharToMultiByte (cp, 0, wide, -1, NULL, 0, NULL, NULL);
00095     if (len == 0)
00096         return NULL;
00097 
00098     char *out = (char *)malloc (len);
00099     if (likely(out != NULL))
00100         WideCharToMultiByte (cp, 0, wide, -1, out, len, NULL, NULL);
00101     free (wide);
00102     return out;
00103 }
00104 
00105 VLC_USED VLC_MALLOC
00106 static inline char *FromCodePage (unsigned cp, const char *mb)
00107 {
00108     int len = MultiByteToWideChar (cp, 0, mb, -1, NULL, 0);
00109     if (len == 0)
00110         return NULL;
00111 
00112     wchar_t *wide = (wchar_t *)malloc (len * sizeof (wchar_t));
00113     if (unlikely(wide == NULL))
00114         return NULL;
00115     MultiByteToWideChar (cp, 0, mb, -1, wide, len);
00116 
00117     char *utf8 = FromWide (wide);
00118     free (wide);
00119     return utf8;
00120 }
00121 
00122 VLC_USED VLC_MALLOC
00123 static inline char *FromANSI (const char *ansi)
00124 {
00125     return FromCodePage (GetACP (), ansi);
00126 }
00127 
00128 VLC_USED VLC_MALLOC
00129 static inline char *ToANSI (const char *utf8)
00130 {
00131     return ToCodePage (GetACP (), utf8);
00132 }
00133 
00134 # ifdef UNICODE
00135 #  define FromT FromWide
00136 #  define ToT   ToWide
00137 # else
00138 #  define FromT FromANSI
00139 #  define ToT   ToANSI
00140 # endif
00141 # define FromLocale    FromANSI
00142 # define ToLocale      ToANSI
00143 # define LocaleFree(s) free((char *)(s))
00144 # define FromLocaleDup FromANSI
00145 # define ToLocaleDup   ToANSI
00146 #endif
00147 
00148 /**
00149  * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
00150  */
00151 static inline char *FromLatin1 (const char *latin)
00152 {
00153     char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
00154     unsigned char c;
00155 
00156     if (str == NULL)
00157         return NULL;
00158 
00159     while ((c = *(latin++)) != '\0')
00160     {
00161          if (c >= 0x80)
00162          {
00163              *(utf8++) = 0xC0 | (c >> 6);
00164              *(utf8++) = 0x80 | (c & 0x3F);
00165          }
00166          else
00167              *(utf8++) = c;
00168     }
00169     *(utf8++) = '\0';
00170 
00171     utf8 = (char *)realloc (str, utf8 - str);
00172     return utf8 ? utf8 : str;
00173 }
00174 
00175 VLC_API double us_strtod( const char *, char ** ) VLC_USED;
00176 VLC_API float us_strtof( const char *, char ** ) VLC_USED;
00177 VLC_API double us_atof( const char * ) VLC_USED;
00178 VLC_API int us_vasprintf( char **, const char *, va_list );
00179 VLC_API int us_asprintf( char **, const char *, ... ) VLC_USED;
00180 
00181 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines