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