Fixed problem with function wordlen.
[yaz-moved-to-github.git] / retrieval / d1_write.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_write.c,v $
7  * Revision 1.5  1998-06-05 08:57:43  adam
8  * Fixed problem with function wordlen.
9  *
10  * Revision 1.4  1998/05/18 13:07:08  adam
11  * Changed the way attribute sets are handled by the retriaval module.
12  * Extended Explain conversion / schema.
13  * Modified server and client to work with ASN.1 compiled protocol handlers.
14  *
15  * Revision 1.3  1997/09/17 12:10:39  adam
16  * YAZ version 1.4.
17  *
18  * Revision 1.2  1995/12/13 17:14:27  quinn
19  * *** empty log message ***
20  *
21  * Revision 1.1  1995/12/13  15:38:43  quinn
22  * Added SGML-output filter.
23  *
24  *
25  */
26
27 #include <string.h>
28 #include <ctype.h>
29
30 #include <data1.h>
31 #include <wrbuf.h>
32
33 #define IDSGML_MARGIN 75
34
35 static int wordlen(char *b, int max)
36 {
37     int l = 0;
38
39     while (l < max && !isspace(*b))
40         l++, b++;
41     return l;
42 }
43
44 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
45 {
46     data1_node *c;
47     char line[1024];
48
49     for (c = n->child; c; c = c->next)
50     {
51         char *tag;
52
53         if (c->which == DATA1N_tag)
54         {
55             if (select && c->u.tag.node_selected)
56                 continue;
57             if (c->u.tag.element && c->u.tag.element->tag)
58                 tag = c->u.tag.element->tag->names->name; /* first name */
59             else
60                 tag = c->u.tag.tag; /* local string tag */
61             if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
62             {
63                 sprintf(line, "<%s>\n", tag);
64                 wrbuf_write(b, line, strlen(line));
65                 col = 0;
66             }
67             if (nodetoidsgml(c, select, b, col) < 0)
68                 return -1;
69             wrbuf_write(b, "</>\n", 4);
70             col = 0;
71         }
72         else if (c->which == DATA1N_data)
73         {
74             char *p = c->u.data.data;
75             int l = c->u.data.len;
76             int first = 1;
77
78             switch (c->u.data.what)
79             {
80             case DATA1I_text:
81                 while (l)
82                 {
83                     int wlen;
84
85                     while (l && isspace(*p))
86                         p++, l--;
87                     if (!l)
88                         break;
89                     /* break if we'll cross margin and word is not too long */
90                     if (col + (wlen = wordlen(p, l)) > IDSGML_MARGIN && wlen <
91                         IDSGML_MARGIN)
92                     {
93                         sprintf(line, "\n");
94                         col = 0;
95                         wrbuf_write(b, line, strlen(line));
96                         first = 1;
97                     }
98                     if (!first)
99                     {
100                         wrbuf_putc(b, ' ');
101                         col++;
102                     }
103                     while (l && !isspace(*p))
104                     {
105 #if 0
106                         if (col > NTOBUF_MARGIN)
107                         {
108                             wrbuf_putc(b, '=');
109                             wrbuf_putc(b, '\n');
110                             sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
111                             wrbuf_write(b, line, strlen(line));
112                             col = indent * NTOBUF_INDENT;
113                         }
114 #endif
115                         wrbuf_putc(b, *p);
116                         p++;
117                         l--;
118                         col++;
119                     }
120                     first = 0;
121                 }
122                 wrbuf_write(b, "\n", 1);
123                 col = 0;
124                 break;
125             case DATA1I_num:
126                 wrbuf_putc(b, ' ');
127                 wrbuf_write(b, c->u.data.data, c->u.data.len);
128                 break;
129             case DATA1I_oid:
130                 wrbuf_putc(b, ' ');
131                 wrbuf_write(b, c->u.data.data, c->u.data.len);
132             }
133         }
134     }
135     return 0;
136 }
137
138 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
139 {
140     WRBUF b = data1_get_wrbuf (dh);
141     char line[1024];
142
143     wrbuf_rewind(b);
144     
145     sprintf(line, "<%s>\n", n->u.root.type);
146     wrbuf_write(b, line, strlen(line));
147     if (nodetoidsgml(n, select, b, 0))
148         return 0;
149     sprintf(line, "</%s>\n", n->u.root.type);
150     wrbuf_write(b, line, strlen(line));
151     *len = wrbuf_len(b);
152     return wrbuf_buf(b);
153 }