charset.c 3.67 KB
Newer Older
1
/*****************************************************************************
2
 * charset.c: Locale's character encoding stuff.
3
 *****************************************************************************
4 5
 * See also unicode.c for Unicode to locale conversion helpers.
 *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
6
 * Copyright (C) 2003-2008 the VideoLAN team
7
 *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
8
 * Authors: Christophe Massiot
9
 *          Rémi Denis-Courmont
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25
 *****************************************************************************/

26 27 28 29
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#include <vlc_common.h>
31 32

#if !defined WIN32
33
# include <locale.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
34
#else
35 36 37
# include <windows.h>
#endif

38
#ifdef __APPLE__
39
#   include <string.h>
40
#   include <xlocale.h>
41 42
#endif

43
#include "libvlc.h"
zorglub's avatar
zorglub committed
44
#include <vlc_charset.h>
45

46 47
char *vlc_fix_readdir( const char *psz_string )
{
48
#ifdef __APPLE__
49 50 51
    vlc_iconv_t hd = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );

    if (hd != (vlc_iconv_t)(-1))
52 53 54 55 56 57 58
    {
        const char *psz_in = psz_string;
        size_t i_in = strlen(psz_in);
        size_t i_out = i_in * 2;
        char *psz_utf8 = malloc(i_out + 1);
        char *psz_out = psz_utf8;

59
        size_t i_ret = vlc_iconv (hd, &psz_in, &i_in, &psz_out, &i_out);
Rémi Denis-Courmont's avatar
Typo  
Rémi Denis-Courmont committed
60
        vlc_iconv_close (hd);
61
        if( i_ret == (size_t)(-1) || i_in )
62 63 64 65 66 67 68 69
        {
            free( psz_utf8 );
            return strdup( psz_string );
        }

        *psz_out = '\0';
        return psz_utf8;
    }
70
#endif
71
    return strdup( psz_string );
72
}
73

74

75
/**
76 77
 * us_strtod() has the same prototype as ANSI C strtod() but it uses the
 * POSIX/C decimal format, regardless of the current numeric locale.
78 79 80
 */
double us_strtod( const char *str, char **end )
{
81 82 83
    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
    locale_t oldloc = uselocale (loc);
    double res = strtod (str, end);
84

85 86 87 88 89 90
    if (loc != (locale_t)0)
    {
        uselocale (oldloc);
        freelocale (loc);
    }
    return res;
91 92
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

/**
 * us_strtof() has the same prototype as ANSI C strtof() but it uses the
 * POSIX/C decimal format, regardless of the current numeric locale.
 */
float us_strtof( const char *str, char **end )
{
    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
    locale_t oldloc = uselocale (loc);
    float res = strtof (str, end);

    if (loc != (locale_t)0)
    {
        uselocale (oldloc);
        freelocale (loc);
    }
    return res;
}


113 114 115 116 117 118 119 120 121
/**
 * us_atof() has the same prototype as ANSI C atof() but it expects a dot
 * as decimal separator, regardless of the system locale.
 */
double us_atof( const char *str )
{
    return us_strtod( str, NULL );
}

Michael Hanselmann's avatar
Michael Hanselmann committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

/**
 * us_asprintf() has the same prototype as asprintf(), but doesn't use
 * the system locale.
 */
int us_asprintf( char **ret, const char *format, ... )
{
    va_list ap;
    locale_t loc = newlocale( LC_NUMERIC_MASK, "C", NULL );
    locale_t oldloc = uselocale( loc );
    int i_rc;

    va_start( ap, format );
    i_rc = vasprintf( ret, format, ap );
    va_end( ap );

    if ( loc != (locale_t)0 )
    {
        uselocale( oldloc );
        freelocale( loc );
    }

    return i_rc;
}