2 * Copyright (C) 1997-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 2000-05-15 15:32:51 adam
8 * Added support for 64 bit input file support.
10 * Revision 1.10 1999/11/30 13:48:04 adam
11 * Improved installation. Updated for inclusion of YAZ header files.
13 * Revision 1.9 1999/06/25 13:47:25 adam
14 * Minor change that prevents MSVC warning.
16 * Revision 1.8 1999/05/26 07:49:14 adam
19 * Revision 1.7 1999/05/20 12:57:18 adam
20 * Implemented TCL filter. Updated recctrl system.
22 * Revision 1.6 1999/02/02 14:51:27 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.5 1997/11/18 10:03:24 adam
26 * Member num_children removed from data1_node.
28 * Revision 1.4 1997/10/27 14:34:26 adam
29 * Fixed bug - data1 root node wasn't tagged at all!
31 * Revision 1.3 1997/09/24 13:36:51 adam
32 * *** empty log message ***
34 * Revision 1.2 1997/09/17 12:19:21 adam
35 * Zebra version corresponds to YAZ version 1.4.
36 * Changed Zebra server so that it doesn't depend on global common_resource.
38 * Revision 1.1 1997/09/04 13:54:40 adam
39 * Added MARC filter - type grs.marc.<syntax> where syntax refers
40 * to abstract syntax. New method tellf in retrieve/extract method.
48 #include <yaz/yaz-util.h>
49 #include <yaz/marcdisp.h>
52 data1_node *data1_mk_node_wp (data1_handle dh, NMEM mem, data1_node *parent)
54 data1_node *res = data1_mk_node (dh, mem);
60 res->root = parent->root;
63 parent->child = parent->last_child = res;
65 parent->last_child->next = res;
66 parent->last_child = res;
71 static void destroy_data (struct data1_node *n)
73 assert (n->which == DATA1N_data);
74 xfree (n->u.data.data);
77 data1_node *data1_mk_node_text (data1_handle dh, NMEM mem, data1_node *parent,
78 const char *buf, size_t len)
80 data1_node *res = data1_mk_node_wp (dh, mem, parent);
81 res->which = DATA1N_data;
82 res->u.data.formatted_text = 0;
83 res->u.data.what = DATA1I_text;
84 res->u.data.len = len;
85 if (res->u.data.len > DATA1_LOCALDATA) {
86 res->u.data.data = (char *) xmalloc (res->u.data.len);
87 res->destroy = destroy_data;
90 res->u.data.data = res->lbuf;
91 memcpy (res->u.data.data, buf, res->u.data.len);
95 data1_node *data1_mk_node_tag (data1_handle dh, NMEM mem, data1_node *parent,
96 const char *tag, size_t len)
98 data1_element *elem = NULL;
99 data1_node *partag = get_parent_tag(dh, parent);
101 data1_element *e = NULL;
104 res = data1_mk_node_wp (dh, mem, parent);
106 res->which = DATA1N_tag;
107 res->u.tag.tag = res->lbuf;
108 res->u.tag.get_bytes = -1;
110 if (len >= DATA1_LOCALDATA)
111 len = DATA1_LOCALDATA-1;
113 memcpy (res->u.tag.tag, tag, len);
114 res->u.tag.tag[len] = '\0';
116 if (parent->which == DATA1N_variant)
119 if (!(e = partag->u.tag.element))
122 elem = data1_getelementbytagname (dh, res->root->u.root.absyn, e,
124 res->u.tag.element = elem;
125 res->u.tag.node_selected = 0;
126 res->u.tag.make_variantlist = 0;
127 res->u.tag.no_data_requested = 0;
133 data1_node *grs_read_marc (struct grs_read_info *p)
138 int indicator_length;
139 int identifier_length;
141 int length_data_entry;
143 int length_implementation;
149 data1_node *res_root;
152 data1_marctab *marctab;
154 if ((*p->readf)(p->fh, buf, 5) != 5)
156 record_length = atoi_n (buf, 5);
157 if (record_length < 25)
159 logf (LOG_WARN, "MARC record length < 25, is %d", record_length);
162 /* read remaining part - attempt to read one byte furhter... */
163 read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
164 if (read_bytes < record_length-5)
166 logf (LOG_WARN, "Couldn't read whole MARC record");
169 if (read_bytes == record_length - 4)
171 off_t cur_offset = (*p->tellf)(p->fh);
172 if (cur_offset <= 27)
175 (*p->endf)(p->fh, cur_offset - 1);
178 logf (LOG_DEBUG, "absynName = %s", absynName);
179 if (!(absyn = data1_get_absyn (p->dh, absynName)))
181 logf (LOG_WARN, "Unknown abstract syntax: %s", absynName);
184 res_root = data1_mk_node_wp (p->dh, p->mem, NULL);
185 res_root->which = DATA1N_root;
186 res_root->u.root.type = (char *) nmem_malloc (p->mem, strlen(absynName)+1);
187 strcpy (res_root->u.root.type, absynName);
188 res_root->u.root.absyn = absyn;
190 marctab = absyn->marc;
192 if (marctab && marctab->force_indicator_length >= 0)
193 indicator_length = marctab->force_indicator_length;
195 indicator_length = atoi_n (buf+10, 1);
196 if (marctab && marctab->force_identifier_length >= 0)
197 identifier_length = marctab->force_identifier_length;
199 identifier_length = atoi_n (buf+11, 1);
200 base_address = atoi_n (buf+12, 4);
203 length_data_entry = atoi_n (buf+20, 1);
204 length_starting = atoi_n (buf+21, 1);
205 length_implementation = atoi_n (buf+22, 1);
207 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
208 entry_p += 3+length_data_entry+length_starting;
209 base_address = entry_p+1;
210 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
218 data1_node *parent = res_root;
220 memcpy (tag, buf+entry_p, 3);
224 /* generate field node */
225 res = data1_mk_node_tag (p->dh, p->mem, res_root, tag, 3);
228 fprintf (outf, "%s ", tag);
230 data_length = atoi_n (buf+entry_p, length_data_entry);
231 entry_p += length_data_entry;
232 data_offset = atoi_n (buf+entry_p, length_starting);
233 entry_p += length_starting;
234 i = data_offset + base_address;
235 end_offset = i+data_length-1;
237 if (memcmp (tag, "00", 2) && indicator_length)
239 /* generate indicator node */
243 res = data1_mk_node_tag (p->dh, p->mem, res, buf+i,
246 for (j = 0; j<indicator_length; j++)
247 fprintf (outf, "%c", buf[j+i]);
249 i += indicator_length;
252 /* traverse sub fields */
254 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
256 if (memcmp (tag, "00", 2) && identifier_length)
259 data1_mk_node_tag (p->dh, p->mem, parent,
260 buf+i+1, identifier_length-1);
262 fprintf (outf, " $");
263 for (j = 1; j<identifier_length; j++)
264 fprintf (outf, "%c", buf[j+i]);
267 i += identifier_length;
269 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
270 buf[i] != ISO2709_FS && i < end_offset)
273 fprintf (outf, "%c", buf[i]);
277 data1_mk_node_text (p->dh, p->mem, res, buf + i0, i - i0);
283 fprintf (outf, "%c", buf[i]);
290 data1_node *res = data1_mk_node_tag (p->dh, p->mem,
292 data1_mk_node_text (p->dh, p->mem, res, buf + i0, i - i0);
295 fprintf (outf, "\n");
297 fprintf (outf, "-- separator but not at end of field\n");
298 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
299 fprintf (outf, "-- no separator at end of field\n");
305 static void *grs_init_marc(void)
310 static void grs_destroy_marc(void *clientData)
314 static struct recTypeGrs marc_type = {
321 RecTypeGrs recTypeGrs_marc = &marc_type;