add copy of iconv_encode_marc8.c
[yaz-moved-to-github.git] / src / opacdisp.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file opacdisp.c
7  * \brief Implements OPAC record display
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14
15 #include <yaz/proto.h>
16 #include <yaz/marcdisp.h>
17
18 static void opac_element_str(WRBUF wrbuf, yaz_iconv_t cd, 
19                              int l, const char *elem,
20                              const char *data)
21 {
22     if (data)
23     {
24         while (--l >= 0)
25             wrbuf_puts(wrbuf, " ");
26         wrbuf_puts(wrbuf, "<");
27         wrbuf_puts(wrbuf, elem);
28         wrbuf_puts(wrbuf, ">");
29         wrbuf_iconv_puts_cdata(wrbuf, cd, data);
30         wrbuf_puts(wrbuf, "</");
31         wrbuf_puts(wrbuf, elem);
32         wrbuf_puts(wrbuf, ">\n");
33     }
34 }
35
36 static void opac_element_bool(WRBUF wrbuf, int l, const char *elem, int *data)
37 {
38     if (data)
39     {
40         while (--l >= 0)
41             wrbuf_puts(wrbuf, " ");
42         wrbuf_puts(wrbuf, "<");
43         wrbuf_puts(wrbuf, elem);
44         if (*data)
45             wrbuf_puts(wrbuf, " value=\"1\"");
46         else
47             wrbuf_puts(wrbuf, " value=\"0\"");
48         wrbuf_puts(wrbuf, "/>\n");
49     }
50 }
51
52 void yaz_opac_decode_wrbuf(yaz_marc_t mt, Z_OPACRecord *r, WRBUF wrbuf)
53 {
54     int i;
55     yaz_iconv_t cd = yaz_marc_get_iconv(mt);
56
57     wrbuf_puts(wrbuf, "<opacRecord>\n");
58     if (r->bibliographicRecord)
59     {
60         Z_External *ext = r->bibliographicRecord;
61         
62         wrbuf_puts(wrbuf, "  <bibliographicRecord>\n");
63         if (ext->which == Z_External_octet)
64             yaz_marc_decode_wrbuf(mt, (const char *) ext->u.octet_aligned->buf,
65                                   ext->u.octet_aligned->len, wrbuf);
66         wrbuf_puts(wrbuf, "  </bibliographicRecord>\n");
67     }
68     wrbuf_puts(wrbuf, "<holdings>\n");
69
70     for (i = 0; i < r->num_holdingsData; i++)
71     {
72         Z_HoldingsRecord *h = r->holdingsData[i];
73         wrbuf_puts(wrbuf, " <holding>\n");
74
75         if (h->which == Z_HoldingsRecord_marcHoldingsRecord)
76         {
77             Z_External *ext = h->u.marcHoldingsRecord;
78
79             wrbuf_puts(wrbuf, "  <marcHoldingsRecord>\n");
80             if (ext->which == Z_External_octet)
81                 yaz_marc_decode_wrbuf(mt, (const char *)
82                                       ext->u.octet_aligned->buf,
83                                       ext->u.octet_aligned->len, wrbuf);
84             wrbuf_puts(wrbuf, "  </marcHoldingsRecord>\n");
85         }
86         else if (h->which == Z_HoldingsRecord_holdingsAndCirc)
87         {
88             int j;
89             
90             Z_HoldingsAndCircData *d = h->u.holdingsAndCirc;
91         
92             opac_element_str(wrbuf, cd, 2, "typeOfRecord", d->typeOfRecord);
93             opac_element_str(wrbuf, cd, 2, "encodingLevel", d->encodingLevel);
94             opac_element_str(wrbuf, cd, 2, "format", d->format);
95             opac_element_str(wrbuf, cd, 2, "receiptAcqStatus", d->receiptAcqStatus);
96             opac_element_str(wrbuf, cd, 2, "generalRetention", d->generalRetention);
97             opac_element_str(wrbuf, cd, 2, "completeness", d->completeness);
98             opac_element_str(wrbuf, cd, 2, "dateOfReport", d->dateOfReport);
99             opac_element_str(wrbuf, cd, 2, "nucCode", d->nucCode);
100             opac_element_str(wrbuf, cd, 2, "localLocation", d->localLocation);
101             opac_element_str(wrbuf, cd, 2, "shelvingLocation", d->shelvingLocation);
102             opac_element_str(wrbuf, cd, 2, "callNumber", d->callNumber);
103             opac_element_str(wrbuf, cd, 2, "shelvingData", d->shelvingData);
104             
105             opac_element_str(wrbuf, cd, 2, "copyNumber", d->copyNumber);
106             opac_element_str(wrbuf, cd, 2, "publicNote", d->publicNote);
107             opac_element_str(wrbuf, cd, 2, "reproductionNote", d->reproductionNote);
108             opac_element_str(wrbuf, cd, 2, "termsUseRepro", d->termsUseRepro);
109             opac_element_str(wrbuf, cd, 2, "enumAndChron", d->enumAndChron);
110             if (d->num_volumes)
111             {
112                 wrbuf_puts (wrbuf, "  <volumes>\n");
113                 for (j = 0; j<d->num_volumes; j++)
114                 {
115                     wrbuf_puts (wrbuf, "   <volume>\n");
116                     opac_element_str(wrbuf, cd, 4, "enumeration",
117                                      d->volumes[j]->enumeration);
118                     opac_element_str(wrbuf, cd, 4, "chronology",
119                                      d->volumes[j]->chronology);
120                     opac_element_str(wrbuf, cd, 4, "enumAndChron",
121                                      d->volumes[j]->enumAndChron);
122                     wrbuf_puts(wrbuf, "   </volume>\n");
123                 }
124                 wrbuf_puts(wrbuf, "  </volumes>\n");
125             }
126             if (d->num_circulationData)
127             {
128                 wrbuf_puts (wrbuf, "  <circulations>\n");
129                 for (j = 0; j<d->num_circulationData; j++)
130                 {
131                     wrbuf_puts(wrbuf,"   <circulation>\n");
132                     opac_element_bool(wrbuf, 4, "availableNow",
133                                       d->circulationData[j]->availableNow);
134                     opac_element_str(wrbuf, cd, 4, "availabiltyDate",
135                                      d->circulationData[j]->availablityDate);
136                     opac_element_str(wrbuf, cd, 4, "availableThru",
137                                       d->circulationData[j]->availableThru);
138                     opac_element_str(wrbuf, cd, 4, "restrictions",
139                                       d->circulationData[j]->restrictions);
140                     opac_element_str(wrbuf, cd, 4, "itemId",
141                                       d->circulationData[j]->itemId);
142                     opac_element_bool(wrbuf, 4, "renewable",
143                                       d->circulationData[j]->renewable);
144                     opac_element_bool(wrbuf, 4, "onHold",
145                                       d->circulationData[j]->onHold);
146                     opac_element_str(wrbuf, cd, 4, "enumAndChron",
147                                       d->circulationData[j]->enumAndChron);
148                     opac_element_str(wrbuf, cd, 4, "midspine",
149                                       d->circulationData[j]->midspine);
150                     opac_element_str(wrbuf, cd, 4, "temporaryLocation",
151                                       d->circulationData[j]->temporaryLocation);
152                     wrbuf_puts(wrbuf, "   </circulation>\n");
153                 }
154                 wrbuf_puts(wrbuf, "  </circulations>\n");
155             }
156         }
157         wrbuf_puts(wrbuf, " </holding>\n");
158     }
159     wrbuf_puts(wrbuf, "</holdings>\n");
160     wrbuf_puts(wrbuf, "</opacRecord>\n");
161 }
162
163 void yaz_display_OPAC(WRBUF wrbuf, Z_OPACRecord *opac_rec, int flags)
164 {
165     yaz_marc_t mt = yaz_marc_create();
166
167     yaz_marc_xml(mt, YAZ_MARC_MARCXML);
168     yaz_opac_decode_wrbuf(mt, opac_rec, wrbuf);
169     yaz_marc_destroy(mt);
170 }
171
172 /*
173  * Local variables:
174  * c-basic-offset: 4
175  * indent-tabs-mode: nil
176  * End:
177  * vim: shiftwidth=4 tabstop=8 expandtab
178  */
179