Code

Added iconv support
authorKalle Wallin <kaw@linux.se>
Fri, 19 Mar 2004 19:15:43 +0000 (19:15 +0000)
committerKalle Wallin <kaw@linux.se>
Fri, 19 Mar 2004 19:15:43 +0000 (19:15 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@308 09075e82-0dd4-0310-85a5-a0d7c8717e4f

configure.ac
mpc.c
screen_file.c
support.c
support.h

index 8adb481ee7d26a49eee5b25eade0fb9ff563e663..1d537c6e98a676d4eef38a89dd00ca30019c1442 100644 (file)
@@ -18,6 +18,7 @@ set -- $CFLAGS
 CFLAGS="-Wall $CFLAGS"
 
 
+
 dnl
 dnl Check for libaries
 dnl
@@ -44,6 +45,18 @@ AC_CHECK_TYPE(socklen_t,
 dnl
 dnl Check for headers
 dnl
+
+AM_ICONV
+
+AC_CHECK_HEADER(langinfo.h,
+               AC_DEFINE(HAVE_LANGINFO_H, 1, langinfo.h),
+               ,)
+
+AC_CHECK_HEADER(locale.h,
+               AC_DEFINE(HAVE_LOCALE_H, 1, locale.h),
+               ,)
+
+
 AC_CHECK_HEADER(libgen.h,
                 AC_DEFINE(HAVE_LIBGEN_H, 1, glibc - libgen.h),
                ,)
diff --git a/mpc.c b/mpc.c
index ccb08359009ff880587e52c25d665cf24cbeac3f..f62d9789c8c91549b9a975bcf59da17df292e3bc 100644 (file)
--- a/mpc.c
+++ b/mpc.c
@@ -16,6 +16,8 @@
 #include "mpc.h"
 #include "options.h"
 
+#define MAX_SONG_LENGTH 1024
+
 void 
 mpc_update_song(mpd_client_t *c)
 {
@@ -186,21 +188,31 @@ mpc_playlist_get_song(mpd_client_t *c, int n)
 char *
 mpc_get_song_name(mpd_Song *song)
 {
+  static char buf[MAX_SONG_LENGTH];
+  char *name;
+  
   if( song->title )
     {
       if( song->artist )
        {
-         static char buf[512];
-
-         snprintf(buf, 512, "%s - %s", song->artist, song->title);
-         return utf8(buf);
+         snprintf(buf, MAX_SONG_LENGTH, "%s - %s", song->artist, song->title);
+         name = utf8_to_locale(buf);
+         strncpy(buf, name, MAX_SONG_LENGTH);
+         free(name);
+         return buf;
        }
       else
        {
-         return utf8(song->title);
+         name = utf8_to_locale(song->title);
+         strncpy(buf, name, MAX_SONG_LENGTH);
+         free(name);
+         return buf;
        }
     }
-  return utf8(basename(song->file));
+  name = utf8_to_locale(song->file);
+  strncpy(buf, name, MAX_SONG_LENGTH);
+  free(name);
+  return buf;
 }
 
 int 
index bd60872e60e438a0060800dc93eb1c9b8adfab10..d83a1870c1735c180cbc349f3c246f2cfa644b27 100644 (file)
 #include "screen.h"
 #include "screen_file.h"
 
+#define BUFSIZE 1024
 
 static char *
 list_callback(int index, int *highlight, void *data)
 {
+  static char buf[BUFSIZE];
   mpd_client_t *c = (mpd_client_t *) data;
   filelist_entry_t *entry;
   mpd_InfoEntity *entity;
@@ -37,10 +39,12 @@ list_callback(int index, int *highlight, void *data)
     }
   if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) 
     {
-
       mpd_Directory *dir = entity->info.directory;
+      char *dirname = utf8_to_locale(dir->path);
 
-      return utf8(basename(dir->path));
+      strncpy(buf, dirname, BUFSIZE);
+      free(dirname);
+      return buf;
     }
   else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG )
     {
index ce67dc81e6b507e3db2f42e7bc5bee7449ac69fb..d2dc9759c7e173f94a0535dce9dd96807e2efd5e 100644 (file)
--- 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
 }
+
+
+
index 834dd8cda14dc52dbe776066b3bc9e34547366c9..c0b9c34dc05b33ca0b88d8c69249de203b5deaa8 100644 (file)
--- a/support.h
+++ b/support.h
@@ -5,5 +5,10 @@
 char *basename(char *path);
 #endif
 
-char *utf8(char *str);
+
+
+int charset_init(void);
+int charset_close(void);
+char *utf8_to_locale(char *str);
+