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