1 /* $Id: d1_read.c,v 1.9 2004-08-04 08:35:22 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 * This module reads "loose" SGML and converts it to data1 tree
32 #include <yaz/xmalloc.h>
33 #include <yaz/yaz-util.h>
37 data1_node *data1_get_root_tag (data1_handle dh, data1_node *n)
41 if (data1_is_xmlmode(dh))
44 while (n && n->which != DATA1N_tag)
51 * get the tag which is the immediate parent of this node (this may mean
52 * traversing intermediate things like variants and stuff.
54 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
56 if (data1_is_xmlmode(dh))
58 for (; n && n->which != DATA1N_root; n = n->parent)
59 if (n->which == DATA1N_tag && n->parent &&
60 n->parent->which != DATA1N_root)
65 for (; n && n->which != DATA1N_root; n = n->parent)
66 if (n->which == DATA1N_tag)
72 data1_node *data1_mk_node (data1_handle dh, NMEM m)
74 return data1_mk_node2 (dh, m, DATA1N_root, 0);
77 data1_node *data1_mk_node_type (data1_handle dh, NMEM m, int type)
79 return data1_mk_node2 (dh, m, type, 0);
82 static void data1_init_node (data1_handle dh, data1_node *r, int type)
90 r->u.tag.no_data_requested = 0;
91 r->u.tag.node_selected = 0;
92 r->u.tag.make_variantlist = 0;
93 r->u.tag.get_bytes = -1;
94 r->u.tag.attributes = 0;
104 r->u.data.formatted_text = 0;
110 r->u.data.formatted_text = 1;
113 r->u.variant.type = 0;
114 r->u.variant.value = 0;
116 case DATA1N_preprocess:
117 r->u.preprocess.target = 0;
118 r->u.preprocess.attributes = 0;
121 logf (LOG_WARN, "data_mk_node_type. bad type = %d\n", type);
125 data1_node *data1_append_node (data1_handle dh, NMEM m, int type,
128 data1_node *r = (data1_node *)nmem_malloc(m, sizeof(*r));
129 r->next = r->child = r->last_child = 0;
137 r->root = parent->root;
139 parent->child = parent->last_child = r;
141 parent->last_child->next = r;
142 parent->last_child = r;
144 data1_init_node(dh, r, type);
148 data1_node *data1_mk_node2 (data1_handle dh, NMEM m, int type,
151 return data1_append_node (dh, m, type, parent);
154 data1_node *data1_insert_node (data1_handle dh, NMEM m, int type,
157 data1_node *r = (data1_node *)nmem_malloc(m, sizeof(*r));
158 r->next = r->child = r->last_child = 0;
165 r->root = parent->root;
168 parent->last_child = r;
170 r->next = parent->child;
173 data1_init_node(dh, r, type);
177 void data1_free_tree (data1_handle dh, data1_node *t)
179 data1_node *p = t->child, *pn;
184 data1_free_tree (dh, p);
191 data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name)
193 data1_absyn *absyn = data1_get_absyn (dh, name);
197 yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'",
199 /* It's now OK for a record not to have an absyn */
201 res = data1_mk_node2 (dh, nmem, DATA1N_root, 0);
202 res->u.root.type = data1_insert_string (dh, res, nmem, name);
203 res->u.root.absyn = absyn;
207 void data1_set_root(data1_handle dh, data1_node *res,
208 NMEM nmem, const char *name)
210 data1_absyn *absyn = data1_get_absyn (dh, name);
212 res->u.root.type = data1_insert_string (dh, res, nmem, name);
213 res->u.root.absyn = absyn;
216 void data1_add_attrs(data1_handle dh, NMEM nmem, const char **attr,
222 while (attr && *attr)
224 *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
225 (*p)->name = nmem_strdup (nmem, *attr++);
226 (*p)->value = nmem_strdup (nmem, *attr++);
227 (*p)->what = DATA1I_text;
234 data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem,
236 const char **attr, data1_node *at)
238 return data1_mk_preprocess_n (dh, nmem, target, strlen(target),
242 data1_node *data1_mk_preprocess_n (data1_handle dh, NMEM nmem,
243 const char *target, size_t len,
244 const char **attr, data1_node *at)
246 data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_preprocess, at);
247 res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
250 data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
254 data1_node *data1_insert_preprocess (data1_handle dh, NMEM nmem,
256 const char **attr, data1_node *at)
258 return data1_insert_preprocess_n (dh, nmem, target, strlen(target),
262 data1_node *data1_insert_preprocess_n (data1_handle dh, NMEM nmem,
263 const char *target, size_t len,
264 const char **attr, data1_node *at)
266 data1_node *res = data1_insert_node (dh, nmem, DATA1N_preprocess, at);
267 res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
270 data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
274 data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem,
275 const char *tag, size_t len, const char **attr,
278 data1_node *partag = get_parent_tag(dh, at);
279 data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_tag, at);
280 data1_element *e = 0;
282 res->u.tag.tag = data1_insert_string_n (dh, res, nmem, tag, len);
284 if (!partag) /* top tag? */
285 e = data1_getelementbytagname (dh, at->root->u.root.absyn,
286 0 /* index as local */,
290 /* only set element for known tags */
291 e = partag->u.tag.element;
293 e = data1_getelementbytagname (dh, at->root->u.root.absyn,
296 res->u.tag.element = e;
297 data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
301 void data1_tag_add_attr (data1_handle dh, NMEM nmem,
302 data1_node *res, const char **attr)
304 if (res->which != DATA1N_tag)
307 data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
310 data1_node *data1_mk_tag (data1_handle dh, NMEM nmem,
311 const char *tag, const char **attr, data1_node *at)
313 return data1_mk_tag_n (dh, nmem, tag, strlen(tag), attr, at);
316 data1_node *data1_search_tag (data1_handle dh, data1_node *n,
321 n = data1_get_root_tag (dh, n);
326 for (; n; n = n->next)
327 if (n->which == DATA1N_tag && n->u.tag.tag &&
328 !yaz_matchstr (n->u.tag.tag, tag))
335 data1_node *data1_mk_tag_uni (data1_handle dh, NMEM nmem,
336 const char *tag, data1_node *at)
338 data1_node *node = data1_search_tag (dh, at->child, tag);
340 node = data1_mk_tag (dh, nmem, tag, 0 /* attr */, at);
342 node->child = node->last_child = 0;
346 data1_node *data1_mk_text_n (data1_handle dh, NMEM mem,
347 const char *buf, size_t len, data1_node *parent)
349 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_data, parent);
350 res->u.data.what = DATA1I_text;
351 res->u.data.len = len;
353 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
357 data1_node *data1_mk_text_nf (data1_handle dh, NMEM mem,
358 const char *buf, size_t len, data1_node *parent)
360 data1_node *res = data1_mk_text_n (dh, mem, buf, len, parent);
361 res->u.data.formatted_text = 1;
365 data1_node *data1_mk_text (data1_handle dh, NMEM mem,
366 const char *buf, data1_node *parent)
368 return data1_mk_text_n (dh, mem, buf, strlen(buf), parent);
371 data1_node *data1_mk_comment_n (data1_handle dh, NMEM mem,
372 const char *buf, size_t len,
375 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_comment, parent);
376 res->u.data.what = DATA1I_text;
377 res->u.data.len = len;
379 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
383 data1_node *data1_mk_comment (data1_handle dh, NMEM mem,
384 const char *buf, data1_node *parent)
386 return data1_mk_comment_n (dh, mem, buf, strlen(buf), parent);
389 char *data1_insert_string_n (data1_handle dh, data1_node *res,
390 NMEM m, const char *str, size_t len)
393 if (len >= DATA1_LOCALDATA)
394 b = (char *) nmem_malloc (m, len+1);
397 memcpy (b, str, len);
402 char *data1_insert_string (data1_handle dh, data1_node *res,
403 NMEM m, const char *str)
405 return data1_insert_string_n (dh, res, m, str, strlen(str));
408 static data1_node *data1_add_insert_taggeddata(data1_handle dh,
410 const char *tagname, NMEM m,
414 data1_node *root = at->root;
415 data1_node *partag = get_parent_tag (dh, at);
416 data1_element *e = NULL;
417 data1_node *datn = 0;
418 data1_node *tagn = 0;
421 e = data1_getelementbytagname (dh, root->u.root.absyn, 0, tagname);
424 e = partag->u.tag.element;
426 e = data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
428 if (local_allowed || e)
431 tagn = data1_insert_node (dh, m, DATA1N_tag, at);
433 tagn = data1_append_node (dh, m, DATA1N_tag, at);
434 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
435 tagn->u.tag.element = e;
436 datn = data1_mk_node2 (dh, m, DATA1N_data, tagn);
441 data1_node *data1_mk_tag_data(data1_handle dh, data1_node *at,
442 const char *tagname, NMEM m)
444 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
449 * Insert a tagged node into the record root as first child of the node at
450 * which should be root or tag itself). Returns pointer to the data node,
451 * which can then be modified.
453 data1_node *data1_mk_tag_data_wd(data1_handle dh, data1_node *at,
454 const char *tagname, NMEM m)
456 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
459 data1_node *data1_insert_taggeddata (data1_handle dh, data1_node *root,
460 data1_node *at, const char *tagname,
463 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
466 data1_node *data1_add_taggeddata (data1_handle dh, data1_node *root,
467 data1_node *at, const char *tagname,
470 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
473 data1_node *data1_mk_tag_data_zint (data1_handle dh, data1_node *at,
474 const char *tag, zint num,
477 data1_node *node_data;
479 node_data = data1_mk_tag_data (dh, at, tag, nmem);
482 node_data->u.data.what = DATA1I_num;
483 node_data->u.data.data = node_data->lbuf;
484 sprintf (node_data->u.data.data, ZINT_FORMAT, num);
485 node_data->u.data.len = strlen (node_data->u.data.data);
489 data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
490 const char *tag, int num,
493 return data1_mk_tag_data_zint(dh, at, tag, num, nmem);
496 data1_node *data1_mk_tag_data_oid (data1_handle dh, data1_node *at,
497 const char *tag, Odr_oid *oid,
500 data1_node *node_data;
501 char str[128], *p = str;
504 node_data = data1_mk_tag_data (dh, at, tag, nmem);
508 for (ii = oid; *ii >= 0; ii++)
512 sprintf (p, "%d", *ii);
515 node_data->u.data.what = DATA1I_oid;
516 node_data->u.data.len = strlen (str);
517 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
522 data1_node *data1_mk_tag_data_text (data1_handle dh, data1_node *at,
523 const char *tag, const char *str,
526 data1_node *node_data;
528 node_data = data1_mk_tag_data (dh, at, tag, nmem);
531 node_data->u.data.what = DATA1I_text;
532 node_data->u.data.len = strlen (str);
533 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
538 data1_node *data1_mk_tag_data_text_uni (data1_handle dh, data1_node *at,
539 const char *tag, const char *str,
542 data1_node *node = data1_search_tag (dh, at->child, tag);
544 return data1_mk_tag_data_text (dh, at, tag, str, nmem);
547 data1_node *node_data = node->child;
548 node_data->u.data.what = DATA1I_text;
549 node_data->u.data.len = strlen (str);
550 node_data->u.data.data = data1_insert_string (dh, node_data,
552 node_data->child = node_data->last_child = 0;
557 static int ampr (int (*get_byte)(void *fh), void *fh, int *amp)
560 int c = (*get_byte)(fh);
564 int c = (*get_byte)(fh);
579 if (!strcmp (ent, "quot"))
581 if (!strcmp (ent, "apos"))
583 if (!strcmp (ent, "gt"))
585 if (!strcmp (ent, "lt"))
587 if (!strcmp (ent, "amp"))
592 else if (c == 0 || d1_isspace(c))
602 data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
603 int (*get_byte)(void *fh), void *fh,
604 WRBUF wrbuf, int *ch, int *amp)
606 data1_xattr *p_first = 0;
607 data1_xattr **pp = &p_first;
613 while (*amp || (c && d1_isspace(c)))
614 c = ampr (get_byte, fh, amp);
615 if (*amp == 0 && (c == 0 || c == '>' || c == '/'))
617 *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
621 p->what = DATA1I_xmltext;
624 while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
626 wrbuf_putc (wrbuf, c);
627 c = ampr (get_byte, fh, amp);
629 wrbuf_putc (wrbuf, '\0');
630 len = wrbuf_len(wrbuf);
631 p->name = (char*) nmem_malloc (m, len);
632 strcpy (p->name, wrbuf_buf(wrbuf));
635 c = ampr (get_byte, fh, amp);
636 if (*amp == 0 && c == '"')
638 c = ampr (get_byte, fh, amp);
640 while (*amp || (c && c != '"'))
642 wrbuf_putc (wrbuf, c);
643 c = ampr (get_byte, fh, amp);
646 c = ampr (get_byte, fh, amp);
648 else if (*amp == 0 && c == '\'')
650 c = ampr (get_byte, fh, amp);
652 while (*amp || (c && c != '\''))
654 wrbuf_putc (wrbuf, c);
655 c = ampr (get_byte, fh, amp);
658 c = ampr (get_byte, fh, amp);
663 while (*amp || (c && c != '>' && c != '/'))
665 wrbuf_putc (wrbuf, c);
666 c = ampr (get_byte, fh, amp);
669 wrbuf_putc (wrbuf, '\0');
670 len = wrbuf_len(wrbuf);
671 p->value = (char*) nmem_malloc (m, len);
672 strcpy (p->value, wrbuf_buf(wrbuf));
680 * Ugh. Sometimes functions just grow and grow on you. This one reads a
681 * 'node' and its children.
683 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
684 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
686 data1_node *d1_stack[256];
693 c = ampr (get_byte, fh, &);
696 data1_node *parent = level ? d1_stack[level-1] : 0;
698 if (amp == 0 && c == '<') /* beginning of tag */
708 c = ampr (get_byte, fh, &);
709 if (amp == 0 && c == '/')
712 c = ampr (get_byte, fh, &);
714 else if (amp == 0 && c == '!')
720 c0 = ampr (get_byte, fh, &0);
721 if (amp0 == 0 && c0 == '\0')
723 c = ampr (get_byte, fh, &);
725 if (amp0 == 0 && c0 == '-' && amp == 0 && c == '-')
727 /* COMMENT: <!-- ... --> */
730 c = ampr (get_byte, fh, &);
733 if (amp == 0 && c == '-')
735 else if (amp == 0 && c == '>' && no_dash >= 2)
741 wrbuf_buf(wrbuf), wrbuf_len(wrbuf)-2,
743 c = ampr (get_byte, fh, &); /* skip > */
748 wrbuf_putc (wrbuf, c);
749 c = ampr (get_byte, fh, &);
754 { /* DIRECTIVE: <! .. > */
759 if (amp == 0 && c == '>' && blevel == 0)
761 c = ampr (get_byte, fh, &);
764 if (amp == 0 && c == '[')
766 if (amp == 0 && c == ']' && blevel > 0)
768 c = ampr (get_byte, fh, &);
773 while (amp || (c && c != '>' && c != '/' && !d1_isspace(c)))
775 if (i < (sizeof(tag)-1))
777 c = ampr (get_byte, fh, &);
780 xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c, &);
782 if (amp == 0 && c == '/')
783 { /* <tag attrs/> or <tag/> */
785 c = ampr (get_byte, fh, &);
789 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
793 c = ampr (get_byte, fh, &);
805 parent = d1_stack[--i];
806 if ((parent->which == DATA1N_root &&
807 !strcmp(tag, parent->u.root.type)) ||
808 (parent->which == DATA1N_tag &&
809 !strcmp(tag, parent->u.tag.tag)))
817 yaz_log (LOG_WARN, "%d: no begin tag for %s",
822 if (data1_is_xmlmode(dh))
834 else if (!strcmp(tag, "var"))
836 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
840 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
842 yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
846 data1_getvartypebyct(dh,
847 parent->root->u.root.absyn->varset,
851 * If we're the first variant in this group, create a parent
852 * variant, and insert it before the current variant.
854 if (parent->which != DATA1N_variant)
856 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
861 * now determine if one of our ancestor triples is of
862 * same type. If so, we break here.
865 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
866 if (d1_stack[i]->u.variant.type == tp)
871 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
872 res->u.variant.type = tp;
873 res->u.variant.value =
874 data1_insert_string (dh, res, m, args + val_offset);
880 /* tag .. acquire our element in the abstract syntax */
883 parent = data1_mk_root (dh, m, tag);
884 res = d1_stack[level] = parent;
886 if (data1_is_xmlmode(dh))
889 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
890 res->u.tag.attributes = xattr;
895 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
896 res->u.tag.attributes = xattr;
899 d1_stack[level] = res;
900 d1_stack[level+1] = 0;
901 if (level < 250 && !null_tag)
904 else /* != '<'... this is a body of text */
910 c = ampr (get_byte, fh, &);
913 res = data1_mk_node2 (dh, m, DATA1N_data, parent);
914 res->u.data.what = DATA1I_xmltext;
915 res->u.data.formatted_text = 0;
916 d1_stack[level] = res;
920 while (amp || (c && c != '<'))
922 wrbuf_putc (wrbuf, c);
923 c = ampr (get_byte, fh, &);
925 len = wrbuf_len(wrbuf);
927 /* use local buffer of nmem if too large */
928 if (len >= DATA1_LOCALDATA)
929 res->u.data.data = (char*) nmem_malloc (m, len);
931 res->u.data.data = res->lbuf;
934 memcpy (res->u.data.data, wrbuf_buf(wrbuf), len);
936 res->u.data.data = 0;
937 res->u.data.len = len;
943 int getc_mem (void *fh)
945 const char **p = (const char **) fh;
951 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
953 WRBUF wrbuf = wrbuf_alloc();
956 node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
957 wrbuf_free (wrbuf, 1);
962 * Read a record in the native syntax.
964 data1_node *data1_read_record(data1_handle dh,
965 int (*rf)(void *, char *, size_t), void *fh,
969 char **buf = data1_get_read_buf (dh, &size);
974 *buf = (char *)xmalloc(*size = 4096);
978 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
980 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
986 return data1_read_node(dh, &bp, m);
995 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
997 const char *bp = buf;
998 return data1_read_node (dh, &bp, m);
1002 static int conv_item (NMEM m, yaz_iconv_t t,
1003 WRBUF wrbuf, char *inbuf, size_t inlen)
1005 wrbuf_rewind (wrbuf);
1006 if (wrbuf->size < 10)
1007 wrbuf_grow (wrbuf, 10);
1010 char *outbuf = wrbuf->buf + wrbuf->pos;
1011 size_t outlen = wrbuf->size - wrbuf->pos;
1012 if (yaz_iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
1013 (size_t)(-1) && yaz_iconv_error(t) != YAZ_ICONV_E2BIG)
1015 /* bad data. stop and skip conversion entirely */
1018 else if (inlen == 0)
1019 { /* finished converting */
1020 wrbuf->pos = wrbuf->size - outlen;
1025 /* buffer too small: make sure we expand buffer */
1026 wrbuf->pos = wrbuf->size - outlen;
1027 wrbuf_grow(wrbuf, 20);
1033 static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
1034 yaz_iconv_t t, WRBUF wrbuf, const char *tocode)
1036 for (; n; n = n->next)
1041 case DATA1N_comment:
1042 if (conv_item (m, t, wrbuf, n->u.data.data, n->u.data.len) == 0)
1045 data1_insert_string_n (dh, n, m, wrbuf->buf,
1047 n->u.data.len = wrbuf->pos;
1051 if (conv_item (m, t, wrbuf, n->u.tag.tag, strlen(n->u.tag.tag))
1055 data1_insert_string_n (dh, n, m,
1056 wrbuf->buf, wrbuf->pos);
1058 if (n->u.tag.attributes)
1061 for (p = n->u.tag.attributes; p; p = p->next)
1064 conv_item(m, t, wrbuf, p->value, strlen(p->value))
1067 wrbuf_puts (wrbuf, "");
1068 p->value = nmem_strdup (m, wrbuf->buf);
1073 case DATA1N_preprocess:
1074 if (strcmp(n->u.preprocess.target, "xml") == 0)
1076 data1_xattr *p = n->u.preprocess.attributes;
1077 for (; p; p = p->next)
1078 if (strcmp (p->name, "encoding") == 0)
1079 p->value = nmem_strdup (m, tocode);
1083 data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
1087 const char *data1_get_encoding (data1_handle dh, data1_node *n)
1089 /* see if we have an xml header that specifies encoding */
1090 if (n && n->child && n->child->which == DATA1N_preprocess &&
1091 strcmp (n->child->u.preprocess.target, "xml") == 0)
1093 data1_xattr *xp = n->child->u.preprocess.attributes;
1094 for (; xp; xp = xp->next)
1095 if (!strcmp (xp->name, "encoding") == 0)
1098 /* no encoding in header, so see if "encoding" was specified for abs */
1099 if (n && n->which == DATA1N_root &&
1100 n->u.root.absyn && n->u.root.absyn->encoding)
1101 return n->u.root.absyn->encoding;
1102 /* none of above, return a hard coded default */
1103 return "ISO-8859-1";
1106 int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
1108 const char *fromcode)
1110 if (yaz_matchstr (tocode, fromcode))
1112 WRBUF wrbuf = wrbuf_alloc();
1113 yaz_iconv_t t = yaz_iconv_open (tocode, fromcode);
1116 data1_iconv_s (dh, m, n, t, wrbuf, tocode);
1117 yaz_iconv_close (t);
1118 wrbuf_free (wrbuf, 1);
1123 void data1_concat_text(data1_handle dh, NMEM m, data1_node *n)
1125 for (; n; n = n->next)
1127 if (n->which == DATA1N_data && n->next &&
1128 n->next->which == DATA1N_data)
1134 for (np = n; np && np->which == DATA1N_data; np=np->next)
1135 sz += np->u.data.len;
1136 ndata = nmem_malloc(m, sz);
1137 for (np = n; np && np->which == DATA1N_data; np=np->next)
1139 memcpy(ndata+off, np->u.data.data, np->u.data.len);
1140 off += np->u.data.len;
1142 n->u.data.data = ndata;
1145 if (!np && n->parent)
1146 n->parent->last_child = n;
1149 data1_concat_text(dh, m, n->child);