source: trunk/gsdl/packages/yaz/retrieval/d1_marc.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

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