d038d19cf4ee1a5114f28e77b73ae7cd7e20a76b
[idzebra-moved-to-github.git] / data1 / d1_marc.c
1 /* $Id: d1_marc.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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/oid.h>
30 #include <yaz/log.h>
31 #include <yaz/marcdisp.h>
32 #include <yaz/readconf.h>
33 #include <yaz/xmalloc.h>
34 #include <yaz/tpath.h>
35 #include <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(LOG_WARN|LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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(LOG_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)
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             for (i = 0; i<*len; i++)
179                 if (!d1_isspace(n->u.data.data[i]))
180                     break;
181             while (*len && d1_isspace(n->u.data.data[*len - 1]))
182                 (*len)--;
183             *len = *len - i;
184             if (*len > 0)
185                 return n->u.data.data + i;
186         }
187         if (n->which == DATA1N_tag)
188             n = n->child;
189         else if (n->which == DATA1N_data)
190             n = n->next;
191         else
192             break;      
193     }
194     r = "";
195     *len = strlen(r);
196     return r;
197 }
198
199 static void memint (char *p, int val, int len)
200 {
201     char buf[10];
202
203     if (len == 1)
204         *p = val + '0';
205     else
206     {
207         sprintf (buf, "%08d", val);
208         memcpy (p, buf+8-len, len);
209     }
210 }
211
212 static int is_indicator (data1_marctab *p, data1_node *subf)
213 {
214 #if 1
215     if (p->indicator_length != 2 ||
216         (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
217         return 1;
218 #else
219     if (subf && subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
220         return 1;
221 #endif
222     return 0;
223 }
224
225 static int nodetomarc(data1_handle dh,
226                       data1_marctab *p, data1_node *n, int selected,
227                       char **buf, int *size)
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     yaz_log (LOG_DEBUG, "nodetomarc");
237
238     for (field = n->child; field; field = field->next)
239     {
240         int is00X = 0;
241
242         if (field->which != DATA1N_tag)
243         {
244             yaz_log(LOG_WARN, "Malformed field composition for marc output.");
245             return -1;
246         }
247         if (selected && !field->u.tag.node_selected)
248             continue;
249
250         subf = field->child;
251         if (!subf)
252             continue;
253
254         len += 4 + p->length_data_entry + p->length_starting
255             + p->length_implementation;
256         base_address += 3 + p->length_data_entry + p->length_starting
257             + p->length_implementation;
258
259         if (subf->which == DATA1N_data)
260             is00X = 1;
261         if (!data1_is_xmlmode(dh))
262         {
263             if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
264                 is00X = 1;
265         }
266             
267         
268         if (!is00X)
269             len += p->indicator_length;  
270         /*  we'll allow no indicator if length is not 2 */
271         if (is_indicator (p, subf))
272             subf = subf->child;
273
274         for (; subf; subf = subf->next)
275         {
276             if (!is00X)
277                 len += p->identifier_length;
278             get_data(subf, &dlen);
279             len += dlen;
280         }
281     }
282
283     if (!*buf)
284         *buf = (char *)xmalloc(*size = len);
285     else if (*size <= len)
286         *buf = (char *)xrealloc(*buf, *size = len);
287         
288     op = *buf;
289     memint (op, len, 5);
290     memcpy (op+5, p->record_status, 1);
291     memcpy (op+6, p->implementation_codes, 4);
292     memint (op+10, p->indicator_length, 1);
293     memint (op+11, p->identifier_length, 1);
294     memint (op+12, base_address, 5);
295     memcpy (op+17, p->user_systems, 3);
296     memint (op+20, p->length_data_entry, 1);
297     memint (op+21, p->length_starting, 1);
298     memint (op+22, p->length_implementation, 1);
299     memcpy (op+23, p->future_use, 1);
300     
301     entry_p = 24;
302     data_p = base_address;
303
304     for (field = n->child; field; field = field->next)
305     {
306         int is00X = 0;
307
308         int data_0 = data_p;
309         char *indicator_data = "    ";
310         if (selected && !field->u.tag.node_selected)
311             continue;
312
313         subf = field->child;
314         if (!subf)
315             continue;
316
317         if (subf->which == DATA1N_data)
318             is00X = 1;
319         if (!data1_is_xmlmode(dh))
320         {
321             if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
322                 is00X = 1;
323         }
324
325         if (is_indicator (p, subf))
326         {
327             indicator_data = subf->u.tag.tag;
328             subf = subf->child;
329         }
330         if (!is00X)
331         {
332             memcpy (op + data_p, indicator_data, p->indicator_length);
333             data_p += p->indicator_length;
334         }
335         for (; subf; subf = subf->next)
336         {
337             char *data;
338
339             if (!is00X)
340             {
341                 const char *identifier = "a";
342                 if (subf->which != DATA1N_tag)
343                     yaz_log(LOG_WARN, "Malformed fields for marc output.");
344                 else
345                     identifier = subf->u.tag.tag;
346                 op[data_p] = ISO2709_IDFS;
347                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
348                 data_p += p->identifier_length;
349             }
350             data = get_data(subf, &dlen);
351             memcpy (op + data_p, data, dlen);
352             data_p += dlen;
353         }
354         op[data_p++] = ISO2709_FS;
355
356         memcpy (op + entry_p, field->u.tag.tag, 3);
357         entry_p += 3;
358         memint (op + entry_p, data_p - data_0, p->length_data_entry);
359         entry_p += p->length_data_entry;
360         memint (op + entry_p, data_0 - base_address, p->length_starting);
361         entry_p += p->length_starting;
362         entry_p += p->length_implementation;
363     }
364     op[entry_p++] = ISO2709_FS;
365     assert (entry_p == base_address);
366     op[data_p++] = ISO2709_RS;
367     assert (data_p == len);
368     return len;
369 }
370
371 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
372                        int selected, int *len)
373 {
374     int *size;
375     char **buf = data1_get_map_buf (dh, &size);
376
377     n = data1_get_root_tag (dh, n);
378     if (!n)
379         return 0;
380     *len = nodetomarc(dh, p, n, selected, buf, size);
381     return *buf;
382 }