sockets: fix for whether poll is available
[yaz-moved-to-github.git] / src / record_render.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file record_render.c
7  * \brief Render Z39.50 records (NamePlusRecord)
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include <yaz/marcdisp.h>
18 #include <yaz/record_render.h>
19 #include <yaz/yaz-iconv.h>
20 #include <yaz/proto.h>
21 #include <yaz/oid_db.h>
22
23 static yaz_iconv_t iconv_create_charset(const char *record_charset,
24                                         yaz_iconv_t *cd2)
25 {
26     char charset_buf[40];
27     yaz_iconv_t cd = 0;
28     char *from_set1 = 0;
29     char *from_set2 = 0;
30     char *to_set = 0;
31     if (record_charset && *record_charset)
32     {
33         char *cp = charset_buf;
34         
35         strncpy(charset_buf, record_charset, sizeof(charset_buf)-1);
36         charset_buf[sizeof(charset_buf)-1] = '\0';
37         
38         from_set1 = cp;
39         while (*cp && *cp != ',' && *cp != '/')
40             cp++;
41         if (*cp == '/')
42         {
43             *cp++ = '\0'; /* terminate from_set1 */
44             from_set2 = cp;
45             while (*cp && *cp != ',')
46                 cp++;
47         }
48         if (*cp == ',')
49         {
50             *cp++ = '\0';  /* terminate from_set1 or from_set2 */
51             to_set = cp;
52             while (*cp)
53                 cp++;
54         }
55     }
56     
57     if (from_set1)
58         cd = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set1);
59     if (cd2)
60     {
61         if (from_set2)
62             *cd2 = yaz_iconv_open(to_set ? to_set : "UTF-8", from_set2);
63         else
64             *cd2 = 0;
65     }
66     return cd;
67 }
68
69 static const char *return_marc_record(WRBUF wrbuf,
70                                       int marc_type,
71                                       int *len,
72                                       const char *buf, int sz,
73                                       const char *record_charset)
74 {
75     yaz_iconv_t cd = iconv_create_charset(record_charset, 0);
76     yaz_marc_t mt = yaz_marc_create();
77     const char *ret_string = 0;
78
79     if (cd)
80         yaz_marc_iconv(mt, cd);
81     yaz_marc_xml(mt, marc_type);
82     if (yaz_marc_decode_wrbuf(mt, buf, sz, wrbuf) > 0)
83     {
84         if (len)
85             *len = wrbuf_len(wrbuf);
86         ret_string = wrbuf_cstr(wrbuf);
87     }
88     yaz_marc_destroy(mt);
89     if (cd)
90         yaz_iconv_close(cd);
91     return ret_string;
92 }
93
94 static const char *return_opac_record(WRBUF wrbuf,
95                                       int marc_type,
96                                       int *len,
97                                       Z_OPACRecord *opac_rec,
98                                       const char *record_charset)
99 {
100     yaz_iconv_t cd2;
101     yaz_iconv_t cd = iconv_create_charset(record_charset, &cd2);
102     yaz_marc_t mt = yaz_marc_create();
103
104     if (cd)
105         yaz_marc_iconv(mt, cd);
106     yaz_marc_xml(mt, marc_type);
107
108     if (cd2)
109         yaz_opac_decode_wrbuf2(mt, opac_rec, wrbuf, cd2);
110     else
111         yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf);
112         
113     yaz_marc_destroy(mt);
114
115     if (cd)
116         yaz_iconv_close(cd);
117     if (cd2)
118         yaz_iconv_close(cd2);
119     if (len)
120         *len = wrbuf_len(wrbuf);
121     return wrbuf_cstr(wrbuf);
122 }
123
124 static const char *return_string_record(WRBUF wrbuf,
125                                         int *len,
126                                         const char *buf, int sz,
127                                         const char *record_charset)
128 {
129     yaz_iconv_t cd = iconv_create_charset(record_charset, 0);
130
131     if (cd)
132     {
133         wrbuf_iconv_write(wrbuf, cd, buf, sz);
134         wrbuf_iconv_reset(wrbuf, cd);
135
136         buf = wrbuf_cstr(wrbuf);
137         sz = wrbuf_len(wrbuf);
138         yaz_iconv_close(cd);
139     }
140     if (len)
141         *len = sz;
142     return buf;
143 }
144
145 static const char *return_record_wrbuf(WRBUF wrbuf, int *len,
146                                        Z_NamePlusRecord *npr,
147                                        int marctype, const char *charset)
148 {
149     Z_External *r = (Z_External *) npr->u.databaseRecord;
150     const Odr_oid *oid = r->direct_reference;
151
152     wrbuf_rewind(wrbuf);
153     /* render bibliographic record .. */
154     if (r->which == Z_External_OPAC)
155     {
156         return return_opac_record(wrbuf, marctype, len,
157                                   r->u.opac, charset);
158     }
159     if (r->which == Z_External_sutrs)
160         return return_string_record(wrbuf, len,
161                                     (char*) r->u.sutrs->buf,
162                                     r->u.sutrs->len,
163                                     charset);
164     else if (r->which == Z_External_octet)
165     {
166         if (yaz_oid_is_iso2709(oid))
167         {
168             const char *ret_buf = return_marc_record(
169                 wrbuf, marctype, len,
170                 (const char *) r->u.octet_aligned->buf,
171                 r->u.octet_aligned->len,
172                 charset);
173             if (ret_buf)
174                 return ret_buf;
175             /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
176             if (marctype != YAZ_MARC_ISO2709)
177                 return 0;
178         }
179         return return_string_record(wrbuf, len,
180                                     (const char *) r->u.octet_aligned->buf,
181                                     r->u.octet_aligned->len,
182                                     charset);
183     }
184     else if (r->which == Z_External_grs1)
185     {
186         yaz_display_grs1(wrbuf, r->u.grs1, 0);
187         return return_string_record(wrbuf, len,
188                                     wrbuf_buf(wrbuf),
189                                     wrbuf_len(wrbuf),
190                                     charset);
191     }
192     return 0;
193 }
194     
195 static const char *get_record_format(WRBUF wrbuf, int *len,
196                                      Z_NamePlusRecord *npr,
197                                      int marctype, const char *charset,
198                                      const char *format)
199 {
200     const char *res = return_record_wrbuf(wrbuf, len, npr, marctype, charset);
201 #if YAZ_HAVE_XML2
202     if (*format == '1' && len)
203     {
204         /* try to XML format res */
205         xmlDocPtr doc;
206         xmlKeepBlanksDefault(0); /* get get xmlDocFormatMemory to work! */
207         doc = xmlParseMemory(res, *len);
208         if (doc)
209         {
210             xmlChar *xml_mem;
211             int xml_size;
212             xmlDocDumpFormatMemory(doc, &xml_mem, &xml_size, 1);
213             wrbuf_rewind(wrbuf);
214             wrbuf_write(wrbuf, (const char *) xml_mem, xml_size);
215             xmlFree(xml_mem);
216             xmlFreeDoc(doc);
217             res = wrbuf_cstr(wrbuf);
218             *len = wrbuf_len(wrbuf);
219         } 
220     }
221 #endif
222     return res;
223 }
224
225 const char *yaz_record_render(Z_NamePlusRecord *npr, const char *schema,
226                               WRBUF wrbuf,
227                               const char *type_spec, int *len)
228 {
229     size_t i;
230     char type[40];
231     char charset[40];
232     char format[3];
233     const char *cp = type_spec;
234
235     for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
236          i++)
237         type[i] = cp[i];
238     type[i] = '\0';
239     charset[0] = '\0';
240     format[0] = '\0';
241     while (1)
242     {
243         while (cp[i] == ' ')
244             i++;
245         if (cp[i] != ';')
246             break;
247         i++;
248         while (cp[i] == ' ')
249             i++;
250         if (!strncmp(cp + i, "charset=", 8))
251         {
252             size_t j = 0;
253             i = i + 8; /* skip charset= */
254             for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
255             {
256                 if (j < sizeof(charset)-1)
257                     charset[j++] = cp[i];
258             }
259             charset[j] = '\0';
260         }
261         else if (!strncmp(cp + i, "format=", 7))
262         {
263             size_t j = 0; 
264             i = i + 7;
265             for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
266             {
267                 if (j < sizeof(format)-1)
268                     format[j++] = cp[i];
269             }
270             format[j] = '\0';
271         } 
272     }
273     if (!strcmp(type, "database"))
274     {
275         if (len)
276             *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
277         return npr->databaseName;
278     }
279     else if (!strcmp(type, "schema"))
280     {
281         if (len)
282             *len = schema ? strlen(schema) : 0;
283         return schema;
284     }
285     else if (!strcmp(type, "syntax"))
286     {
287         const char *desc = 0;   
288         if (npr->which == Z_NamePlusRecord_databaseRecord)
289         {
290             Z_External *r = (Z_External *) npr->u.databaseRecord;
291             desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
292         }
293         if (!desc)
294             desc = "none";
295         if (len)
296             *len = strlen(desc);
297         return desc;
298     }
299     if (npr->which != Z_NamePlusRecord_databaseRecord)
300         return 0;
301
302     /* from now on - we have a database record .. */
303     if (!strcmp(type, "render"))
304     {
305         return get_record_format(wrbuf, len, npr, YAZ_MARC_LINE, charset, format);
306     }
307     else if (!strcmp(type, "xml"))
308     {
309         return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
310                                  format);
311     }
312     else if (!strcmp(type, "txml"))
313     {
314         return get_record_format(wrbuf, len, npr, YAZ_MARC_TURBOMARC, charset,
315                                  format);
316     }
317     else if (!strcmp(type, "raw"))
318     {
319         return get_record_format(wrbuf, len, npr, YAZ_MARC_ISO2709, charset,
320             format);
321     }
322     else if (!strcmp(type, "ext"))
323     {
324         if (len) *len = -1;
325         return (const char *) npr->u.databaseRecord;
326     }
327     else if (!strcmp(type, "opac"))
328     {
329         if (npr->u.databaseRecord->which == Z_External_OPAC)
330             return get_record_format(wrbuf, len, npr, YAZ_MARC_MARCXML, charset,
331                                      format);
332     }
333     return 0;
334 }
335
336 /*
337  * Local variables:
338  * c-basic-offset: 4
339  * c-file-style: "Stroustrup"
340  * indent-tabs-mode: nil
341  * End:
342  * vim: shiftwidth=4 tabstop=8 expandtab
343  */
344