d3d11_quad: fix YUVA viewport on NV12/P010
[vlc.git] / include / vlc_charset.h
blob05092257a4a1f09451dc1e956d07b02093908dd4
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$
8 * Author: Rémi Denis-Courmont <rem # videolan,org>
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.
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.
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 *****************************************************************************/
25 #ifndef VLC_CHARSET_H
26 #define VLC_CHARSET_H 1
28 /**
29 * \file
30 * Characters sets handling
32 * \ingroup strings
33 * @{
36 /**
37 * Decodes a code point from UTF-8.
39 * Converts the first character in a UTF-8 sequence into a Unicode code point.
41 * \param str an UTF-8 bytes sequence [IN]
42 * \param pwc address of a location to store the code point [OUT]
44 * \return the number of bytes occupied by the decoded code point
46 * \retval (size_t)-1 not a valid UTF-8 sequence
47 * \retval 0 null character (i.e. str points to an empty string)
48 * \retval 1 (non-null) ASCII character
49 * \retval 2-4 non-ASCII character
51 VLC_API size_t vlc_towc(const char *str, uint32_t *restrict pwc);
53 /**
54 * Checks UTF-8 validity.
56 * Checks whether a null-terminated string is a valid UTF-8 bytes sequence.
58 * \param str string to check
60 * \retval str the string is a valid null-terminated UTF-8 sequence
61 * \retval NULL the string is not an UTF-8 sequence
63 VLC_USED static inline const char *IsUTF8(const char *str)
65 size_t n;
66 uint32_t cp;
68 while ((n = vlc_towc(str, &cp)) != 0)
69 if (likely(n != (size_t)-1))
70 str += n;
71 else
72 return NULL;
73 return str;
76 /**
77 * Removes non-UTF-8 sequences.
79 * Replaces invalid or <i>over-long</i> UTF-8 bytes sequences within a
80 * null-terminated string with question marks. This is so that the string can
81 * be printed at least partially.
83 * \warning Do not use this were correctness is critical. use IsUTF8() and
84 * handle the error case instead. This function is mainly for display or debug.
86 * \note Converting from Latin-1 to UTF-8 in place is not possible (the string
87 * size would be increased). So it is not attempted even if it would otherwise
88 * be less disruptive.
90 * \retval str the string is a valid null-terminated UTF-8 sequence
91 * (i.e. no changes were made)
92 * \retval NULL the string is not an UTF-8 sequence
94 static inline char *EnsureUTF8(char *str)
96 char *ret = str;
97 size_t n;
98 uint32_t cp;
100 while ((n = vlc_towc(str, &cp)) != 0)
101 if (likely(n != (size_t)-1))
102 str += n;
103 else
105 *str++ = '?';
106 ret = NULL;
108 return ret;
111 /* iconv wrappers (defined in src/extras/libc.c) */
112 #define VLC_ICONV_ERR ((size_t) -1)
113 typedef void *vlc_iconv_t;
114 VLC_API vlc_iconv_t vlc_iconv_open( const char *, const char * ) VLC_USED;
115 VLC_API size_t vlc_iconv( vlc_iconv_t, const char **, size_t *, char **, size_t * ) VLC_USED;
116 VLC_API int vlc_iconv_close( vlc_iconv_t );
118 #include <stdarg.h>
120 VLC_API int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap );
121 VLC_API int utf8_fprintf( FILE *, const char *, ... ) VLC_FORMAT( 2, 3 );
122 VLC_API char * vlc_strcasestr(const char *, const char *) VLC_USED;
124 VLC_API char * FromCharset( const char *charset, const void *data, size_t data_size ) VLC_USED;
125 VLC_API void * ToCharset( const char *charset, const char *in, size_t *outsize ) VLC_USED;
127 #ifdef _WIN32
128 VLC_USED
129 static inline char *FromWide (const wchar_t *wide)
131 size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
132 if (len == 0)
133 return NULL;
135 char *out = (char *)malloc (len);
137 if (likely(out))
138 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
139 return out;
142 VLC_USED
143 static inline wchar_t *ToWide (const char *utf8)
145 int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
146 if (len == 0)
147 return NULL;
149 wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
151 if (likely(out))
152 MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
153 return out;
156 VLC_USED VLC_MALLOC
157 static inline char *ToCodePage (unsigned cp, const char *utf8)
159 wchar_t *wide = ToWide (utf8);
160 if (wide == NULL)
161 return NULL;
163 size_t len = WideCharToMultiByte (cp, 0, wide, -1, NULL, 0, NULL, NULL);
164 if (len == 0) {
165 free(wide);
166 return NULL;
169 char *out = (char *)malloc (len);
170 if (likely(out != NULL))
171 WideCharToMultiByte (cp, 0, wide, -1, out, len, NULL, NULL);
172 free (wide);
173 return out;
176 VLC_USED VLC_MALLOC
177 static inline char *FromCodePage (unsigned cp, const char *mb)
179 int len = MultiByteToWideChar (cp, 0, mb, -1, NULL, 0);
180 if (len == 0)
181 return NULL;
183 wchar_t *wide = (wchar_t *)malloc (len * sizeof (wchar_t));
184 if (unlikely(wide == NULL))
185 return NULL;
186 MultiByteToWideChar (cp, 0, mb, -1, wide, len);
188 char *utf8 = FromWide (wide);
189 free (wide);
190 return utf8;
193 VLC_USED VLC_MALLOC
194 static inline char *FromANSI (const char *ansi)
196 return FromCodePage (GetACP (), ansi);
199 VLC_USED VLC_MALLOC
200 static inline char *ToANSI (const char *utf8)
202 return ToCodePage (GetACP (), utf8);
205 # ifdef UNICODE
206 # define FromT FromWide
207 # define ToT ToWide
208 # else
209 # define FromT FromANSI
210 # define ToT ToANSI
211 # endif
212 # define FromLocale FromANSI
213 # define ToLocale ToANSI
214 # define LocaleFree(s) free((char *)(s))
215 # define FromLocaleDup FromANSI
216 # define ToLocaleDup ToANSI
218 #elif defined(__OS2__)
220 VLC_USED static inline char *FromLocale (const char *locale)
222 return locale ? FromCharset ((char *)"", locale, strlen(locale)) : NULL;
225 VLC_USED static inline char *ToLocale (const char *utf8)
227 size_t outsize;
228 return utf8 ? (char *)ToCharset ("", utf8, &outsize) : NULL;
231 VLC_USED static inline void LocaleFree (const char *str)
233 free ((char *)str);
236 VLC_USED static inline char *FromLocaleDup (const char *locale)
238 return FromCharset ("", locale, strlen(locale));
241 VLC_USED static inline char *ToLocaleDup (const char *utf8)
243 size_t outsize;
244 return (char *)ToCharset ("", utf8, &outsize);
247 #else
249 # define FromLocale(l) (l)
250 # define ToLocale(u) (u)
251 # define LocaleFree(s) ((void)(s))
252 # define FromLocaleDup strdup
253 # define ToLocaleDup strdup
254 #endif
257 * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
259 static inline char *FromLatin1 (const char *latin)
261 char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
262 unsigned char c;
264 if (str == NULL)
265 return NULL;
267 while ((c = *(latin++)) != '\0')
269 if (c >= 0x80)
271 *(utf8++) = 0xC0 | (c >> 6);
272 *(utf8++) = 0x80 | (c & 0x3F);
274 else
275 *(utf8++) = c;
277 *(utf8++) = '\0';
279 utf8 = (char *)realloc (str, utf8 - str);
280 return utf8 ? utf8 : str;
283 /** @} */
285 VLC_API double us_strtod( const char *, char ** ) VLC_USED;
286 VLC_API float us_strtof( const char *, char ** ) VLC_USED;
287 VLC_API double us_atof( const char * ) VLC_USED;
288 VLC_API int us_vasprintf( char **, const char *, va_list );
289 VLC_API int us_asprintf( char **, const char *, ... ) VLC_USED;
291 #endif