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