Use GNU libunistring and Gnulib modules needed by R6RS bytevectors and ports.
[bpt/guile.git] / m4 / iconv_open.m4
1 # iconv_open.m4 serial 5
2 dnl Copyright (C) 2007-2009 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 Test whether iconv_open accepts standardized encoding names.
14 dnl We know that GNU libiconv and GNU libc do.
15 AC_EGREP_CPP([gnu_iconv], [
16 #include <iconv.h>
17 #if defined _LIBICONV_VERSION || defined __GLIBC__
18 gnu_iconv
19 #endif
20 ], [gl_func_iconv_gnu=yes], [gl_func_iconv_gnu=no])
21 if test $gl_func_iconv_gnu = no; then
22 iconv_flavor=
23 case "$host_os" in
24 aix*) iconv_flavor=ICONV_FLAVOR_AIX ;;
25 irix*) iconv_flavor=ICONV_FLAVOR_IRIX ;;
26 hpux*) iconv_flavor=ICONV_FLAVOR_HPUX ;;
27 osf*) iconv_flavor=ICONV_FLAVOR_OSF ;;
28 esac
29 if test -n "$iconv_flavor"; then
30 AC_DEFINE_UNQUOTED([ICONV_FLAVOR], [$iconv_flavor],
31 [Define to a symbolic name denoting the flavor of iconv_open()
32 implementation.])
33 gl_REPLACE_ICONV_OPEN
34 fi
35 fi
36 fi
37 ])
38
39 AC_DEFUN([gl_REPLACE_ICONV_OPEN],
40 [
41 gl_REPLACE_ICONV_H
42 REPLACE_ICONV_OPEN=1
43 AC_LIBOBJ([iconv_open])
44 ])
45
46 AC_DEFUN([gl_FUNC_ICONV_OPEN_UTF],
47 [
48 AC_REQUIRE([gl_FUNC_ICONV_OPEN])
49 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
50 AC_REQUIRE([gl_ICONV_H_DEFAULTS])
51 if test "$am_cv_func_iconv" = yes; then
52 if test -n "$am_cv_proto_iconv_arg1"; then
53 ICONV_CONST="const"
54 else
55 ICONV_CONST=
56 fi
57 AC_SUBST([ICONV_CONST])
58 AC_CACHE_CHECK([whether iconv supports conversion between UTF-8 and UTF-{16,32}{BE,LE}],
59 [gl_cv_func_iconv_supports_utf],
60 [
61 save_LIBS="$LIBS"
62 LIBS="$LIBS $LIBICONV"
63 AC_TRY_RUN([
64 #include <iconv.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #define ASSERT(expr) if (!(expr)) return 1;
70 int main ()
71 {
72 /* Test conversion from UTF-8 to UTF-16BE with no errors. */
73 {
74 static const char input[] =
75 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
76 static const char expected[] =
77 "\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]";
78 iconv_t cd;
79 char buf[100];
80 const char *inptr;
81 size_t inbytesleft;
82 char *outptr;
83 size_t outbytesleft;
84 size_t res;
85 cd = iconv_open ("UTF-16BE", "UTF-8");
86 ASSERT (cd != (iconv_t)(-1));
87 inptr = input;
88 inbytesleft = sizeof (input) - 1;
89 outptr = buf;
90 outbytesleft = sizeof (buf);
91 res = iconv (cd,
92 (ICONV_CONST char **) &inptr, &inbytesleft,
93 &outptr, &outbytesleft);
94 ASSERT (res == 0 && inbytesleft == 0);
95 ASSERT (outptr == buf + (sizeof (expected) - 1));
96 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
97 ASSERT (iconv_close (cd) == 0);
98 }
99 /* Test conversion from UTF-8 to UTF-16LE with no errors. */
100 {
101 static const char input[] =
102 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
103 static const char expected[] =
104 "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";
105 iconv_t cd;
106 char buf[100];
107 const char *inptr;
108 size_t inbytesleft;
109 char *outptr;
110 size_t outbytesleft;
111 size_t res;
112 cd = iconv_open ("UTF-16LE", "UTF-8");
113 ASSERT (cd != (iconv_t)(-1));
114 inptr = input;
115 inbytesleft = sizeof (input) - 1;
116 outptr = buf;
117 outbytesleft = sizeof (buf);
118 res = iconv (cd,
119 (ICONV_CONST char **) &inptr, &inbytesleft,
120 &outptr, &outbytesleft);
121 ASSERT (res == 0 && inbytesleft == 0);
122 ASSERT (outptr == buf + (sizeof (expected) - 1));
123 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
124 ASSERT (iconv_close (cd) == 0);
125 }
126 /* Test conversion from UTF-8 to UTF-32BE with no errors. */
127 {
128 static const char input[] =
129 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
130 static const char expected[] =
131 "\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]";
132 iconv_t cd;
133 char buf[100];
134 const char *inptr;
135 size_t inbytesleft;
136 char *outptr;
137 size_t outbytesleft;
138 size_t res;
139 cd = iconv_open ("UTF-32BE", "UTF-8");
140 ASSERT (cd != (iconv_t)(-1));
141 inptr = input;
142 inbytesleft = sizeof (input) - 1;
143 outptr = buf;
144 outbytesleft = sizeof (buf);
145 res = iconv (cd,
146 (ICONV_CONST char **) &inptr, &inbytesleft,
147 &outptr, &outbytesleft);
148 ASSERT (res == 0 && inbytesleft == 0);
149 ASSERT (outptr == buf + (sizeof (expected) - 1));
150 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
151 ASSERT (iconv_close (cd) == 0);
152 }
153 /* Test conversion from UTF-8 to UTF-32LE with no errors. */
154 {
155 static const char input[] =
156 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
157 static const char expected[] =
158 "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";
159 iconv_t cd;
160 char buf[100];
161 const char *inptr;
162 size_t inbytesleft;
163 char *outptr;
164 size_t outbytesleft;
165 size_t res;
166 cd = iconv_open ("UTF-32LE", "UTF-8");
167 ASSERT (cd != (iconv_t)(-1));
168 inptr = input;
169 inbytesleft = sizeof (input) - 1;
170 outptr = buf;
171 outbytesleft = sizeof (buf);
172 res = iconv (cd,
173 (ICONV_CONST char **) &inptr, &inbytesleft,
174 &outptr, &outbytesleft);
175 ASSERT (res == 0 && inbytesleft == 0);
176 ASSERT (outptr == buf + (sizeof (expected) - 1));
177 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
178 ASSERT (iconv_close (cd) == 0);
179 }
180 /* Test conversion from UTF-16BE to UTF-8 with no errors.
181 This test fails on NetBSD 3.0. */
182 {
183 static const char input[] =
184 "\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]";
185 static const char expected[] =
186 "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
187 iconv_t cd;
188 char buf[100];
189 const char *inptr;
190 size_t inbytesleft;
191 char *outptr;
192 size_t outbytesleft;
193 size_t res;
194 cd = iconv_open ("UTF-8", "UTF-16BE");
195 ASSERT (cd != (iconv_t)(-1));
196 inptr = input;
197 inbytesleft = sizeof (input) - 1;
198 outptr = buf;
199 outbytesleft = sizeof (buf);
200 res = iconv (cd,
201 (ICONV_CONST char **) &inptr, &inbytesleft,
202 &outptr, &outbytesleft);
203 ASSERT (res == 0 && inbytesleft == 0);
204 ASSERT (outptr == buf + (sizeof (expected) - 1));
205 ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
206 ASSERT (iconv_close (cd) == 0);
207 }
208 return 0;
209 }], [gl_cv_func_iconv_supports_utf=yes], [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 ])