Omit CVS Id. Update copyright year.
[idzebra-moved-to-github.git] / data1 / d1_marc.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 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
152             yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
153                     *argv);
154
155     fclose(f);
156     return res;
157 }
158
159
160 static void get_data2(data1_node *n, int *len, char *dst, size_t max)
161 {
162     *len = 0;
163
164     while (n)
165     {
166         if (n->which == DATA1N_data)
167         {
168             if (dst && *len < max)
169             {
170                 size_t copy_len = max - *len;
171                 if (copy_len > n->u.data.len)
172                     copy_len = n->u.data.len;
173                 memcpy(dst + *len, n->u.data.data, copy_len);
174             }
175             *len += n->u.data.len;
176         }
177         if (n->which == DATA1N_tag)
178             n = n->child;
179         else if (n->which == DATA1N_data)
180             n = n->next;
181         else
182             break;      
183     }
184 }
185
186 static void memint (char *p, int val, int len)
187 {
188     char buf[10];
189
190     if (len == 1)
191         *p = val + '0';
192     else
193     {
194         sprintf (buf, "%08d", val);
195         memcpy (p, buf+8-len, len);
196     }
197 }
198
199 /* check for indicator. non MARCXML only */
200 static int is_indicator (data1_marctab *p, data1_node *subf)
201 {
202     if (p->indicator_length != 2 ||
203         (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
204         return 1;
205     return 0;
206 }
207
208 static int nodetomarc(data1_handle dh,
209                       data1_marctab *p, data1_node *n, int selected,
210                       char **buf, int *size)
211 {
212     char leader[24];
213
214     int len = 26;
215     int dlen;
216     int base_address = 25;
217     int entry_p, data_p;
218     char *op;
219     data1_node *field, *subf;
220
221 #if 0
222     data1_pr_tree(dh, n, stdout);
223 #endif
224     yaz_log (YLOG_DEBUG, "nodetomarc");
225
226     memcpy (leader+5, p->record_status, 1);
227     memcpy (leader+6, p->implementation_codes, 4);
228     memint (leader+10, p->indicator_length, 1);
229     memint (leader+11, p->identifier_length, 1);
230     memcpy (leader+17, p->user_systems, 3);
231     memint (leader+20, p->length_data_entry, 1);
232     memint (leader+21, p->length_starting, 1);
233     memint (leader+22, p->length_implementation, 1);
234     memcpy (leader+23, p->future_use, 1);
235
236     for (field = n->child; field; field = field->next)
237     {
238         int control_field = 0;  /* 00X fields - usually! */
239         int marc_xml = 0;
240
241         if (field->which != DATA1N_tag)
242             continue;
243         if (selected && !field->u.tag.node_selected)
244             continue;
245             
246         subf = field->child;
247         if (!subf)
248             continue;
249         
250         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
251             continue;
252         else if (!strcmp(field->u.tag.tag, "leader"))
253         {
254             int dlen = 0;
255             get_data2(subf, &dlen, leader, 24);
256             continue;
257         }
258         else if (!strcmp(field->u.tag.tag, "controlfield"))
259         {
260             control_field = 1;
261             marc_xml = 1;
262         }
263         else if (!strcmp(field->u.tag.tag, "datafield"))
264         {
265             control_field = 0;
266             marc_xml = 1;
267         }
268         else if (subf->which == DATA1N_data)
269         {
270             control_field = 1;
271             marc_xml = 0;
272         }
273         else
274         {
275             control_field = 0;
276             marc_xml = 0;
277         }
278
279         len += 4 + p->length_data_entry + p->length_starting
280             + p->length_implementation;
281         base_address += 3 + p->length_data_entry + p->length_starting
282             + p->length_implementation;
283
284         if (!control_field)
285             len += p->indicator_length;  
286
287         /* we'll allow no indicator if length is not 2 */
288         /* select when old XML format, since indicator is an element */
289         if (marc_xml == 0 && is_indicator (p, subf))
290             subf = subf->child;
291         
292         for (; subf; subf = subf->next)
293         {
294             if (!control_field)
295             {
296                 if (marc_xml && subf->which != DATA1N_tag)
297                     continue; /* we skip comments, cdata .. */
298                 len += p->identifier_length;
299             }
300             get_data2(subf, &dlen, 0, 0);
301             len += dlen;
302         }
303     }
304
305     if (!*buf)
306         *buf = (char *)xmalloc(*size = len);
307     else if (*size <= len)
308         *buf = (char *)xrealloc(*buf, *size = len);
309         
310     op = *buf;
311
312     /* we know the base address now */
313     memint (leader+12, base_address, 5);
314
315     /* copy temp leader to real output buf op */
316     memcpy (op, leader, 24);
317     memint (op, len, 5);
318     
319     entry_p = 24;
320     data_p = base_address;
321
322     for (field = n->child; field; field = field->next)
323     {
324         int control_field = 0;
325         int marc_xml = 0;
326         const char *tag = 0;
327
328         int data_0 = data_p;
329         char indicator_data[6];
330
331         memset (indicator_data, ' ', sizeof(indicator_data)-1);
332         indicator_data[sizeof(indicator_data)-1] = '\0';
333
334         if (field->which != DATA1N_tag)
335             continue;
336
337         if (selected && !field->u.tag.node_selected)
338             continue;
339
340         subf = field->child;
341         if (!subf)
342             continue;
343         
344         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
345             continue;
346         else if (!strcmp(field->u.tag.tag, "leader"))
347             continue;
348         else if (!strcmp(field->u.tag.tag, "controlfield"))
349         {
350             control_field = 1;
351             marc_xml = 1;
352         }
353         else if (!strcmp(field->u.tag.tag, "datafield"))
354         {
355             control_field = 0;
356             marc_xml = 1;
357         }
358         else if (subf->which == DATA1N_data)
359         {
360             control_field = 1;
361             marc_xml = 0;
362         }
363         else
364         {
365             control_field = 0;
366             marc_xml = 0;
367         }
368         if (marc_xml == 0 && is_indicator (p, subf))
369         {
370             strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
371             subf = subf->child;
372         }
373         else if (marc_xml == 1 && !control_field)
374         {
375             data1_xattr *xa;
376             for (xa = field->u.tag.attributes; xa; xa = xa->next)
377             {
378                 if (!strcmp(xa->name, "ind1"))
379                     indicator_data[0] = xa->value[0];
380                 if (!strcmp(xa->name, "ind2"))
381                     indicator_data[1] = xa->value[0];
382                 if (!strcmp(xa->name, "ind3"))
383                     indicator_data[2] = xa->value[0];
384             }
385         }
386         if (!control_field)
387         {
388             memcpy (op + data_p, indicator_data, p->indicator_length);
389             data_p += p->indicator_length;
390         }
391         for (; subf; subf = subf->next)
392         {
393             if (!control_field)
394             {
395                 const char *identifier = "a";
396                 if (marc_xml)
397                 {
398                     data1_xattr *xa;
399                     if (subf->which != DATA1N_tag)
400                         continue;
401                     if (strcmp(subf->u.tag.tag, "subfield"))
402                         yaz_log(YLOG_WARN, "Unhandled tag %s",
403                                 subf->u.tag.tag);
404                     
405                     for (xa = subf->u.tag.attributes; xa; xa = xa->next)
406                         if (!strcmp(xa->name, "code"))
407                             identifier = xa->value;
408                 }
409                 else if (subf->which != DATA1N_tag)
410                     yaz_log(YLOG_WARN, "Malformed fields for marc output.");
411                 else
412                     identifier = subf->u.tag.tag;
413                 op[data_p] = ISO2709_IDFS;
414                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
415                 data_p += p->identifier_length;
416             }
417             get_data2(subf, &dlen, op + data_p, 100000);
418             data_p += dlen;
419         }
420         op[data_p++] = ISO2709_FS;
421
422         if (marc_xml)
423         {
424             data1_xattr *xa;
425             for (xa = field->u.tag.attributes; xa; xa = xa->next)
426                 if (!strcmp(xa->name, "tag"))
427                     tag = xa->value;
428         }
429         else
430             tag = field->u.tag.tag;
431
432         if (!tag || strlen(tag) != 3)
433             tag = "000";
434         memcpy (op + entry_p, tag, 3);
435         
436         entry_p += 3;
437         memint (op + entry_p, data_p - data_0, p->length_data_entry);
438         entry_p += p->length_data_entry;
439         memint (op + entry_p, data_0 - base_address, p->length_starting);
440         entry_p += p->length_starting;
441         entry_p += p->length_implementation;
442     }
443     op[entry_p++] = ISO2709_FS;
444     assert (entry_p == base_address);
445     op[data_p++] = ISO2709_RS;
446     assert (data_p == len);
447     return len;
448 }
449
450 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
451                        int selected, int *len)
452 {
453     int *size;
454     char **buf = data1_get_map_buf (dh, &size);
455
456     n = data1_get_root_tag (dh, n);
457     if (!n)
458         return 0;
459     *len = nodetomarc(dh, p, n, selected, buf, size);
460     return *buf;
461 }
462 /*
463  * Local variables:
464  * c-basic-offset: 4
465  * indent-tabs-mode: nil
466  * End:
467  * vim: shiftwidth=4 tabstop=8 expandtab
468  */
469