apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mo DeJong <mdej...@cygnus.com>
Subject Re: [PATCH] iconv tweak
Date Mon, 05 Feb 2001 07:20:00 GMT
On Sun, 4 Feb 2001, Greg Stein wrote:

> There was some discussion about iconv prototyes here recently, but I kind of
> missed it.
> 
> Do we always need to live with a warning about a type mismatch, or is the
> cast below appropriate?
> 
> IOW, do some headers have "const" on them, and others not?

Yup, you cannot just use a cast, you need to use a HAVE...
test because headers differ and newer versions of
gcc will generate an error on a plain cast.
Here is the bit of code from the Jikes java compiler
that deals with this issue.

                    size_t n = iconv(_converter,
#ifdef HAVE_ERROR_CALL_ICONV_CONST
                                     (char **)
#endif
                                     &source_ptr, &srcl,
                                     (char **)&chp, &chl
                    );


And here is the autoconf macro that checks for
the error that will require a cast.


dnl @synopsis AC_CHECK_ICONV
dnl
dnl Check to see if the iconv functions and libs are present
dnl @version $Id: ac_check_iconv.m4,v 1.1 2000/10/10 07:06:50 mdejong Exp $
dnl @author Mo DeJong <mdejong@redhat.com>
dnl
AC_DEFUN(AC_CHECK_ICONV,
[
dnl	Check for the iconv header file
AC_CHECK_HEADERS(iconv.h)

dnl	Check if iconv library is present
AC_CHECK_LIB(iconv, iconv)

iconv_includes="
#ifdef HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
#  include <memory.h>
# endif
# include <string.h>
#else
# ifdef HAVE_STRINGS_H
#  include <strings.h>
# endif
#endif
#ifdef HAVE_WCHAR_H
# include <wchar.h>
#endif
#ifdef HAVE_ICONV_H
# include <iconv.h>
#endif
"

dnl	Make sure we can compile a simple call to iconv
AC_CACHE_CHECK(simple call to iconv,
  ac_cv_call_iconv,
[
AC_TRY_COMPILE($iconv_includes, [iconv(0, NULL, NULL, NULL, NULL)],
  ac_cv_call_iconv=yes,
  ac_cv_call_iconv=no)
])

dnl No additional testing is possible if regular call does not work.
dnl In this case, we just assume iconv can not be used by the application

if test "$ac_cv_call_iconv" = "yes" ; then

dnl	Check for declaration of iconv without const qualifier in 2nd argument
AC_CACHE_CHECK(for error calling iconv with const 2nd argument,
  ac_cv_error_call_iconv_const,
[AC_TRY_COMPILE($iconv_includes, [
const char ** s = NULL;
iconv(0, s, NULL, NULL, NULL);
],
 ac_cv_error_call_iconv_const=no, ac_cv_error_call_iconv_const=yes)
])

if test "$ac_cv_error_call_iconv_const" = "yes"; then

  dnl define symbol so we know when to cast the argument to iconv
  dnl from const char ** to char *

  AC_DEFINE(HAVE_ERROR_CALL_ICONV_CONST, ,
    [Defined when the compiler would generate an error on a call to iconv 
with a non const 2nd argument. This is a known problem on IRIX systems.])

  dnl Double check that our suggested fix works.

  AC_CACHE_CHECK(fix for calling iconv with non const argument,
    ac_cv_call_iconv_non_const,
  [AC_TRY_COMPILE($iconv_includes, [
  const wchar_t * s = NULL;
  iconv( 0,
  #ifdef HAVE_ERROR_CALL_ICONV_CONST
           (char **)
  #endif
         s, NULL, NULL, NULL);
  ],
    ac_cv_call_iconv_non_const=yes,
    AC_MSG_ERROR([Could not compile with iconv const cast fix])
  )])

fi

fi

]
)

cheers
Mo DeJong
Red Hat Inc

Mime
View raw message