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