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