2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: d1_write.c,v 1.10 2002-04-15 09:06:30 adam Exp $
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
14 #define IDSGML_MARGIN 75
16 static int wordlen(char *b, int max)
20 while (l < max && !d1_isspace(*b))
25 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
30 for (c = n->child; c; c = c->next)
34 if (c->which == DATA1N_tag)
36 if (select && c->u.tag.node_selected)
39 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
41 if (nodetoidsgml(c, select, b, col) < 0)
49 sprintf (line, "%*s<", col, "");
53 for (p = c->u.tag.attributes; p; p = p->next)
56 wrbuf_puts (b, p->name);
59 wrbuf_puts (b, p->value);
64 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
66 sprintf (line, "%*s</%s>\n", col, "", tag);
67 wrbuf_write(b, line, strlen(line));
70 else if (c->which == DATA1N_data)
72 char *p = c->u.data.data;
73 int l = c->u.data.len;
77 sprintf(line, "%*s", col, "");
78 wrbuf_write(b, line, strlen(line));
79 switch (c->u.data.what)
86 while (l && d1_isspace(*p))
90 /* break if we'll cross margin and word is not too long */
91 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN && wlen <
94 sprintf(line, "\n%*s", col, "");
96 wrbuf_write(b, line, strlen(line));
104 while (l && !d1_isspace(*p))
113 wrbuf_write(b, "\n", 1);
116 wrbuf_write(b, c->u.data.data, c->u.data.len);
119 wrbuf_write(b, c->u.data.data, c->u.data.len);
126 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
128 WRBUF b = data1_get_wrbuf (dh);
133 sprintf(line, "<%s>\n", n->u.root.type);
134 wrbuf_write(b, line, strlen(line));
135 if (nodetoidsgml(n, select, b, 0))
137 sprintf(line, "</%s>\n", n->u.root.type);
138 wrbuf_write(b, line, strlen(line));