aboutsummaryrefslogtreecommitdiffstats
path: root/support.c
diff options
context:
space:
mode:
authorKalle Wallin <kaw@linux.se>2004-03-19 19:15:43 +0000
committerKalle Wallin <kaw@linux.se>2004-03-19 19:15:43 +0000
commita48c2fe2c2d13fbc879a6b5111fc9f3c866ac5b3 (patch)
tree8a5ff0674412243fd2610f0c0b3f21cd6e176506 /support.c
parent526b460e750f52f5b9b04df72b6601fe4c1fd5f7 (diff)
downloadmpd-a48c2fe2c2d13fbc879a6b5111fc9f3c866ac5b3.tar.gz
mpd-a48c2fe2c2d13fbc879a6b5111fc9f3c866ac5b3.tar.xz
mpd-a48c2fe2c2d13fbc879a6b5111fc9f3c866ac5b3.zip
Added iconv support
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@308 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'support.c')
-rw-r--r--support.c124
1 files changed, 110 insertions, 14 deletions
diff --git a/support.c b/support.c
index ce67dc81e..d2dc9759c 100644
--- a/support.c
+++ b/support.c
@@ -1,16 +1,35 @@
-/*
- * $Id: support.c,v 1.2 2004/03/17 23:17:09 kalle Exp $
- *
- */
-
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "config.h"
+
+#ifdef HAVE_LOCALE_H
+#ifdef HAVE_LANGINFO_H
+#ifdef HAVE_ICONV
+#include <locale.h>
+#include <langinfo.h>
+#include <iconv.h>
+#define ENABLE_CHARACTER_SET_CONVERSION
+#endif
+#endif
+#endif
+
#include "support.h"
+#define BUFSIZE 1024
+
+#ifdef ENABLE_CHARACTER_SET_CONVERSION
+static char *locale = NULL;
+static char *charset = NULL;
+iconv_t iconv_from_uft8 = (iconv_t)(-1);
+iconv_t iconv_to_uft8 = (iconv_t)(-1);
+#endif
+
+
+
#ifndef HAVE_LIBGEN_H
char *
@@ -48,17 +67,94 @@ basename(char *path)
#endif /* HAVE_LIBGEN_H */
-char *
-utf8(char *str)
+
+int
+charset_init(void)
{
- static const gchar *charset = NULL;
- static gboolean locale_is_utf8 = FALSE;
+#ifdef ENABLE_CHARACTER_SET_CONVERSION
+ /* get current locale */
+ if( (locale=setlocale(LC_CTYPE,"")) == NULL )
+ {
+ fprintf(stderr,"setlocale() - failed!\n");
+ return -1;
+ }
+
+ /* get charset */
+ if( (charset=nl_langinfo(CODESET)) == NULL )
+ {
+ fprintf(stderr,"nl_langinfo() - failed!\n");
+ return -1;
+ }
+
+ /* allocate descriptor for character set conversion */
+ iconv_from_uft8 = iconv_open(charset, "UTF-8");
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ {
+ perror("iconv_open");
+ return -1;
+ }
+#endif
+
+ return 0;
+}
- if( !charset )
- locale_is_utf8 = g_get_charset(&charset);
+int
+charset_close(void)
+{
+#ifdef ENABLE_CHARACTER_SET_CONVERSION
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ {
+ iconv_close(iconv_from_uft8);
+ iconv_from_uft8 = (iconv_t)(-1);
+ }
+ if( iconv_to_uft8 == (iconv_t)(-1) )
+ {
+ iconv_close(iconv_to_uft8);
+ iconv_to_uft8 = (iconv_t)(-1);
+ }
+#endif
+ return 0;
+}
- if( locale_is_utf8 )
- return str;
- return g_locale_from_utf8(str, -1, NULL, NULL, NULL);
+
+char *
+utf8_to_locale(char *str)
+{
+#ifdef ENABLE_CHARACTER_SET_CONVERSION
+ size_t inleft;
+ size_t retlen;
+ char *ret;
+
+ if( iconv_from_uft8 == (iconv_t)(-1) )
+ return strdup(str);
+
+ ret = NULL;
+ retlen = 0;
+ inleft = strlen(str);
+ while( inleft>0 )
+ {
+ char buf[BUFSIZE];
+ size_t outleft = BUFSIZE;
+ char *bufp = buf;
+
+ if( iconv(iconv_from_uft8, &str, &inleft, &bufp, &outleft) <0 )
+ {
+ perror("iconv");
+ free(ret);
+ return NULL;
+ }
+ ret = realloc(ret, BUFSIZE-outleft+1);
+ memcpy(ret+retlen, buf, BUFSIZE-outleft);
+ retlen += BUFSIZE-outleft;
+ ret[retlen] = '\0';
+ }
+ return ret;
+
+#else
+ return strdup(str);
+#endif
}
+
+
+