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