]> git.sesse.net Git - vlc/blob - include/vlc_charset.h
Merge branch 'master' into lpcm_encoder
[vlc] / include / vlc_charset.h
1 /*****************************************************************************
2  * charset.h: Unicode UTF-8 wrappers function
3  *****************************************************************************
4  * Copyright (C) 2003-2005 the VideoLAN team
5  * Copyright © 2005-2010 Rémi Denis-Courmont
6  * $Id$
7  *
8  * Author: Rémi Denis-Courmont <rem # videolan,org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef VLC_CHARSET_H
26 #define VLC_CHARSET_H 1
27
28 /**
29  * \file
30  * This files handles locale conversions in vlc
31  */
32
33 #include <stdarg.h>
34
35 VLC_EXPORT( void, LocaleFree, ( const char * ) );
36 VLC_EXPORT( char *, FromLocale, ( const char * ) LIBVLC_USED );
37 VLC_EXPORT( char *, FromLocaleDup, ( const char * ) LIBVLC_USED );
38 VLC_EXPORT( char *, ToLocale, ( const char * ) LIBVLC_USED );
39 VLC_EXPORT( char *, ToLocaleDup, ( const char * ) LIBVLC_USED );
40
41 VLC_EXPORT( int, utf8_vfprintf, ( FILE *stream, const char *fmt, va_list ap ) );
42 VLC_EXPORT( int, utf8_fprintf, ( FILE *, const char *, ... ) LIBVLC_FORMAT( 2, 3 ) );
43
44 VLC_EXPORT( char *, EnsureUTF8, ( char * ) );
45 VLC_EXPORT( const char *, IsUTF8, ( const char * ) LIBVLC_USED );
46
47 #ifdef WIN32
48 LIBVLC_USED
49 static inline char *FromWide (const wchar_t *wide)
50 {
51     size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
52     if (len == 0)
53         return NULL;
54
55     char *out = (char *)malloc (len);
56
57     if (likely(out))
58         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
59     return out;
60 }
61
62 LIBVLC_USED
63 static inline wchar_t *ToWide (const char *utf8)
64 {
65     int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
66     if (len == 0)
67         return NULL;
68
69     wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
70
71     if (likely(out))
72         MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
73     return out;
74 }
75 #endif
76
77 /**
78  * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
79  */
80 static inline char *FromLatin1 (const char *latin)
81 {
82     char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
83     unsigned char c;
84
85     if (str == NULL)
86         return NULL;
87
88     while ((c = *(latin++)) != '\0')
89     {
90          if (c >= 0x80)
91          {
92              *(utf8++) = 0xC0 | (c >> 6);
93              *(utf8++) = 0x80 | (c & 0x3F);
94          }
95          else
96              *(utf8++) = c;
97     }
98     *(utf8++) = '\0';
99
100     utf8 = (char *)realloc (str, utf8 - str);
101     return utf8 ? utf8 : str;
102 }
103
104 VLC_EXPORT( char *, FromCharset, ( const char *charset, const void *data, size_t data_size ) LIBVLC_USED );
105
106 VLC_EXPORT( double, us_strtod, ( const char *, char ** ) LIBVLC_USED );
107 VLC_EXPORT( float, us_strtof, ( const char *, char ** ) LIBVLC_USED );
108 VLC_EXPORT( double, us_atof, ( const char * ) LIBVLC_USED );
109 VLC_EXPORT( int, us_asprintf, ( char **, const char *, ... ) LIBVLC_USED );
110
111 #endif