Fix compile bug for systems that have nl_langinfo but CODESET undefined
[yaz-moved-to-github.git] / util / marcdump.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: marcdump.c,v 1.22 2003-02-25 18:35:49 adam Exp $
7  */
8
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #if HAVE_LOCALE_H
19 #include <locale.h>
20 #endif
21 #if HAVE_LANGINFO_H
22 #include <langinfo.h>
23 #endif
24
25 #include <yaz/marcdisp.h>
26 #include <yaz/yaz-util.h>
27 #include <yaz/xmalloc.h>
28 #include <yaz/options.h>
29
30 #ifndef SEEK_SET
31 #define SEEK_SET 0
32 #endif
33 #ifndef SEEK_END
34 #define SEEK_END 2
35 #endif
36
37 static void usage(const char *prog)
38 {
39     fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-O] [-X] [-v] file...\n",
40              prog);
41
42
43 int main (int argc, char **argv)
44 {
45     int r;
46     char *arg;
47     int verbose = 0;
48     FILE *inf;
49     char buf[100001];
50     char *prog = *argv;
51     int no = 0;
52     int xml = 0;
53     FILE *cfile = 0;
54     char *from = 0, *to = 0;
55
56     
57 #if HAVE_LOCALE_H
58     setlocale(LC_CTYPE, "");
59 #endif
60 #if HAVE_LANGINFO_H
61 #ifdef CODESET
62     to = nl_langinfo(CODESET);
63 #endif
64 #endif
65
66     while ((r = options("vc:xOXf:t:", argv, argc, &arg)) != -2)
67     {
68         int count;
69         no++;
70         switch (r)
71         {
72         case 'f':
73             from = arg;
74             break;
75         case 't':
76             to = arg;
77             break;
78         case 'c':
79             if (cfile)
80                 fclose (cfile);
81             cfile = fopen (arg, "w");
82             break;
83         case 'x':
84             xml = YAZ_MARC_SIMPLEXML;
85             break;
86         case 'O':
87             xml = YAZ_MARC_OAIMARC;
88             break;
89         case 'X':
90             xml = YAZ_MARC_MARCXML;
91             break;
92         case 0:
93             inf = fopen (arg, "r");
94             count = 0;
95             if (!inf)
96             {
97                 fprintf (stderr, "%s: cannot open %s:%s\n",
98                          prog, arg, strerror (errno));
99                 exit(1);
100             }
101             if (cfile)
102                 fprintf (cfile, "char *marc_records[] = {\n");
103             if (1)
104             {
105                 yaz_marc_t mt = yaz_marc_create();
106                 yaz_iconv_t cd = 0;
107
108                 if (from && to)
109                 {
110                     cd = yaz_iconv_open(to, from);
111                     if (!cd)
112                     {
113                         fprintf(stderr, "conversion from %s to %s "
114                                 "unsupported\n", from, to);
115                         exit(2);
116                     }
117                 }
118                 yaz_marc_xml(mt, xml);
119                 yaz_marc_debug(mt, verbose);
120                 while (1)
121                 {
122                     int len;
123                     char *result;
124                     int rlen;
125                     
126                     r = fread (buf, 1, 5, inf);
127                     if (r < 5)
128                         break;
129                     len = atoi_n(buf, 5);
130                     if (len < 25 || len > 100000)
131                         break;
132                     len = len - 5;
133                     r = fread (buf + 5, 1, len, inf);
134                     if (r < len)
135                         break;
136                     r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
137                     if (r <= 0)
138                         break;
139                     if (!cd)
140                         fwrite (result, rlen, 1, stdout);
141                     else
142                     {
143                         char outbuf[12];
144                         size_t inbytesleft = rlen;
145                         const char *inp = result;
146                         
147                         while (inbytesleft)
148                         {
149                             size_t outbytesleft = sizeof(outbuf);
150                             char *outp = outbuf;
151                             size_t r = yaz_iconv (cd, (char**) &inp,
152                                                   &inbytesleft, 
153                                                   &outp, &outbytesleft);
154                             if (r == (size_t) (-1))
155                             {
156                                 int e = yaz_iconv_error(cd);
157                                 if (e != YAZ_ICONV_E2BIG)
158                                     break;
159                             }
160                             fwrite (outbuf, outp - outbuf, 1, stdout);
161                         }
162                     }
163
164                     if (cfile)
165                     {
166                         char *p = buf;
167                         int i;
168                         if (count)
169                             fprintf (cfile, ",");
170                         fprintf (cfile, "\n");
171                         for (i = 0; i < r; i++)
172                         {
173                             if ((i & 15) == 0)
174                                 fprintf (cfile, "  \"");
175                             fprintf (cfile, "\\x%02X", p[i] & 255);
176                             
177                             if (i < r - 1 && (i & 15) == 15)
178                                 fprintf (cfile, "\"\n");
179                             
180                         }
181                         fprintf (cfile, "\"\n");
182                     }
183                 }
184                 count++;
185                 if (cd)
186                     yaz_iconv_close(cd);
187             }
188             if (cfile)
189                 fprintf (cfile, "};\n");
190             break;
191         case 'v':
192             verbose++;
193             break;
194         default:
195             usage(prog);
196             exit (1);
197         }
198     }
199     if (cfile)
200         fclose (cfile);
201     if (!no)
202     {
203         usage(prog);
204         exit (1);
205     }
206     exit (0);
207 }