vlc_charset.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * charset.h: Unicode UTF-8 wrappers function
00003  *****************************************************************************
00004  * Copyright (C) 2003-2005 the VideoLAN team
00005  * Copyright © 2005-2010 Rémi Denis-Courmont
00006  * $Id: 8674aeed8e2b446657f5f7345f43b13f7990e9b9 $
00007  *
00008  * Author: Rémi Denis-Courmont <rem # videolan,org>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 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 General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, 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 #include <stdarg.h>
00034 
00035 VLC_EXPORT( void, LocaleFree, ( const char * ) );
00036 VLC_EXPORT( char *, FromLocale, ( const char * ) LIBVLC_USED );
00037 VLC_EXPORT( char *, FromLocaleDup, ( const char * ) LIBVLC_USED );
00038 VLC_EXPORT( char *, ToLocale, ( const char * ) LIBVLC_USED );
00039 VLC_EXPORT( char *, ToLocaleDup, ( const char * ) LIBVLC_USED );
00040 
00041 VLC_EXPORT( int, utf8_vfprintf, ( FILE *stream, const char *fmt, va_list ap ) );
00042 VLC_EXPORT( int, utf8_fprintf, ( FILE *, const char *, ... ) LIBVLC_FORMAT( 2, 3 ) );
00043 
00044 VLC_EXPORT( char *, EnsureUTF8, ( char * ) );
00045 VLC_EXPORT( const char *, IsUTF8, ( const char * ) LIBVLC_USED );
00046 
00047 #ifdef WIN32
00048 LIBVLC_USED
00049 static inline char *FromWide (const wchar_t *wide)
00050 {
00051     size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
00052     if (len == 0)
00053         return NULL;
00054 
00055     char *out = (char *)malloc (len);
00056 
00057     if (likely(out))
00058         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
00059     return out;
00060 }
00061 
00062 LIBVLC_USED
00063 static inline wchar_t *ToWide (const char *utf8)
00064 {
00065     int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
00066     if (len == 0)
00067         return NULL;
00068 
00069     wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
00070 
00071     if (likely(out))
00072         MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
00073     return out;
00074 }
00075 #endif
00076 
00077 /**
00078  * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
00079  */
00080 static inline char *FromLatin1 (const char *latin)
00081 {
00082     char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
00083     unsigned char c;
00084 
00085     if (str == NULL)
00086         return NULL;
00087 
00088     while ((c = *(latin++)) != '\0')
00089     {
00090          if (c >= 0x80)
00091          {
00092              *(utf8++) = 0xC0 | (c >> 6);
00093              *(utf8++) = 0x80 | (c & 0x3F);
00094          }
00095          else
00096              *(utf8++) = c;
00097     }
00098     *(utf8++) = '\0';
00099 
00100     utf8 = (char *)realloc (str, utf8 - str);
00101     return utf8 ? utf8 : str;
00102 }
00103 
00104 VLC_EXPORT( char *, FromCharset, ( const char *charset, const void *data, size_t data_size ) LIBVLC_USED );
00105 
00106 VLC_EXPORT( double, us_strtod, ( const char *, char ** ) LIBVLC_USED );
00107 VLC_EXPORT( float, us_strtof, ( const char *, char ** ) LIBVLC_USED );
00108 VLC_EXPORT( double, us_atof, ( const char * ) LIBVLC_USED );
00109 VLC_EXPORT( int, us_asprintf, ( char **, const char *, ... ) LIBVLC_USED );
00110 
00111 #endif

Generated on Mon Nov 22 07:55:19 2010 for VLC by  doxygen 1.5.6