fec436191dd104fd6fcc9120f60f94e6843c1133
[yaz-moved-to-github.git] / util / tsticonv.c
1 /*
2  * Copyright (c) 2002-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: tsticonv.c,v 1.4 2003-06-22 11:47:18 adam Exp $
6  */
7
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <errno.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 #include <yaz/yaz-util.h>
17
18 /* some test strings in ISO-8859-1 format */
19 const char *buf[] = {
20     "ax" ,
21     "\330",
22     "eneb\346r",
23     "\xfc",
24     "\xfb",
25     "\xfbr",
26     0 };
27
28 /* some test strings in MARC-8 format */
29 const char *marc8_strings[] = {
30     "ax",   
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 */
36     0
37 };
38
39 static void marc8_tst()
40 {
41     int i;
42     yaz_iconv_t cd;
43
44     cd = yaz_iconv_open("ISO-8859-1", "MARC8");
45     for (i = 0; buf[i]; i++)
46     {
47         size_t r;
48         char *inbuf= (char*) marc8_strings[i];
49         size_t inbytesleft = strlen(inbuf);
50         char outbuf0[24];
51         char *outbuf = outbuf0;
52         size_t outbytesleft = sizeof(outbuf0);
53
54         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
55         if (r == (size_t) (-1))
56         {
57             int e = yaz_iconv_error(cd);
58
59             printf ("tsticonv 6 i=%d e=%d\n", i, e);
60             exit(6);
61         }
62         if ((outbuf - outbuf0) != strlen(buf[i]) 
63             || memcmp(outbuf0, buf[i], strlen(buf[i])))
64         {
65             printf ("tsticonv 7 i=%d\n", i);
66             printf ("buf=%s   out=%s\n", buf[i], outbuf0);
67             exit(7);
68         }
69     }
70     yaz_iconv_close(cd);
71 }
72
73 static dconvert(int mandatory, const char *tmpcode)
74 {
75     int i;
76     yaz_iconv_t cd;
77     for (i = 0; buf[i]; i++)
78     {
79         int j;
80         size_t r;
81         char *inbuf = (char*) buf[i];
82         size_t inbytesleft = strlen(inbuf);
83         char outbuf0[24];
84         char outbuf1[10];
85         char *outbuf = outbuf0;
86         size_t outbytesleft = sizeof(outbuf0);
87
88         cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
89         if (!cd)
90         {
91             if (!mandatory)
92                 return;
93             printf ("tsticonv 1\n");
94             exit(1);
95         }
96         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
97         if (r == (size_t)(-1))
98         {
99             int e = yaz_iconv_error(cd);
100
101             printf ("tsticonv 2 e=%d\n", e);
102             exit(2);
103         }
104         yaz_iconv_close(cd);
105         
106         cd = yaz_iconv_open("ISO-8859-1", tmpcode);
107         if (!cd)
108         {
109             if (!mandatory)
110                 return;
111             printf ("tsticonv 3\n");
112             exit(3);
113         }
114         inbuf = outbuf0;
115         inbytesleft = sizeof(outbuf0) - outbytesleft;
116
117         outbuf = outbuf1;
118         outbytesleft = sizeof(outbuf1);
119         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
120         if (r == (size_t)(-1)) {
121             int e = yaz_iconv_error(cd);
122
123             printf ("tsticonv 4 e=%d\n", e);
124             exit(4);
125         }
126         if (strlen(buf[i]) == (sizeof(outbuf1) - outbytesleft) &&
127             memcmp(outbuf1, buf[i], strlen(buf[i])))
128         {
129             printf ("tsticonv 5\n");
130             exit(5);
131         }
132         yaz_iconv_close(cd);
133     }
134 }
135         
136 int main (int argc, char **argv)
137 {
138     dconvert(1, "UTF-8");
139     dconvert(1, "ISO-8859-1");
140     dconvert(1, "UCS4");
141     dconvert(0, "CP865");
142     marc8_tst();
143     exit (0);
144 }