From e6deb358509739750ef01fd67e6b99fe8d7bcefc Mon Sep 17 00:00:00 2001 From: englebass Date: Tue, 2 Feb 2010 04:49:38 +0000 Subject: [PATCH] copy ecore_txt_convert to eina_str_convert git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@45790 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- configure.ac | 31 +++++++++++++++++++ src/include/eina_str.h | 2 ++ src/lib/Makefile.am | 7 +++-- src/lib/eina_str.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index b825df5..9fca9f6 100644 --- a/configure.ac +++ b/configure.ac @@ -379,6 +379,36 @@ if test "x$res" = "xno"; then fi AC_SUBST(fnmatch_libs) +# iconv library +iconv_cflags="" +iconv_libs="" +have_iconv="no" +AC_ARG_WITH([iconv-link], + AC_HELP_STRING([--with-iconv-link=ICONV_LINK], [explicitly specify an iconv link option]), + [ + iconv_libs=$withval + have_iconv="yes" + ]) + +AC_MSG_CHECKING(for explicit iconv link options) +if test "x${iconv_libs}" = "x" ; then + AC_MSG_RESULT([no explicit iconv link option]) +else + AC_MSG_RESULT([$iconv_libs]) +fi + +AM_ICONV + +if test "x${have_iconv}" = "xno" && test "x${am_cv_func_iconv}" = "xyes" ; then + iconv_cflags=${LIBICONV} + iconv_libs=${LTLIBICONV} + have_iconv="yes" +fi + +AC_SUBST(iconv_cflags) +AC_SUBST(iconv_libs) + + ### Modules if test "x${have_default_mempool}" = "xyes" ; then @@ -477,6 +507,7 @@ echo " Report string usage..: ${have_stringshare_usage}" echo " Default mempool......: ${have_default_mempool}" echo " Thread Support.......: ${have_pthread} (spinlock: ${have_pthread_spinlock})" echo " Amalgamation.........: ${do_amalgamation}" +echo " Iconv support........: ${have_iconv}" echo echo " Documentation........: ${build_doc}" echo " Tests................: ${enable_tests}" diff --git a/src/include/eina_str.h b/src/include/eina_str.h index 41a26b2..d2f2a4b 100644 --- a/src/include/eina_str.h +++ b/src/include/eina_str.h @@ -25,6 +25,8 @@ EAPI char **eina_str_split(const char *string, const char *delimiter, EAPI size_t eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len); +EAPI char *eina_str_convert(const char *enc_from, const char *enc_to, const char *text); + /** * @brief Join two strings of known length. diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 787efd2..313e2b7 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -7,7 +7,8 @@ AM_CPPFLAGS = \ -DPACKAGE_LIB_DIR=\"$(libdir)\" \ -DPACKAGE_DATA_DIR=\"$(datadir)/$(PACKAGE)\" \ @EINA_CPPFLAGS@ \ -@EFL_EINA_BUILD@ +@EFL_EINA_BUILD@ \ +@iconv_cflags@ base_sources = \ eina_error.c \ @@ -128,8 +129,8 @@ libeina_la_SOURCES = $(base_sources) endif libeina_la_LIBADD = @EINA_LIBS@ @dlopen_libs@ -libeina_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@ @EFL_PTHREAD_LIBS@ -libeina_la_CFLAGS = @EINA_CFLAGS@ @EFL_PTHREAD_CFLAGS@ +libeina_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@ @EFL_PTHREAD_LIBS@ @iconv_libs@ +libeina_la_CFLAGS = @EINA_CFLAGS@ @EFL_PTHREAD_CFLAGS@ @iconv_cflags@ clean-local: rm -rf *.gcno eina_amalgamation.c diff --git a/src/lib/eina_str.c b/src/lib/eina_str.c index bc1077d..020357a 100644 --- a/src/lib/eina_str.c +++ b/src/lib/eina_str.c @@ -32,6 +32,11 @@ #include #include +#ifdef HAVE_ICONV +# include +# include +#endif + #include "eina_private.h" /*============================================================================* @@ -339,3 +344,79 @@ eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, dst[off + b_len] = '\0'; return ret; } + +/** + * @brief Use iconv to convert a text string from one encoding to another + * + * @param enc_from encoding to convert from + * @param enc_to encoding to convert to + * @param text text to convert + * + */ +EAPI char * +eina_str_convert(const char *enc_from, const char *enc_to, const char *text) +{ +#ifdef HAVE_ICONV + iconv_t ic; + char *new_txt, *inp, *outp; + size_t inb, outb, outlen, tob, outalloc; + + if (!text) return NULL; + ic = iconv_open(enc_to, enc_from); + if (ic == (iconv_t)(-1)) return NULL; + new_txt = malloc(64); + inb = strlen(text); + outb = 64; + inp = (char*)text; + outp = new_txt; + outalloc = 64; + outlen = 0; + + for (;;) + { + size_t count; + + tob = outb; + count = iconv(ic, &inp, &inb, &outp, &outb); + outlen += tob - outb; + if (count == (size_t)(-1)) + { + if (errno == E2BIG) + { + new_txt = realloc(new_txt, outalloc + 64); + outp = new_txt + outlen; + outalloc += 64; + outb += 64; + } + else if (errno == EILSEQ) + { + if (new_txt) free(new_txt); + new_txt = NULL; + break; + } + else if (errno == EINVAL) + { + if (new_txt) free(new_txt); + new_txt = NULL; + break; + } + else + { + if (new_txt) free(new_txt); + new_txt = NULL; + break; + } + } + if (inb == 0) + { + if (outalloc == outlen) new_txt = realloc(new_txt, outalloc + 1); + new_txt[outlen] = 0; + break; + } + } + iconv_close(ic); + return new_txt; +#else + return NULL; +#endif +} -- 2.7.4