Renamed logf function to yaz_log. Removed VC++ project files.
[yaz-moved-to-github.git] / retrieval / d1_marc.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_marc.c,v $
7  * Revision 1.14  1999-08-27 09:40:32  adam
8  * Renamed logf function to yaz_log. Removed VC++ project files.
9  *
10  * Revision 1.13  1998/10/13 16:09:52  adam
11  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
12  * Added support for multiple attribute set references and tagset references
13  * from an abstract syntax file.
14  * Fixed many bad logs-calls in routines that read the various
15  * specifications regarding data1 (*.abs,*.att,...) and made the messages
16  * consistent whenever possible.
17  * Added extra 'lineno' argument to function readconf_line.
18  *
19  * Revision 1.12  1998/02/23 10:57:09  adam
20  * Take care of integer data nodes as well in conversion.
21  *
22  * Revision 1.11  1998/02/11 11:53:35  adam
23  * Changed code so that it compiles as C++.
24  *
25  * Revision 1.10  1997/09/30 11:50:04  adam
26  * Added handler data1_get_map_buf that is used by data1_nodetomarc.
27  *
28  * Revision 1.9  1997/09/24 13:35:45  adam
29  * Added two members to data1_marctab to ease reading of weird MARC records.
30  *
31  * Revision 1.8  1997/09/17 12:10:37  adam
32  * YAZ version 1.4.
33  *
34  * Revision 1.7  1997/09/05 09:50:57  adam
35  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
36  *
37  * Revision 1.6  1997/09/04 13:51:58  adam
38  * Added data1 to marc conversion with indicators.
39  *
40  * Revision 1.5  1997/09/04 13:48:04  adam
41  * Added data1 to marc conversion.
42  *
43  * Revision 1.4  1996/03/25 10:18:03  quinn
44  * Removed trailing whitespace from data elements
45  *
46  * Revision 1.3  1995/11/01  16:34:57  quinn
47  * Making data1 look for tables in data1_tabpath
48  *
49  * Revision 1.2  1995/11/01  13:54:48  quinn
50  * Minor adjustments
51  *
52  * Revision 1.1  1995/11/01  11:56:08  quinn
53  * Added Retrieval (data management) functions en masse.
54  *
55  *
56  */
57
58
59 #include <assert.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <ctype.h>
63
64 #include <oid.h>
65 #include <log.h>
66 #include <marcdisp.h>
67 #include <readconf.h>
68 #include <xmalloc.h>
69 #include <data1.h>
70 #include <tpath.h>
71
72 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
73 {
74     FILE *f;
75     NMEM mem = data1_nmem_get (dh);
76     data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
77     char line[512], *argv[50];
78     int lineno = 0;
79     int argc;
80     
81     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
82     {
83         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
84         return 0;
85     }
86
87     res->name = 0;
88     res->reference = VAL_NONE;
89     res->next = 0;
90     res->length_data_entry = 4;
91     res->length_starting = 5;
92     res->length_implementation = 0;
93     strcpy(res->future_use, "4");
94
95     strcpy(res->record_status, "n");
96     strcpy(res->implementation_codes, "    ");
97     res->indicator_length = 2;
98     res->identifier_length = 2;
99     res->force_indicator_length = -1;
100     res->force_identifier_length = -1;
101     strcpy(res->user_systems, "z  ");
102     
103     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
104         if (!strcmp(*argv, "name"))
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->name = nmem_strdup(mem, argv[1]);
113         }
114         else if (!strcmp(*argv, "reference"))
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             if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
123             {
124                 yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
125                         file, lineno, argv[1]);
126                 continue;
127             }
128         }
129         else if (!strcmp(*argv, "length-data-entry"))
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->length_data_entry = atoi(argv[1]);
138         }
139         else if (!strcmp(*argv, "length-starting"))
140         {
141             if (argc != 2)
142             {
143                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
144                         *argv);
145                 continue;
146             }
147             res->length_starting = atoi(argv[1]);
148         }
149         else if (!strcmp(*argv, "length-implementation"))
150         {
151             if (argc != 2)
152             {
153                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
154                         *argv);
155                 continue;
156             }
157             res->length_implementation = atoi(argv[1]);
158         }
159         else if (!strcmp(*argv, "future-use"))
160         {
161             if (argc != 2)
162             {
163                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
164                         *argv);
165                 continue;
166             }
167             strncpy(res->future_use, argv[1], 2);
168         }
169         else if (!strcmp(*argv, "force-indicator-length"))
170         {
171             if (argc != 2)
172             {
173                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
174                         *argv);
175                 continue;
176             }
177             res->force_indicator_length = atoi(argv[1]);
178         }
179         else if (!strcmp(*argv, "force-identifier-length"))
180         {
181             if (argc != 2)
182             {
183                 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
184                         *argv);
185                 continue;
186             }
187             res->force_identifier_length = atoi(argv[1]);
188         }
189         else
190             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
191                     *argv);
192
193     fclose(f);
194     return res;
195 }
196
197 /*
198  * Locate some data under this node. This routine should handle variants
199  * prettily.
200  */
201 static char *get_data(data1_node *n, int *len)
202 {
203     char *r;
204
205     while (n->which != DATA1N_data && n->child)
206         n = n->child;
207     if (n->which != DATA1N_data || 
208         (n->u.data.what != DATA1I_text && n->u.data.what != DATA1I_num))
209     {
210         r = "[Structured/included data]";
211         *len = strlen(r);
212         return r;
213     }
214
215     *len = n->u.data.len;
216     while (*len && isspace(n->u.data.data[*len - 1]))
217         (*len)--;
218     return n->u.data.data;
219 }
220
221 static void memint (char *p, int val, int len)
222 {
223     char buf[10];
224
225     if (len == 1)
226         *p = val + '0';
227     else
228     {
229         sprintf (buf, "%08d", val);
230         memcpy (p, buf+8-len, len);
231     }
232 }
233
234 static int is_indicator (data1_marctab *p, data1_node *subf)
235 {
236 #if 1
237     if (p->indicator_length != 2 ||
238         (subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
239         return 1;
240 #else
241     if (subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
242         return 1;
243 #endif
244     return 0;
245 }
246
247 static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
248     char **buf, int *size)
249 {
250     int len = 26;
251     int dlen;
252     int base_address = 25;
253     int entry_p, data_p;
254     char *op;
255     data1_node *field, *subf;
256
257     yaz_log (LOG_DEBUG, "nodetomarc");
258     for (field = n->child; field; field = field->next)
259     {
260         if (field->which != DATA1N_tag)
261         {
262             yaz_log(LOG_WARN, "Malformed field composition for marc output.");
263             return -1;
264         }
265         if (selected && !field->u.tag.node_selected)
266             continue;
267         len += 4 + p->length_data_entry + p->length_starting
268             + p->length_implementation;
269         base_address += 3 + p->length_data_entry + p->length_starting
270             + p->length_implementation;
271         if (strncmp(field->u.tag.tag, "00", 2))
272             len += p->indicator_length;      /* this is fairly bogus */
273         subf = field->child;
274         
275         /*  we'll allow no indicator if length is not 2 */
276         if (is_indicator (p, subf))
277             subf = subf->child;
278
279         for (; subf; subf = subf->next)
280         {
281             if (subf->which != DATA1N_tag)
282             {
283                 yaz_log(LOG_WARN,
284                     "Malformed subfield composition for marc output.");
285                 return -1;
286             }
287             if (strncmp(field->u.tag.tag, "00", 2))
288                 len += p->identifier_length;
289             get_data(subf, &dlen);
290             len += dlen;
291         }
292     }
293
294     if (!*buf)
295         *buf = (char *)xmalloc(*size = len);
296     else if (*size <= len)
297         *buf = (char *)xrealloc(*buf, *size = len);
298         
299     op = *buf;
300     memint (op, len, 5);
301     memcpy (op+5, p->record_status, 1);
302     memcpy (op+6, p->implementation_codes, 4);
303     memint (op+10, p->indicator_length, 1);
304     memint (op+11, p->identifier_length, 1);
305     memint (op+12, base_address, 5);
306     memcpy (op+17, p->user_systems, 3);
307     memint (op+20, p->length_data_entry, 1);
308     memint (op+21, p->length_starting, 1);
309     memint (op+22, p->length_implementation, 1);
310     memcpy (op+23, p->future_use, 1);
311     
312     entry_p = 24;
313     data_p = base_address;
314
315     for (field = n->child; field; field = field->next)
316     {
317         int data_0 = data_p;
318         char *indicator_data = "    ";
319         if (selected && !field->u.tag.node_selected)
320             continue;
321
322         subf = field->child;
323
324         if (is_indicator (p, subf))
325         {
326             indicator_data = subf->u.tag.tag;
327             subf = subf->child;
328         }
329         if (strncmp(field->u.tag.tag, "00", 2))   /* bogus */
330         {
331             memcpy (op + data_p, indicator_data, p->indicator_length);
332             data_p += p->indicator_length;
333         }
334         for (; subf; subf = subf->next)
335         {
336             char *data;
337
338             if (strncmp(field->u.tag.tag, "00", 2))
339             {
340                 op[data_p] = ISO2709_IDFS;
341                 memcpy (op + data_p+1, subf->u.tag.tag, p->identifier_length-1);
342                 data_p += p->identifier_length;
343             }
344             data = get_data(subf, &dlen);
345             memcpy (op + data_p, data, dlen);
346             data_p += dlen;
347         }
348         op[data_p++] = ISO2709_FS;
349
350         memcpy (op + entry_p, field->u.tag.tag, 3);
351         entry_p += 3;
352         memint (op + entry_p, data_p - data_0, p->length_data_entry);
353         entry_p += p->length_data_entry;
354         memint (op + entry_p, data_0 - base_address, p->length_starting);
355         entry_p += p->length_starting;
356         entry_p += p->length_implementation;
357     }
358     op[entry_p++] = ISO2709_FS;
359     assert (entry_p == base_address);
360     op[data_p++] = ISO2709_RS;
361     assert (data_p == len);
362     return len;
363 }
364
365 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
366                        int selected, int *len)
367 {
368     int *size;
369     char **buf = data1_get_map_buf (dh, &size);
370
371     *len = nodetomarc(p, n, selected, buf, size);
372     return *buf;
373 }