a091cc635b739c90d5d2261b347ae1a92fe8833f
[idzebra-moved-to-github.git] / data1 / d1_marc.c
1 /* $Id: d1_marc.c,v 1.19 2007-06-27 22:17:20 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 /* converts data1 tree to ISO2709/MARC record */
24
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <yaz/log.h>
30 #include <yaz/oid_db.h>
31 #include <yaz/marcdisp.h>
32 #include <yaz/readconf.h>
33 #include <yaz/xmalloc.h>
34 #include <yaz/tpath.h>
35 #include <idzebra/data1.h>
36
37 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
38 {
39     FILE *f;
40     NMEM mem = data1_nmem_get (dh);
41     data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
42     char line[512], *argv[50];
43     int lineno = 0;
44     int argc;
45     
46     if (!(f = data1_path_fopen(dh, file, "r")))
47         return 0;
48
49     res->name = 0;
50     res->oid = 0;
51     res->next = 0;
52     res->length_data_entry = 4;
53     res->length_starting = 5;
54     res->length_implementation = 0;
55     strcpy(res->future_use, "4");
56
57     strcpy(res->record_status, "n");
58     strcpy(res->implementation_codes, "    ");
59     res->indicator_length = 2;
60     res->identifier_length = 2;
61     res->force_indicator_length = -1;
62     res->force_identifier_length = -1;
63     strcpy(res->user_systems, "z  ");
64     
65     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
66         if (!strcmp(*argv, "name"))
67         {
68             if (argc != 2)
69             {
70                 yaz_log(YLOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
71                         *argv);
72                 continue;
73             }
74             res->name = nmem_strdup(mem, argv[1]);
75         }
76         else if (!strcmp(*argv, "reference"))
77         {
78             if (argc != 2)
79             {
80                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
81                         *argv);
82                 continue;
83             }
84             res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
85                                               CLASS_TAGSET, argv[1], 
86                                               mem);
87             if (!res->oid)
88             {
89                 yaz_log(YLOG_WARN, "%s:%d: Unknown tagset reference '%s'",
90                         file, lineno, argv[1]);
91                 continue;
92             }
93         }
94         else if (!strcmp(*argv, "length-data-entry"))
95         {
96             if (argc != 2)
97             {
98                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
99                         *argv);
100                 continue;
101             }
102             res->length_data_entry = atoi(argv[1]);
103         }
104         else if (!strcmp(*argv, "length-starting"))
105         {
106             if (argc != 2)
107             {
108                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
109                         *argv);
110                 continue;
111             }
112             res->length_starting = atoi(argv[1]);
113         }
114         else if (!strcmp(*argv, "length-implementation"))
115         {
116             if (argc != 2)
117             {
118                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
119                         *argv);
120                 continue;
121             }
122             res->length_implementation = atoi(argv[1]);
123         }
124         else if (!strcmp(*argv, "future-use"))
125         {
126             if (argc != 2)
127             {
128                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
129                         *argv);
130                 continue;
131             }
132             strncpy(res->future_use, argv[1], 2);
133         }
134         else if (!strcmp(*argv, "force-indicator-length"))
135         {
136             if (argc != 2)
137             {
138                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
139                         *argv);
140                 continue;
141             }
142             res->force_indicator_length = atoi(argv[1]);
143         }
144         else if (!strcmp(*argv, "force-identifier-length"))
145         {
146             if (argc != 2)
147             {
148                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
149                         *argv);
150                 continue;
151             }
152             res->force_identifier_length = atoi(argv[1]);
153         }
154         else
155             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
156                     *argv);
157
158     fclose(f);
159     return res;
160 }
161
162
163 static void get_data2(data1_node *n, int *len, char *dst, size_t max)
164 {
165     *len = 0;
166
167     while (n)
168     {
169         if (n->which == DATA1N_data)
170         {
171             if (dst && *len < max)
172             {
173                 size_t copy_len = max - *len;
174                 if (copy_len > n->u.data.len)
175                     copy_len = n->u.data.len;
176                 memcpy(dst + *len, n->u.data.data, copy_len);
177             }
178             *len += n->u.data.len;
179         }
180         if (n->which == DATA1N_tag)
181             n = n->child;
182         else if (n->which == DATA1N_data)
183             n = n->next;
184         else
185             break;      
186     }
187 }
188
189 static void memint (char *p, int val, int len)
190 {
191     char buf[10];
192
193     if (len == 1)
194         *p = val + '0';
195     else
196     {
197         sprintf (buf, "%08d", val);
198         memcpy (p, buf+8-len, len);
199     }
200 }
201
202 /* check for indicator. non MARCXML only */
203 static int is_indicator (data1_marctab *p, data1_node *subf)
204 {
205     if (p->indicator_length != 2 ||
206         (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
207         return 1;
208     return 0;
209 }
210
211 static int nodetomarc(data1_handle dh,
212                       data1_marctab *p, data1_node *n, int selected,
213                       char **buf, int *size)
214 {
215     char leader[24];
216
217     int len = 26;
218     int dlen;
219     int base_address = 25;
220     int entry_p, data_p;
221     char *op;
222     data1_node *field, *subf;
223
224 #if 0
225     data1_pr_tree(dh, n, stdout);
226 #endif
227     yaz_log (YLOG_DEBUG, "nodetomarc");
228
229     memcpy (leader+5, p->record_status, 1);
230     memcpy (leader+6, p->implementation_codes, 4);
231     memint (leader+10, p->indicator_length, 1);
232     memint (leader+11, p->identifier_length, 1);
233     memcpy (leader+17, p->user_systems, 3);
234     memint (leader+20, p->length_data_entry, 1);
235     memint (leader+21, p->length_starting, 1);
236     memint (leader+22, p->length_implementation, 1);
237     memcpy (leader+23, p->future_use, 1);
238
239     for (field = n->child; field; field = field->next)
240     {
241         int control_field = 0;  /* 00X fields - usually! */
242         int marc_xml = 0;
243
244         if (field->which != DATA1N_tag)
245             continue;
246         if (selected && !field->u.tag.node_selected)
247             continue;
248             
249         subf = field->child;
250         if (!subf)
251             continue;
252         
253         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
254             continue;
255         else if (!strcmp(field->u.tag.tag, "leader"))
256         {
257             int dlen = 0;
258             get_data2(subf, &dlen, leader, 24);
259             continue;
260         }
261         else if (!strcmp(field->u.tag.tag, "controlfield"))
262         {
263             control_field = 1;
264             marc_xml = 1;
265         }
266         else if (!strcmp(field->u.tag.tag, "datafield"))
267         {
268             control_field = 0;
269             marc_xml = 1;
270         }
271         else if (subf->which == DATA1N_data)
272         {
273             control_field = 1;
274             marc_xml = 0;
275         }
276         else
277         {
278             control_field = 0;
279             marc_xml = 0;
280         }
281
282         len += 4 + p->length_data_entry + p->length_starting
283             + p->length_implementation;
284         base_address += 3 + p->length_data_entry + p->length_starting
285             + p->length_implementation;
286
287         if (!control_field)
288             len += p->indicator_length;  
289
290         /* we'll allow no indicator if length is not 2 */
291         /* select when old XML format, since indicator is an element */
292         if (marc_xml == 0 && is_indicator (p, subf))
293             subf = subf->child;
294         
295         for (; subf; subf = subf->next)
296         {
297             if (!control_field)
298             {
299                 if (marc_xml && subf->which != DATA1N_tag)
300                     continue; /* we skip comments, cdata .. */
301                 len += p->identifier_length;
302             }
303             get_data2(subf, &dlen, 0, 0);
304             len += dlen;
305         }
306     }
307
308     if (!*buf)
309         *buf = (char *)xmalloc(*size = len);
310     else if (*size <= len)
311         *buf = (char *)xrealloc(*buf, *size = len);
312         
313     op = *buf;
314
315     /* we know the base address now */
316     memint (leader+12, base_address, 5);
317
318     /* copy temp leader to real output buf op */
319     memcpy (op, leader, 24);
320     memint (op, len, 5);
321     
322     entry_p = 24;
323     data_p = base_address;
324
325     for (field = n->child; field; field = field->next)
326     {
327         int control_field = 0;
328         int marc_xml = 0;
329         const char *tag = 0;
330
331         int data_0 = data_p;
332         char indicator_data[6];
333
334         memset (indicator_data, ' ', sizeof(indicator_data)-1);
335         indicator_data[sizeof(indicator_data)-1] = '\0';
336
337         if (field->which != DATA1N_tag)
338             continue;
339
340         if (selected && !field->u.tag.node_selected)
341             continue;
342
343         subf = field->child;
344         if (!subf)
345             continue;
346         
347         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
348             continue;
349         else if (!strcmp(field->u.tag.tag, "leader"))
350             continue;
351         else if (!strcmp(field->u.tag.tag, "controlfield"))
352         {
353             control_field = 1;
354             marc_xml = 1;
355         }
356         else if (!strcmp(field->u.tag.tag, "datafield"))
357         {
358             control_field = 0;
359             marc_xml = 1;
360         }
361         else if (subf->which == DATA1N_data)
362         {
363             control_field = 1;
364             marc_xml = 0;
365         }
366         else
367         {
368             control_field = 0;
369             marc_xml = 0;
370         }
371         if (marc_xml == 0 && is_indicator (p, subf))
372         {
373             strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
374             subf = subf->child;
375         }
376         else if (marc_xml == 1 && !control_field)
377         {
378             data1_xattr *xa;
379             for (xa = field->u.tag.attributes; xa; xa = xa->next)
380             {
381                 if (!strcmp(xa->name, "ind1"))
382                     indicator_data[0] = xa->value[0];
383                 if (!strcmp(xa->name, "ind2"))
384                     indicator_data[1] = xa->value[0];
385                 if (!strcmp(xa->name, "ind3"))
386                     indicator_data[2] = xa->value[0];
387             }
388         }
389         if (!control_field)
390         {
391             memcpy (op + data_p, indicator_data, p->indicator_length);
392             data_p += p->indicator_length;
393         }
394         for (; subf; subf = subf->next)
395         {
396             if (!control_field)
397             {
398                 const char *identifier = "a";
399                 if (marc_xml)
400                 {
401                     data1_xattr *xa;
402                     if (subf->which != DATA1N_tag)
403                         continue;
404                     if (strcmp(subf->u.tag.tag, "subfield"))
405                         yaz_log(YLOG_WARN, "Unhandled tag %s",
406                                 subf->u.tag.tag);
407                     
408                     for (xa = subf->u.tag.attributes; xa; xa = xa->next)
409                         if (!strcmp(xa->name, "code"))
410                             identifier = xa->value;
411                 }
412                 else if (subf->which != DATA1N_tag)
413                     yaz_log(YLOG_WARN, "Malformed fields for marc output.");
414                 else
415                     identifier = subf->u.tag.tag;
416                 op[data_p] = ISO2709_IDFS;
417                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
418                 data_p += p->identifier_length;
419             }
420             get_data2(subf, &dlen, op + data_p, 100000);
421             data_p += dlen;
422         }
423         op[data_p++] = ISO2709_FS;
424
425         if (marc_xml)
426         {
427             data1_xattr *xa;
428             for (xa = field->u.tag.attributes; xa; xa = xa->next)
429                 if (!strcmp(xa->name, "tag"))
430                     tag = xa->value;
431         }
432         else
433             tag = field->u.tag.tag;
434
435         if (!tag || strlen(tag) != 3)
436             tag = "000";
437         memcpy (op + entry_p, tag, 3);
438         
439         entry_p += 3;
440         memint (op + entry_p, data_p - data_0, p->length_data_entry);
441         entry_p += p->length_data_entry;
442         memint (op + entry_p, data_0 - base_address, p->length_starting);
443         entry_p += p->length_starting;
444         entry_p += p->length_implementation;
445     }
446     op[entry_p++] = ISO2709_FS;
447     assert (entry_p == base_address);
448     op[data_p++] = ISO2709_RS;
449     assert (data_p == len);
450     return len;
451 }
452
453 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
454                        int selected, int *len)
455 {
456     int *size;
457     char **buf = data1_get_map_buf (dh, &size);
458
459     n = data1_get_root_tag (dh, n);
460     if (!n)
461         return 0;
462     *len = nodetomarc(dh, p, n, selected, buf, size);
463     return *buf;
464 }
465 /*
466  * Local variables:
467  * c-basic-offset: 4
468  * indent-tabs-mode: nil
469  * End:
470  * vim: shiftwidth=4 tabstop=8 expandtab
471  */
472