1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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 */
22 #include <idzebra/data1.h>
24 #define NTOBUF_INDENT 2
25 #define NTOBUF_MARGIN 75
27 static int wordlen(char *b, int i)
31 while (i && !d1_isspace(*b))
36 static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
41 for (c = n->child; c; c = c->next)
45 if (c->which == DATA1N_tag)
47 if (select && !c->u.tag.node_selected)
49 if (c->u.tag.element && c->u.tag.element->tag)
50 tag = c->u.tag.element->tag->names->name; /* first name */
52 tag = c->u.tag.tag; /* local string tag */
53 if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
57 sprintf(line, "%*s%s:", indent * NTOBUF_INDENT, "", tag);
58 wrbuf_write(b, line, strlen(line));
61 if (nodetobuf(c, select, b, indent+1, col) < 0)
64 else if (c->which == DATA1N_data)
66 char *p = c->u.data.data;
67 int l = c->u.data.len;
70 if ((c->u.data.what == DATA1I_text ||
71 c->u.data.what == DATA1I_xmltext) && c->u.data.formatted_text)
74 wrbuf_write(b, c->u.data.data, c->u.data.len);
75 sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
76 wrbuf_write(b, line, strlen(line));
77 col = indent * NTOBUF_INDENT;
79 else if (c->u.data.what == DATA1I_text ||
80 c->u.data.what == DATA1I_xmltext)
86 while (l && d1_isspace(*p))
90 /* break if we'll cross margin and word is not too long */
91 if (col + (wlen = wordlen(p, l)) > NTOBUF_MARGIN && wlen <
92 NTOBUF_MARGIN - indent * NTOBUF_INDENT)
94 sprintf(line, "\n%*s", indent * NTOBUF_INDENT, "");
95 wrbuf_write(b, line, strlen(line));
96 col = indent * NTOBUF_INDENT;
104 while (l && !d1_isspace(*p))
106 if (col > NTOBUF_MARGIN)
110 sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
111 wrbuf_write(b, line, strlen(line));
112 col = indent * NTOBUF_INDENT;
122 else if (c->u.data.what == DATA1I_num)
125 wrbuf_write(b, c->u.data.data, c->u.data.len);
133 * Return area containing SUTRS-formatted data. Ownership of this data
134 * remains in this module, and the buffer is reused on next call. This may
138 char *data1_nodetobuf (data1_handle dh, data1_node *n, int select, int *len)
140 WRBUF b = data1_get_wrbuf (dh);
143 if (nodetobuf(n, select, b, 0, 0))
152 * c-file-style: "Stroustrup"
153 * indent-tabs-mode: nil
155 * vim: shiftwidth=4 tabstop=8 expandtab