Fix sample PQF
[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.5 2003-06-22 11:48:33 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 void dconvert(int mandatory, const char *tmpcode)
74 {
75     int i;
76     yaz_iconv_t cd;
77     for (i = 0; buf[i]; i++)
78     {
79         size_t r;
80         char *inbuf = (char*) buf[i];
81         size_t inbytesleft = strlen(inbuf);
82         char outbuf0[24];
83         char outbuf1[10];
84         char *outbuf = outbuf0;
85         size_t outbytesleft = sizeof(outbuf0);
86
87         cd = yaz_iconv_open(tmpcode, "ISO-8859-1");
88         if (!cd)
89         {
90             if (!mandatory)
91                 return;
92             printf ("tsticonv 1\n");
93             exit(1);
94         }
95         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
96         if (r == (size_t)(-1))
97         {
98             int e = yaz_iconv_error(cd);
99
100             printf ("tsticonv 2 e=%d\n", e);
101             exit(2);
102         }
103         yaz_iconv_close(cd);
104         
105         cd = yaz_iconv_open("ISO-8859-1", tmpcode);
106         if (!cd)
107         {
108             if (!mandatory)
109                 return;
110             printf ("tsticonv 3\n");
111             exit(3);
112         }
113         inbuf = outbuf0;
114         inbytesleft = sizeof(outbuf0) - outbytesleft;
115
116         outbuf = outbuf1;
117         outbytesleft = sizeof(outbuf1);
118         r = yaz_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
119         if (r == (size_t)(-1)) {
120             int e = yaz_iconv_error(cd);
121
122             printf ("tsticonv 4 e=%d\n", e);
123             exit(4);
124         }
125         if (strlen(buf[i]) == (sizeof(outbuf1) - outbytesleft) &&
126             memcmp(outbuf1, buf[i], strlen(buf[i])))
127         {
128             printf ("tsticonv 5\n");
129             exit(5);
130         }
131         yaz_iconv_close(cd);
132     }
133 }
134         
135 int main (int argc, char **argv)
136 {
137     dconvert(1, "UTF-8");
138     dconvert(1, "ISO-8859-1");
139     dconvert(1, "UCS4");
140     dconvert(0, "CP865");
141     marc8_tst();
142     exit (0);
143 }