File: yaz-record-conv.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (129 lines) | stat: -rw-r--r-- 3,440 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <yaz/options.h>
#include <yaz/log.h>
#include <yaz/record_conv.h>
#include <yaz/backtrace.h>

const char *prog = "yaz-record-conv";

static void usage(void)
{
    fprintf(stderr, "%s: usage\nyaz-record-conv config file ..\n", prog);
    exit(1);
}
int main (int argc, char **argv)
{
    int r;
    char *arg;
    yaz_record_conv_t p = 0;
    int no_errors = 0;

    yaz_enable_panic_backtrace(*argv);
    while ((r = options("v:V", argv, argc, &arg)) != -2)
    {
        switch (r)
        {
        case 'v':
            yaz_log_init(yaz_log_mask_str(arg), "", 0);
            break;
        case 'V':
            break;
        case 0:
#if YAZ_HAVE_XML2
            if (!p)
            {
                xmlDocPtr doc = xmlParseFile(arg);
                int r = -1;

                p = yaz_record_conv_create();
                if (doc)
                {
                    xmlNodePtr ptr = xmlDocGetRootElement(doc);
                    if (ptr)
                    {
                        r = yaz_record_conv_configure(p, ptr);
                        if (r)
                        {
                            fprintf(stderr, "record conf error: %s\n",
                                    yaz_record_conv_get_error(p));
                        }
                    }
                }
                xmlFreeDoc(doc);
                if (r)
                {
                    yaz_record_conv_destroy(p);
                    exit(2);
                }
            }
            else
            {
                WRBUF input_record = wrbuf_alloc();
                WRBUF output_record = wrbuf_alloc();
                FILE *f = fopen(arg, "rb");
                int c, r;
                if (!f)
                {
                    fprintf(stderr, "%s: open failed: %s\n",
                            prog, arg);
                    exit(3);
                }
                while ((c = getc(f)) != EOF)
                    wrbuf_putc(input_record, c);

                r = yaz_record_conv_record(p,
                                           wrbuf_buf(input_record),
                                           wrbuf_len(input_record),
                                           output_record);
                if (r)
                {
                    fprintf(stderr, "%s: %s: Error %s\n",
                            prog, arg,
                            yaz_record_conv_get_error(p));
                    no_errors++;
                }
                else
                {
                    fwrite(wrbuf_buf(output_record), 1,
                           wrbuf_len(output_record), stdout);
                }
                wrbuf_destroy(input_record);
                wrbuf_destroy(output_record);
                fclose(f);
            }
            break;
#else
            fprintf(stderr, "%s: YAZ not compiled with Libxml2 support\n",
                prog);
            usage();
            break;
#endif
        default:
            usage();
        }
    }
#if YAZ_HAVE_XML2
    yaz_record_conv_destroy(p);
#endif
    if (no_errors)
        exit(1);
    exit(0);
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */