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