comparison src/flac113/plugin_common/charset.c @ 103:117bc56d906b trunk

[svn] flac -> flac113
author nenolod
date Mon, 23 Oct 2006 19:51:39 -0700
parents src/flac/plugin_common/charset.c@3da1b8942b8b
children
comparison
equal deleted inserted replaced
102:aff1cf3e86dd 103:117bc56d906b
1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002,2003,2004,2005 Josh Coalson
3 *
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>
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #ifdef HAVE_ICONV
33 #include <iconv.h>
34 #endif
35
36 #ifdef HAVE_LANGINFO_CODESET
37 #include <langinfo.h>
38 #endif
39
40 #include "charset.h"
41
42
43 /*************
44 * Functions *
45 *************/
46
47 char* FLAC_plugin__charset_get_current (void)
48 {
49 char *charset = getenv("CHARSET");
50
51 #ifdef HAVE_LANGINFO_CODESET
52 if (!charset)
53 charset = nl_langinfo(CODESET);
54 #endif
55
56 if (charset)
57 return strdup(charset);
58
59 return strdup("ISO-8859-1");
60 }
61
62
63 #ifdef HAVE_ICONV
64 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
65 {
66 size_t outleft, outsize, length;
67 iconv_t cd;
68 char *out, *outptr;
69 const char *input = string;
70
71 if (!string)
72 return NULL;
73
74 length = strlen(string);
75
76 if ((cd = iconv_open(to, from)) == (iconv_t)-1)
77 {
78 #ifdef DEBUG
79 fprintf(stderr, "convert_string(): Conversion not supported. Charsets: %s -> %s", from, to);
80 #endif
81 return strdup(string);
82 }
83
84 /* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
85 /* + 1 for nul in case len == 1 */
86 outsize = ((length + 3) & ~3) + 1;
87 out = (char*)malloc(outsize);
88 outleft = outsize - 1;
89 outptr = out;
90
91 retry:
92 #if defined __OpenBSD__ || defined __NetBSD__
93 if (iconv(cd, &input, &length, &outptr, &outleft) == (size_t)-1)
94 #else
95 if (iconv(cd, (char**)&input, &length, &outptr, &outleft) == (size_t)-1)
96 #endif
97 {
98 int used;
99 switch (errno)
100 {
101 case E2BIG:
102 used = outptr - out;
103 outsize = (outsize - 1) * 2 + 1;
104 out = realloc(out, outsize);
105 outptr = out + used;
106 outleft = outsize - 1 - used;
107 goto retry;
108 case EINVAL:
109 break;
110 case EILSEQ:
111 /* Invalid sequence, try to get the rest of the string */
112 input++;
113 length = strlen(input);
114 goto retry;
115 default:
116 #ifdef DEBUG
117 fprintf(stderr, "convert_string(): Conversion failed. Inputstring: %s; Error: %s", string, strerror(errno));
118 #endif
119 break;
120 }
121 }
122 *outptr = '\0';
123
124 iconv_close(cd);
125 return out;
126 }
127 #else
128 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
129 {
130 (void)from, (void)to;
131 if (!string)
132 return NULL;
133 return strdup(string);
134 }
135 #endif
136
137 #ifdef HAVE_ICONV
138 int FLAC_plugin__charset_test_conversion (char *from, char *to)
139 {
140 iconv_t cd;
141
142 if ((cd=iconv_open(to,from)) == (iconv_t)-1)
143 {
144 /* Conversion not supported */
145 return 0;
146 }
147 iconv_close(cd);
148 return 1;
149 }
150 #else
151 int FLAC_plugin__charset_test_conversion (char *from, char *to)
152 {
153 (void)from, (void)to;
154 return 1;
155 }
156 #endif