4d4bc2a867a1c28ebc31187a4964ef232d4c17b3
[idzebra-moved-to-github.git] / data1 / d1_soif.c
1 /* $Id: d1_soif.c,v 1.3 2004-09-28 10:15:03 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 /*
25  * This module generates SOIF (Simple Object Interchange Format) records
26  * from d1-nodes. nested elements are flattened out, depth first, by
27  * concatenating the tag names at each level.
28  */
29
30 #include <yaz/wrbuf.h>
31 #include <idzebra/data1.h>
32
33 static int nodetoelement(data1_node *n, int select, char *prefix, WRBUF b)
34 {
35     data1_node *c;
36     char tmp[1024];
37
38     for (c = n->child; c; c = c->next)
39     {
40         char *tag;
41
42         if (c->which == DATA1N_tag)
43         {
44             if (select && !c->u.tag.node_selected)
45                 continue;
46             if (c->u.tag.element && c->u.tag.element->tag)
47                 tag = c->u.tag.element->tag->names->name; /* first name */
48             else
49             tag = c->u.tag.tag; /* local string tag */
50
51             if (*prefix)
52                 sprintf(tmp, "%s-%s", prefix, tag);
53             else
54                 strcpy(tmp, tag);
55
56             if (nodetoelement(c, select, tmp, b) < 0)
57                 return 0;
58         }
59         else if (c->which == DATA1N_data)
60         {
61             char *p = c->u.data.data;
62             int l = c->u.data.len;
63
64             wrbuf_write(b, prefix, strlen(prefix));
65
66             sprintf(tmp, "{%d}:\t", l);
67             wrbuf_write(b, tmp, strlen(tmp));
68             wrbuf_write(b, p, l);
69             wrbuf_putc(b, '\n');
70         }
71     }
72     return 0;
73 }
74
75 char *data1_nodetosoif (data1_handle dh, data1_node *n, int select, int *len)
76 {
77     WRBUF b = data1_get_wrbuf (dh);
78     char buf[128];
79
80     wrbuf_rewind(b);
81     
82     if (n->which != DATA1N_root)
83         return 0;
84     sprintf(buf, "@%s{\n", n->u.root.type);
85     wrbuf_write(b, buf, strlen(buf));
86     if (nodetoelement(n, select, "", b))
87         return 0;
88     wrbuf_write(b, "}\n", 2);
89     *len = wrbuf_len(b);
90     return wrbuf_buf(b);
91 }