IDZEBRA 2.2.8
d1_soif.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20
21/*
22 * This module generates SOIF (Simple Object Interchange Format) records
23 * from d1-nodes. nested elements are flattened out, depth first, by
24 * concatenating the tag names at each level.
25 */
26
27#if HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <yaz/wrbuf.h>
31#include <yaz/snprintf.h>
32#include <idzebra/data1.h>
33
34static int nodetoelement(data1_node *n, int select, const char *prefix, WRBUF b)
35{
36 data1_node *c;
37 char tmp[1024];
38
39 for (c = n->child; c; c = c->next)
40 {
41 char *tag;
42
43 if (c->which == DATA1N_tag)
44 {
45 if (select && !c->u.tag.node_selected)
46 continue;
47 if (c->u.tag.element && c->u.tag.element->tag)
48 tag = c->u.tag.element->tag->names->name; /* first name */
49 else
50 tag = c->u.tag.tag; /* local string tag */
51
52 if (prefix)
53 yaz_snprintf(tmp, sizeof(tmp), "%s-%s", prefix, tag);
54 else
55 yaz_snprintf(tmp, sizeof(tmp), "%s", tag);
56
57 if (nodetoelement(c, select, tmp, b) < 0)
58 return 0;
59 }
60 else if (c->which == DATA1N_data)
61 {
62 char *p = c->u.data.data;
63 int l = c->u.data.len;
64
65 wrbuf_puts(b, prefix);
66 wrbuf_printf(b, "{%d}:\t", l);
67 wrbuf_write(b, p, l);
68 wrbuf_putc(b, '\n');
69 }
70 }
71 return 0;
72}
73
74char *data1_nodetosoif(data1_handle dh, data1_node *n, int select, int *len)
75{
76 WRBUF b = data1_get_wrbuf(dh);
77
78 wrbuf_rewind(b);
79 if (n->which != DATA1N_root)
80 return 0;
81 wrbuf_printf(b, "@%s{\n", n->u.root.type);
82 if (nodetoelement(n, select, 0, b))
83 return 0;
84 wrbuf_puts(b, "}\n");
85 *len = wrbuf_len(b);
86 return wrbuf_buf(b);
87}
88/*
89 * Local variables:
90 * c-basic-offset: 4
91 * c-file-style: "Stroustrup"
92 * indent-tabs-mode: nil
93 * End:
94 * vim: shiftwidth=4 tabstop=8 expandtab
95 */
96
static int nodetoelement(data1_node *n, int select, const char *prefix, WRBUF b)
Definition d1_soif.c:34
char * data1_nodetosoif(data1_handle dh, data1_node *n, int select, int *len)
Definition d1_soif.c:74
WRBUF data1_get_wrbuf(data1_handle dp)
Definition d1_handle.c:102
#define DATA1N_tag
Definition data1.h:276
#define DATA1N_data
Definition data1.h:278
#define DATA1N_root
Definition data1.h:274
struct data1_node::@2::@3 root
char * type
Definition data1.h:290
struct data1_node * child
Definition data1.h:341
char * tag
Definition data1.h:296
char * data
Definition data1.h:307
struct data1_node * next
Definition data1.h:340
union data1_node::@2 u
int which
Definition data1.h:285