1016718141934dda717c31e44b120f5acc1151f9
[pspp] / m4 / iconv_open.m4
1 # iconv_open.m4 serial 10
2 dnl Copyright (C) 2007-2010 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_ICONV_OPEN],
8 [
9   AC_REQUIRE([AM_ICONV])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_REQUIRE([gl_ICONV_H_DEFAULTS])
12   if test "$am_cv_func_iconv" = yes; then
13     dnl Provide the <iconv.h> override, for the sake of the C++ aliases.
14     gl_REPLACE_ICONV_H
15     dnl Test whether iconv_open accepts standardized encoding names.
16     dnl We know that GNU libiconv and GNU libc do.
17     AC_EGREP_CPP([gnu_iconv], [
18       #include <iconv.h>
19       #if defined _LIBICONV_VERSION || (defined __GLIBC__ && !defined __UCLIBC__)
20        gnu_iconv
21       #endif
22       ], [gl_func_iconv_gnu=yes], [gl_func_iconv_gnu=no])
23     if test $gl_func_iconv_gnu = no; then
24       iconv_flavor=
25       case "$host_os" in
26         aix*)     iconv_flavor=ICONV_FLAVOR_AIX ;;
27         irix*)    iconv_flavor=ICONV_FLAVOR_IRIX ;;
28         hpux*)    iconv_flavor=ICONV_FLAVOR_HPUX ;;
29         osf*)     iconv_flavor=ICONV_FLAVOR_OSF ;;
30         solaris*) iconv_flavor=ICONV_FLAVOR_SOLARIS ;;
31       esac
32       if test -n "$iconv_flavor"; then
33         AC_DEFINE_UNQUOTED([ICONV_FLAVOR], [$iconv_flavor],
34           [Define to a symbolic name denoting the flavor of iconv_open()
35            implementation.])
36         gl_REPLACE_ICONV_OPEN
37       fi
38     fi
39   fi
40 ])
41
42 AC_DEFUN([gl_REPLACE_ICONV_OPEN],
43 [
44   gl_REPLACE_ICONV_H
45   REPLACE_ICONV_OPEN=1
46   AC_LIBOBJ([iconv_open])
47 ])
48
49 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF],
50 [
51   AC_REQUIRE([gl_FUNC_ICONV_OPEN])
52   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
53   AC_REQUIRE([gl_ICONV_H_DEFAULTS])
54   if test "$am_cv_func_iconv" = yes; then
55     AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
56       [gl_cv_func_iconv_supports_utf],
57       [
58         save_LIBS="$LIBS"
59         LIBS="$LIBS $LIBICONV"
60         AC_RUN_IFELSE(
61           [AC_LANG_SOURCE([[
62 #include <iconv.h>
63 #include <errno.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #define ASSERT(expr) if (!(expr)) return 1;
68 int main ()
69 {
70   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
71   {
72     static const char input[] =
73       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
74     static const char expected[] =
75       "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
76     iconv_t cd;
77     char buf[100];
78     const char *inptr;
79     size_t inbytesleft;
80     char *outptr;
81     size_t outbytesleft;
82     size_t res;
83     cd = iconv_open ("UTF-16BE", "UTF-8");
84     ASSERT (cd != (iconv_t)(-1));
85     inptr = input;
86     inbytesleft = sizeof (input) - 1;
87     outptr = buf;
88     outbytesleft = sizeof (buf);
89     res = iconv (cd,
90                  (ICONV_CONST char **) &inptr, &inbytesleft,
91                  &outptr, &outbytesleft);
92     ASSERT (res == 0 && inbytesleft == 0);
93     ASSERT (outptr == buf + (sizeof (expected) - 1));
94     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
95     ASSERT (iconv_close (cd) == 0);
96   }
97   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
98   {
99     static const char input[] =
100       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
101     static const char expected[] =
102       "J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
103     iconv_t cd;
104     char buf[100];
105     const char *inptr;
106     size_t inbytesleft;
107     char *outptr;
108     size_t outbytesleft;
109     size_t res;
110     cd = iconv_open ("UTF-16LE", "UTF-8");
111     ASSERT (cd != (iconv_t)(-1));
112     inptr = input;
113     inbytesleft = sizeof (input) - 1;
114     outptr = buf;
115     outbytesleft = sizeof (buf);
116     res = iconv (cd,
117                  (ICONV_CONST char **) &inptr, &inbytesleft,
118                  &outptr, &outbytesleft);
119     ASSERT (res == 0 && inbytesleft == 0);
120     ASSERT (outptr == buf + (sizeof (expected) - 1));
121     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
122     ASSERT (iconv_close (cd) == 0);
123   }
124   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
125   {
126     static const char input[] =
127       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
128     static const char expected[] =
129       "\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
130     iconv_t cd;
131     char buf[100];
132     const char *inptr;
133     size_t inbytesleft;
134     char *outptr;
135     size_t outbytesleft;
136     size_t res;
137     cd = iconv_open ("UTF-32BE", "UTF-8");
138     ASSERT (cd != (iconv_t)(-1));
139     inptr = input;
140     inbytesleft = sizeof (input) - 1;
141     outptr = buf;
142     outbytesleft = sizeof (buf);
143     res = iconv (cd,
144                  (ICONV_CONST char **) &inptr, &inbytesleft,
145                  &outptr, &outbytesleft);
146     ASSERT (res == 0 && inbytesleft == 0);
147     ASSERT (outptr == buf + (sizeof (expected) - 1));
148     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
149     ASSERT (iconv_close (cd) == 0);
150   }
151   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
152   {
153     static const char input[] =
154       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
155     static const char expected[] =
156       "J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\000\345\145\000\000\054\147\000\000\236\212\000\000)\000\000\000 \000\000\000[\000\000\000\015\325\001\000\036\325\001\000\055\325\001\000]\000\000\000";
157     iconv_t cd;
158     char buf[100];
159     const char *inptr;
160     size_t inbytesleft;
161     char *outptr;
162     size_t outbytesleft;
163     size_t res;
164     cd = iconv_open ("UTF-32LE", "UTF-8");
165     ASSERT (cd != (iconv_t)(-1));
166     inptr = input;
167     inbytesleft = sizeof (input) - 1;
168     outptr = buf;
169     outbytesleft = sizeof (buf);
170     res = iconv (cd,
171                  (ICONV_CONST char **) &inptr, &inbytesleft,
172                  &outptr, &outbytesleft);
173     ASSERT (res == 0 && inbytesleft == 0);
174     ASSERT (outptr == buf + (sizeof (expected) - 1));
175     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
176     ASSERT (iconv_close (cd) == 0);
177   }
178   /* Test conversion from UTF-16BE to UTF-8 with no errors.
179      This test fails on NetBSD 3.0.  */
180   {
181     static const char input[] =
182       "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
183     static const char expected[] =
184       "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
185     iconv_t cd;
186     char buf[100];
187     const char *inptr;
188     size_t inbytesleft;
189     char *outptr;
190     size_t outbytesleft;
191     size_t res;
192     cd = iconv_open ("UTF-8", "UTF-16BE");
193     ASSERT (cd != (iconv_t)(-1));
194     inptr = input;
195     inbytesleft = sizeof (input) - 1;
196     outptr = buf;
197     outbytesleft = sizeof (buf);
198     res = iconv (cd,
199                  (ICONV_CONST char **) &inptr, &inbytesleft,
200                  &outptr, &outbytesleft);
201     ASSERT (res == 0 && inbytesleft == 0);
202     ASSERT (outptr == buf + (sizeof (expected) - 1));
203     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
204     ASSERT (iconv_close (cd) == 0);
205   }
206   return 0;
207 }]])],
208           [gl_cv_func_iconv_supports_utf=yes],
209           [gl_cv_func_iconv_supports_utf=no],
210           [
211            dnl We know that GNU libiconv, GNU libc, and Solaris >= 9 do.
212            dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
213            dnl direction.
214            gl_cv_func_iconv_supports_utf=no
215            if test $gl_func_iconv_gnu = yes; then
216              gl_cv_func_iconv_supports_utf=yes
217            else
218 changequote(,)dnl
219              case "$host_os" in
220                solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;
221              esac
222 changequote([,])dnl
223            fi
224           ])
225         LIBS="$save_LIBS"
226       ])
227     if test $gl_cv_func_iconv_supports_utf = no; then
228       REPLACE_ICONV_UTF=1
229       AC_DEFINE([REPLACE_ICONV_UTF], [1],
230         [Define if the iconv() functions are enhanced to handle the UTF-{16,32}{BE,LE} encodings.])
231       REPLACE_ICONV=1
232       gl_REPLACE_ICONV_OPEN
233       AC_LIBOBJ([iconv])
234       AC_LIBOBJ([iconv_close])
235     fi
236   fi
237 ])