1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * Only slightly modified charset.c from:
5 * EasyTAG - Tag editor for MP3 and OGG files
6 * Copyright (C) 1999-2001 Håvard Kvålen <havardk@xmms.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #ifdef HAVE_LANGINFO_CODESET
47 char* FLAC_plugin__charset_get_current (void)
49 char *charset = getenv("CHARSET");
51 #ifdef HAVE_LANGINFO_CODESET
53 charset = nl_langinfo(CODESET);
56 charset = "ISO-8859-1";
63 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
65 size_t outleft, outsize, length;
68 const char *input = string;
73 length = strlen(string);
75 if ((cd = iconv_open(to, from)) == (iconv_t)-1)
78 fprintf(stderr, "convert_string(): Conversion not supported. Charsets: %s -> %s", from, to);
80 return strdup(string);
83 /* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
84 /* + 1 for nul in case len == 1 */
85 outsize = ((length + 3) & ~3) + 1;
86 if(outsize < length) /* overflow check */
88 out = malloc(outsize);
89 outleft = outsize - 1;
93 if (iconv(cd, (char**)&input, &length, &outptr, &outleft) == (size_t)(-1))
100 if((outsize - 1) * 2 + 1 <= outsize) { /* overflow check */
104 outsize = (outsize - 1) * 2 + 1;
105 out = realloc(out, outsize);
107 outleft = outsize - 1 - used;
112 /* Invalid sequence, try to get the rest of the string */
114 length = strlen(input);
118 fprintf(stderr, "convert_string(): Conversion failed. Inputstring: %s; Error: %s", string, strerror(errno));
129 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
131 (void)from, (void)to;
134 return strdup(string);
139 int FLAC_plugin__charset_test_conversion (char *from, char *to)
143 if ((cd=iconv_open(to,from)) == (iconv_t)-1)
145 /* Conversion not supported */
152 int FLAC_plugin__charset_test_conversion (char *from, char *to)
154 (void)from, (void)to;