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
20 #include "mpd_types.h"
30 static iconv_t char_conv_iconv
;
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
;
56 char_conv_latin1ToUtf8
*= -1;
58 } else if (!strcmp(to
, char_conv_to
) &&
59 !strcmp(from
,char_conv_from
)) {
64 closeCharSetConversion();
66 if (0 == strcmp(to
, from
)) {
68 char_conv_to
= xstrdup(to
);
69 char_conv_from
= xstrdup(from
);
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
);
85 if ((char_conv_iconv
= iconv_open(to
, from
)) == (iconv_t
) (-1))
88 char_conv_to
= xstrdup(to
);
89 char_conv_from
= xstrdup(from
);
90 char_conv_use_iconv
= 1;
98 char *convStrDup(char *string
)
104 return xstrdup(string
);
107 if (char_conv_use_iconv
) {
108 char buffer
[BUFFER_SIZE
];
109 size_t inleft
= strlen(string
);
121 outleft
= BUFFER_SIZE
;
123 iconv(char_conv_iconv
, &string
, &inleft
, &bufferPtr
,
125 if (outleft
== BUFFER_SIZE
126 || (err
== -1L && errno
!= E2BIG
)) {
131 ret
= xrealloc(ret
, retlen
+ BUFFER_SIZE
- outleft
+ 1);
132 memcpy(ret
+ retlen
, buffer
, BUFFER_SIZE
- outleft
);
133 retlen
+= BUFFER_SIZE
- outleft
;
141 switch (char_conv_latin1ToUtf8
) {
143 return latin1StrToUtf8Dup(string
);
146 return utf8StrToLatin1Dup(string
);
153 static void closeCharSetConversion(void)
157 if (char_conv_use_iconv
)
158 iconv_close(char_conv_iconv
);
161 free(char_conv_from
);
163 char_conv_from
= NULL
;
165 char_conv_latin1ToUtf8
= 0;
166 char_conv_use_iconv
= 0;