Add struct stat into struct directory, special case for symbolic links
[cmus.git] / utf8_encode.c
blobf182260a6320638c0e5430082529ec58b93e5083
1 /*
2 * Copyright 2004 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
17 * 02111-1307, USA.
20 #include <utf8_encode.h>
21 #include <xmalloc.h>
23 #include <iconv.h>
24 #include <string.h>
25 #include <errno.h>
27 int utf8_encode(const char *inbuf, const char *encoding, char **outbuf)
29 const char *in;
30 char *out;
31 size_t inbuf_size, outbuf_size, i;
32 iconv_t cd;
33 int rc;
35 cd = iconv_open("UTF-8", encoding);
36 if (cd == (iconv_t)-1)
37 return -1;
38 inbuf_size = strlen(inbuf);
39 outbuf_size = inbuf_size;
40 for (i = 0; i < inbuf_size; i++) {
41 unsigned char ch;
43 ch = inbuf[i];
44 if (ch > 127)
45 outbuf_size++;
47 *outbuf = xnew(char, outbuf_size + 1);
48 in = inbuf;
49 out = *outbuf;
50 rc = iconv(cd, (char **)&in, &inbuf_size, &out, &outbuf_size);
51 *out = 0;
52 if (rc == -1) {
53 int save = errno;
54 iconv_close(cd);
55 free(*outbuf);
56 *outbuf = NULL;
57 errno = save;
58 return -1;
60 rc = iconv_close(cd);
61 if (rc == -1) {
62 int save = errno;
63 free(*outbuf);
64 *outbuf = NULL;
65 errno = save;
66 return -1;
68 return 0;