2 * Copyright (C) 1997-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1999-02-02 14:51:27 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.5 1997/11/18 10:03:24 adam
11 * Member num_children removed from data1_node.
13 * Revision 1.4 1997/10/27 14:34:26 adam
14 * Fixed bug - data1 root node wasn't tagged at all!
16 * Revision 1.3 1997/09/24 13:36:51 adam
17 * *** empty log message ***
19 * Revision 1.2 1997/09/17 12:19:21 adam
20 * Zebra version corresponds to YAZ version 1.4.
21 * Changed Zebra server so that it doesn't depend on global common_resource.
23 * Revision 1.1 1997/09/04 13:54:40 adam
24 * Added MARC filter - type grs.marc.<syntax> where syntax refers
25 * to abstract syntax. New method tellf in retrieve/extract method.
37 data1_node *data1_mk_node_wp (data1_handle dh, NMEM mem, data1_node *parent)
39 data1_node *res = data1_mk_node (dh, mem);
45 res->root = parent->root;
48 parent->child = parent->last_child = res;
50 parent->last_child->next = res;
51 parent->last_child = res;
56 static void destroy_data (struct data1_node *n)
58 assert (n->which == DATA1N_data);
59 xfree (n->u.data.data);
62 data1_node *data1_mk_node_text (data1_handle dh, NMEM mem, data1_node *parent,
63 const char *buf, size_t len)
65 data1_node *res = data1_mk_node_wp (dh, mem, parent);
66 res->which = DATA1N_data;
67 res->u.data.formatted_text = 0;
68 res->u.data.what = DATA1I_text;
69 res->u.data.len = len;
70 if (res->u.data.len > DATA1_LOCALDATA) {
71 res->u.data.data = xmalloc (res->u.data.len);
72 res->destroy = destroy_data;
75 res->u.data.data = res->lbuf;
76 memcpy (res->u.data.data, buf, res->u.data.len);
80 data1_node *data1_mk_node_tag (data1_handle dh, NMEM mem, data1_node *parent,
81 const char *tag, size_t len)
83 data1_element *elem = NULL;
84 data1_node *partag = get_parent_tag(dh, parent);
86 data1_element *e = NULL;
89 res = data1_mk_node_wp (dh, mem, parent);
91 res->which = DATA1N_tag;
92 res->u.tag.tag = res->lbuf;
93 res->u.tag.get_bytes = -1;
95 if (len >= DATA1_LOCALDATA)
96 len = DATA1_LOCALDATA-1;
98 memcpy (res->u.tag.tag, tag, len);
99 res->u.tag.tag[len] = '\0';
101 if (parent->which == DATA1N_variant)
104 if (!(e = partag->u.tag.element))
107 elem = data1_getelementbytagname (dh, res->root->u.root.absyn, e,
109 res->u.tag.element = elem;
110 res->u.tag.node_selected = 0;
111 res->u.tag.make_variantlist = 0;
112 res->u.tag.no_data_requested = 0;
118 data1_node *grs_read_marc (struct grs_read_info *p)
123 int indicator_length;
124 int identifier_length;
126 int length_data_entry;
128 int length_implementation;
134 data1_node *res_root;
137 data1_marctab *marctab;
139 if ((*p->readf)(p->fh, buf, 5) != 5)
141 record_length = atoi_n (buf, 5);
142 if (record_length < 25)
144 logf (LOG_WARN, "MARC record length < 25, is %d", record_length);
147 /* read remaining part - attempt to read one byte furhter... */
148 read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
149 if (read_bytes < record_length-5)
151 logf (LOG_WARN, "Couldn't read whole MARC record");
154 if (read_bytes == record_length - 4)
156 off_t cur_offset = (*p->tellf)(p->fh);
157 assert (cur_offset > 26);
159 (*p->endf)(p->fh, cur_offset - 1);
162 logf (LOG_DEBUG, "absynName = %s", absynName);
163 if (!(absyn = data1_get_absyn (p->dh, absynName)))
165 logf (LOG_WARN, "Unknown abstract syntax: %s", absynName);
168 res_root = data1_mk_node_wp (p->dh, p->mem, NULL);
169 res_root->which = DATA1N_root;
170 res_root->u.root.type = nmem_malloc (p->mem, strlen(absynName)+1);
171 strcpy (res_root->u.root.type, absynName);
172 res_root->u.root.absyn = absyn;
174 marctab = absyn->marc;
176 if (marctab && marctab->force_indicator_length >= 0)
177 indicator_length = marctab->force_indicator_length;
179 indicator_length = atoi_n (buf+10, 1);
180 if (marctab && marctab->force_identifier_length >= 0)
181 identifier_length = marctab->force_identifier_length;
183 identifier_length = atoi_n (buf+11, 1);
184 base_address = atoi_n (buf+12, 4);
187 length_data_entry = atoi_n (buf+20, 1);
188 length_starting = atoi_n (buf+21, 1);
189 length_implementation = atoi_n (buf+22, 1);
191 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
192 entry_p += 3+length_data_entry+length_starting;
193 base_address = entry_p+1;
194 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
202 data1_node *parent = res_root;
204 memcpy (tag, buf+entry_p, 3);
208 /* generate field node */
209 res = data1_mk_node_tag (p->dh, p->mem, res_root, tag, 3);
212 fprintf (outf, "%s ", tag);
214 data_length = atoi_n (buf+entry_p, length_data_entry);
215 entry_p += length_data_entry;
216 data_offset = atoi_n (buf+entry_p, length_starting);
217 entry_p += length_starting;
218 i = data_offset + base_address;
219 end_offset = i+data_length-1;
221 if (memcmp (tag, "00", 2) && indicator_length)
223 /* generate indicator node */
227 res = data1_mk_node_tag (p->dh, p->mem, res, buf+i,
230 for (j = 0; j<indicator_length; j++)
231 fprintf (outf, "%c", buf[j+i]);
233 i += indicator_length;
236 /* traverse sub fields */
238 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
240 if (memcmp (tag, "00", 2) && identifier_length)
243 data1_mk_node_tag (p->dh, p->mem, parent,
244 buf+i+1, identifier_length-1);
246 fprintf (outf, " $");
247 for (j = 1; j<identifier_length; j++)
248 fprintf (outf, "%c", buf[j+i]);
251 i += identifier_length;
253 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
254 buf[i] != ISO2709_FS && i < end_offset)
257 fprintf (outf, "%c", buf[i]);
261 data1_mk_node_text (p->dh, p->mem, res, buf + i0, i - i0);
267 fprintf (outf, "%c", buf[i]);
274 data1_node *res = data1_mk_node_tag (p->dh, p->mem,
276 data1_mk_node_text (p->dh, p->mem, res, buf + i0, i - i0);
279 fprintf (outf, "\n");
281 fprintf (outf, "-- separator but not at end of field\n");
282 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
283 fprintf (outf, "-- no separator at end of field\n");