Added SOIF syntax.
[yaz-moved-to-github.git] / retrieval / d1_soif.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_soif.c,v $
7  * Revision 1.1  1996-10-08 10:43:20  quinn
8  * Added SOIF syntax.
9  *
10  *
11  */
12
13 #include <wrbuf.h>
14
15 #include <data1.h>
16
17 /*
18  * This module generates SOIF (Simple Object Interchange Format) records
19  * from d1-nodes. nested elements are flattened out, depth first, by
20  * concatenating the tag names at each level.
21  */
22
23 static int nodetoelement(data1_node *n, int select, char *prefix, WRBUF b)
24 {
25     data1_node *c;
26     char tmp[1024];
27
28     for (c = n->child; c; c = c->next)
29     {
30         char *tag;
31
32         if (c->which == DATA1N_tag)
33         {
34             if (select && !c->u.tag.node_selected)
35                 continue;
36             if (c->u.tag.element && c->u.tag.element->tag)
37                 tag = c->u.tag.element->tag->names->name; /* first name */
38             else
39             tag = c->u.tag.tag; /* local string tag */
40
41             if (*prefix)
42                 sprintf(tmp, "%s-%s", prefix, tag);
43             else
44                 strcpy(tmp, tag);
45
46             if (nodetoelement(c, select, tmp, b) < 0)
47                 return 0;
48         }
49         else if (c->which == DATA1N_data)
50         {
51             char *p = c->u.data.data;
52             int l = c->u.data.len;
53
54             wrbuf_write(b, prefix, strlen(prefix));
55
56             sprintf(tmp, "{%d}:\t", l);
57             wrbuf_write(b, tmp, strlen(tmp));
58             wrbuf_write(b, p, l);
59             wrbuf_putc(b, '\n');
60         }
61     }
62     return 0;
63 }
64
65 char *data1_nodetosoif(data1_node *n, int select, int *len)
66 {
67     static WRBUF b = 0;
68     char buf[128];
69
70     if (!b)
71         b = wrbuf_alloc();
72     else
73         wrbuf_rewind(b);
74     
75     if (n->which != DATA1N_root)
76         return 0;
77     sprintf(buf, "@%s{\n", n->u.root.type);
78     wrbuf_write(b, buf, strlen(buf));
79     if (nodetoelement(n, select, "", b))
80         return 0;
81     wrbuf_write(b, "}\n", 2);
82     *len = wrbuf_len(b);
83     return wrbuf_buf(b);
84 }