51f2ff37c234d41755b4884bcd70e67d55444f5a
[yaz-moved-to-github.git] / retrieval / d1_write.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: d1_write.c,v 1.10 2002-04-15 09:06:30 adam Exp $
7  */
8
9 #include <string.h>
10
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
13
14 #define IDSGML_MARGIN 75
15
16 static int wordlen(char *b, int max)
17 {
18     int l = 0;
19
20     while (l < max && !d1_isspace(*b))
21         l++, b++;
22     return l;
23 }
24
25 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
26 {
27     data1_node *c;
28     char line[1024];
29
30     for (c = n->child; c; c = c->next)
31     {
32         char *tag;
33
34         if (c->which == DATA1N_tag)
35         {
36             if (select && c->u.tag.node_selected)
37                 continue;
38             tag = c->u.tag.tag;
39             if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
40             {
41                 if (nodetoidsgml(c, select, b, col) < 0)
42                     return -1;
43             }
44             else
45             {
46 #if DATA1_USING_XATTR
47                 data1_xattr *p;
48 #endif
49                 sprintf (line, "%*s<", col, "");
50                 wrbuf_puts (b, line);
51                 wrbuf_puts (b, tag);
52 #if DATA1_USING_XATTR
53                 for (p = c->u.tag.attributes; p; p = p->next)
54                 {
55                     wrbuf_putc (b, ' ');
56                     wrbuf_puts (b, p->name);
57                     wrbuf_putc (b, '=');
58                     wrbuf_putc (b, '"');
59                     wrbuf_puts (b, p->value);
60                     wrbuf_putc (b, '"');
61                 }
62 #endif
63                 wrbuf_puts(b, ">\n");
64                 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
65                     return -1;
66                 sprintf (line, "%*s</%s>\n", col, "", tag);
67                 wrbuf_write(b, line, strlen(line));
68             }
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 = 1;
75             int lcol = col;
76
77             sprintf(line, "%*s", col, "");
78             wrbuf_write(b, line, strlen(line));
79             switch (c->u.data.what)
80             {
81             case DATA1I_text:
82                 while (l)
83                 {
84                     int wlen;
85                     
86                     while (l && d1_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 (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN && wlen <
92                         IDSGML_MARGIN)
93                     {
94                         sprintf(line, "\n%*s", col, "");
95                         lcol = col;
96                         wrbuf_write(b, line, strlen(line));
97                         first = 1;
98                     }
99                     if (!first)
100                     {
101                         wrbuf_putc(b, ' ');
102                         lcol++;
103                     }
104                     while (l && !d1_isspace(*p))
105                     {
106                         wrbuf_putc(b, *p);
107                         p++;
108                         l--;
109                         lcol++;
110                     }
111                     first = 0;
112                 }
113                 wrbuf_write(b, "\n", 1);
114                 break;
115             case DATA1I_num:
116                 wrbuf_write(b, c->u.data.data, c->u.data.len);
117                 break;
118             case DATA1I_oid:
119                 wrbuf_write(b, c->u.data.data, c->u.data.len);
120             }
121         }
122     }
123     return 0;
124 }
125
126 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
127 {
128     WRBUF b = data1_get_wrbuf (dh);
129     char line[1024];
130     
131     wrbuf_rewind(b);
132     
133     sprintf(line, "<%s>\n", n->u.root.type);
134     wrbuf_write(b, line, strlen(line));
135     if (nodetoidsgml(n, select, b, 0))
136         return 0;
137     sprintf(line, "</%s>\n", n->u.root.type);
138     wrbuf_write(b, line, strlen(line));
139     *len = wrbuf_len(b);
140     return wrbuf_buf(b);
141 }