[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-gnulib] iconvme again
From: |
Simon Josefsson |
Subject: |
[bug-gnulib] iconvme again |
Date: |
Tue, 22 Feb 2005 08:34:40 +0100 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/22.0.50 (gnu/linux) |
Hello. iconvme.* has been installed into libc. This patch propose to
add it to gnulib via libc. What do you think?
2005-02-22 Simon Josefsson <address@hidden>
* modules/iconvme: New file.
* MODULES.html.sh: Add iconvme.
2005-02-22 Simon Josefsson <address@hidden>
* iconvme.m4: New file.
2005-02-22 Simon Josefsson <address@hidden>
* iconvme.h, iconvme.c: New files, from libc.
2005-02-22 Simon Josefsson <address@hidden>
* srclist.txt: Sync iconvme.h, iconvme.c from libc.
Index: modules/iconvme
===================================================================
RCS file: modules/iconvme
diff -N modules/iconvme
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/iconvme 22 Feb 2005 07:33:54 -0000
@@ -0,0 +1,27 @@
+Description:
+Character set conversion of strings made easy, uses iconv.
+
+Files:
+lib/iconvme.h
+lib/iconvme.c
+m4/iconvme.m4
+
+Depends-on:
+iconv
+strdup
+
+configure.ac:
+gl_ICONVME
+
+Makefile.am:
+lib_SOURCES += iconvme.h iconvme.c
+lib_LIBADD += $(LTLIBICONV)
+
+Include:
+"iconvme.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: m4/iconvme.m4
===================================================================
RCS file: m4/iconvme.m4
diff -N m4/iconvme.m4
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ m4/iconvme.m4 22 Feb 2005 07:33:54 -0000
@@ -0,0 +1,17 @@
+# iconvme.m4 serial 1
+dnl Copyright (C) 2004 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.
+
+AC_DEFUN([gl_ICONVME],
+[
+ gl_PREREQ_ICONVME
+])
+
+# Prerequisites of lib/iconvme.c.
+AC_DEFUN([gl_PREREQ_ICONVME], [
+ AC_REQUIRE([AM_ICONV])
+])
Index: lib/iconvme.h
===================================================================
RCS file: lib/iconvme.h
diff -N lib/iconvme.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/iconvme.h 22 Feb 2005 07:33:54 -0000
@@ -0,0 +1,159 @@
+/* Recode strings between character sets, using iconv.
+ Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get prototype. */
+#include "iconvme.h"
+
+/* Get malloc. */
+#include <stdlib.h>
+
+/* Get strcmp. */
+#include <string.h>
+
+/* Get errno. */
+#include <errno.h>
+
+#ifdef _LIBC
+# define HAVE_ICONV 1
+#else
+/* Get strdup. */
+# include "strdup.h"
+#endif
+
+#if HAVE_ICONV
+/* Get iconv etc. */
+# include <iconv.h>
+/* Get MB_LEN_MAX. */
+# include <limits.h>
+#endif
+
+/* Convert a zero-terminated string STR from the FROM_CODSET code set
+ to the TO_CODESET code set. The returned string is allocated using
+ malloc, and must be dellocated by the caller using free. On
+ failure, NULL is returned and errno holds the error reason. Note
+ that if TO_CODESET uses \0 for anything but to terminate the
+ string, the caller of this function may have difficulties finding
+ out the length of the output string. */
+char *
+iconv_string (const char *str, const char *from_codeset,
+ const char *to_codeset)
+{
+ char *dest = NULL;
+#if HAVE_ICONV
+ iconv_t cd;
+ char *outp;
+ char *p = (char *) str;
+ size_t inbytes_remaining = strlen (p);
+ /* Guess the maximum length the output string can have. */
+ size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
+ size_t outbytes_remaining = outbuf_size - 1; /* -1 for NUL */
+ size_t err;
+ int have_error = 0;
+#endif
+
+ if (strcmp (to_codeset, from_codeset) == 0)
+ return strdup (str);
+
+#if HAVE_ICONV
+ cd = iconv_open (to_codeset, from_codeset);
+ if (cd == (iconv_t) -1)
+ return NULL;
+
+ outp = dest = (char *) malloc (outbuf_size);
+ if (dest == NULL)
+ goto out;
+
+again:
+ err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
+
+ if (err == (size_t) - 1)
+ {
+ switch (errno)
+ {
+ case EINVAL:
+ /* Incomplete text, do not report an error */
+ break;
+
+ case E2BIG:
+ {
+ size_t used = outp - dest;
+ size_t newsize = outbuf_size * 2;
+ char *newdest;
+
+ if (newsize <= outbuf_size)
+ {
+ errno = ENOMEM;
+ have_error = 1;
+ goto out;
+ }
+ newdest = (char *) realloc (dest, newsize);
+ if (newdest == NULL)
+ {
+ have_error = 1;
+ goto out;
+ }
+ dest = newdest;
+ outbuf_size = newsize;
+
+ outp = dest + used;
+ outbytes_remaining = outbuf_size - used - 1; /* -1 for NUL */
+
+ goto again;
+ }
+ break;
+
+ case EILSEQ:
+ have_error = 1;
+ break;
+
+ default:
+ have_error = 1;
+ break;
+ }
+ }
+
+ *outp = '\0';
+
+out:
+ {
+ int save_errno = errno;
+
+ if (iconv_close (cd) < 0 && !have_error)
+ {
+ /* If we didn't have a real error before, make sure we restore
+ the iconv_close error below. */
+ save_errno = errno;
+ have_error = 1;
+ }
+
+ if (have_error && dest)
+ {
+ free (dest);
+ dest = NULL;
+ errno = save_errno;
+ }
+ }
+#else
+ errno = ENOSYS;
+#endif
+
+ return dest;
+}
Index: lib/iconvme.c
===================================================================
RCS file: lib/iconvme.c
diff -N lib/iconvme.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/iconvme.c 22 Feb 2005 07:33:54 -0000
@@ -0,0 +1,159 @@
+/* Recode strings between character sets, using iconv.
+ Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1, or (at
+ your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get prototype. */
+#include "iconvme.h"
+
+/* Get malloc. */
+#include <stdlib.h>
+
+/* Get strcmp. */
+#include <string.h>
+
+/* Get errno. */
+#include <errno.h>
+
+#ifdef _LIBC
+# define HAVE_ICONV 1
+#else
+/* Get strdup. */
+# include "strdup.h"
+#endif
+
+#if HAVE_ICONV
+/* Get iconv etc. */
+# include <iconv.h>
+/* Get MB_LEN_MAX. */
+# include <limits.h>
+#endif
+
+/* Convert a zero-terminated string STR from the FROM_CODSET code set
+ to the TO_CODESET code set. The returned string is allocated using
+ malloc, and must be dellocated by the caller using free. On
+ failure, NULL is returned and errno holds the error reason. Note
+ that if TO_CODESET uses \0 for anything but to terminate the
+ string, the caller of this function may have difficulties finding
+ out the length of the output string. */
+char *
+iconv_string (const char *str, const char *from_codeset,
+ const char *to_codeset)
+{
+ char *dest = NULL;
+#if HAVE_ICONV
+ iconv_t cd;
+ char *outp;
+ char *p = (char *) str;
+ size_t inbytes_remaining = strlen (p);
+ /* Guess the maximum length the output string can have. */
+ size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
+ size_t outbytes_remaining = outbuf_size - 1; /* -1 for NUL */
+ size_t err;
+ int have_error = 0;
+#endif
+
+ if (strcmp (to_codeset, from_codeset) == 0)
+ return strdup (str);
+
+#if HAVE_ICONV
+ cd = iconv_open (to_codeset, from_codeset);
+ if (cd == (iconv_t) -1)
+ return NULL;
+
+ outp = dest = (char *) malloc (outbuf_size);
+ if (dest == NULL)
+ goto out;
+
+again:
+ err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
+
+ if (err == (size_t) - 1)
+ {
+ switch (errno)
+ {
+ case EINVAL:
+ /* Incomplete text, do not report an error */
+ break;
+
+ case E2BIG:
+ {
+ size_t used = outp - dest;
+ size_t newsize = outbuf_size * 2;
+ char *newdest;
+
+ if (newsize <= outbuf_size)
+ {
+ errno = ENOMEM;
+ have_error = 1;
+ goto out;
+ }
+ newdest = (char *) realloc (dest, newsize);
+ if (newdest == NULL)
+ {
+ have_error = 1;
+ goto out;
+ }
+ dest = newdest;
+ outbuf_size = newsize;
+
+ outp = dest + used;
+ outbytes_remaining = outbuf_size - used - 1; /* -1 for NUL */
+
+ goto again;
+ }
+ break;
+
+ case EILSEQ:
+ have_error = 1;
+ break;
+
+ default:
+ have_error = 1;
+ break;
+ }
+ }
+
+ *outp = '\0';
+
+out:
+ {
+ int save_errno = errno;
+
+ if (iconv_close (cd) < 0 && !have_error)
+ {
+ /* If we didn't have a real error before, make sure we restore
+ the iconv_close error below. */
+ save_errno = errno;
+ have_error = 1;
+ }
+
+ if (have_error && dest)
+ {
+ free (dest);
+ dest = NULL;
+ errno = save_errno;
+ }
+ }
+#else
+ errno = ENOSYS;
+#endif
+
+ return dest;
+}
Index: tests/test-iconvme.c
===================================================================
RCS file: tests/test-iconvme.c
diff -N tests/test-iconvme.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/test-iconvme.c 22 Feb 2005 07:33:54 -0000
@@ -0,0 +1,81 @@
+/* Recode strings between character sets, using iconv.
+ Copyright (C) 2004 Free Software Foundation, Inc.
+ Written by Simon Josefsson.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "iconvme.h"
+
+int main (int ac, char *av[])
+{
+ char *in = NULL, *out = NULL;
+ char *to = NULL, *from = NULL;
+
+ if (ac > 1)
+ from = av[1];
+
+ if (ac > 2)
+ to = av[2];
+
+ if (ac > 3)
+ in = av[3];
+
+ if (!in)
+ {
+ size_t len = 0;
+ printf ("Enter string to convert:\n\t> ");
+ if (getline (&in, &len, stdin) < 0)
+ perror ("getline");
+ if (in[strlen (in) - 1] == '\n')
+ in[strlen (in) - 1] = '\0';
+ }
+
+ if (!to)
+ {
+ size_t len = 0;
+ printf ("Enter destination code set:\n\t> ");
+ if (getline (&to, &len, stdin) < 0)
+ perror ("getline");
+ if (to[strlen (to) - 1] == '\n')
+ to[strlen (to) - 1] = '\0';
+ }
+
+ if (!from)
+ {
+ size_t len = 0;
+ printf ("Enter source code set:\n\t> ");
+ if (getline (&from, &len, stdin) < 0)
+ perror ("getline");
+ if (from[strlen (from) - 1] == '\n')
+ from[strlen (from) - 1] = '\0';
+ }
+
+ printf (" Input string: `%s'\n"
+ "From code set: `%s'\n"
+ " To code set: `%s'\n",
+ in, from, to);
+
+ out = iconv_string (in, from, to);
+
+ if (out == NULL)
+ perror ("iconv");
+ else
+ printf ("\nOutput: `%s'\n", out);
+
+ return EXIT_SUCCESS;
+}
Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.75
diff -u -p -r1.75 MODULES.html.sh
--- MODULES.html.sh 28 Jan 2005 12:08:30 -0000 1.75
+++ MODULES.html.sh 22 Feb 2005 07:33:55 -0000
@@ -1905,6 +1905,7 @@ func_all_modules ()
func_begin_table
func_module gettext
func_module iconv
+ func_module iconvme
func_module localcharset
func_module hard-locale
func_module mbswidth
Index: config/srclist.txt
===================================================================
RCS file: /cvsroot/gnulib/gnulib/config/srclist.txt,v
retrieving revision 1.53
diff -u -p -r1.53 srclist.txt
--- config/srclist.txt 25 Jan 2005 14:33:30 -0000 1.53
+++ config/srclist.txt 22 Feb 2005 07:33:55 -0000
@@ -90,6 +90,8 @@ $LIBCSRC/argp/argp-pv.c lib gpl
#$LIBCSRC/argp/argp-pvh.c lib gpl
$LIBCSRC/argp/argp-xinl.c lib gpl
#$LIBCSRC/argp/argp.h lib gpl
+$LIBCSRC/libidn/iconvme.h lib gpl
+$LIBCSRC/libidn/iconvme.c lib gpl
$LIBCSRC/stdlib/getsubopt.c lib gpl
#$LIBCSRC/posix/getopt.c lib gpl
#$LIBCSRC/posix/getopt.h lib gpl (getopt_.h in gnulib)
- [bug-gnulib] iconvme again,
Simon Josefsson <=
- Re: [bug-gnulib] iconvme again, Paul Eggert, 2005/02/22
- [bug-gnulib] Re: iconvme again, Simon Josefsson, 2005/02/22
- [bug-gnulib] Re: iconvme again, Paul Eggert, 2005/02/22
- [bug-gnulib] Re: iconvme again, Simon Josefsson, 2005/02/23
- [bug-gnulib] Re: iconvme again, Paul Eggert, 2005/02/23
- [bug-gnulib] Re: iconvme again, Simon Josefsson, 2005/02/23
- [bug-gnulib] Re: iconvme again, Paul Eggert, 2005/02/23
- [bug-gnulib] Re: iconvme again, Simon Josefsson, 2005/02/24
- Re: [bug-gnulib] Re: iconvme again, Stepan Kasal, 2005/02/24
- Re: [bug-gnulib] Re: iconvme again, Paul Eggert, 2005/02/24