1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
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
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
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
20 /* converts data1 tree to ISO2709/MARC record */
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>
34 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
37 NMEM mem = data1_nmem_get (dh);
38 data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
39 char line[512], *argv[50];
43 if (!(f = data1_path_fopen(dh, file, "r")))
49 res->length_data_entry = 4;
50 res->length_starting = 5;
51 res->length_implementation = 0;
52 strcpy(res->future_use, "4");
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 ");
62 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
63 if (!strcmp(*argv, "name"))
67 yaz_log(YLOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
71 res->name = nmem_strdup(mem, argv[1]);
73 else if (!strcmp(*argv, "reference"))
77 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
81 res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
82 CLASS_TAGSET, argv[1],
86 yaz_log(YLOG_WARN, "%s:%d: Unknown tagset reference '%s'",
87 file, lineno, argv[1]);
91 else if (!strcmp(*argv, "length-data-entry"))
95 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
99 res->length_data_entry = atoi(argv[1]);
101 else if (!strcmp(*argv, "length-starting"))
105 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
109 res->length_starting = atoi(argv[1]);
111 else if (!strcmp(*argv, "length-implementation"))
115 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
119 res->length_implementation = atoi(argv[1]);
121 else if (!strcmp(*argv, "future-use"))
125 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
129 strncpy(res->future_use, argv[1], 2);
131 else if (!strcmp(*argv, "force-indicator-length"))
135 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
139 res->force_indicator_length = atoi(argv[1]);
141 else if (!strcmp(*argv, "force-identifier-length"))
145 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
149 res->force_identifier_length = atoi(argv[1]);
151 else if (!strcmp(*argv, "implementation-codes"))
155 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
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);
164 memcpy(res->implementation_codes, argv[1], strlen(argv[1]));
167 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
175 static void get_data2(data1_node *n, int *len, char *dst, size_t max)
181 if (n->which == DATA1N_data)
183 if (dst && *len < max)
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);
190 *len += n->u.data.len;
192 if (n->which == DATA1N_tag)
194 else if (n->which == DATA1N_data)
201 static void memint (char *p, int val, int len)
209 sprintf (buf, "%08d", val);
210 memcpy (p, buf+8-len, len);
214 /* check for indicator. non MARCXML only */
215 static int is_indicator (data1_marctab *p, data1_node *subf)
217 if (p->indicator_length != 2 ||
218 (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
223 static int nodetomarc(data1_handle dh,
224 data1_marctab *p, data1_node *n, int selected,
225 char **buf, int *size)
231 int base_address = 25;
234 data1_node *field, *subf;
237 data1_pr_tree(dh, n, stdout);
239 yaz_log (YLOG_DEBUG, "nodetomarc");
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);
251 for (field = n->child; field; field = field->next)
253 int control_field = 0; /* 00X fields - usually! */
256 if (field->which != DATA1N_tag)
258 if (selected && !field->u.tag.node_selected)
265 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
267 else if (!strcmp(field->u.tag.tag, "leader"))
270 get_data2(subf, &dlen, leader, 24);
273 else if (!strcmp(field->u.tag.tag, "controlfield"))
278 else if (!strcmp(field->u.tag.tag, "datafield"))
283 else if (subf->which == DATA1N_data)
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;
300 len += p->indicator_length;
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))
307 for (; subf; subf = subf->next)
311 if (marc_xml && subf->which != DATA1N_tag)
312 continue; /* we skip comments, cdata .. */
313 len += p->identifier_length;
315 get_data2(subf, &dlen, 0, 0);
321 *buf = (char *)xmalloc(*size = len);
322 else if (*size <= len)
323 *buf = (char *)xrealloc(*buf, *size = len);
327 /* we know the base address now */
328 memint (leader+12, base_address, 5);
330 /* copy temp leader to real output buf op */
331 memcpy (op, leader, 24);
335 data_p = base_address;
337 for (field = n->child; field; field = field->next)
339 int control_field = 0;
344 char indicator_data[6];
346 memset (indicator_data, ' ', sizeof(indicator_data)-1);
347 indicator_data[sizeof(indicator_data)-1] = '\0';
349 if (field->which != DATA1N_tag)
352 if (selected && !field->u.tag.node_selected)
359 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
361 else if (!strcmp(field->u.tag.tag, "leader"))
363 else if (!strcmp(field->u.tag.tag, "controlfield"))
368 else if (!strcmp(field->u.tag.tag, "datafield"))
373 else if (subf->which == DATA1N_data)
383 if (marc_xml == 0 && is_indicator (p, subf))
385 strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
388 else if (marc_xml == 1 && !control_field)
391 for (xa = field->u.tag.attributes; xa; xa = xa->next)
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];
403 memcpy (op + data_p, indicator_data, p->indicator_length);
404 data_p += p->indicator_length;
406 for (; subf; subf = subf->next)
410 const char *identifier = "a";
414 if (subf->which != DATA1N_tag)
416 if (strcmp(subf->u.tag.tag, "subfield"))
417 yaz_log(YLOG_WARN, "Unhandled tag %s",
420 for (xa = subf->u.tag.attributes; xa; xa = xa->next)
421 if (!strcmp(xa->name, "code"))
422 identifier = xa->value;
424 else if (subf->which != DATA1N_tag)
425 yaz_log(YLOG_WARN, "Malformed fields for marc output.");
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;
432 get_data2(subf, &dlen, op + data_p, 100000);
435 op[data_p++] = ISO2709_FS;
440 for (xa = field->u.tag.attributes; xa; xa = xa->next)
441 if (!strcmp(xa->name, "tag"))
445 tag = field->u.tag.tag;
447 if (!tag || strlen(tag) != 3)
449 memcpy (op + entry_p, tag, 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;
458 op[entry_p++] = ISO2709_FS;
459 assert (entry_p == base_address);
460 op[data_p++] = ISO2709_RS;
461 assert (data_p == len);
465 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
466 int selected, int *len)
469 char **buf = data1_get_map_buf (dh, &size);
471 n = data1_get_root_tag (dh, n);
474 *len = nodetomarc(dh, p, n, selected, buf, size);
480 * c-file-style: "Stroustrup"
481 * indent-tabs-mode: nil
483 * vim: shiftwidth=4 tabstop=8 expandtab