2 * Copyright (c) 2002-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: tsticonv.c,v 1.2 2004-03-15 21:39:06 adam Exp $
16 #include <yaz/yaz-util.h>
18 /* some test strings in ISO-8859-1 format */
19 static const char *iso_8859_1_a[] = {
28 /* same test strings in MARC-8 format */
29 static const char *marc8_a[] = {
31 "\xa2", /* latin capital letter o with stroke */
32 "eneb\xb5r", /* latin small letter ae */
33 "\xe8\x75", /* latin small letter u with umlaut */
34 "\xe3\x75", /* latin small letter u with circumflex */
35 "\xe3\x75r", /* latin small letter u with circumflex */
39 static void marc8_tst_a()
44 cd = yaz_iconv_open("ISO-8859-1", "MARC8");
47 printf("tsticonv 10 yaz_iconv_open failed\n");
50 for (i = 0; iso_8859_1_a[i]; i++)
53 char *inbuf= (char*) marc8_a[i];
54 size_t inbytesleft = strlen(inbuf);
56 char *outbuf = outbuf0;
57 size_t outbytesleft = sizeof(outbuf0);
59 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
60 if (r == (size_t) (-1))
62 int e = yaz_iconv_error(cd);
64 printf ("tsticonv 11 i=%d e=%d\n", i, e);
67 if ((outbuf - outbuf0) != strlen(iso_8859_1_a[i])
68 || memcmp(outbuf0, iso_8859_1_a[i],
69 strlen(iso_8859_1_a[i])))
71 printf ("tsticonv 12 i=%d\n", i);
72 printf ("buf=%s out=%s\n", iso_8859_1_a[i], outbuf0);
79 static void marc8_tst_b()
81 static const char *marc8_b[] = {
82 "\033$1" "\x21\x2B\x3B" /* FF1F */ "\033(B" "o",
83 "\033$1" "\x6F\x77\x29" /* AE0E */ "\x6F\x52\x7C" /* c0F4 */ "\033(B",
85 "\x21\x50\x6E" /* 7CFB */
86 "\x21\x51\x31" /* 7D71 */
87 "\x21\x3A\x67" /* 5B89 */
88 "\x21\x33\x22" /* 5168 */
89 "\x21\x33\x53" /* 5206 */
90 "\x21\x44\x2B" /* 6790 */
94 static const char *ucs4_b[] = {
95 "\x00\x00\xFF\x1F" "\x00\x00\x00o",
96 "\x00\x00\xAE\x0E" "\x00\x00\xC0\xF4",
108 cd = yaz_iconv_open("UCS4", "MARC8");
111 printf ("tsticonv 20 yaz_iconv_open failed\n");
114 for (i = 0; marc8_b[i]; i++)
118 size_t expect_len = (i == 2 ? 24 : 8);
119 char *inbuf= (char*) marc8_b[i];
120 size_t inbytesleft = strlen(inbuf);
122 char *outbuf = outbuf0;
123 size_t outbytesleft = sizeof(outbuf0);
125 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
126 if (r == (size_t) (-1))
128 int e = yaz_iconv_error(cd);
130 printf ("tsticonv 21 i=%d e=%d\n", i, e);
133 len = outbuf - outbuf0;
134 if (len != expect_len || memcmp(outbuf0, ucs4_b[i], len))
136 printf ("tsticonv 22 len=%d gotlen=%d i=%d\n", expect_len, len, i);
143 static void marc8_tst_c()
145 static const char *ucs4_c[] = {
146 "\x00\x00\xFF\x1F\x00\x00\x00o",
147 "\x00\x00\xAE\x0E\x00\x00\xC0\xF4",
150 static const char *utf8_c[] = {
152 "\xEA\xB8\x8E\xEC\x83\xB4",
159 cd = yaz_iconv_open("UTF8", "UCS4");
162 printf ("tsticonv 30 yaz_iconv_open failed\n");
165 for (i = 0; ucs4_c[i]; i++)
169 char *inbuf= (char*) ucs4_c[i];
170 size_t inbytesleft = 8;
172 char *outbuf = outbuf0;
173 size_t outbytesleft = sizeof(outbuf0);
175 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
176 if (r == (size_t) (-1))
178 int e = yaz_iconv_error(cd);
180 printf ("tsticonv 31 i=%d e=%d\n", i, e);
183 len = outbuf - outbuf0;
184 if (len != strlen(utf8_c[i]) || memcmp(outbuf0, utf8_c[i], len))
186 printf ("tsticonv 32 len=%d gotlen=%d i=%d\n",
187 strlen(utf8_c[i]), len, i);
194 static void dconvert(int mandatory, const char *tmpcode)
198 for (i = 0; iso_8859_1_a[i]; i++)
201 char *inbuf = (char*) iso_8859_1_a[i];
202 size_t inbytesleft = strlen(inbuf);
205 char *outbuf = outbuf0;
206 size_t outbytesleft = sizeof(outbuf0);
208 cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
213 printf ("tsticonv code=%s 1\n", tmpcode);
216 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
217 if (r == (size_t)(-1))
219 int e = yaz_iconv_error(cd);
221 printf ("tsticonv code=%s 2 e=%d\n", tmpcode, e);
226 cd = yaz_iconv_open("ISO-8859-1", tmpcode);
231 printf ("tsticonv code=%s 3\n", tmpcode);
235 inbytesleft = sizeof(outbuf0) - outbytesleft;
238 outbytesleft = sizeof(outbuf1);
239 r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
240 if (r == (size_t)(-1)) {
241 int e = yaz_iconv_error(cd);
243 printf ("tsticonv code=%s 4 e=%d\n", tmpcode, e);
246 if (strlen(iso_8859_1_a[i]) ==
247 (sizeof(outbuf1) - outbytesleft) &&
248 memcmp(outbuf1, iso_8859_1_a[i],
249 strlen(iso_8859_1_a[i])))
251 printf ("tsticonv code=%s 5\n", tmpcode);
258 int main (int argc, char **argv)
260 dconvert(1, "UTF-8");
261 dconvert(1, "ISO-8859-1");
263 dconvert(1, "UCS4LE");
264 dconvert(0, "CP865");