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