1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by GOTO Masanori <gotom@debian.or.jp>, 2004
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #define SIZE 256 /* enough room for conversion */
27 #define SAMPLESTR "abc"
41 /* test builtin transformation */
42 struct convcode testcode
[] = {
51 int number
= (int) sizeof (testcode
) / sizeof (struct convcode
);
54 convert (const char *tocode
, const char *fromcode
, char *inbufp
,
55 size_t inbytesleft
, char *outbufp
, size_t outbytesleft
)
58 size_t outbytes
= outbytesleft
;
61 ic
= iconv_open (tocode
, fromcode
);
62 if (ic
== (iconv_t
*) - 1)
64 printf ("iconv_open failed: from: %s, to: %s: %s",
65 fromcode
, tocode
, strerror (errno
));
69 while (inbytesleft
> 0)
71 ret
= iconv (ic
, &inbufp
, &inbytesleft
, &outbufp
, &outbytes
);
74 printf ("iconv failed: from: %s, to: %s: %s",
75 fromcode
, tocode
, strerror (errno
));
80 ret
= iconv_close (ic
);
83 printf ("iconv_close failed: from: %s, to: %s: %s",
84 fromcode
, tocode
, strerror (errno
));
88 return outbytesleft
- outbytes
;
93 test_unalign (struct convcode
*codes
, char *str
, int len
)
95 struct unalign
*inbufp
, *outbufp
;
97 size_t inbytesleft
, outbytesleft
;
100 /* allocating unaligned buffer for both inbuf and outbuf */
101 inbufp
= (struct unalign
*) malloc (sizeof (struct unalign
));
104 printf ("no memory available\n");
107 inbuf
= inbufp
->str2
;
109 outbufp
= (struct unalign
*) malloc (sizeof (struct unalign
));
112 printf ("no memory available\n");
115 outbuf
= outbufp
->str2
;
117 /* first iconv phase */
118 memcpy (inbuf
, str
, len
);
120 outbytesleft
= sizeof (struct unalign
);
121 retlen
= convert (codes
->tocode
, codes
->fromcode
, inbuf
, inbytesleft
,
122 outbuf
, outbytesleft
);
123 if (retlen
== -1) /* failed */
126 /* second round trip iconv phase */
127 memcpy (inbuf
, outbuf
, retlen
);
128 inbytesleft
= retlen
;
129 outbytesleft
= sizeof (struct unalign
);
130 retlen
= convert (codes
->fromcode
, codes
->tocode
, inbuf
, inbytesleft
,
131 outbuf
, outbytesleft
);
132 if (retlen
== -1) /* failed */
142 main (int argc
, char *argv
[])
147 for (i
= 0; i
< number
; i
++)
149 ret
= test_unalign (&testcode
[i
], (char *) SAMPLESTR
, sizeof (SAMPLESTR
));
152 printf ("iconv: %s <-> %s: ok\n",
153 testcode
[i
].fromcode
, testcode
[i
].tocode
);
155 printf ("Succeeded.\n");