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