Character set negotiation updates
[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.15 2002-07-25 12:52:53 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 #define PRETTY_FORMAT 0
17
18 static int wordlen(char *b, int max)
19 {
20     int l = 0;
21
22     while (l < max && !d1_isspace(*b))
23         l++, b++;
24     return l;
25 }
26
27 static void indent (WRBUF b, int col)
28 {
29     int i;
30     for (i = 0; i<col; i++)
31         wrbuf_putc (b, ' ');
32 }
33
34 static void wrbuf_write_cdata(WRBUF b, const char *msg, int len)
35 {
36     int i;
37
38     for (i = 0; i < len; i++)
39     {
40         switch (msg[i])
41         {
42         case '"':
43             wrbuf_puts (b, "&quot;");
44             break;
45         case '>':
46             wrbuf_puts (b, "&gt;");
47             break;
48         case '<':
49             wrbuf_puts (b, "&lt;");
50             break;
51 #if 0
52         case '&':
53             wrbuf_puts (b, "&amp;");
54             break;
55 #endif
56         default:
57             wrbuf_putc(b, msg[i]);
58         }
59     }
60 }
61
62 static void wrbuf_put_cdata(WRBUF b, const char *msg)
63 {
64     wrbuf_write_cdata (b, msg, strlen(msg));
65 }
66
67 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
68                         int pretty_format)
69 {
70     data1_node *c;
71
72     for (c = n->child; c; c = c->next)
73     {
74         char *tag;
75
76         if (c->which == DATA1N_preprocess)
77         {
78             data1_xattr *p;
79
80             if (pretty_format)
81                 indent (b, col);
82             wrbuf_puts (b, "<?");
83             wrbuf_put_cdata (b, c->u.preprocess.target);
84             for (p = c->u.preprocess.attributes; p; p = p->next)
85             {
86                 wrbuf_putc (b, ' ');
87                 wrbuf_put_cdata (b, p->name);
88                 wrbuf_putc (b, '=');
89                 wrbuf_putc (b, '"');
90                 wrbuf_put_cdata (b, p->value);
91                 wrbuf_putc (b, '"');
92             }
93             if (c->child)
94                 wrbuf_puts(b, " ");
95             if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
96                              pretty_format) < 0)
97                 return -1;
98             wrbuf_puts (b, "?>\n");
99         }
100         else if (c->which == DATA1N_tag)
101         {
102             if (select && c->u.tag.node_selected)
103                 continue;
104             tag = c->u.tag.tag;
105             if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
106             {
107                 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
108                     return -1;
109             }
110             else
111             {
112                 data1_xattr *p;
113
114                 if (pretty_format)
115                     indent (b, col);
116                 wrbuf_puts (b, "<");    
117                 wrbuf_put_cdata (b, tag);
118                 for (p = c->u.tag.attributes; p; p = p->next)
119                 {
120                     wrbuf_putc (b, ' ');
121                     wrbuf_put_cdata (b, p->name);
122                     wrbuf_putc (b, '=');
123                     wrbuf_putc (b, '"');
124                     wrbuf_put_cdata (b, p->value);
125                     wrbuf_putc (b, '"');
126                 }
127                 wrbuf_puts(b, ">");
128                 if (pretty_format)
129                     wrbuf_puts(b, "\n");
130                 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
131                                  pretty_format) < 0)
132                     return -1;
133                 if (pretty_format)
134                     indent (b, col);
135                 wrbuf_puts(b, "</");
136                 wrbuf_put_cdata(b, tag);
137                 wrbuf_puts(b, ">");
138                 if (pretty_format)
139                     wrbuf_puts (b, "\n");
140             }
141         }
142         else if (c->which == DATA1N_data || c->which == DATA1N_comment)
143         {
144             char *p = c->u.data.data;
145             int l = c->u.data.len;
146             int first = 1;
147             int lcol = col;
148
149             if (pretty_format && !c->u.data.formatted_text)
150                 indent (b, col);
151             if (c->which == DATA1N_comment)
152                 wrbuf_puts (b, "<!--");
153             switch (c->u.data.what)
154             {
155             case DATA1I_text:
156                 if (!pretty_format || c->u.data.formatted_text)
157                 {
158                     wrbuf_write_cdata (b, p, l);
159                 }
160                 else
161                 {
162                     while (l)
163                     {
164                         int wlen;
165                         
166                         while (l && d1_isspace(*p))
167                             p++, l--;
168                         if (!l)
169                             break;
170                         /* break if we cross margin and word is not too long */
171                         if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
172                             wlen < IDSGML_MARGIN)
173                         {
174                             wrbuf_puts (b, "\n");
175                             indent (b, col);
176                             lcol = col;
177                             first = 1;
178                         }
179                         if (!first)
180                         {
181                             wrbuf_putc(b, ' ');
182                             lcol++;
183                         }
184                         while (l && !d1_isspace(*p))
185                         {
186                             wrbuf_putc(b, *p);
187                             p++;
188                             l--;
189                             lcol++;
190                         }
191                         first = 0;
192                     }
193                     wrbuf_puts(b, "\n");
194                 }
195                 break;
196             case DATA1I_num:
197                 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
198                 if (pretty_format)
199                     wrbuf_puts(b, "\n");
200                 break;
201             case DATA1I_oid:
202                 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
203                 if (pretty_format)
204                     wrbuf_puts(b, "\n");
205             }
206             if (c->which == DATA1N_comment)
207             {
208                 wrbuf_puts(b, "-->");
209                 if (pretty_format)
210                     wrbuf_puts(b, "\n");
211             }
212         }
213     }
214     return 0;
215 }
216
217 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
218 {
219     WRBUF b = data1_get_wrbuf (dh);
220     
221     wrbuf_rewind(b);
222     
223     if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
224         return 0;
225     *len = wrbuf_len(b);
226     return wrbuf_buf(b);
227 }