From: Kalle Wallin Date: Sun, 28 Mar 2004 20:45:29 +0000 (+0000) Subject: Started to use glib (not iconv) for character set conversions. X-Git-Tag: v0.12_alpha1~631 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a01b91b2c10797ad2bf6d9d7901bf59ea7d2e193;p=ncmpc.git Started to use glib (not iconv) for character set conversions. git-svn-id: https://svn.musicpd.org/ncmpc/trunk@524 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- diff --git a/configure.ac b/configure.ac index 67b4c93..d11ba0f 100644 --- a/configure.ac +++ b/configure.ac @@ -48,17 +48,6 @@ dnl dnl Check for libraries dnl -dnl iconv -AM_ICONV - -dnl nl_langinfo() -AM_LANGINFO_CODESET -if test $am_cv_langinfo_codeset = no; then - LANGINFO_OBJS=langinfo.o -fi -AC_SUBST(LANGINFO_OBJS) - - dnl ncurses AC_CHECK_LIB(ncurses, initscr,, [AC_MSG_ERROR(ncurses library is required)]) diff --git a/langinfo.c b/langinfo.c deleted file mode 100644 index b7961ed..0000000 --- a/langinfo.c +++ /dev/null @@ -1,119 +0,0 @@ -/* - * This is a quick-and-dirty emulator of the nl_langinfo(CODESET) - * function defined in the Single Unix Specification for those systems - * (FreeBSD, etc.) that don't have one yet. It behaves as if it had - * been called after setlocale(LC_CTYPE, ""), that is it looks at - * the locale environment variables. - * - * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html - * - * Please extend it as needed and suggest improvements to the author. - * This emulator will hopefully become redundant soon as - * nl_langinfo(CODESET) becomes more widely implemented. - * - * Since the proposed Li18nux encoding name registry is still not mature, - * the output follows the MIME registry where possible: - * - * http://www.iana.org/assignments/character-sets - * - * A possible autoconf test for the availability of nl_langinfo(CODESET) - * can be found in - * - * http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate - * - * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11 - * Permission to use, copy, modify, and distribute this software - * for any purpose and without fee is hereby granted. The author - * disclaims all warranties with regard to this software. - * - * Latest version: - * - * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c - */ - -#include -#include -#include "support.h" - -#define C_CODESET "US-ASCII" /* Return this as the encoding of the - * C/POSIX locale. Could as well one day - * become "UTF-8". */ - -#define digit(x) ((x) >= '0' && (x) <= '9') - -static char buf[16]; - -char *nl_langinfo(nl_item item) -{ - char *l, *p; - - if (item != CODESET) - return NULL; - - if (((l = getenv("LC_ALL")) && *l) || - ((l = getenv("LC_CTYPE")) && *l) || - ((l = getenv("LANG")) && *l)) { - /* check standardized locales */ - if (!strcmp(l, "C") || !strcmp(l, "POSIX")) - return C_CODESET; - /* check for encoding name fragment */ - if (strstr(l, "UTF") || strstr(l, "utf")) - return "UTF-8"; - if ((p = strstr(l, "8859-"))) { - memcpy(buf, "ISO-8859-\0\0", 12); - p += 5; - if (digit(*p)) { - buf[9] = *p++; - if (digit(*p)) buf[10] = *p++; - return buf; - } - } - if (strstr(l, "KOI8-R")) return "KOI8-R"; - if (strstr(l, "KOI8-U")) return "KOI8-U"; - if (strstr(l, "620")) return "TIS-620"; - if (strstr(l, "2312")) return "GB2312"; - if (strstr(l, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */ - if (strstr(l, "Big5") || strstr(l, "BIG5")) return "Big5"; - if (strstr(l, "GBK")) return "GBK"; /* no MIME charset */ - if (strstr(l, "18030")) return "GB18030"; /* no MIME charset */ - if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return "Shift_JIS"; - /* check for conclusive modifier */ - if (strstr(l, "euro")) return "ISO-8859-15"; - /* check for language (and perhaps country) codes */ - if (strstr(l, "zh_TW")) return "Big5"; - if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */ - if (strstr(l, "zh")) return "GB2312"; - if (strstr(l, "ja")) return "EUC-JP"; - if (strstr(l, "ko")) return "EUC-KR"; - if (strstr(l, "ru")) return "KOI8-R"; - if (strstr(l, "uk")) return "KOI8-U"; - if (strstr(l, "pl") || strstr(l, "hr") || - strstr(l, "hu") || strstr(l, "cs") || - strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2"; - if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3"; - if (strstr(l, "el")) return "ISO-8859-7"; - if (strstr(l, "he")) return "ISO-8859-8"; - if (strstr(l, "tr")) return "ISO-8859-9"; - if (strstr(l, "th")) return "TIS-620"; /* or ISO-8859-11 */ - if (strstr(l, "lt")) return "ISO-8859-13"; - if (strstr(l, "cy")) return "ISO-8859-14"; - if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */ - if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8"; - /* Send me further rules if you like, but don't forget that we are - * *only* interested in locale naming conventions on platforms - * that do not already provide an nl_langinfo(CODESET) implementation. */ - return "ISO-8859-1"; /* should perhaps be "UTF-8" instead */ - } - return C_CODESET; -} - -/* For a demo, compile with "gcc -W -Wall -o langinfo -D TEST langinfo.c" */ - -#ifdef TEST -#include -int main() -{ - printf("%s\n", nl_langinfo(CODESET)); - return 0; -} -#endif diff --git a/m4/codeset.m4 b/m4/codeset.m4 deleted file mode 100644 index 59535eb..0000000 --- a/m4/codeset.m4 +++ /dev/null @@ -1,23 +0,0 @@ -# codeset.m4 serial AM1 (gettext-0.10.40) -dnl Copyright (C) 2000-2002 Free Software Foundation, Inc. -dnl This file is free software, distributed under the terms of the GNU -dnl General Public License. As a special exception to the GNU General -dnl Public License, this file may be distributed as part of a program -dnl that contains a configuration script generated by Autoconf, under -dnl the same distribution terms as the rest of that program. - -dnl From Bruno Haible. - -AC_DEFUN([AM_LANGINFO_CODESET], -[ - AC_CACHE_CHECK([for nl_langinfo and CODESET], am_cv_langinfo_codeset, - [AC_TRY_LINK([#include ], - [char* cs = nl_langinfo(CODESET);], - am_cv_langinfo_codeset=yes, - am_cv_langinfo_codeset=no) - ]) - if test $am_cv_langinfo_codeset = yes; then - AC_DEFINE(HAVE_LANGINFO_CODESET, 1, - [Define if you have and nl_langinfo(CODESET).]) - fi -]) diff --git a/m4/iconv.m4 b/m4/iconv.m4 deleted file mode 100644 index 1723734..0000000 --- a/m4/iconv.m4 +++ /dev/null @@ -1,69 +0,0 @@ -#serial AM2 - -dnl From Bruno Haible. - -AC_DEFUN([AM_ICONV], -[ - dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and - dnl those with the standalone portable GNU libiconv installed). - - AC_ARG_WITH([libiconv-prefix], -[ --with-libiconv-prefix=DIR search for libiconv in DIR/include and DIR/lib], [ - for dir in `echo "$withval" | tr : ' '`; do - if test -d $dir/include; then CPPFLAGS="$CPPFLAGS -I$dir/include"; fi - if test -d $dir/lib; then LDFLAGS="$LDFLAGS -L$dir/lib"; fi - done - ]) - - AC_CACHE_CHECK(for iconv, am_cv_func_iconv, [ - am_cv_func_iconv="no, consider installing GNU libiconv" - am_cv_lib_iconv=no - AC_TRY_LINK([#include -#include ], - [iconv_t cd = iconv_open("",""); - iconv(cd,NULL,NULL,NULL,NULL); - iconv_close(cd);], - am_cv_func_iconv=yes) - if test "$am_cv_func_iconv" != yes; then - am_save_LIBS="$LIBS" - LIBS="$LIBS -liconv" - AC_TRY_LINK([#include -#include ], - [iconv_t cd = iconv_open("",""); - iconv(cd,NULL,NULL,NULL,NULL); - iconv_close(cd);], - am_cv_lib_iconv=yes - am_cv_func_iconv=yes) - LIBS="$am_save_LIBS" - fi - ]) - if test "$am_cv_func_iconv" = yes; then - AC_DEFINE(HAVE_ICONV, 1, [Define if you have the iconv() function.]) - AC_MSG_CHECKING([for iconv declaration]) - AC_CACHE_VAL(am_cv_proto_iconv, [ - AC_TRY_COMPILE([ -#include -#include -extern -#ifdef __cplusplus -"C" -#endif -#if defined(__STDC__) || defined(__cplusplus) -size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft); -#else -size_t iconv(); -#endif -], [], am_cv_proto_iconv_arg1="", am_cv_proto_iconv_arg1="const") - am_cv_proto_iconv="extern size_t iconv (iconv_t cd, $am_cv_proto_iconv_arg1 char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);"]) - am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'` - AC_MSG_RESULT([$]{ac_t:- - }[$]am_cv_proto_iconv) - AC_DEFINE_UNQUOTED(ICONV_CONST, $am_cv_proto_iconv_arg1, - [Define as const if the declaration of iconv() needs const.]) - fi - LIBICONV= - if test "$am_cv_lib_iconv" = yes; then - LIBICONV="-liconv" - fi - AC_SUBST(LIBICONV) -]) diff --git a/support.c b/support.c index 907c508..2b5d46c 100644 --- a/support.c +++ b/support.c @@ -1,6 +1,4 @@ #include -#include -#include #include #include #include @@ -12,27 +10,11 @@ #include #endif -#ifdef HAVE_ICONV -#include -#endif - -#ifdef HAVE_LANGINFO_CODESET -#include -#endif - #define BUFSIZE 1024 - -static char *charset = NULL; - -#ifdef HAVE_LOCALE_H -static char *locale = NULL; -#endif - -#ifdef HAVE_ICONV -iconv_t iconv_from_uft8 = (iconv_t)(-1); -iconv_t iconv_to_uft8 = (iconv_t)(-1); -#endif +static const char *charset = NULL; +static const char *locale = NULL; +static gboolean noconvert = TRUE; char * trim(char *str) @@ -104,11 +86,11 @@ concat_path(char *p1, char *p2) size += strlen(p2); size++; - path = calloc(size, sizeof(char)); - strncpy(path, p1, size); + path = g_malloc0(size*sizeof(char)); + g_strlcpy(path, p1, size); if( append_slash ) - strncat(path, "/", size); - strncat(path, p2, size); + g_strlcat(path, "/", size); + g_strlcat(path, p2, size); return path; } @@ -151,115 +133,41 @@ charset_init(void) /* get current locale */ if( (locale=setlocale(LC_CTYPE,"")) == NULL ) { - fprintf(stderr,"setlocale() - failed!\n"); + g_printerr("setlocale() - failed!\n"); return -1; } #endif /* get charset */ - if( (charset=nl_langinfo(CODESET)) == NULL ) - { - fprintf(stderr, - "nl_langinfo() failed using default:" DEFAULT_CHARSET "\n"); - charset = DEFAULT_CHARSET; - } + noconvert = g_get_charset(&charset); + #ifdef DEBUG - fprintf(stderr, "charset: %s\n", charset); + g_printerr("charset: %s\n", charset); #endif -#ifdef HAVE_ICONV - /* 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; - } - iconv_to_uft8 = iconv_open("UTF-8", charset); - if( iconv_to_uft8 == (iconv_t)(-1) ) - { - perror("iconv_open"); - return -1; - } -#endif - return 0; } int charset_close(void) { -#ifdef HAVE_ICONV - 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; } -#ifdef HAVE_ICONV -static char * -charconv(iconv_t iv, char *str) -{ - size_t inleft; - size_t retlen; - char *ret; - - if( str==NULL ) - return NULL; - - if( iv == (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(iv, &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; -} -#endif - char * utf8_to_locale(char *str) { -#ifdef HAVE_ICONV - return charconv(iconv_from_uft8, str); -#else - return strdup(str); -#endif + if( noconvert ) + return g_strup(str); + return g_locale_from_utf8(str, -1, NULL, NULL, NULL); } char * locale_to_utf8(char *str) { -#ifdef HAVE_ICONV - return charconv(iconv_to_uft8, str); -#else - return strdup(str); -#endif + if( noconvert ) + return g_strup(str); + return g_locale_to_utf8(str, -1, NULL, NULL, NULL); } diff --git a/support.h b/support.h index b82751d..adba8a0 100644 --- a/support.h +++ b/support.h @@ -3,12 +3,6 @@ #include #endif -#ifndef HAVE_LANGINFO_CODESET -#define CODESET ((nl_item) 1) -typedef int nl_item; -char *nl_langinfo(nl_item); -#endif - char *concat_path(char *p1, char *p2); #ifndef HAVE_BASENAME