fix noinst_HEADERS; missing vorbiscomment.h
[platform/upstream/flac.git] / src / plugin_common / charset.c
1 /* plugin_common - Routines common to several plugins
2  * Copyright (C) 2002  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #ifdef HAVE_ICONV
29 #include <iconv.h>
30 #endif
31
32 #ifdef HAVE_LANGINFO_CODESET
33 #include <langinfo.h>
34 #endif
35
36 #include "charset.h"
37
38
39 /*************
40  * Functions *
41  *************/
42
43 char* FLAC_plugin__charset_get_current (void)
44 {
45         char *charset = getenv("CHARSET");
46
47 #ifdef HAVE_LANGINFO_CODESET
48         if (!charset)
49                 charset = nl_langinfo(CODESET);
50 #endif
51         if (!charset)
52                 charset = "ISO-8859-1";
53
54         return charset;
55 }
56
57
58 #ifdef HAVE_ICONV
59 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
60 {
61         size_t outleft, outsize, length;
62         iconv_t cd;
63         char *out, *outptr;
64         const char *input = string;
65
66         if (!string)
67                 return NULL;
68
69         length = strlen(string);
70
71         if ((cd = iconv_open(to, from)) == (iconv_t)-1)
72         {
73 #ifdef DEBUG
74                 fprintf(stderr, "convert_string(): Conversion not supported. Charsets: %s -> %s", from, to);
75 #endif
76                 return strdup(string);
77         }
78
79         /* Due to a GLIBC bug, round outbuf_size up to a multiple of 4 */
80         /* + 1 for nul in case len == 1 */
81         outsize = ((length + 3) & ~3) + 1;
82         out = malloc(outsize);
83         outleft = outsize - 1;
84         outptr = out;
85
86 retry:
87         if (iconv(cd, &input, &length, &outptr, &outleft) == -1)
88         {
89                 int used;
90                 switch (errno)
91                 {
92                         case E2BIG:
93                                 used = outptr - out;
94                                 outsize = (outsize - 1) * 2 + 1;
95                                 out = realloc(out, outsize);
96                                 outptr = out + used;
97                                 outleft = outsize - 1 - used;
98                                 goto retry;
99                         case EINVAL:
100                                 break;
101                         case EILSEQ:
102                                 /* Invalid sequence, try to get the rest of the string */
103                                 input++;
104                                 length = strlen(input);
105                                 goto retry;
106                         default:
107 #ifdef DEBUG
108                                 fprintf(stderr, "convert_string(): Conversion failed. Inputstring: %s; Error: %s", string, strerror(errno));
109 #endif
110                                 break;
111                 }
112         }
113         *outptr = '\0';
114
115         iconv_close(cd);
116         return out;
117 }
118 #else
119 char* FLAC_plugin__charset_convert_string (const char *string, char *from, char *to)
120 {
121         (void)from, (void)to;
122         if (!string)
123                 return NULL;
124         return strdup(string);
125 }
126 #endif
127
128 #ifdef HAVE_ICONV
129 int FLAC_plugin__charset_test_conversion (char *from, char *to)
130 {
131         iconv_t cd;
132
133         if ((cd=iconv_open(to,from)) == (iconv_t)-1)
134         {
135                 /* Conversion not supported */
136                 return 0;
137         }
138         iconv_close(cd);
139         return ;
140 }
141 #else
142 int FLAC_plugin__charset_test_conversion (char *from, char *to)
143 {
144         (void)from, (void)to;
145         return 1;
146 }
147 #endif