Added body-of-text to BIB-1 ANY and the WAIS profile
[yaz-moved-to-github.git] / retrieval / d1_sutrs.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_sutrs.c,v $
7  * Revision 1.3  1995-12-15 16:57:11  quinn
8  * Added formatted-text.
9  *
10  * Revision 1.2  1995/11/01  13:54:49  quinn
11  * Minor adjustments
12  *
13  * Revision 1.1  1995/11/01  11:56:09  quinn
14  * Added Retrieval (data management) functions en masse.
15  *
16  *
17  */
18
19 #include <ctype.h>
20
21 #include <wrbuf.h>
22
23 #include <data1.h>
24
25 #define NTOBUF_INDENT   2
26 #define NTOBUF_MARGIN 75
27
28 static int wordlen(char *b)
29 {
30     int l = 0;
31
32     while (*b && !isspace(*b))
33         l++, b++;
34     return l;
35 }
36
37 static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
38 {
39     data1_node *c;
40     char line[1024];
41
42     for (c = n->child; c; c = c->next)
43     {
44         char *tag;
45
46         if (c->which == DATA1N_tag)
47         {
48             if (select && !c->u.tag.node_selected)
49                 continue;
50             if (c->u.tag.element && c->u.tag.element->tag)
51                 tag = c->u.tag.element->tag->names->name; /* first name */
52             else
53                 tag = c->u.tag.tag; /* local string tag */
54             if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
55             {
56                 if (col)
57                     wrbuf_putc(b, '\n');
58                 sprintf(line, "%*s%s:", indent * NTOBUF_INDENT, "", tag);
59                 wrbuf_write(b, line, strlen(line));
60                 col = strlen(line);
61             }
62             if (nodetobuf(c, select, b, indent+1, col) < 0)
63                 return 0;
64         }
65         else if (c->which == DATA1N_data)
66         {
67             char *p = c->u.data.data;
68             int l = c->u.data.len;
69             int first = 0;
70
71             if (c->u.data.what == DATA1I_text && c->u.data.formatted_text)
72             {
73                 wrbuf_putc(b, '\n');
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;
78             }
79             else if (c->u.data.what == DATA1I_text)
80             {
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)) > NTOBUF_MARGIN && wlen <
91                         NTOBUF_MARGIN - indent * NTOBUF_INDENT)
92                     {
93                         sprintf(line, "\n%*s", indent * NTOBUF_INDENT, "");
94                         wrbuf_write(b, line, strlen(line));
95                         col = indent * NTOBUF_INDENT;
96                         first = 1;
97                     }
98                     if (!first)
99                     {
100                         wrbuf_putc(b, ' ');
101                         col++;
102                     }
103                     while (l && !isspace(*p))
104                     {
105                         if (col > NTOBUF_MARGIN)
106                         {
107                             wrbuf_putc(b, '=');
108                             wrbuf_putc(b, '\n');
109                             sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
110                             wrbuf_write(b, line, strlen(line));
111                             col = indent * NTOBUF_INDENT;
112                         }
113                         wrbuf_putc(b, *p);
114                         p++;
115                         l--;
116                         col++;
117                     }
118                     first = 0;
119                 }
120             }
121             else if (c->u.data.what == DATA1I_num)
122             {
123                 wrbuf_putc(b, ' ');
124                 wrbuf_write(b, c->u.data.data, c->u.data.len);
125             }
126         }
127     }
128     return 0;
129 }
130
131 /*
132  * Return area containing SUTRS-formatted data. Ownership of this data
133  * remains in this module, and the buffer is reused on next call. This may
134  * need changing.
135  */
136
137 char *data1_nodetobuf(data1_node *n, int select, int *len)
138 {
139     static WRBUF b = 0;
140
141     if (!b)
142         b = wrbuf_alloc();
143     else
144         wrbuf_rewind(b);
145     if (nodetobuf(n, select, b, 0, 0))
146         return 0;
147     wrbuf_putc(b, '\n');
148     *len = wrbuf_len(b);
149     return wrbuf_buf(b);
150 }