XML reader for data1 (EXPAT)
[yaz-moved-to-github.git] / retrieval / d1_write.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: d1_write.c,v 1.11 2002-05-13 14:13:37 adam Exp $
7  */
8
9 #include <string.h>
10
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
13
14 #define IDSGML_MARGIN 75
15
16 static int wordlen(char *b, int max)
17 {
18     int l = 0;
19
20     while (l < max && !d1_isspace(*b))
21         l++, b++;
22     return l;
23 }
24
25 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
26 {
27     data1_node *c;
28     char line[1024];
29
30     for (c = n->child; c; c = c->next)
31     {
32         char *tag;
33
34         if (c->which == DATA1N_tag)
35         {
36             if (select && c->u.tag.node_selected)
37                 continue;
38             tag = c->u.tag.tag;
39             if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
40             {
41                 if (nodetoidsgml(c, select, b, col) < 0)
42                     return -1;
43             }
44             else
45             {
46                 data1_xattr *p;
47
48                 sprintf (line, "%*s<", col, "");
49                 wrbuf_puts (b, line);
50                 wrbuf_puts (b, tag);
51                 for (p = c->u.tag.attributes; p; p = p->next)
52                 {
53                     wrbuf_putc (b, ' ');
54                     wrbuf_puts (b, p->name);
55                     wrbuf_putc (b, '=');
56                     wrbuf_putc (b, '"');
57                     wrbuf_puts (b, p->value);
58                     wrbuf_putc (b, '"');
59                 }
60                 wrbuf_puts(b, ">\n");
61                 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
62                     return -1;
63                 sprintf (line, "%*s</%s>\n", col, "", tag);
64                 wrbuf_write(b, line, strlen(line));
65             }
66         }
67         else if (c->which == DATA1N_data)
68         {
69             char *p = c->u.data.data;
70             int l = c->u.data.len;
71             int first = 1;
72             int lcol = col;
73
74             sprintf(line, "%*s", col, "");
75             wrbuf_write(b, line, strlen(line));
76             switch (c->u.data.what)
77             {
78             case DATA1I_text:
79                 while (l)
80                 {
81                     int wlen;
82                     
83                     while (l && d1_isspace(*p))
84                         p++, l--;
85                     if (!l)
86                         break;
87                     /* break if we'll cross margin and word is not too long */
88                     if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN && wlen <
89                         IDSGML_MARGIN)
90                     {
91                         sprintf(line, "\n%*s", col, "");
92                         lcol = col;
93                         wrbuf_write(b, line, strlen(line));
94                         first = 1;
95                     }
96                     if (!first)
97                     {
98                         wrbuf_putc(b, ' ');
99                         lcol++;
100                     }
101                     while (l && !d1_isspace(*p))
102                     {
103                         wrbuf_putc(b, *p);
104                         p++;
105                         l--;
106                         lcol++;
107                     }
108                     first = 0;
109                 }
110                 wrbuf_write(b, "\n", 1);
111                 break;
112             case DATA1I_num:
113                 wrbuf_write(b, c->u.data.data, c->u.data.len);
114                 break;
115             case DATA1I_oid:
116                 wrbuf_write(b, c->u.data.data, c->u.data.len);
117             }
118         }
119     }
120     return 0;
121 }
122
123 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
124 {
125     WRBUF b = data1_get_wrbuf (dh);
126     char line[1024];
127     
128     wrbuf_rewind(b);
129     
130     sprintf(line, "<%s>\n", n->u.root.type);
131     wrbuf_write(b, line, strlen(line));
132     if (nodetoidsgml(n, select, b, 0))
133         return 0;
134     sprintf(line, "</%s>\n", n->u.root.type);
135     wrbuf_write(b, line, strlen(line));
136     *len = wrbuf_len(b);
137     return wrbuf_buf(b);
138 }