QVisu
Qt-based visualization for smart homes
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
vlc_charset.h
Go to the documentation of this file.
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: 4de57ab70f369c91338676f8ca154ecab427ca5e $
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 
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 #ifdef _WIN32
52 VLC_USED
53 static inline char *FromWide (const wchar_t *wide)
54 {
55  size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
56  if (len == 0)
57  return NULL;
58 
59  char *out = (char *)malloc (len);
60 
61  if (likely(out))
62  WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
63  return out;
64 }
65 
66 VLC_USED
67 static inline wchar_t *ToWide (const char *utf8)
68 {
69  int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
70  if (len == 0)
71  return NULL;
72 
73  wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
74 
75  if (likely(out))
76  MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
77  return out;
78 }
79 
80 VLC_USED VLC_MALLOC
81 static inline char *ToCodePage (unsigned cp, const char *utf8)
82 {
83  wchar_t *wide = ToWide (utf8);
84  if (wide == NULL)
85  return NULL;
86 
87  size_t len = WideCharToMultiByte (cp, 0, wide, -1, NULL, 0, NULL, NULL);
88  if (len == 0)
89  return NULL;
90 
91  char *out = (char *)malloc (len);
92  if (likely(out != NULL))
93  WideCharToMultiByte (cp, 0, wide, -1, out, len, NULL, NULL);
94  free (wide);
95  return out;
96 }
97 
98 VLC_USED VLC_MALLOC
99 static inline char *FromCodePage (unsigned cp, const char *mb)
100 {
101  int len = MultiByteToWideChar (cp, 0, mb, -1, NULL, 0);
102  if (len == 0)
103  return NULL;
104 
105  wchar_t *wide = (wchar_t *)malloc (len * sizeof (wchar_t));
106  if (unlikely(wide == NULL))
107  return NULL;
108  MultiByteToWideChar (cp, 0, mb, -1, wide, len);
109 
110  char *utf8 = FromWide (wide);
111  free (wide);
112  return utf8;
113 }
114 
115 VLC_USED VLC_MALLOC
116 static inline char *FromANSI (const char *ansi)
117 {
118  return FromCodePage (GetACP (), ansi);
119 }
120 
121 VLC_USED VLC_MALLOC
122 static inline char *ToANSI (const char *utf8)
123 {
124  return ToCodePage (GetACP (), utf8);
125 }
126 
127 # ifdef UNICODE
128 # define FromT FromWide
129 # define ToT ToWide
130 # else
131 # define FromT FromANSI
132 # define ToT ToANSI
133 # endif
134 # define FromLocale FromANSI
135 # define ToLocale ToANSI
136 # define LocaleFree(s) free((char *)(s))
137 # define FromLocaleDup FromANSI
138 # define ToLocaleDup ToANSI
139 
140 #elif defined(__OS2__)
141 
142 VLC_USED static inline char *FromLocale (const char *locale)
143 {
144  return locale ? FromCharset ((char *)"", locale, strlen(locale)) : NULL;
145 }
146 
147 VLC_USED static inline char *ToLocale (const char *utf8)
148 {
149  size_t outsize;
150  return utf8 ? (char *)ToCharset ("", utf8, &outsize) : NULL;
151 }
152 
153 VLC_USED static inline void LocaleFree (const char *str)
154 {
155  free ((char *)str);
156 }
157 
158 VLC_USED static inline char *FromLocaleDup (const char *locale)
159 {
160  return FromCharset ("", locale, strlen(locale));
161 }
162 
163 VLC_USED static inline char *ToLocaleDup (const char *utf8)
164 {
165  size_t outsize;
166  return (char *)ToCharset ("", utf8, &outsize);
167 }
168 
169 #else
170 
171 # define FromLocale(l) (l)
172 # define ToLocale(u) (u)
173 # define LocaleFree(s) ((void)(s))
174 # define FromLocaleDup strdup
175 # define ToLocaleDup strdup
176 #endif
177 
181 static inline char *FromLatin1 (const char *latin)
182 {
183  char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
184  unsigned char c;
185 
186  if (str == NULL)
187  return NULL;
188 
189  while ((c = *(latin++)) != '\0')
190  {
191  if (c >= 0x80)
192  {
193  *(utf8++) = 0xC0 | (c >> 6);
194  *(utf8++) = 0x80 | (c & 0x3F);
195  }
196  else
197  *(utf8++) = c;
198  }
199  *(utf8++) = '\0';
200 
201  utf8 = (char *)realloc (str, utf8 - str);
202  return utf8 ? utf8 : str;
203 }
204 
205 VLC_API double us_strtod( const char *, char ** ) VLC_USED;
206 VLC_API float us_strtof( const char *, char ** ) VLC_USED;
207 VLC_API double us_atof( const char * ) VLC_USED;
208 VLC_API int us_vasprintf( char **, const char *, va_list );
209 VLC_API int us_asprintf( char **, const char *, ... ) VLC_USED;
210 
211 #endif