Initial revision 6759
[qball-mpd.git] / src / charConv.c
blob69777c47a6e99a4c5c3b8a845caf2d381ccdceaa
1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "charConv.h"
20 #include "mpd_types.h"
21 #include "utf8.h"
22 #include "utils.h"
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
28 #ifdef HAVE_ICONV
29 #include <iconv.h>
30 static iconv_t char_conv_iconv;
31 #endif
33 static char *char_conv_to;
34 static char *char_conv_from;
35 static mpd_sint8 char_conv_same;
36 static mpd_sint8 char_conv_use_iconv;
38 /* 1 is to use latin1ToUtf8
39 0 is not to use latin1/utf8 converter
40 -1 is to use utf8ToLatin1*/
41 static mpd_sint8 char_conv_latin1ToUtf8;
43 #define BUFFER_SIZE 1024
45 static void closeCharSetConversion(void);
47 int setCharSetConversion(char *to, char *from)
49 if (char_conv_to && char_conv_from) {
50 if (char_conv_latin1ToUtf8 &&
51 !strcmp(from, char_conv_to) &&
52 !strcmp(to, char_conv_from)) {
53 char *swap = char_conv_from;
54 char_conv_from = char_conv_to;
55 char_conv_to = swap;
56 char_conv_latin1ToUtf8 *= -1;
57 return 0;
58 } else if (!strcmp(to, char_conv_to) &&
59 !strcmp(from,char_conv_from)) {
60 return 0;
64 closeCharSetConversion();
66 if (0 == strcmp(to, from)) {
67 char_conv_same = 1;
68 char_conv_to = xstrdup(to);
69 char_conv_from = xstrdup(from);
70 return 0;
73 if (strcmp(to, "UTF-8") == 0 && strcmp(from, "ISO-8859-1") == 0) {
74 char_conv_latin1ToUtf8 = 1;
75 } else if (strcmp(to, "ISO-8859-1") == 0 && strcmp(from, "UTF-8") == 0) {
76 char_conv_latin1ToUtf8 = -1;
79 if (char_conv_latin1ToUtf8 != 0) {
80 char_conv_to = xstrdup(to);
81 char_conv_from = xstrdup(from);
82 return 0;
84 #ifdef HAVE_ICONV
85 if ((char_conv_iconv = iconv_open(to, from)) == (iconv_t) (-1))
86 return -1;
88 char_conv_to = xstrdup(to);
89 char_conv_from = xstrdup(from);
90 char_conv_use_iconv = 1;
92 return 0;
93 #endif
95 return -1;
98 char *convStrDup(char *string)
100 if (!char_conv_to)
101 return NULL;
103 if (char_conv_same)
104 return xstrdup(string);
106 #ifdef HAVE_ICONV
107 if (char_conv_use_iconv) {
108 char buffer[BUFFER_SIZE];
109 size_t inleft = strlen(string);
110 char *ret;
111 size_t outleft;
112 size_t retlen = 0;
113 size_t err;
114 char *bufferPtr;
116 ret = xmalloc(1);
117 ret[0] = '\0';
119 while (inleft) {
120 bufferPtr = buffer;
121 outleft = BUFFER_SIZE;
122 err =
123 iconv(char_conv_iconv, &string, &inleft, &bufferPtr,
124 &outleft);
125 if (outleft == BUFFER_SIZE
126 || (err == -1L && errno != E2BIG)) {
127 free(ret);
128 return NULL;
131 ret = xrealloc(ret, retlen + BUFFER_SIZE - outleft + 1);
132 memcpy(ret + retlen, buffer, BUFFER_SIZE - outleft);
133 retlen += BUFFER_SIZE - outleft;
134 ret[retlen] = '\0';
137 return ret;
139 #endif
141 switch (char_conv_latin1ToUtf8) {
142 case 1:
143 return latin1StrToUtf8Dup(string);
144 break;
145 case -1:
146 return utf8StrToLatin1Dup(string);
147 break;
150 return NULL;
153 static void closeCharSetConversion(void)
155 if (char_conv_to) {
156 #ifdef HAVE_ICONV
157 if (char_conv_use_iconv)
158 iconv_close(char_conv_iconv);
159 #endif
160 free(char_conv_to);
161 free(char_conv_from);
162 char_conv_to = NULL;
163 char_conv_from = NULL;
164 char_conv_same = 0;
165 char_conv_latin1ToUtf8 = 0;
166 char_conv_use_iconv = 0;