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