1 /* $Id: d1_read.c,v 1.7 2004-07-26 12:20:07 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)
306 if (res->which != DATA1N_tag)
309 p = &res->u.tag.attributes;
310 data1_add_attrs(dh, nmem, attr, p);
314 data1_node *data1_mk_tag (data1_handle dh, NMEM nmem,
315 const char *tag, const char **attr, data1_node *at)
317 return data1_mk_tag_n (dh, nmem, tag, strlen(tag), attr, at);
320 data1_node *data1_search_tag (data1_handle dh, data1_node *n,
325 n = data1_get_root_tag (dh, n);
330 for (; n; n = n->next)
331 if (n->which == DATA1N_tag && n->u.tag.tag &&
332 !yaz_matchstr (n->u.tag.tag, tag))
339 data1_node *data1_mk_tag_uni (data1_handle dh, NMEM nmem,
340 const char *tag, data1_node *at)
342 data1_node *node = data1_search_tag (dh, at->child, tag);
344 node = data1_mk_tag (dh, nmem, tag, 0 /* attr */, at);
346 node->child = node->last_child = 0;
350 data1_node *data1_mk_text_n (data1_handle dh, NMEM mem,
351 const char *buf, size_t len, data1_node *parent)
353 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_data, parent);
354 res->u.data.what = DATA1I_text;
355 res->u.data.len = len;
357 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
361 data1_node *data1_mk_text_nf (data1_handle dh, NMEM mem,
362 const char *buf, size_t len, data1_node *parent)
364 data1_node *res = data1_mk_text_n (dh, mem, buf, len, parent);
365 res->u.data.formatted_text = 1;
369 data1_node *data1_mk_text (data1_handle dh, NMEM mem,
370 const char *buf, data1_node *parent)
372 return data1_mk_text_n (dh, mem, buf, strlen(buf), parent);
375 data1_node *data1_mk_comment_n (data1_handle dh, NMEM mem,
376 const char *buf, size_t len,
379 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_comment, parent);
380 res->u.data.what = DATA1I_text;
381 res->u.data.len = len;
383 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
387 data1_node *data1_mk_comment (data1_handle dh, NMEM mem,
388 const char *buf, data1_node *parent)
390 return data1_mk_comment_n (dh, mem, buf, strlen(buf), parent);
393 char *data1_insert_string_n (data1_handle dh, data1_node *res,
394 NMEM m, const char *str, size_t len)
397 if (len >= DATA1_LOCALDATA)
398 b = (char *) nmem_malloc (m, len+1);
401 memcpy (b, str, len);
406 char *data1_insert_string (data1_handle dh, data1_node *res,
407 NMEM m, const char *str)
409 return data1_insert_string_n (dh, res, m, str, strlen(str));
412 static data1_node *data1_add_insert_taggeddata(data1_handle dh,
414 const char *tagname, NMEM m,
418 data1_node *root = at->root;
419 data1_node *partag = get_parent_tag (dh, at);
420 data1_element *e = NULL;
421 data1_node *datn = 0;
422 data1_node *tagn = 0;
425 e = data1_getelementbytagname (dh, root->u.root.absyn, 0, tagname);
428 e = partag->u.tag.element;
430 e = data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
432 if (local_allowed || e)
435 tagn = data1_insert_node (dh, m, DATA1N_tag, at);
437 tagn = data1_append_node (dh, m, DATA1N_tag, at);
438 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
439 tagn->u.tag.element = e;
440 datn = data1_mk_node2 (dh, m, DATA1N_data, tagn);
445 data1_node *data1_mk_tag_data(data1_handle dh, data1_node *at,
446 const char *tagname, NMEM m)
448 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
453 * Insert a tagged node into the record root as first child of the node at
454 * which should be root or tag itself). Returns pointer to the data node,
455 * which can then be modified.
457 data1_node *data1_mk_tag_data_wd(data1_handle dh, data1_node *at,
458 const char *tagname, NMEM m)
460 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
463 data1_node *data1_insert_taggeddata (data1_handle dh, data1_node *root,
464 data1_node *at, const char *tagname,
467 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
470 data1_node *data1_add_taggeddata (data1_handle dh, data1_node *root,
471 data1_node *at, const char *tagname,
474 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
477 data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
478 const char *tag, int num,
481 data1_node *node_data;
483 node_data = data1_mk_tag_data (dh, at, tag, nmem);
486 node_data->u.data.what = DATA1I_num;
487 node_data->u.data.data = node_data->lbuf;
488 sprintf (node_data->u.data.data, "%d", num);
489 node_data->u.data.len = strlen (node_data->u.data.data);
493 data1_node *data1_mk_tag_data_oid (data1_handle dh, data1_node *at,
494 const char *tag, Odr_oid *oid,
497 data1_node *node_data;
498 char str[128], *p = str;
501 node_data = data1_mk_tag_data (dh, at, tag, nmem);
505 for (ii = oid; *ii >= 0; ii++)
509 sprintf (p, "%d", *ii);
512 node_data->u.data.what = DATA1I_oid;
513 node_data->u.data.len = strlen (str);
514 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
519 data1_node *data1_mk_tag_data_text (data1_handle dh, data1_node *at,
520 const char *tag, const char *str,
523 data1_node *node_data;
525 node_data = data1_mk_tag_data (dh, at, tag, nmem);
528 node_data->u.data.what = DATA1I_text;
529 node_data->u.data.len = strlen (str);
530 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
535 data1_node *data1_mk_tag_data_text_uni (data1_handle dh, data1_node *at,
536 const char *tag, const char *str,
539 data1_node *node = data1_search_tag (dh, at->child, tag);
541 return data1_mk_tag_data_text (dh, at, tag, str, nmem);
544 data1_node *node_data = node->child;
545 node_data->u.data.what = DATA1I_text;
546 node_data->u.data.len = strlen (str);
547 node_data->u.data.data = data1_insert_string (dh, node_data,
549 node_data->child = node_data->last_child = 0;
554 static int ampr (int (*get_byte)(void *fh), void *fh, int *amp)
557 int c = (*get_byte)(fh);
561 int c = (*get_byte)(fh);
576 if (!strcmp (ent, "quot"))
578 if (!strcmp (ent, "apos"))
580 if (!strcmp (ent, "gt"))
582 if (!strcmp (ent, "lt"))
584 if (!strcmp (ent, "amp"))
589 else if (c == 0 || d1_isspace(c))
599 data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
600 int (*get_byte)(void *fh), void *fh,
601 WRBUF wrbuf, int *ch, int *amp)
603 data1_xattr *p_first = 0;
604 data1_xattr **pp = &p_first;
610 while (*amp || (c && d1_isspace(c)))
611 c = ampr (get_byte, fh, amp);
612 if (*amp == 0 && (c == 0 || c == '>' || c == '/'))
614 *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
618 p->what = DATA1I_xmltext;
621 while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
623 wrbuf_putc (wrbuf, c);
624 c = ampr (get_byte, fh, amp);
626 wrbuf_putc (wrbuf, '\0');
627 len = wrbuf_len(wrbuf);
628 p->name = (char*) nmem_malloc (m, len);
629 strcpy (p->name, wrbuf_buf(wrbuf));
632 c = ampr (get_byte, fh, amp);
633 if (*amp == 0 && c == '"')
635 c = ampr (get_byte, fh, amp);
637 while (*amp || (c && c != '"'))
639 wrbuf_putc (wrbuf, c);
640 c = ampr (get_byte, fh, amp);
643 c = ampr (get_byte, fh, amp);
645 else if (*amp == 0 && c == '\'')
647 c = ampr (get_byte, fh, amp);
649 while (*amp || (c && c != '\''))
651 wrbuf_putc (wrbuf, c);
652 c = ampr (get_byte, fh, amp);
655 c = ampr (get_byte, fh, amp);
660 while (*amp || (c && c != '>' && c != '/'))
662 wrbuf_putc (wrbuf, c);
663 c = ampr (get_byte, fh, amp);
666 wrbuf_putc (wrbuf, '\0');
667 len = wrbuf_len(wrbuf);
668 p->value = (char*) nmem_malloc (m, len);
669 strcpy (p->value, wrbuf_buf(wrbuf));
677 * Ugh. Sometimes functions just grow and grow on you. This one reads a
678 * 'node' and its children.
680 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
681 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
683 data1_node *d1_stack[256];
690 c = ampr (get_byte, fh, &);
693 data1_node *parent = level ? d1_stack[level-1] : 0;
695 if (amp == 0 && c == '<') /* beginning of tag */
705 c = ampr (get_byte, fh, &);
706 if (amp == 0 && c == '/')
709 c = ampr (get_byte, fh, &);
711 else if (amp == 0 && c == '!')
717 c0 = ampr (get_byte, fh, &0);
718 if (amp0 == 0 && c0 == '\0')
720 c = ampr (get_byte, fh, &);
722 if (amp0 == 0 && c0 == '-' && amp == 0 && c == '-')
724 /* COMMENT: <!-- ... --> */
727 c = ampr (get_byte, fh, &);
730 if (amp == 0 && c == '-')
732 else if (amp == 0 && c == '>' && no_dash >= 2)
738 wrbuf_buf(wrbuf), wrbuf_len(wrbuf)-2,
740 c = ampr (get_byte, fh, &); /* skip > */
745 wrbuf_putc (wrbuf, c);
746 c = ampr (get_byte, fh, &);
751 { /* DIRECTIVE: <! .. > */
756 if (amp == 0 && c == '>' && blevel == 0)
758 c = ampr (get_byte, fh, &);
761 if (amp == 0 && c == '[')
763 if (amp == 0 && c == ']' && blevel > 0)
765 c = ampr (get_byte, fh, &);
770 while (amp || (c && c != '>' && c != '/' && !d1_isspace(c)))
772 if (i < (sizeof(tag)-1))
774 c = ampr (get_byte, fh, &);
777 xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c, &);
779 if (amp == 0 && c == '/')
780 { /* <tag attrs/> or <tag/> */
782 c = ampr (get_byte, fh, &);
786 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
790 c = ampr (get_byte, fh, &);
802 parent = d1_stack[--i];
803 if ((parent->which == DATA1N_root &&
804 !strcmp(tag, parent->u.root.type)) ||
805 (parent->which == DATA1N_tag &&
806 !strcmp(tag, parent->u.tag.tag)))
814 yaz_log (LOG_WARN, "%d: no begin tag for %s",
819 if (data1_is_xmlmode(dh))
831 else if (!strcmp(tag, "var"))
833 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
837 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
839 yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
843 data1_getvartypebyct(dh,
844 parent->root->u.root.absyn->varset,
848 * If we're the first variant in this group, create a parent
849 * variant, and insert it before the current variant.
851 if (parent->which != DATA1N_variant)
853 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
858 * now determine if one of our ancestor triples is of
859 * same type. If so, we break here.
862 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
863 if (d1_stack[i]->u.variant.type == tp)
868 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
869 res->u.variant.type = tp;
870 res->u.variant.value =
871 data1_insert_string (dh, res, m, args + val_offset);
877 /* tag .. acquire our element in the abstract syntax */
880 parent = data1_mk_root (dh, m, tag);
881 res = d1_stack[level] = parent;
883 if (data1_is_xmlmode(dh))
886 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
887 res->u.tag.attributes = xattr;
892 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
893 res->u.tag.attributes = xattr;
896 d1_stack[level] = res;
897 d1_stack[level+1] = 0;
898 if (level < 250 && !null_tag)
901 else /* != '<'... this is a body of text */
907 c = ampr (get_byte, fh, &);
910 res = data1_mk_node2 (dh, m, DATA1N_data, parent);
911 res->u.data.what = DATA1I_xmltext;
912 res->u.data.formatted_text = 0;
913 d1_stack[level] = res;
917 while (amp || (c && c != '<'))
919 wrbuf_putc (wrbuf, c);
920 c = ampr (get_byte, fh, &);
922 len = wrbuf_len(wrbuf);
924 /* use local buffer of nmem if too large */
925 if (len >= DATA1_LOCALDATA)
926 res->u.data.data = (char*) nmem_malloc (m, len);
928 res->u.data.data = res->lbuf;
931 memcpy (res->u.data.data, wrbuf_buf(wrbuf), len);
933 res->u.data.data = 0;
934 res->u.data.len = len;
940 int getc_mem (void *fh)
942 const char **p = (const char **) fh;
948 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
950 WRBUF wrbuf = wrbuf_alloc();
953 node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
954 wrbuf_free (wrbuf, 1);
959 * Read a record in the native syntax.
961 data1_node *data1_read_record(data1_handle dh,
962 int (*rf)(void *, char *, size_t), void *fh,
966 char **buf = data1_get_read_buf (dh, &size);
971 *buf = (char *)xmalloc(*size = 4096);
975 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
977 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
983 return data1_read_node(dh, &bp, m);
992 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
994 const char *bp = buf;
995 return data1_read_node (dh, &bp, m);
999 static int conv_item (NMEM m, yaz_iconv_t t,
1000 WRBUF wrbuf, char *inbuf, size_t inlen)
1002 wrbuf_rewind (wrbuf);
1003 if (wrbuf->size < 10)
1004 wrbuf_grow (wrbuf, 10);
1007 char *outbuf = wrbuf->buf + wrbuf->pos;
1008 size_t outlen = wrbuf->size - wrbuf->pos;
1009 if (yaz_iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
1010 (size_t)(-1) && yaz_iconv_error(t) != YAZ_ICONV_E2BIG)
1012 /* bad data. stop and skip conversion entirely */
1015 else if (inlen == 0)
1016 { /* finished converting */
1017 wrbuf->pos = wrbuf->size - outlen;
1022 /* buffer too small: make sure we expand buffer */
1023 wrbuf->pos = wrbuf->size - outlen;
1024 wrbuf_grow(wrbuf, 20);
1030 static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
1031 yaz_iconv_t t, WRBUF wrbuf, const char *tocode)
1033 for (; n; n = n->next)
1038 case DATA1N_comment:
1039 if (conv_item (m, t, wrbuf, n->u.data.data, n->u.data.len) == 0)
1042 data1_insert_string_n (dh, n, m, wrbuf->buf,
1044 n->u.data.len = wrbuf->pos;
1048 if (conv_item (m, t, wrbuf, n->u.tag.tag, strlen(n->u.tag.tag))
1052 data1_insert_string_n (dh, n, m,
1053 wrbuf->buf, wrbuf->pos);
1055 if (n->u.tag.attributes)
1058 for (p = n->u.tag.attributes; p; p = p->next)
1061 conv_item(m, t, wrbuf, p->value, strlen(p->value))
1064 wrbuf_puts (wrbuf, "");
1065 p->value = nmem_strdup (m, wrbuf->buf);
1070 case DATA1N_preprocess:
1071 if (strcmp(n->u.preprocess.target, "xml") == 0)
1073 data1_xattr *p = n->u.preprocess.attributes;
1074 for (; p; p = p->next)
1075 if (strcmp (p->name, "encoding") == 0)
1076 p->value = nmem_strdup (m, tocode);
1080 data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
1084 const char *data1_get_encoding (data1_handle dh, data1_node *n)
1086 /* see if we have an xml header that specifies encoding */
1087 if (n && n->child && n->child->which == DATA1N_preprocess &&
1088 strcmp (n->child->u.preprocess.target, "xml") == 0)
1090 data1_xattr *xp = n->child->u.preprocess.attributes;
1091 for (; xp; xp = xp->next)
1092 if (!strcmp (xp->name, "encoding") == 0)
1095 /* no encoding in header, so see if "encoding" was specified for abs */
1096 if (n && n->which == DATA1N_root &&
1097 n->u.root.absyn && n->u.root.absyn->encoding)
1098 return n->u.root.absyn->encoding;
1099 /* none of above, return a hard coded default */
1100 return "ISO-8859-1";
1103 int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
1105 const char *fromcode)
1107 if (yaz_matchstr (tocode, fromcode))
1109 WRBUF wrbuf = wrbuf_alloc();
1110 yaz_iconv_t t = yaz_iconv_open (tocode, fromcode);
1113 data1_iconv_s (dh, m, n, t, wrbuf, tocode);
1114 yaz_iconv_close (t);
1115 wrbuf_free (wrbuf, 1);
1120 void data1_concat_text(data1_handle dh, NMEM m, data1_node *n)
1122 for (; n; n = n->next)
1124 if (n->which == DATA1N_data && n->next &&
1125 n->next->which == DATA1N_data)
1131 for (np = n; np && np->which == DATA1N_data; np=np->next)
1132 sz += np->u.data.len;
1133 ndata = nmem_malloc(m, sz);
1134 for (np = n; np && np->which == DATA1N_data; np=np->next)
1136 memcpy(ndata+off, np->u.data.data, np->u.data.len);
1137 off += np->u.data.len;
1139 n->u.data.data = ndata;
1142 if (!np && n->parent)
1143 n->parent->last_child = n;
1146 data1_concat_text(dh, m, n->child);