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