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