]> git.sesse.net Git - vlc/blob - src/text/charset.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / text / charset.c
1 /*****************************************************************************
2  * charset.c: Locale's character encoding stuff.
3  *****************************************************************************
4  * See also unicode.c for Unicode to locale conversion helpers.
5  *
6  * Copyright (C) 2003-2008 the VideoLAN team
7  *
8  * Authors: Christophe Massiot
9  *          RĂ©mi Denis-Courmont
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31
32 #if !defined WIN32
33 # include <locale.h>
34 #else
35 # include <windows.h>
36 #endif
37
38 #ifdef __APPLE__
39 #   include <string.h>
40 #   include <xlocale.h>
41 #endif
42
43 #include "libvlc.h"
44 #include <vlc_charset.h>
45
46 char *vlc_fix_readdir( const char *psz_string )
47 {
48 #ifdef __APPLE__
49     vlc_iconv_t hd = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
50
51     if (hd != (vlc_iconv_t)(-1))
52     {
53         const char *psz_in = psz_string;
54         size_t i_in = strlen(psz_in);
55         size_t i_out = i_in * 2;
56         char *psz_utf8 = malloc(i_out + 1);
57         char *psz_out = psz_utf8;
58
59         size_t i_ret = vlc_iconv (hd, &psz_in, &i_in, &psz_out, &i_out);
60         vlc_iconv_close (hd);
61         if( i_ret == (size_t)(-1) || i_in )
62         {
63             free( psz_utf8 );
64             return strdup( psz_string );
65         }
66
67         *psz_out = '\0';
68         return psz_utf8;
69     }
70 #endif
71     return strdup( psz_string );
72 }
73
74
75 /**
76  * us_strtod() has the same prototype as ANSI C strtod() but it uses the
77  * POSIX/C decimal format, regardless of the current numeric locale.
78  */
79 double us_strtod( const char *str, char **end )
80 {
81     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
82     locale_t oldloc = uselocale (loc);
83     double res = strtod (str, end);
84
85     if (loc != (locale_t)0)
86     {
87         uselocale (oldloc);
88         freelocale (loc);
89     }
90     return res;
91 }
92
93
94 /**
95  * us_strtof() has the same prototype as ANSI C strtof() but it uses the
96  * POSIX/C decimal format, regardless of the current numeric locale.
97  */
98 float us_strtof( const char *str, char **end )
99 {
100     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
101     locale_t oldloc = uselocale (loc);
102     float res = strtof (str, end);
103
104     if (loc != (locale_t)0)
105     {
106         uselocale (oldloc);
107         freelocale (loc);
108     }
109     return res;
110 }
111
112
113 /**
114  * us_atof() has the same prototype as ANSI C atof() but it expects a dot
115  * as decimal separator, regardless of the system locale.
116  */
117 double us_atof( const char *str )
118 {
119     return us_strtod( str, NULL );
120 }
121
122
123 /**
124  * us_asprintf() has the same prototype as asprintf(), but doesn't use
125  * the system locale.
126  */
127 int us_asprintf( char **ret, const char *format, ... )
128 {
129     va_list ap;
130     locale_t loc = newlocale( LC_NUMERIC_MASK, "C", NULL );
131     locale_t oldloc = uselocale( loc );
132     int i_rc;
133
134     va_start( ap, format );
135     i_rc = vasprintf( ret, format, ap );
136     va_end( ap );
137
138     if ( loc != (locale_t)0 )
139     {
140         uselocale( oldloc );
141         freelocale( loc );
142     }
143
144     return i_rc;
145 }