Added support for string components in simpleelement specs. Supported
[idzebra-moved-to-github.git] / data1 / d1_marc.c
1 /* $Id: d1_marc.c,v 1.12 2005-02-02 19:37:27 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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.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     {
48         yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", file);
49         return 0;
50     }
51
52     res->name = 0;
53     res->reference = VAL_NONE;
54     res->next = 0;
55     res->length_data_entry = 4;
56     res->length_starting = 5;
57     res->length_implementation = 0;
58     strcpy(res->future_use, "4");
59
60     strcpy(res->record_status, "n");
61     strcpy(res->implementation_codes, "    ");
62     res->indicator_length = 2;
63     res->identifier_length = 2;
64     res->force_indicator_length = -1;
65     res->force_identifier_length = -1;
66     strcpy(res->user_systems, "z  ");
67     
68     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
69         if (!strcmp(*argv, "name"))
70         {
71             if (argc != 2)
72             {
73                 yaz_log(YLOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
74                         *argv);
75                 continue;
76             }
77             res->name = nmem_strdup(mem, argv[1]);
78         }
79         else if (!strcmp(*argv, "reference"))
80         {
81             if (argc != 2)
82             {
83                 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
84                         *argv);
85                 continue;
86             }
87             if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
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 /*
164  * Locate some data under this node. This routine should handle variants
165  * prettily.
166  */
167 static char *get_data(data1_node *n, int *len, int chop)
168 {
169     char *r;
170
171     while (n)
172     {
173         if (n->which == DATA1N_data)
174         {
175             int i;
176             *len = n->u.data.len;
177             
178             if (chop)
179             {
180                 for (i = 0; i<*len; i++)
181                     if (!d1_isspace(n->u.data.data[i]))
182                         break;
183                 while (*len && d1_isspace(n->u.data.data[*len - 1]))
184                     (*len)--;
185                 *len = *len - i;
186                 if (*len > 0)
187                     return n->u.data.data + i;
188             }
189             else
190                 if (*len > 0)
191                     return n->u.data.data;
192         }
193         if (n->which == DATA1N_tag)
194             n = n->child;
195         else if (n->which == DATA1N_data)
196             n = n->next;
197         else
198             break;      
199     }
200     r = "";
201     *len = strlen(r);
202     return r;
203 }
204
205 static void memint (char *p, int val, int len)
206 {
207     char buf[10];
208
209     if (len == 1)
210         *p = val + '0';
211     else
212     {
213         sprintf (buf, "%08d", val);
214         memcpy (p, buf+8-len, len);
215     }
216 }
217
218 /* check for indicator. non MARCXML only */
219 static int is_indicator (data1_marctab *p, data1_node *subf)
220 {
221     if (p->indicator_length != 2 ||
222         (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
223         return 1;
224     return 0;
225 }
226
227 static int nodetomarc(data1_handle dh,
228                       data1_marctab *p, data1_node *n, int selected,
229                       char **buf, int *size)
230 {
231     char leader[24];
232
233     int len = 26;
234     int dlen;
235     int base_address = 25;
236     int entry_p, data_p;
237     char *op;
238     data1_node *field, *subf;
239
240 #if 0
241     data1_pr_tree(dh, n, stdout);
242 #endif
243     yaz_log (YLOG_DEBUG, "nodetomarc");
244
245     memcpy (leader+5, p->record_status, 1);
246     memcpy (leader+6, p->implementation_codes, 4);
247     memint (leader+10, p->indicator_length, 1);
248     memint (leader+11, p->identifier_length, 1);
249     memcpy (leader+17, p->user_systems, 3);
250     memint (leader+20, p->length_data_entry, 1);
251     memint (leader+21, p->length_starting, 1);
252     memint (leader+22, p->length_implementation, 1);
253     memcpy (leader+23, p->future_use, 1);
254
255     for (field = n->child; field; field = field->next)
256     {
257         int control_field = 0;  /* 00X fields - usually! */
258         int marc_xml = 0;
259
260         if (field->which != DATA1N_tag)
261             continue;
262         if (selected && !field->u.tag.node_selected)
263             continue;
264             
265         subf = field->child;
266         if (!subf)
267             continue;
268         
269         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
270             continue;
271         else if (!strcmp(field->u.tag.tag, "leader"))
272         {
273             int dlen = 0;
274             char *dbuf = get_data(subf, &dlen, 0);
275             if (dlen > 24)
276                 dlen = 24;
277             if (dbuf && dlen > 0)
278                 memcpy (leader, dbuf, dlen);
279             continue;
280         }
281         else if (!strcmp(field->u.tag.tag, "controlfield"))
282         {
283             control_field = 1;
284             marc_xml = 1;
285         }
286         else if (!strcmp(field->u.tag.tag, "datafield"))
287         {
288             control_field = 0;
289             marc_xml = 1;
290         }
291         else if (subf->which == DATA1N_data)
292         {
293             control_field = 1;
294             marc_xml = 0;
295         }
296         else
297         {
298             control_field = 0;
299             marc_xml = 0;
300         }
301
302         len += 4 + p->length_data_entry + p->length_starting
303             + p->length_implementation;
304         base_address += 3 + p->length_data_entry + p->length_starting
305             + p->length_implementation;
306
307         if (!control_field)
308             len += p->indicator_length;  
309
310         /* we'll allow no indicator if length is not 2 */
311         /* select when old XML format, since indicator is an element */
312         if (marc_xml == 0 && is_indicator (p, subf))
313             subf = subf->child;
314         
315         for (; subf; subf = subf->next)
316         {
317             if (!control_field)
318                 len += p->identifier_length;
319             get_data(subf, &dlen, control_field ? 0 : 1);
320             len += dlen;
321         }
322     }
323
324     if (!*buf)
325         *buf = (char *)xmalloc(*size = len);
326     else if (*size <= len)
327         *buf = (char *)xrealloc(*buf, *size = len);
328         
329     op = *buf;
330
331     /* we know the base address now */
332     memint (leader+12, base_address, 5);
333
334     /* copy temp leader to real output buf op */
335     memcpy (op, leader, 24);
336     memint (op, len, 5);
337     
338     entry_p = 24;
339     data_p = base_address;
340
341     for (field = n->child; field; field = field->next)
342     {
343         int control_field = 0;
344         int marc_xml = 0;
345         const char *tag = 0;
346
347         int data_0 = data_p;
348         char indicator_data[6];
349
350         memset (indicator_data, ' ', sizeof(indicator_data)-1);
351         indicator_data[sizeof(indicator_data)-1] = '\0';
352
353         if (field->which != DATA1N_tag)
354             continue;
355
356         if (selected && !field->u.tag.node_selected)
357             continue;
358
359         subf = field->child;
360         if (!subf)
361             continue;
362         
363         if (!yaz_matchstr(field->u.tag.tag, "mc?"))
364             continue;
365         else if (!strcmp(field->u.tag.tag, "leader"))
366             continue;
367         else if (!strcmp(field->u.tag.tag, "controlfield"))
368         {
369             control_field = 1;
370             marc_xml = 1;
371         }
372         else if (!strcmp(field->u.tag.tag, "datafield"))
373         {
374             control_field = 0;
375             marc_xml = 1;
376         }
377         else if (subf->which == DATA1N_data)
378         {
379             control_field = 1;
380             marc_xml = 0;
381         }
382         else
383         {
384             control_field = 0;
385             marc_xml = 0;
386         }
387         if (marc_xml == 0 && is_indicator (p, subf))
388         {
389             strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
390             subf = subf->child;
391         }
392         else if (marc_xml == 1 && !control_field)
393         {
394             data1_xattr *xa;
395             for (xa = field->u.tag.attributes; xa; xa = xa->next)
396             {
397                 if (!strcmp(xa->name, "ind1"))
398                     indicator_data[0] = xa->value[0];
399                 if (!strcmp(xa->name, "ind2"))
400                     indicator_data[1] = xa->value[0];
401                 if (!strcmp(xa->name, "ind3"))
402                     indicator_data[2] = xa->value[0];
403             }
404         }
405         if (!control_field)
406         {
407             memcpy (op + data_p, indicator_data, p->indicator_length);
408             data_p += p->indicator_length;
409         }
410         for (; subf; subf = subf->next)
411         {
412             char *data;
413
414             if (!control_field)
415             {
416                 const char *identifier = "a";
417                 if (marc_xml)
418                 {
419                     if (subf->which == DATA1N_tag &&
420                         !strcmp(subf->u.tag.tag, "subfield"))
421                     {
422                         data1_xattr *xa;
423                         for (xa = subf->u.tag.attributes; xa; xa = xa->next)
424                             if (!strcmp(xa->name, "code"))
425                                 identifier = xa->value;
426                     }
427                 }
428                 else if (subf->which != DATA1N_tag)
429                     yaz_log(YLOG_WARN, "Malformed fields for marc output.");
430                 else
431                     identifier = subf->u.tag.tag;
432                 op[data_p] = ISO2709_IDFS;
433                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
434                 data_p += p->identifier_length;
435             }
436             data = get_data(subf, &dlen, control_field ? 0 : 1);
437             memcpy (op + data_p, data, dlen);
438             data_p += dlen;
439         }
440         op[data_p++] = ISO2709_FS;
441
442         if (marc_xml)
443         {
444             data1_xattr *xa;
445             for (xa = field->u.tag.attributes; xa; xa = xa->next)
446                 if (!strcmp(xa->name, "tag"))
447                     tag = xa->value;
448         }
449         else
450             tag = field->u.tag.tag;
451
452         if (!tag || strlen(tag) != 3)
453             tag = "000";
454         memcpy (op + entry_p, tag, 3);
455         
456         entry_p += 3;
457         memint (op + entry_p, data_p - data_0, p->length_data_entry);
458         entry_p += p->length_data_entry;
459         memint (op + entry_p, data_0 - base_address, p->length_starting);
460         entry_p += p->length_starting;
461         entry_p += p->length_implementation;
462     }
463     op[entry_p++] = ISO2709_FS;
464     assert (entry_p == base_address);
465     op[data_p++] = ISO2709_RS;
466     assert (data_p == len);
467     return len;
468 }
469
470 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
471                        int selected, int *len)
472 {
473     int *size;
474     char **buf = data1_get_map_buf (dh, &size);
475
476     n = data1_get_root_tag (dh, n);
477     if (!n)
478         return 0;
479     *len = nodetomarc(dh, p, n, selected, buf, size);
480     return *buf;
481 }