6411064cdc8c28fa58a5af5c936182cec41a66a9
[idzebra-moved-to-github.git] / data1 / d1_soif.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 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 #include <yaz/wrbuf.h>
28 #include <idzebra/data1.h>
29
30 static int nodetoelement(data1_node *n, int select, char *prefix, WRBUF b)
31 {
32     data1_node *c;
33     char tmp[1024];
34
35     for (c = n->child; c; c = c->next)
36     {
37         char *tag;
38
39         if (c->which == DATA1N_tag)
40         {
41             if (select && !c->u.tag.node_selected)
42                 continue;
43             if (c->u.tag.element && c->u.tag.element->tag)
44                 tag = c->u.tag.element->tag->names->name; /* first name */
45             else
46             tag = c->u.tag.tag; /* local string tag */
47
48             if (*prefix)
49                 sprintf(tmp, "%s-%s", prefix, tag);
50             else
51                 strcpy(tmp, tag);
52
53             if (nodetoelement(c, select, tmp, b) < 0)
54                 return 0;
55         }
56         else if (c->which == DATA1N_data)
57         {
58             char *p = c->u.data.data;
59             int l = c->u.data.len;
60
61             wrbuf_write(b, prefix, strlen(prefix));
62
63             sprintf(tmp, "{%d}:\t", l);
64             wrbuf_write(b, tmp, strlen(tmp));
65             wrbuf_write(b, p, l);
66             wrbuf_putc(b, '\n');
67         }
68     }
69     return 0;
70 }
71
72 char *data1_nodetosoif (data1_handle dh, data1_node *n, int select, int *len)
73 {
74     WRBUF b = data1_get_wrbuf (dh);
75     char buf[128];
76
77     wrbuf_rewind(b);
78     
79     if (n->which != DATA1N_root)
80         return 0;
81     sprintf(buf, "@%s{\n", n->u.root.type);
82     wrbuf_write(b, buf, strlen(buf));
83     if (nodetoelement(n, select, "", b))
84         return 0;
85     wrbuf_write(b, "}\n", 2);
86     *len = wrbuf_len(b);
87     return wrbuf_buf(b);
88 }
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * indent-tabs-mode: nil
93  * End:
94  * vim: shiftwidth=4 tabstop=8 expandtab
95  */
96