Dumper only keeps one record at a time in memory.
[yaz-moved-to-github.git] / util / marcdump.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: marcdump.c,v $
7  * Revision 1.13  2001-02-10 01:21:59  adam
8  * Dumper only keeps one record at a time in memory.
9  *
10  * Revision 1.12  2000/10/02 11:07:45  adam
11  * Added peer_name member for bend_init handler. Changed the YAZ
12  * client so that tcp: can be avoided in target spec.
13  *
14  * Revision 1.11  2000/07/04 08:53:22  adam
15  * Fixed bug.
16  *
17  * Revision 1.10  2000/02/29 13:44:55  adam
18  * Check for config.h (currently not generated).
19  *
20  * Revision 1.9  1999/11/30 13:47:12  adam
21  * Improved installation. Moved header files to include/yaz.
22  *
23  * Revision 1.8  1999/05/26 07:49:35  adam
24  * C++ compilation.
25  *
26  * Revision 1.7  1998/02/11 11:53:36  adam
27  * Changed code so that it compiles as C++.
28  *
29  * Revision 1.6  1997/12/12 06:32:33  adam
30  * Added include of string.h.
31  *
32  * Revision 1.5  1997/09/24 13:29:40  adam
33  * Added verbose option -v to marcdump utility.
34  *
35  * Revision 1.4  1995/11/01 13:55:05  quinn
36  * Minor adjustments
37  *
38  * Revision 1.3  1995/05/16  08:51:12  quinn
39  * License, documentation, and memory fixes
40  *
41  * Revision 1.2  1995/05/15  11:56:56  quinn
42  * Debuggng & adjustments.
43  *
44  * Revision 1.1  1995/04/10  10:28:47  quinn
45  * Added copy of CCL and MARC display
46  *
47  */
48
49 #if HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <errno.h>
57 #include <yaz/marcdisp.h>
58 #include <yaz/yaz-util.h>
59 #include <yaz/xmalloc.h>
60 #include <yaz/options.h>
61
62 #ifndef SEEK_SET
63 #define SEEK_SET 0
64 #endif
65 #ifndef SEEK_END
66 #define SEEK_END 2
67 #endif
68  
69 int main (int argc, char **argv)
70 {
71     int r;
72     char *arg;
73     int verbose = 0;
74     FILE *inf;
75     char buf[100001];
76     char *prog = *argv;
77     int no = 0;
78     FILE *cfile = 0;
79
80     while ((r = options("vc:", argv, argc, &arg)) != -2)
81     {
82         int count;
83         no++;
84         switch (r)
85         {
86         case 'c':
87             if (cfile)
88                 fclose (cfile);
89             cfile = fopen (arg, "w");
90             break;
91         case 0:
92             inf = fopen (arg, "r");
93             count = 0;
94             if (!inf)
95             {
96                 fprintf (stderr, "%s: cannot open %s:%s\n",
97                          prog, arg, strerror (errno));
98                 exit (1);
99             }
100             if (cfile)
101                 fprintf (cfile, "char *marc_records[] = {\n");
102             while (1)
103             {
104                 int len;
105                 
106                 r = fread (buf, 1, 5, inf);
107                 if (r < 5)
108                     break;
109                 len = atoi_n(buf, 5);
110                 if (len < 25 || len > 100000)
111                     break;
112                 len = len - 5;
113                 r = fread (buf + 5, 1, len, inf);
114                 if (r < len)
115                     break;
116                 r = marc_display_ex (buf, stdout, verbose);
117                 if (r <= 0)
118                     break;
119                 if (cfile)
120                 {
121                     char *p = buf;
122                     int i;
123                     if (count)
124                         fprintf (cfile, ",");
125                     fprintf (cfile, "{\n");
126                     for (i = 0; i < r; i++)
127                     {
128                         if ((i & 15) == 0)
129                             fprintf (cfile, "  \"");
130                         fprintf (cfile, "\\x%02X", p[i] & 255);
131                         
132                         if (i < r - 1 && (i & 15) == 15)
133                             fprintf (cfile, "\"\n");
134                         
135                         }
136                     fprintf (cfile, "\"\n}");
137                 }
138                 count++;
139             }
140             if (cfile)
141                 fprintf (cfile, "};\n");
142             break;
143         case 'v':
144             verbose++;
145             break;
146         default:
147             fprintf (stderr, "Usage: %s [-c cfile] [-v] file...\n", prog);
148             exit (1);
149         }
150     }
151     if (cfile)
152         fclose (cfile);
153     if (!no)
154     {
155         fprintf (stderr, "Usage: %s [-v] file...\n", prog);
156         exit (1);
157     }
158     exit (0);
159 }