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.12 2002-05-21 07:43:16 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)
48 sprintf (line, "%*s<", col, "");
51 for (p = c->u.tag.attributes; p; p = p->next)
54 wrbuf_puts (b, p->name);
57 wrbuf_puts (b, p->value);
61 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
63 sprintf (line, "%*s</%s>\n", col, "", tag);
64 wrbuf_write(b, line, strlen(line));
67 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
69 char *p = c->u.data.data;
70 int l = c->u.data.len;
74 if (!c->u.data.formatted_text)
76 sprintf(line, "%*s", col, "");
77 wrbuf_write(b, line, strlen(line));
79 if (c->which == DATA1N_comment)
81 wrbuf_write (b, "<!--", 4);
83 switch (c->u.data.what)
86 if (c->u.data.formatted_text)
88 wrbuf_write (b, p, l);
96 while (l && d1_isspace(*p))
100 /* break if we cross margin and word is not too long */
101 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
102 wlen < IDSGML_MARGIN)
104 sprintf(line, "\n%*s", col, "");
106 wrbuf_write(b, line, strlen(line));
114 while (l && !d1_isspace(*p))
123 wrbuf_write(b, "\n", 1);
127 wrbuf_write(b, c->u.data.data, c->u.data.len);
130 wrbuf_write(b, c->u.data.data, c->u.data.len);
132 if (c->which == DATA1N_comment)
134 wrbuf_write (b, "-->", 3);
141 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
143 WRBUF b = data1_get_wrbuf (dh);
148 sprintf(line, "<%s>\n", n->u.root.type);
149 wrbuf_write(b, line, strlen(line));
150 if (nodetoidsgml(n, select, b, 0))
152 sprintf(line, "</%s>\n", n->u.root.type);
153 wrbuf_write(b, line, strlen(line));