[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Best practice finding a suitable iconv()?
From: |
Rüdiger Kuhlmann |
Subject: |
Best practice finding a suitable iconv()? |
Date: |
Fri, 9 May 2003 19:04:42 +0200 |
User-agent: |
Mutt/1.5.4i |
Hi,
I'm developing an application that makes use of iconv(), but needs to
guarantee that a few basic encodings are supported. However, the test from
iconv.m4 only tests for existance (and the way it checks shows a fundamental
flaw in checks for function existance, as the function may just be a macro,
or has a mangled symbol like select() for mingw), but I need more than that.
Currently I'm using the following construct:
if test "x$ac_arg_use_iconv" != "xno"; then
AC_CHECK_FUNC([iconv], [ac_arg_use_iconv=system], [ac_arg_use_iconv=builtin])
if test "x$ac_arg_use_iconv" = "xbuiltin"; then
AC_CHECK_LIB([iconv], [iconv], [ac_arg_use_iconv=iconv])
if test "x$ac_arg_use_iconv" = "xbuiltin"; then
AC_CHECK_LIB([iconv], [libiconv], [ac_arg_use_iconv=libiconv])
fi
if test "x$ac_arg_use_iconv" != "xbuiltin"; then
LIBS="-liconv $LIBS"
fi
fi
else
ac_arg_use_iconv=builtin
fi
if test "x$ac_arg_use_iconv" != "xbuiltin"; then
AC_CACHE_CHECK(for iconv declaration, ac_cv_iconv_const,
[AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
#include <iconv.h>]],
[[#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]])],
[ac_cv_iconv_const=no-const],
[ac_cv_iconv_const=const])])
if test $ac_cv_iconv_const = const; then
AC_DEFINE([ICONV_CONST], const, [Define as const if the declaration of
iconv() needs const.])
else
AC_DEFINE([ICONV_CONST], /* no-const */)
fi
AC_CACHE_CHECK(for iconv minimal functionality, ac_cv_iconv_min,
[AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
#include <iconv.h>]],
[[iconv_t res;
res = iconv_open ("UTF-8", "ISO-8859-1");
if (res == (iconv_t)-1 || !res)
{
res = iconv_open ("UTF-8", "ISO8859-1");
if (res == (iconv_t)-1 || !res) exit (2);
res = iconv_open ("ISO8859-1", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (3);
}
else
{
res = iconv_open ("ISO-8859-1", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (4);
}
res = iconv_open ("UTF-8", "ISO-8859-15");
if (res == (iconv_t)-1 || !res)
{
res = iconv_open ("UTF-8", "ISO8859-15");
if (res == (iconv_t)-1 || !res) exit (5);
res = iconv_open ("ISO8859-15", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (6);
}
else
{
res = iconv_open ("ISO-8859-15", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (7);
}
res = iconv_open ("UTF-8", "KOI8-U");
if (res == (iconv_t)-1 || !res) exit (8);
res = iconv_open ("KOI8-U", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (9);
res = iconv_open ("UTF-8", "CP1251");
if (res == (iconv_t)-1 || !res) exit (10);
res = iconv_open ("CP1251", "UTF-8");
if (res == (iconv_t)-1 || !res) exit (11);
exit (0);]])],
[ac_cv_iconv_min=ok],
[ac_cv_iconv_min=broken],
[ac_cv_iconv_min=cross])
])
if test $ac_cv_iconv_min != ok; then
ac_arg_use_iconv=buildin
AC_DEFINE([ICONV_LATIN1_NAME], ["ISO-8859-1"], [the name of the latin1
encoding])
AC_DEFINE([ICONV_LATIN9_NAME], ["ISO-8859-15"], [the name of the latin9
encoding])
else
AC_CACHE_CHECK(for iconv name of latin-1 encoding, ac_cv_iconv_latin1_name,
[AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
#include <iconv.h>]],
[[iconv_t res;
res = iconv_open ("UTF-8", "ISO-8859-1");
if (res == (iconv_t)-1 || !res) exit (1);
exit (0);]])],
[ac_cv_iconv_latin1_name=ISO-8859-1],
[ac_cv_iconv_latin1_name=ISO8859-1],
[ac_cv_iconv_latin1_name=cannothappen])
])
AC_DEFINE_UNQUOTED([ICONV_LATIN1_NAME], ["$ac_cv_iconv_latin1_name"])
AC_CACHE_CHECK(for iconv name of latin-9 encoding, ac_cv_iconv_latin9_name,
[AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
#include <iconv.h>]],
[[iconv_t res;
res = iconv_open ("UTF-8", "ISO-8859-15");
if (res == (iconv_t)-1 || !res) exit (1);
exit (0);]])],
[ac_cv_iconv_latin9_name=ISO-8859-15],
[ac_cv_iconv_latin9_name=ISO8859-15],
[ac_cv_iconv_latin9_name=cannothappen])
])
AC_DEFINE_UNQUOTED([ICONV_LATIN9_NAME], ["$ac_cv_iconv_latin9_name"])
fi
else
AC_DEFINE([ICONV_LATIN1_NAME], ["ISO-8859-1"])
AC_DEFINE([ICONV_LATIN9_NAME], ["ISO-8859-15"])
fi
AC_MSG_CHECKING([whether to enable iconv and which iconv lib to use])
AC_MSG_RESULT([$ac_arg_use_iconv])
Is there any better way to do this? Or any accepted standard procedure?
--
100 DM = 51 € 13 ¢.
100 € = 195 DM 58 pf.
mailto:address@hidden
http://www.ruediger-kuhlmann.de/