5c156011c7e29f9a6b174da9ae48fa4b0e7c8dbb
[yaz-moved-to-github.git] / retrieval / d1_marc.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: d1_marc.c,v 1.24 2002-08-23 14:27:18 adam Exp $
7  */
8
9
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <yaz/oid.h>
15 #include <yaz/log.h>
16 #include <yaz/marcdisp.h>
17 #include <yaz/readconf.h>
18 #include <yaz/xmalloc.h>
19 #include <yaz/data1.h>
20 #include <yaz/tpath.h>
21
22 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
23 {
24     FILE *f;
25     NMEM mem = data1_nmem_get (dh);
26     data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
27     char line[512], *argv[50];
28     int lineno = 0;
29     int argc;
30     
31     if (!(f = data1_path_fopen(dh, file, "r")))
32     {
33         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
34         return 0;
35     }
36
37     res->name = 0;
38     res->reference = VAL_NONE;
39     res->next = 0;
40     res->length_data_entry = 4;
41     res->length_starting = 5;
42     res->length_implementation = 0;
43     strcpy(res->future_use, "4");
44
45     strcpy(res->record_status, "n");
46     strcpy(res->implementation_codes, "    ");
47     res->indicator_length = 2;
48     res->identifier_length = 2;
49     res->force_indicator_length = -1;
50     res->force_identifier_length = -1;
51     strcpy(res->user_systems, "z  ");
52     
53     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
54         if (!strcmp(*argv, "name"))
55         {
56             if (argc != 2)
57             {
58                 yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
59                         *argv);
60                 continue;
61             }
62             res->name = nmem_strdup(mem, argv[1]);
63         }
64         else if (!strcmp(*argv, "reference"))
65         {
66             if (argc != 2)
67             {
68                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
69                         *argv);
70                 continue;
71             }
72             if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
73             {
74                 yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
75                         file, lineno, argv[1]);
76                 continue;
77             }
78         }
79         else if (!strcmp(*argv, "length-data-entry"))
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             res->length_data_entry = atoi(argv[1]);
88         }
89         else if (!strcmp(*argv, "length-starting"))
90         {
91             if (argc != 2)
92             {
93                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
94                         *argv);
95                 continue;
96             }
97             res->length_starting = atoi(argv[1]);
98         }
99         else if (!strcmp(*argv, "length-implementation"))
100         {
101             if (argc != 2)
102             {
103                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
104                         *argv);
105                 continue;
106             }
107             res->length_implementation = atoi(argv[1]);
108         }
109         else if (!strcmp(*argv, "future-use"))
110         {
111             if (argc != 2)
112             {
113                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
114                         *argv);
115                 continue;
116             }
117             strncpy(res->future_use, argv[1], 2);
118         }
119         else if (!strcmp(*argv, "force-indicator-length"))
120         {
121             if (argc != 2)
122             {
123                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
124                         *argv);
125                 continue;
126             }
127             res->force_indicator_length = atoi(argv[1]);
128         }
129         else if (!strcmp(*argv, "force-identifier-length"))
130         {
131             if (argc != 2)
132             {
133                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
134                         *argv);
135                 continue;
136             }
137             res->force_identifier_length = atoi(argv[1]);
138         }
139         else
140             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
141                     *argv);
142
143     fclose(f);
144     return res;
145 }
146
147
148 /*
149  * Locate some data under this node. This routine should handle variants
150  * prettily.
151  */
152 static char *get_data(data1_node *n, int *len)
153 {
154     char *r;
155     data1_node *np = 0;
156
157     while (n)
158     {
159         if (n->which == DATA1N_data)
160         {
161             int i;
162             *len = n->u.data.len;
163
164             for (i = 0; i<*len; i++)
165                 if (!d1_isspace(n->u.data.data[i]))
166                     break;
167             while (*len && d1_isspace(n->u.data.data[*len - 1]))
168                 (*len)--;
169             *len = *len - i;
170             if (*len > 0)
171                 return n->u.data.data + i;
172         }
173         if (n->which == DATA1N_tag)
174             np = n->child;
175         n = n->next;
176         if (!n)
177         {
178             n = np;
179             np = 0;
180         }
181     }
182     r = "";
183     *len = strlen(r);
184     return r;
185 }
186
187 static void memint (char *p, int val, int len)
188 {
189     char buf[10];
190
191     if (len == 1)
192         *p = val + '0';
193     else
194     {
195         sprintf (buf, "%08d", val);
196         memcpy (p, buf+8-len, len);
197     }
198 }
199
200 static int is_indicator (data1_marctab *p, data1_node *subf)
201 {
202 #if 1
203     if (p->indicator_length != 2 ||
204         (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
205         return 1;
206 #else
207     if (subf && subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
208         return 1;
209 #endif
210     return 0;
211 }
212
213 static int nodetomarc(data1_handle dh,
214                       data1_marctab *p, data1_node *n, int selected,
215                       char **buf, int *size)
216 {
217     int len = 26;
218     int dlen;
219     int base_address = 25;
220     int entry_p, data_p;
221     char *op;
222     data1_node *field, *subf;
223
224     yaz_log (LOG_DEBUG, "nodetomarc");
225
226     for (field = n->child; field; field = field->next)
227     {
228         int is00X = 0;
229
230         if (field->which != DATA1N_tag)
231         {
232             yaz_log(LOG_WARN, "Malformed field composition for marc output.");
233             return -1;
234         }
235         if (selected && !field->u.tag.node_selected)
236             continue;
237
238         subf = field->child;
239         if (!subf)
240             continue;
241
242         len += 4 + p->length_data_entry + p->length_starting
243             + p->length_implementation;
244         base_address += 3 + p->length_data_entry + p->length_starting
245             + p->length_implementation;
246
247         if (subf->which == DATA1N_data)
248             is00X = 1;
249         if (!data1_is_xmlmode(dh))
250         {
251             if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
252                 is00X = 1;
253         }
254             
255         
256         if (!is00X)
257             len += p->indicator_length;  
258         /*  we'll allow no indicator if length is not 2 */
259         if (is_indicator (p, subf))
260             subf = subf->child;
261
262         for (; subf; subf = subf->next)
263         {
264             if (!is00X)
265                 len += p->identifier_length;
266             get_data(subf, &dlen);
267             len += dlen;
268         }
269     }
270
271     if (!*buf)
272         *buf = (char *)xmalloc(*size = len);
273     else if (*size <= len)
274         *buf = (char *)xrealloc(*buf, *size = len);
275         
276     op = *buf;
277     memint (op, len, 5);
278     memcpy (op+5, p->record_status, 1);
279     memcpy (op+6, p->implementation_codes, 4);
280     memint (op+10, p->indicator_length, 1);
281     memint (op+11, p->identifier_length, 1);
282     memint (op+12, base_address, 5);
283     memcpy (op+17, p->user_systems, 3);
284     memint (op+20, p->length_data_entry, 1);
285     memint (op+21, p->length_starting, 1);
286     memint (op+22, p->length_implementation, 1);
287     memcpy (op+23, p->future_use, 1);
288     
289     entry_p = 24;
290     data_p = base_address;
291
292     for (field = n->child; field; field = field->next)
293     {
294         int is00X = 0;
295
296         int data_0 = data_p;
297         char *indicator_data = "    ";
298         if (selected && !field->u.tag.node_selected)
299             continue;
300
301         subf = field->child;
302         if (!subf)
303             continue;
304
305         if (subf->which == DATA1N_data)
306             is00X = 1;
307         if (!data1_is_xmlmode(dh))
308         {
309             if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
310                 is00X = 1;
311         }
312
313         if (is_indicator (p, subf))
314         {
315             indicator_data = subf->u.tag.tag;
316             subf = subf->child;
317         }
318         if (!is00X)
319         {
320             memcpy (op + data_p, indicator_data, p->indicator_length);
321             data_p += p->indicator_length;
322         }
323         for (; subf; subf = subf->next)
324         {
325             char *data;
326
327             if (!is00X)
328             {
329                 const char *identifier = "a";
330                 if (subf->which != DATA1N_tag)
331                     yaz_log(LOG_WARN, "Malformed fields for marc output.");
332                 else
333                     identifier = subf->u.tag.tag;
334                 op[data_p] = ISO2709_IDFS;
335                 memcpy (op + data_p+1, identifier, p->identifier_length-1);
336                 data_p += p->identifier_length;
337             }
338             data = get_data(subf, &dlen);
339             memcpy (op + data_p, data, dlen);
340             data_p += dlen;
341         }
342         op[data_p++] = ISO2709_FS;
343
344         memcpy (op + entry_p, field->u.tag.tag, 3);
345         entry_p += 3;
346         memint (op + entry_p, data_p - data_0, p->length_data_entry);
347         entry_p += p->length_data_entry;
348         memint (op + entry_p, data_0 - base_address, p->length_starting);
349         entry_p += p->length_starting;
350         entry_p += p->length_implementation;
351     }
352     op[entry_p++] = ISO2709_FS;
353     assert (entry_p == base_address);
354     op[data_p++] = ISO2709_RS;
355     assert (data_p == len);
356     return len;
357 }
358
359 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
360                        int selected, int *len)
361 {
362     int *size;
363     char **buf = data1_get_map_buf (dh, &size);
364
365     n = data1_get_root_tag (dh, n);
366     if (!n)
367         return 0;
368     *len = nodetomarc(dh, p, n, selected, buf, size);
369     return *buf;
370 }