Happy new year
[yaz-moved-to-github.git] / util / yaz-record-conv.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #if HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <stdlib.h>
11 #include <yaz/options.h>
12 #include <yaz/record_conv.h>
13
14 const char *prog = "yaz-record-conv";
15
16 static void usage(void)
17 {
18     fprintf(stderr, "%s: usage\nyaz-record-conf config file ..\n", prog);
19     exit(1);
20 }
21 int main (int argc, char **argv)
22 {
23     int r;
24     char *arg;
25     yaz_record_conv_t p = 0;
26     int no_errors = 0;
27     while ((r = options("V", argv, argc, &arg)) != -2)
28     {
29         switch (r)
30         {
31         case 'V':
32             break;
33         case 0:
34 #if YAZ_HAVE_XML2
35             if (!p)
36             {
37                 xmlDocPtr doc = xmlParseFile(arg);
38                 int r = -1;
39
40                 p = yaz_record_conv_create();
41                 if (doc)
42                 {
43                     xmlNodePtr ptr = xmlDocGetRootElement(doc);
44                     if (ptr)
45                     {
46                         r = yaz_record_conv_configure(p, ptr);
47                         if (r)
48                         {
49                             fprintf(stderr, "record conf error: %s\n",
50                                     yaz_record_conv_get_error(p));
51                         }
52                     }
53                 }
54                 xmlFreeDoc(doc);
55                 if (r)
56                 {
57                     yaz_record_conv_destroy(p);
58                     exit(2);
59                 }
60             }
61             else
62             {
63                 WRBUF input_record = wrbuf_alloc();
64                 WRBUF output_record = wrbuf_alloc();
65                 FILE *f = fopen(arg, "rb");
66                 int c, r;
67                 if (!f)
68                 {
69                     fprintf(stderr, "%s: open failed: %s\n",
70                             prog, arg);
71                     exit(3);
72                 }
73                 while ((c = getc(f)) != EOF)
74                     wrbuf_putc(input_record, c);
75                 
76                 r = yaz_record_conv_record(p, 
77                                            wrbuf_buf(input_record),
78                                            wrbuf_len(input_record),
79                                            output_record);
80                 if (r)
81                 {
82                     fprintf(stderr, "%s: %s: Error %s\n",
83                             prog, arg, 
84                             yaz_record_conv_get_error(p));
85                     no_errors++;
86                 }
87                 else
88                 {
89                     fwrite(wrbuf_buf(output_record), 1,
90                            wrbuf_len(output_record), stdout);
91                 }
92                 wrbuf_destroy(input_record);
93                 wrbuf_destroy(output_record);
94                 fclose(f);
95             }
96             break;
97 #else
98             fprintf(stderr, "%s: YAZ not compiled with Libxml2 support\n",
99                 prog);
100             usage();
101             break;
102 #endif
103         default:
104             usage();
105         }
106     }
107 #if YAZ_HAVE_XML2
108     yaz_record_conv_destroy(p);
109 #endif
110     if (no_errors)
111         exit(1);
112     exit(0);
113 }
114 /*
115  * Local variables:
116  * c-basic-offset: 4
117  * c-file-style: "Stroustrup"
118  * indent-tabs-mode: nil
119  * End:
120  * vim: shiftwidth=4 tabstop=8 expandtab
121  */
122