Updated.
[yaz-moved-to-github.git] / retrieval / d1_sutrs.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_sutrs.c,v $
7  * Revision 1.4  1997-09-17 12:10:38  adam
8  * YAZ version 1.4.
9  *
10  * Revision 1.3  1995/12/15 16:57:11  quinn
11  * Added formatted-text.
12  *
13  * Revision 1.2  1995/11/01  13:54:49  quinn
14  * Minor adjustments
15  *
16  * Revision 1.1  1995/11/01  11:56:09  quinn
17  * Added Retrieval (data management) functions en masse.
18  *
19  *
20  */
21
22 #include <ctype.h>
23
24 #include <data1.h>
25
26 #define NTOBUF_INDENT   2
27 #define NTOBUF_MARGIN 75
28
29 static int wordlen(char *b)
30 {
31     int l = 0;
32
33     while (*b && !isspace(*b))
34         l++, b++;
35     return l;
36 }
37
38 static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
39 {
40     data1_node *c;
41     char line[1024];
42
43     for (c = n->child; c; c = c->next)
44     {
45         char *tag;
46
47         if (c->which == DATA1N_tag)
48         {
49             if (select && !c->u.tag.node_selected)
50                 continue;
51             if (c->u.tag.element && c->u.tag.element->tag)
52                 tag = c->u.tag.element->tag->names->name; /* first name */
53             else
54                 tag = c->u.tag.tag; /* local string tag */
55             if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
56             {
57                 if (col)
58                     wrbuf_putc(b, '\n');
59                 sprintf(line, "%*s%s:", indent * NTOBUF_INDENT, "", tag);
60                 wrbuf_write(b, line, strlen(line));
61                 col = strlen(line);
62             }
63             if (nodetobuf(c, select, b, indent+1, col) < 0)
64                 return 0;
65         }
66         else if (c->which == DATA1N_data)
67         {
68             char *p = c->u.data.data;
69             int l = c->u.data.len;
70             int first = 0;
71
72             if (c->u.data.what == DATA1I_text && c->u.data.formatted_text)
73             {
74                 wrbuf_putc(b, '\n');
75                 wrbuf_write(b, c->u.data.data, c->u.data.len);
76                 sprintf(line, "%*s", indent * NTOBUF_INDENT, "");
77                 wrbuf_write(b, line, strlen(line));
78                 col = indent * NTOBUF_INDENT;
79             }
80             else if (c->u.data.what == DATA1I_text)
81             {
82                 while (l)
83                 {
84                     int wlen;
85
86                     while (l && isspace(*p))
87                         p++, l--;
88                     if (!l)
89                         break;
90                     /* break if we'll cross margin and word is not too long */
91                     if (col + (wlen = wordlen(p)) > NTOBUF_MARGIN && wlen <
92                         NTOBUF_MARGIN - indent * NTOBUF_INDENT)
93                     {
94                         sprintf(line, "\n%*s", indent * NTOBUF_INDENT, "");
95                         wrbuf_write(b, line, strlen(line));
96                         col = indent * NTOBUF_INDENT;
97                         first = 1;
98                     }
99                     if (!first)
100                     {
101                         wrbuf_putc(b, ' ');
102                         col++;
103                     }
104                     while (l && !isspace(*p))
105                     {
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                         wrbuf_putc(b, *p);
115                         p++;
116                         l--;
117                         col++;
118                     }
119                     first = 0;
120                 }
121             }
122             else if (c->u.data.what == DATA1I_num)
123             {
124                 wrbuf_putc(b, ' ');
125                 wrbuf_write(b, c->u.data.data, c->u.data.len);
126             }
127         }
128     }
129     return 0;
130 }
131
132 /*
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
135  * need changing.
136  */
137
138 char *data1_nodetobuf (data1_handle dh, data1_node *n, int select, int *len)
139 {
140     WRBUF b = data1_get_wrbuf (dh);
141
142     wrbuf_rewind(b);
143     if (nodetobuf(n, select, b, 0, 0))
144         return 0;
145     wrbuf_putc(b, '\n');
146     *len = wrbuf_len(b);
147     return wrbuf_buf(b);
148 }