]> git.sesse.net Git - vlc/blob - src/text/charset.c
dsm: change item b_net and i_type
[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 VLC authors and VideoLAN
7  *
8  * Authors: Christophe Massiot
9  *          RĂ©mi Denis-Courmont
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * 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 /**
47  * us_strtod() has the same prototype as ANSI C strtod() but it uses the
48  * POSIX/C decimal format, regardless of the current numeric locale.
49  */
50 double us_strtod( const char *str, char **end )
51 {
52     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
53     locale_t oldloc = uselocale (loc);
54     double res = strtod (str, end);
55
56     if (loc != (locale_t)0)
57     {
58         uselocale (oldloc);
59         freelocale (loc);
60     }
61     return res;
62 }
63
64
65 /**
66  * us_strtof() has the same prototype as ANSI C strtof() but it uses the
67  * POSIX/C decimal format, regardless of the current numeric locale.
68  */
69 float us_strtof( const char *str, char **end )
70 {
71     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
72     locale_t oldloc = uselocale (loc);
73     float res = strtof (str, end);
74
75     if (loc != (locale_t)0)
76     {
77         uselocale (oldloc);
78         freelocale (loc);
79     }
80     return res;
81 }
82
83
84 /**
85  * us_atof() has the same prototype as ANSI C atof() but it expects a dot
86  * as decimal separator, regardless of the system locale.
87  */
88 double us_atof( const char *str )
89 {
90     return us_strtod( str, NULL );
91 }
92
93
94 /**
95  * us_vasprintf() has the same prototype as vasprintf(), but doesn't use
96  * the system locale.
97  */
98 int us_vasprintf( char **ret, const char *format, va_list ap )
99 {
100     locale_t loc = newlocale( LC_NUMERIC_MASK, "C", NULL );
101     locale_t oldloc = uselocale( loc );
102
103     int i_rc = vasprintf( ret, format, ap );
104
105     if ( loc != (locale_t)0 )
106     {
107         uselocale( oldloc );
108         freelocale( loc );
109     }
110
111     return i_rc;
112 }
113
114
115 /**
116  * us_asprintf() has the same prototype as asprintf(), but doesn't use
117  * the system locale.
118  */
119 int us_asprintf( char **ret, const char *format, ... )
120 {
121     va_list ap;
122     int i_rc;
123
124     va_start( ap, format );
125     i_rc = us_vasprintf( ret, format, ap );
126     va_end( ap );
127
128     return i_rc;
129 }