MARC records embedded in C code for test server
[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.16 2002-03-18 21:33:48 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 static void usage(const char *prog)
31 {
32     fprintf (stderr, "Usage: %s [-c cfile] [-x] [-v] file...\n", prog);
33
34
35 int main (int argc, char **argv)
36 {
37     int r;
38     char *arg;
39     int verbose = 0;
40     FILE *inf;
41     char buf[100001];
42     char *prog = *argv;
43     int no = 0;
44     int xml = 0;
45     FILE *cfile = 0;
46
47     while ((r = options("vc:x", argv, argc, &arg)) != -2)
48     {
49         int count;
50         no++;
51         switch (r)
52         {
53         case 'c':
54             if (cfile)
55                 fclose (cfile);
56             cfile = fopen (arg, "w");
57             break;
58         case 'x':
59             xml = 1;
60             break;
61         case 0:
62             inf = fopen (arg, "r");
63             count = 0;
64             if (!inf)
65             {
66                 fprintf (stderr, "%s: cannot open %s:%s\n",
67                          prog, arg, strerror (errno));
68                 exit (1);
69             }
70             if (cfile)
71                 fprintf (cfile, "char *marc_records[] = {\n");
72             while (1)
73             {
74                 WRBUF wr = wrbuf_alloc();
75
76                 int len;
77                 
78                 r = fread (buf, 1, 5, inf);
79                 if (r < 5)
80                     break;
81                 len = atoi_n(buf, 5);
82                 if (len < 25 || len > 100000)
83                     break;
84                 len = len - 5;
85                 r = fread (buf + 5, 1, len, inf);
86                 if (r < len)
87                     break;
88                 r = yaz_marc_decode (buf, wr, verbose, -1, xml);
89                 if (r <= 0)
90                     break;
91                 fwrite (wrbuf_buf(wr), wrbuf_len(wr), 1, stdout);
92                 if (cfile)
93                 {
94                     char *p = buf;
95                     int i;
96                     if (count)
97                         fprintf (cfile, ",");
98                     fprintf (cfile, "\n");
99                     for (i = 0; i < r; i++)
100                     {
101                         if ((i & 15) == 0)
102                             fprintf (cfile, "  \"");
103                         fprintf (cfile, "\\x%02X", p[i] & 255);
104                         
105                         if (i < r - 1 && (i & 15) == 15)
106                             fprintf (cfile, "\"\n");
107                         
108                         }
109                     fprintf (cfile, "\"\n");
110                 }
111                 count++;
112             }
113             if (cfile)
114                 fprintf (cfile, "};\n");
115             break;
116         case 'v':
117             verbose++;
118             break;
119         default:
120             usage(prog);
121             exit (1);
122         }
123     }
124     if (cfile)
125         fclose (cfile);
126     if (!no)
127     {
128         usage(prog);
129         exit (1);
130     }
131     exit (0);
132 }