Merge branch 'master' into wip-manual-2
[bpt/guile.git] / m4 / iconv_open.m4
1 # iconv_open.m4 serial 7
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__
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 if test -n "$am_cv_proto_iconv_arg1"; then
56 ICONV_CONST="const"
57 else
58 ICONV_CONST=
59 fi
60 AC_SUBST([ICONV_CONST])
61 AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
62 [gl_cv_func_iconv_supports_utf],
63 [
64 save_LIBS="$LIBS"
65 LIBS="$LIBS $LIBICONV"
66 AC_TRY_RUN([
67 #include <iconv.h>
68 #include <errno.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #define ASSERT(expr) if (!(expr)) return 1;
73 int main ()
74 {
75 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
76 {
77 static const char input[] =
78 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
79 static const char expected[] =
80 "\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]";
81 iconv_t cd;
82 char buf[100];
83 const char *inptr;
84 size_t inbytesleft;
85 char *outptr;
86 size_t outbytesleft;
87 size_t res;
88 cd = iconv_open ("UTF-16BE", "UTF-8");
89 ASSERT (cd != (iconv_t)(-1));
90 inptr = input;
91 inbytesleft = sizeof (input) - 1;
92 outptr = buf;
93 outbytesleft = sizeof (buf);
94 res = iconv (cd,
95 (ICONV_CONST char **) &inptr, &inbytesleft,
96 &outptr, &outbytesleft);
97 ASSERT (res == 0 && inbytesleft == 0);
98 ASSERT (outptr == buf + (sizeof (expected) - 1));
99 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
100 ASSERT (iconv_close (cd) == 0);
101 }
102 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
103 {
104 static const char input[] =
105 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
106 static const char expected[] =
107 "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";
108 iconv_t cd;
109 char buf[100];
110 const char *inptr;
111 size_t inbytesleft;
112 char *outptr;
113 size_t outbytesleft;
114 size_t res;
115 cd = iconv_open ("UTF-16LE", "UTF-8");
116 ASSERT (cd != (iconv_t)(-1));
117 inptr = input;
118 inbytesleft = sizeof (input) - 1;
119 outptr = buf;
120 outbytesleft = sizeof (buf);
121 res = iconv (cd,
122 (ICONV_CONST char **) &inptr, &inbytesleft,
123 &outptr, &outbytesleft);
124 ASSERT (res == 0 && inbytesleft == 0);
125 ASSERT (outptr == buf + (sizeof (expected) - 1));
126 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
127 ASSERT (iconv_close (cd) == 0);
128 }
129 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
130 {
131 static const char input[] =
132 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
133 static const char expected[] =
134 "\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]";
135 iconv_t cd;
136 char buf[100];
137 const char *inptr;
138 size_t inbytesleft;
139 char *outptr;
140 size_t outbytesleft;
141 size_t res;
142 cd = iconv_open ("UTF-32BE", "UTF-8");
143 ASSERT (cd != (iconv_t)(-1));
144 inptr = input;
145 inbytesleft = sizeof (input) - 1;
146 outptr = buf;
147 outbytesleft = sizeof (buf);
148 res = iconv (cd,
149 (ICONV_CONST char **) &inptr, &inbytesleft,
150 &outptr, &outbytesleft);
151 ASSERT (res == 0 && inbytesleft == 0);
152 ASSERT (outptr == buf + (sizeof (expected) - 1));
153 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
154 ASSERT (iconv_close (cd) == 0);
155 }
156 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
157 {
158 static const char input[] =
159 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
160 static const char expected[] =
161 "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";
162 iconv_t cd;
163 char buf[100];
164 const char *inptr;
165 size_t inbytesleft;
166 char *outptr;
167 size_t outbytesleft;
168 size_t res;
169 cd = iconv_open ("UTF-32LE", "UTF-8");
170 ASSERT (cd != (iconv_t)(-1));
171 inptr = input;
172 inbytesleft = sizeof (input) - 1;
173 outptr = buf;
174 outbytesleft = sizeof (buf);
175 res = iconv (cd,
176 (ICONV_CONST char **) &inptr, &inbytesleft,
177 &outptr, &outbytesleft);
178 ASSERT (res == 0 && inbytesleft == 0);
179 ASSERT (outptr == buf + (sizeof (expected) - 1));
180 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
181 ASSERT (iconv_close (cd) == 0);
182 }
183 /* Test conversion from UTF-16BE to UTF-8 with no errors.
184 This test fails on NetBSD 3.0. */
185 {
186 static const char input[] =
187 "\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]";
188 static const char expected[] =
189 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
190 iconv_t cd;
191 char buf[100];
192 const char *inptr;
193 size_t inbytesleft;
194 char *outptr;
195 size_t outbytesleft;
196 size_t res;
197 cd = iconv_open ("UTF-8", "UTF-16BE");
198 ASSERT (cd != (iconv_t)(-1));
199 inptr = input;
200 inbytesleft = sizeof (input) - 1;
201 outptr = buf;
202 outbytesleft = sizeof (buf);
203 res = iconv (cd,
204 (ICONV_CONST char **) &inptr, &inbytesleft,
205 &outptr, &outbytesleft);
206 ASSERT (res == 0 && inbytesleft == 0);
207 ASSERT (outptr == buf + (sizeof (expected) - 1));
208 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
209 ASSERT (iconv_close (cd) == 0);
210 }
211 return 0;
212 }], [gl_cv_func_iconv_supports_utf=yes], [gl_cv_func_iconv_supports_utf=no],
213 [
214 dnl We know that GNU libiconv, GNU libc, and Solaris >= 9 do.
215 dnl OSF/1 5.1 has these encodings, but inserts a BOM in the "to"
216 dnl direction.
217 gl_cv_func_iconv_supports_utf=no
218 if test $gl_func_iconv_gnu = yes; then
219 gl_cv_func_iconv_supports_utf=yes
220 else
221 changequote(,)dnl
222 case "$host_os" in
223 solaris2.9 | solaris2.1[0-9]) gl_cv_func_iconv_supports_utf=yes ;;
224 esac
225 changequote([,])dnl
226 fi
227 ])
228 LIBS="$save_LIBS"
229 ])
230 if test $gl_cv_func_iconv_supports_utf = no; then
231 REPLACE_ICONV_UTF=1
232 AC_DEFINE([REPLACE_ICONV_UTF], [1],
233 [Define if the iconv() functions are enhanced to handle the UTF-{16,32}{BE,LE} encodings.])
234 REPLACE_ICONV=1
235 gl_REPLACE_ICONV_OPEN
236 AC_LIBOBJ([iconv])
237 AC_LIBOBJ([iconv_close])
238 fi
239 fi
240 ])