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