Avoid printf %.*s for arbitrary strings
[yaz-moved-to-github.git] / src / opacdisp.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 opacdisp.c
7  * \brief Implements OPAC record display
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include <yaz/proto.h>
18 #include <yaz/marcdisp.h>
19
20 static void opac_element_str(WRBUF wrbuf, yaz_iconv_t cd1, yaz_iconv_t cd2,
21                              int l, const char *elem,
22                              const char *data)
23 {
24     if (data)
25     {
26         size_t pos;
27         while (--l >= 0)
28             wrbuf_puts(wrbuf, " ");
29         wrbuf_puts(wrbuf, "<");
30         wrbuf_puts(wrbuf, elem);
31         wrbuf_puts(wrbuf, ">");
32         
33         pos = wrbuf->pos; /* save position */
34         if (wrbuf_iconv_write_x(wrbuf, cd1, data, strlen(data), 1) && cd2)
35         {
36             wrbuf->pos = pos; /* rewind to pos */
37             wrbuf_iconv_write_x(wrbuf, cd2, data, strlen(data), 1);
38         }
39         wrbuf_puts(wrbuf, "</");
40         wrbuf_puts(wrbuf, elem);
41         wrbuf_puts(wrbuf, ">\n");
42     }
43 }
44
45 static void opac_element_bool(WRBUF wrbuf, int l, const char *elem, int *data)
46 {
47     if (data)
48     {
49         while (--l >= 0)
50             wrbuf_puts(wrbuf, " ");
51         wrbuf_puts(wrbuf, "<");
52         wrbuf_puts(wrbuf, elem);
53         if (*data)
54             wrbuf_puts(wrbuf, " value=\"1\"");
55         else
56             wrbuf_puts(wrbuf, " value=\"0\"");
57         wrbuf_puts(wrbuf, "/>\n");
58     }
59 }
60
61 void yaz_opac_decode_wrbuf2(yaz_marc_t mt, Z_OPACRecord *r, WRBUF wrbuf,
62                             yaz_iconv_t cd2)
63 {
64     int i;
65     yaz_iconv_t cd1 = yaz_marc_get_iconv(mt);
66
67     wrbuf_puts(wrbuf, "<opacRecord>\n");
68     if (r->bibliographicRecord)
69     {
70         Z_External *ext = r->bibliographicRecord;
71         
72         wrbuf_puts(wrbuf, "  <bibliographicRecord>\n");
73         if (ext->which == Z_External_octet)
74             yaz_marc_decode_wrbuf(mt, (const char *) ext->u.octet_aligned->buf,
75                                   ext->u.octet_aligned->len, wrbuf);
76         wrbuf_puts(wrbuf, "  </bibliographicRecord>\n");
77     }
78     wrbuf_puts(wrbuf, "<holdings>\n");
79
80     for (i = 0; i < r->num_holdingsData; i++)
81     {
82         Z_HoldingsRecord *h = r->holdingsData[i];
83         wrbuf_puts(wrbuf, " <holding>\n");
84
85         if (h->which == Z_HoldingsRecord_marcHoldingsRecord)
86         {
87             Z_External *ext = h->u.marcHoldingsRecord;
88
89             wrbuf_puts(wrbuf, "  <marcHoldingsRecord>\n");
90             if (ext->which == Z_External_octet)
91                 yaz_marc_decode_wrbuf(mt, (const char *)
92                                       ext->u.octet_aligned->buf,
93                                       ext->u.octet_aligned->len, wrbuf);
94             wrbuf_puts(wrbuf, "  </marcHoldingsRecord>\n");
95         }
96         else if (h->which == Z_HoldingsRecord_holdingsAndCirc)
97         {
98             int j;
99             
100             Z_HoldingsAndCircData *d = h->u.holdingsAndCirc;
101         
102             opac_element_str(wrbuf, cd1, cd2, 2, "typeOfRecord",
103                              d->typeOfRecord);
104             opac_element_str(wrbuf, cd1, cd2, 2, "encodingLevel",
105                              d->encodingLevel);
106             opac_element_str(wrbuf, cd1, cd2, 2, "format",
107                              d->format);
108             opac_element_str(wrbuf, cd1, cd2, 2, "receiptAcqStatus",
109                              d->receiptAcqStatus);
110             opac_element_str(wrbuf, cd1, cd2, 2, "generalRetention",
111                              d->generalRetention);
112             opac_element_str(wrbuf, cd1, cd2, 2, "completeness",
113                              d->completeness);
114             opac_element_str(wrbuf, cd1, cd2, 2, "dateOfReport",
115                              d->dateOfReport);
116             opac_element_str(wrbuf, cd1, cd2, 2, "nucCode", d->nucCode);
117             opac_element_str(wrbuf, cd1, cd2, 2, "localLocation",
118                              d->localLocation);
119             opac_element_str(wrbuf, cd1, cd2, 2, "shelvingLocation",
120                              d->shelvingLocation);
121             opac_element_str(wrbuf, cd1, cd2, 2, "callNumber",
122                              d->callNumber);
123             opac_element_str(wrbuf, cd1, cd2, 2, "shelvingData",
124                              d->shelvingData);
125             opac_element_str(wrbuf, cd1, cd2, 2, "copyNumber",
126                              d->copyNumber);
127             opac_element_str(wrbuf, cd1, cd2, 2, "publicNote",
128                              d->publicNote);
129             opac_element_str(wrbuf, cd1, cd2, 2, "reproductionNote",
130                              d->reproductionNote);
131             opac_element_str(wrbuf, cd1, cd2, 2, "termsUseRepro",
132                              d->termsUseRepro);
133             opac_element_str(wrbuf, cd1, cd2, 2, "enumAndChron",
134                              d->enumAndChron);
135             if (d->num_volumes)
136             {
137                 wrbuf_puts (wrbuf, "  <volumes>\n");
138                 for (j = 0; j<d->num_volumes; j++)
139                 {
140                     wrbuf_puts (wrbuf, "   <volume>\n");
141                     opac_element_str(wrbuf, cd1, cd2, 4, "enumeration",
142                                      d->volumes[j]->enumeration);
143                     opac_element_str(wrbuf, cd1, cd2, 4, "chronology",
144                                      d->volumes[j]->chronology);
145                     opac_element_str(wrbuf, cd1, cd2, 4, "enumAndChron",
146                                      d->volumes[j]->enumAndChron);
147                     wrbuf_puts(wrbuf, "   </volume>\n");
148                 }
149                 wrbuf_puts(wrbuf, "  </volumes>\n");
150             }
151             if (d->num_circulationData)
152             {
153                 wrbuf_puts (wrbuf, "  <circulations>\n");
154                 for (j = 0; j<d->num_circulationData; j++)
155                 {
156                     wrbuf_puts(wrbuf,"   <circulation>\n");
157                     opac_element_bool(wrbuf, 4, "availableNow",
158                                       d->circulationData[j]->availableNow);
159                     opac_element_str(wrbuf, cd1, cd2, 4, "availabiltyDate",
160                                      d->circulationData[j]->availablityDate);
161                     opac_element_str(wrbuf, cd1, cd2, 4, "availableThru",
162                                       d->circulationData[j]->availableThru);
163                     opac_element_str(wrbuf, cd1, cd2, 4, "restrictions",
164                                       d->circulationData[j]->restrictions);
165                     opac_element_str(wrbuf, cd1, cd2, 4, "itemId",
166                                       d->circulationData[j]->itemId);
167                     opac_element_bool(wrbuf, 4, "renewable",
168                                       d->circulationData[j]->renewable);
169                     opac_element_bool(wrbuf, 4, "onHold",
170                                       d->circulationData[j]->onHold);
171                     opac_element_str(wrbuf, cd1, cd2, 4, "enumAndChron",
172                                       d->circulationData[j]->enumAndChron);
173                     opac_element_str(wrbuf, cd1, cd2, 4, "midspine",
174                                       d->circulationData[j]->midspine);
175                     opac_element_str(wrbuf, cd1, cd2, 4, "temporaryLocation",
176                                       d->circulationData[j]->temporaryLocation);
177                     wrbuf_puts(wrbuf, "   </circulation>\n");
178                 }
179                 wrbuf_puts(wrbuf, "  </circulations>\n");
180             }
181         }
182         wrbuf_puts(wrbuf, " </holding>\n");
183     }
184     wrbuf_puts(wrbuf, "</holdings>\n");
185     wrbuf_puts(wrbuf, "</opacRecord>\n");
186 }
187
188 void yaz_opac_decode_wrbuf(yaz_marc_t mt, Z_OPACRecord *r, WRBUF wrbuf)
189 {
190     yaz_opac_decode_wrbuf2(mt, r, wrbuf, 0);
191 }
192
193 /*
194  * Local variables:
195  * c-basic-offset: 4
196  * c-file-style: "Stroustrup"
197  * indent-tabs-mode: nil
198  * End:
199  * vim: shiftwidth=4 tabstop=8 expandtab
200  */
201