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