772cc296db97f1b47180498c58a01a638eef5bfc
[yaz-moved-to-github.git] / util / marcdump.c
1 /*
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: marcdump.c,v 1.14 2002-02-28 14:28:40 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 #include <yaz/wrbuf.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/yaz-util.h>
20 #include <yaz/xmalloc.h>
21 #include <yaz/options.h>
22
23 #ifndef SEEK_SET
24 #define SEEK_SET 0
25 #endif
26 #ifndef SEEK_END
27 #define SEEK_END 2
28 #endif
29  
30 int main (int argc, char **argv)
31 {
32     int r;
33     char *arg;
34     int verbose = 0;
35     FILE *inf;
36     char buf[100001];
37     char *prog = *argv;
38     int no = 0;
39     int xml = 0;
40     FILE *cfile = 0;
41
42     while ((r = options("vc:x", argv, argc, &arg)) != -2)
43     {
44         int count;
45         no++;
46         switch (r)
47         {
48         case 'c':
49             if (cfile)
50                 fclose (cfile);
51             cfile = fopen (arg, "w");
52             break;
53         case 'x':
54             xml = 1;
55             break;
56         case 0:
57             inf = fopen (arg, "r");
58             count = 0;
59             if (!inf)
60             {
61                 fprintf (stderr, "%s: cannot open %s:%s\n",
62                          prog, arg, strerror (errno));
63                 exit (1);
64             }
65             if (cfile)
66                 fprintf (cfile, "char *marc_records[] = {\n");
67             while (1)
68             {
69                 WRBUF wr = wrbuf_alloc();
70
71                 int len;
72                 
73                 r = fread (buf, 1, 5, inf);
74                 if (r < 5)
75                     break;
76                 len = atoi_n(buf, 5);
77                 if (len < 25 || len > 100000)
78                     break;
79                 len = len - 5;
80                 r = fread (buf + 5, 1, len, inf);
81                 if (r < len)
82                     break;
83                 r = yaz_marc_decode (buf, wr, verbose, -1, xml);
84                 if (r <= 0)
85                     break;
86                 fwrite (wrbuf_buf(wr), wrbuf_len(wr), 1, stdout);
87                 if (cfile)
88                 {
89                     char *p = buf;
90                     int i;
91                     if (count)
92                         fprintf (cfile, ",");
93                     fprintf (cfile, "{\n");
94                     for (i = 0; i < r; i++)
95                     {
96                         if ((i & 15) == 0)
97                             fprintf (cfile, "  \"");
98                         fprintf (cfile, "\\x%02X", p[i] & 255);
99                         
100                         if (i < r - 1 && (i & 15) == 15)
101                             fprintf (cfile, "\"\n");
102                         
103                         }
104                     fprintf (cfile, "\"\n}");
105                 }
106                 count++;
107             }
108             if (cfile)
109                 fprintf (cfile, "};\n");
110             break;
111         case 'v':
112             verbose++;
113             break;
114         default:
115             fprintf (stderr, "Usage: %s [-c cfile] [-v] file...\n", prog);
116             exit (1);
117         }
118     }
119     if (cfile)
120         fclose (cfile);
121     if (!no)
122     {
123         fprintf (stderr, "Usage: %s [-v] file...\n", prog);
124         exit (1);
125     }
126     exit (0);
127 }