]> git.sesse.net Git - vlc/blob - include/vlc_charset.h
Win32: add ToCodePage() and FromCodePage()
[vlc] / include / vlc_charset.h
1 /*****************************************************************************
2  * vlc_charset.h: Unicode UTF-8 wrappers function
3  *****************************************************************************
4  * Copyright (C) 2003-2005 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 /* iconv wrappers (defined in src/extras/libc.c) */
34 typedef void *vlc_iconv_t;
35 VLC_API vlc_iconv_t vlc_iconv_open( const char *, const char * ) VLC_USED;
36 VLC_API size_t vlc_iconv( vlc_iconv_t, const char **, size_t *, char **, size_t * ) VLC_USED;
37 VLC_API int vlc_iconv_close( vlc_iconv_t );
38
39 #include <stdarg.h>
40
41 VLC_API int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap );
42 VLC_API int utf8_fprintf( FILE *, const char *, ... ) VLC_FORMAT( 2, 3 );
43 VLC_API char * vlc_strcasestr(const char *, const char *) VLC_USED;
44
45 VLC_API char * EnsureUTF8( char * );
46 VLC_API const char * IsUTF8( const char * ) VLC_USED;
47
48 VLC_API char * FromCharset( const char *charset, const void *data, size_t data_size ) VLC_USED;
49 VLC_API void * ToCharset( const char *charset, const char *in, size_t *outsize ) VLC_USED;
50
51 #ifndef WIN32
52 # define FromLocale(l) (l)
53 # define ToLocale(u)   (u)
54 # define LocaleFree(s) ((void)(s))
55 # define FromLocaleDup strdup
56 # define ToLocaleDup   strdup
57
58 #else
59 VLC_USED
60 static inline char *FromLocale (const char *locale)
61 {
62     return locale ? FromCharset ("", locale, strlen (locale)) : NULL;
63 }
64
65 VLC_USED
66 static inline char *ToLocale (const char *utf8)
67 {
68     size_t outsize;
69     return utf8 ? (char *)ToCharset ("", utf8, &outsize) : (char *)NULL;
70 }
71 # define LocaleFree(s) free((char *)(s))
72 # define FromLocaleDup FromLocale
73 # define ToLocaleDup   ToLocale
74
75 VLC_USED
76 static inline char *FromWide (const wchar_t *wide)
77 {
78     size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
79     if (len == 0)
80         return NULL;
81
82     char *out = (char *)malloc (len);
83
84     if (likely(out))
85         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
86     return out;
87 }
88
89 VLC_USED
90 static inline wchar_t *ToWide (const char *utf8)
91 {
92     int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
93     if (len == 0)
94         return NULL;
95
96     wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
97
98     if (likely(out))
99         MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
100     return out;
101 }
102
103 VLC_USED VLC_MALLOC
104 static inline char *ToCodePage (unsigned cp, const char *utf8)
105 {
106     wchar_t *wide = ToWide (utf8);
107     if (wide == NULL)
108         return NULL;
109
110     size_t len = WideCharToMultiByte (cp, 0, wide, -1, NULL, 0, NULL, NULL);
111     if (len == 0)
112         return NULL;
113
114     char *out = (char *)malloc (len);
115     if (likely(out != NULL))
116         WideCharToMultiByte (cp, 0, wide, -1, out, len, NULL, NULL);
117     free (wide);
118     return out;
119 }
120
121 VLC_USED VLC_MALLOC
122 static inline char *FromCodePage (unsigned cp, const char *mb)
123 {
124     int len = MultiByteToWideChar (cp, 0, mb, -1, NULL, 0);
125     if (len == 0)
126         return NULL;
127
128     wchar_t *wide = (wchar_t *)malloc (len * sizeof (wchar_t));
129     if (unlikely(wide == NULL))
130         return NULL;
131     MultiByteToWideChar (cp, 0, mb, -1, wide, len);
132
133     char *utf8 = FromWide (wide);
134     free (wide);
135     return utf8;
136 }
137
138 # ifdef UNICODE
139 #  define FromT FromWide
140 #  define ToT   ToWide
141 # else
142 #  define FromT FromLocaleDup
143 #  define ToT   ToLocaleDup
144 # endif
145 #endif
146
147 /**
148  * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
149  */
150 static inline char *FromLatin1 (const char *latin)
151 {
152     char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
153     unsigned char c;
154
155     if (str == NULL)
156         return NULL;
157
158     while ((c = *(latin++)) != '\0')
159     {
160          if (c >= 0x80)
161          {
162              *(utf8++) = 0xC0 | (c >> 6);
163              *(utf8++) = 0x80 | (c & 0x3F);
164          }
165          else
166              *(utf8++) = c;
167     }
168     *(utf8++) = '\0';
169
170     utf8 = (char *)realloc (str, utf8 - str);
171     return utf8 ? utf8 : str;
172 }
173
174 VLC_API double us_strtod( const char *, char ** ) VLC_USED;
175 VLC_API float us_strtof( const char *, char ** ) VLC_USED;
176 VLC_API double us_atof( const char * ) VLC_USED;
177 VLC_API int us_vasprintf( char **, const char *, va_list );
178 VLC_API int us_asprintf( char **, const char *, ... ) VLC_USED;
179
180 #endif