1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* converts data1 tree to SUTRS record */
25 #include <idzebra/data1.h>
27 #define NTOBUF_INDENT 2
28 #define NTOBUF_MARGIN 75
30 static int wordlen(char *b, int i)
34 while (i && !d1_isspace(*b))
39 static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
44 for (c = n->child; c; c = c->next)
48 if (c->which == DATA1N_tag)
50 if (select && !c->u.tag.node_selected)
52 if (c->u.tag.element && c->u.tag.element->tag)
53 tag = c->u.tag.element->tag->names->name; /* first name */
55 tag = c->u.tag.tag; /* local string tag */
56 if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
60 sprintf(line, "%*s%s:", indent * NTOBUF_INDENT, "", tag);
61 wrbuf_write(b, line, strlen(line));
64 if (nodetobuf(c, select, b, indent+1, col) < 0)
67 else if (c->which == DATA1N_data)
69 char *p = c->u.data.data;
70 int l = c->u.data.len;
73 if ((c->u.data.what == DATA1I_text ||
74 c->u.data.what == DATA1I_xmltext) && c->u.data.formatted_text)
77 wrbuf_write(b, c->u.data.data, c->u.data.len);
78 sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
79 wrbuf_write(b, line, strlen(line));
80 col = indent * NTOBUF_INDENT;
82 else if (c->u.data.what == DATA1I_text ||
83 c->u.data.what == DATA1I_xmltext)
89 while (l && d1_isspace(*p))
93 /* break if we'll cross margin and word is not too long */
94 if (col + (wlen = wordlen(p, l)) > NTOBUF_MARGIN && wlen <
95 NTOBUF_MARGIN - indent * NTOBUF_INDENT)
97 sprintf(line, "\n%*s", indent * NTOBUF_INDENT, "");
98 wrbuf_write(b, line, strlen(line));
99 col = indent * NTOBUF_INDENT;
107 while (l && !d1_isspace(*p))
109 if (col > NTOBUF_MARGIN)
113 sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
114 wrbuf_write(b, line, strlen(line));
115 col = indent * NTOBUF_INDENT;
125 else if (c->u.data.what == DATA1I_num)
128 wrbuf_write(b, c->u.data.data, c->u.data.len);
136 * Return area containing SUTRS-formatted data. Ownership of this data
137 * remains in this module, and the buffer is reused on next call. This may
141 char *data1_nodetobuf (data1_handle dh, data1_node *n, int select, int *len)
143 WRBUF b = data1_get_wrbuf (dh);
146 if (nodetobuf(n, select, b, 0, 0))
155 * c-file-style: "Stroustrup"
156 * indent-tabs-mode: nil
158 * vim: shiftwidth=4 tabstop=8 expandtab