1 /* $Id: d1_read.c,v 1.19 2006-06-13 12:02:02 adam Exp $
2 Copyright (C) 1995-2005
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/yaz-util.h>
35 data1_node *data1_get_root_tag (data1_handle dh, data1_node *n)
39 if (data1_is_xmlmode(dh))
42 while (n && n->which != DATA1N_tag)
49 * get the tag which is the immediate parent of this node (this may mean
50 * traversing intermediate things like variants and stuff.
52 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
54 if (data1_is_xmlmode(dh))
56 for (; n && n->which != DATA1N_root; n = n->parent)
57 if (n->which == DATA1N_tag && n->parent &&
58 n->parent->which != DATA1N_root)
63 for (; n && n->which != DATA1N_root; n = n->parent)
64 if (n->which == DATA1N_tag)
70 data1_node *data1_mk_node (data1_handle dh, NMEM m)
72 return data1_mk_node2 (dh, m, DATA1N_root, 0);
75 data1_node *data1_mk_node_type (data1_handle dh, NMEM m, int type)
77 return data1_mk_node2 (dh, m, type, 0);
80 static void data1_init_node (data1_handle dh, data1_node *r, int type)
88 r->u.tag.no_data_requested = 0;
89 r->u.tag.node_selected = 0;
90 r->u.tag.make_variantlist = 0;
91 r->u.tag.get_bytes = -1;
92 r->u.tag.attributes = 0;
102 r->u.data.formatted_text = 0;
108 r->u.data.formatted_text = 1;
111 r->u.variant.type = 0;
112 r->u.variant.value = 0;
114 case DATA1N_preprocess:
115 r->u.preprocess.target = 0;
116 r->u.preprocess.attributes = 0;
119 yaz_log (YLOG_WARN, "data_mk_node_type. bad type = %d\n", type);
123 data1_node *data1_append_node (data1_handle dh, NMEM m, int type,
126 data1_node *r = (data1_node *)nmem_malloc(m, sizeof(*r));
127 r->next = r->child = r->last_child = 0;
135 r->root = parent->root;
137 parent->child = parent->last_child = r;
139 parent->last_child->next = r;
140 parent->last_child = r;
142 data1_init_node(dh, r, type);
146 data1_node *data1_mk_node2 (data1_handle dh, NMEM m, int type,
149 return data1_append_node (dh, m, type, parent);
152 data1_node *data1_insert_node (data1_handle dh, NMEM m, int type,
155 data1_node *r = (data1_node *)nmem_malloc(m, sizeof(*r));
156 r->next = r->child = r->last_child = 0;
163 r->root = parent->root;
166 parent->last_child = r;
168 r->next = parent->child;
171 data1_init_node(dh, r, type);
175 void data1_free_tree (data1_handle dh, data1_node *t)
177 data1_node *p = t->child, *pn;
182 data1_free_tree (dh, p);
189 data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name)
191 data1_absyn *absyn = data1_get_absyn(dh, name, 1);
195 yaz_log(YLOG_WARN, "Unable to acquire abstract syntax " "for '%s'",
197 /* It's now OK for a record not to have an absyn */
199 res = data1_mk_node2 (dh, nmem, DATA1N_root, 0);
200 res->u.root.type = data1_insert_string (dh, res, nmem, name);
201 res->u.root.absyn = absyn;
205 void data1_set_root(data1_handle dh, data1_node *res,
206 NMEM nmem, const char *name)
208 data1_absyn *absyn = data1_get_absyn(
209 dh, name, DATA1_XPATH_INDEXING_ENABLE);
211 res->u.root.type = data1_insert_string (dh, res, nmem, name);
212 res->u.root.absyn = absyn;
215 void data1_add_attrs(data1_handle dh, NMEM nmem, const char **attr,
221 while (attr && *attr)
223 *p = (data1_xattr*) nmem_malloc (nmem, sizeof(**p));
224 (*p)->name = nmem_strdup (nmem, *attr++);
225 (*p)->value = nmem_strdup (nmem, *attr++);
226 (*p)->what = DATA1I_text;
233 data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem,
235 const char **attr, data1_node *at)
237 return data1_mk_preprocess_n (dh, nmem, target, strlen(target),
241 data1_node *data1_mk_preprocess_n (data1_handle dh, NMEM nmem,
242 const char *target, size_t len,
243 const char **attr, data1_node *at)
245 data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_preprocess, at);
246 res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
249 data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
253 data1_node *data1_insert_preprocess (data1_handle dh, NMEM nmem,
255 const char **attr, data1_node *at)
257 return data1_insert_preprocess_n (dh, nmem, target, strlen(target),
261 data1_node *data1_insert_preprocess_n (data1_handle dh, NMEM nmem,
262 const char *target, size_t len,
263 const char **attr, data1_node *at)
265 data1_node *res = data1_insert_node (dh, nmem, DATA1N_preprocess, at);
266 res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
269 data1_add_attrs(dh, nmem, attr, &res->u.preprocess.attributes);
273 data1_node *data1_mk_tag_n (data1_handle dh, NMEM nmem,
274 const char *tag, size_t len, const char **attr,
277 data1_node *partag = get_parent_tag(dh, at);
278 data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_tag, at);
279 data1_element *e = 0;
281 res->u.tag.tag = data1_insert_string_n (dh, res, nmem, tag, len);
283 if (!partag) /* top tag? */
284 e = data1_getelementbytagname (dh, at->root->u.root.absyn,
285 0 /* index as local */,
289 /* only set element for known tags */
290 e = partag->u.tag.element;
292 e = data1_getelementbytagname (dh, at->root->u.root.absyn,
295 res->u.tag.element = e;
296 data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
300 void data1_tag_add_attr (data1_handle dh, NMEM nmem,
301 data1_node *res, const char **attr)
303 if (res->which != DATA1N_tag)
306 data1_add_attrs(dh, nmem, attr, &res->u.tag.attributes);
309 data1_node *data1_mk_tag (data1_handle dh, NMEM nmem,
310 const char *tag, const char **attr, data1_node *at)
312 return data1_mk_tag_n (dh, nmem, tag, strlen(tag), attr, at);
315 data1_node *data1_search_tag (data1_handle dh, data1_node *n,
320 n = data1_get_root_tag (dh, n);
325 for (; n; n = n->next)
326 if (n->which == DATA1N_tag && n->u.tag.tag &&
327 !yaz_matchstr (n->u.tag.tag, tag))
334 data1_node *data1_mk_tag_uni (data1_handle dh, NMEM nmem,
335 const char *tag, data1_node *at)
337 data1_node *node = data1_search_tag (dh, at->child, tag);
339 node = data1_mk_tag (dh, nmem, tag, 0 /* attr */, at);
341 node->child = node->last_child = 0;
345 data1_node *data1_mk_text_n (data1_handle dh, NMEM mem,
346 const char *buf, size_t len, data1_node *parent)
348 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_data, parent);
349 res->u.data.what = DATA1I_text;
350 res->u.data.len = len;
352 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
356 data1_node *data1_mk_text_nf (data1_handle dh, NMEM mem,
357 const char *buf, size_t len, data1_node *parent)
359 data1_node *res = data1_mk_text_n (dh, mem, buf, len, parent);
360 res->u.data.formatted_text = 1;
364 data1_node *data1_mk_text (data1_handle dh, NMEM mem,
365 const char *buf, data1_node *parent)
367 return data1_mk_text_n (dh, mem, buf, strlen(buf), parent);
370 data1_node *data1_mk_comment_n (data1_handle dh, NMEM mem,
371 const char *buf, size_t len,
374 data1_node *res = data1_mk_node2 (dh, mem, DATA1N_comment, parent);
375 res->u.data.what = DATA1I_text;
376 res->u.data.len = len;
378 res->u.data.data = data1_insert_string_n (dh, res, mem, buf, len);
382 data1_node *data1_mk_comment (data1_handle dh, NMEM mem,
383 const char *buf, data1_node *parent)
385 return data1_mk_comment_n (dh, mem, buf, strlen(buf), parent);
388 char *data1_insert_string_n (data1_handle dh, data1_node *res,
389 NMEM m, const char *str, size_t len)
392 if (len >= DATA1_LOCALDATA)
393 b = (char *) nmem_malloc (m, len+1);
396 memcpy (b, str, len);
401 char *data1_insert_string (data1_handle dh, data1_node *res,
402 NMEM m, const char *str)
404 return data1_insert_string_n (dh, res, m, str, strlen(str));
407 static data1_node *data1_add_insert_taggeddata(data1_handle dh,
409 const char *tagname, NMEM m,
413 data1_node *root = at->root;
414 data1_node *partag = get_parent_tag (dh, at);
415 data1_element *e = NULL;
416 data1_node *datn = 0;
417 data1_node *tagn = 0;
420 e = data1_getelementbytagname (dh, root->u.root.absyn, 0, tagname);
423 e = partag->u.tag.element;
425 e = data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
427 if (local_allowed || e)
430 tagn = data1_insert_node (dh, m, DATA1N_tag, at);
432 tagn = data1_append_node (dh, m, DATA1N_tag, at);
433 tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
434 tagn->u.tag.element = e;
435 datn = data1_mk_node2 (dh, m, DATA1N_data, tagn);
440 data1_node *data1_mk_tag_data(data1_handle dh, data1_node *at,
441 const char *tagname, NMEM m)
443 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
448 * Insert a tagged node into the record root as first child of the node at
449 * which should be root or tag itself). Returns pointer to the data node,
450 * which can then be modified.
452 data1_node *data1_mk_tag_data_wd(data1_handle dh, data1_node *at,
453 const char *tagname, NMEM m)
455 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
458 data1_node *data1_insert_taggeddata (data1_handle dh, data1_node *root,
459 data1_node *at, const char *tagname,
462 return data1_add_insert_taggeddata (dh, at, tagname, m, 0, 1);
465 data1_node *data1_add_taggeddata (data1_handle dh, data1_node *root,
466 data1_node *at, const char *tagname,
469 return data1_add_insert_taggeddata (dh, at, tagname, m, 1, 0);
472 data1_node *data1_mk_tag_data_zint (data1_handle dh, data1_node *at,
473 const char *tag, zint num,
476 data1_node *node_data;
478 node_data = data1_mk_tag_data (dh, at, tag, nmem);
481 node_data->u.data.what = DATA1I_num;
482 node_data->u.data.data = node_data->lbuf;
483 sprintf (node_data->u.data.data, ZINT_FORMAT, num);
484 node_data->u.data.len = strlen (node_data->u.data.data);
488 data1_node *data1_mk_tag_data_int (data1_handle dh, data1_node *at,
489 const char *tag, int num,
492 return data1_mk_tag_data_zint(dh, at, tag, num, nmem);
495 data1_node *data1_mk_tag_data_oid (data1_handle dh, data1_node *at,
496 const char *tag, Odr_oid *oid,
499 data1_node *node_data;
500 char str[128], *p = str;
503 node_data = data1_mk_tag_data (dh, at, tag, nmem);
507 for (ii = oid; *ii >= 0; ii++)
511 sprintf (p, "%d", *ii);
514 node_data->u.data.what = DATA1I_oid;
515 node_data->u.data.len = strlen (str);
516 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
521 data1_node *data1_mk_tag_data_text (data1_handle dh, data1_node *at,
522 const char *tag, const char *str,
525 data1_node *node_data;
527 node_data = data1_mk_tag_data (dh, at, tag, nmem);
530 node_data->u.data.what = DATA1I_text;
531 node_data->u.data.len = strlen (str);
532 node_data->u.data.data = data1_insert_string (dh, node_data, nmem, str);
537 data1_node *data1_mk_tag_data_text_uni (data1_handle dh, data1_node *at,
538 const char *tag, const char *str,
541 data1_node *node = data1_search_tag (dh, at->child, tag);
543 return data1_mk_tag_data_text (dh, at, tag, str, nmem);
546 data1_node *node_data = node->child;
547 node_data->u.data.what = DATA1I_text;
548 node_data->u.data.len = strlen (str);
549 node_data->u.data.data = data1_insert_string (dh, node_data,
551 node_data->child = node_data->last_child = 0;
556 static int ampr (int (*get_byte)(void *fh), void *fh, int *amp)
559 int c = (*get_byte)(fh);
563 int c = (*get_byte)(fh);
578 if (!strcmp (ent, "quot"))
580 if (!strcmp (ent, "apos"))
582 if (!strcmp (ent, "gt"))
584 if (!strcmp (ent, "lt"))
586 if (!strcmp (ent, "amp"))
591 else if (c == 0 || d1_isspace(c))
601 data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
602 int (*get_byte)(void *fh), void *fh,
603 WRBUF wrbuf, int *ch, int *amp)
605 data1_xattr *p_first = 0;
606 data1_xattr **pp = &p_first;
612 while (*amp || (c && d1_isspace(c)))
613 c = ampr (get_byte, fh, amp);
614 if (*amp == 0 && (c == 0 || c == '>' || c == '/'))
616 *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
620 p->what = DATA1I_xmltext;
623 while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
625 wrbuf_putc (wrbuf, c);
626 c = ampr (get_byte, fh, amp);
628 wrbuf_putc (wrbuf, '\0');
629 len = wrbuf_len(wrbuf);
630 p->name = (char*) nmem_malloc (m, len);
631 strcpy (p->name, wrbuf_buf(wrbuf));
634 c = ampr (get_byte, fh, amp);
635 if (*amp == 0 && c == '"')
637 c = ampr (get_byte, fh, amp);
639 while (*amp || (c && c != '"'))
641 wrbuf_putc (wrbuf, c);
642 c = ampr (get_byte, fh, amp);
645 c = ampr (get_byte, fh, amp);
647 else if (*amp == 0 && c == '\'')
649 c = ampr (get_byte, fh, amp);
651 while (*amp || (c && c != '\''))
653 wrbuf_putc (wrbuf, c);
654 c = ampr (get_byte, fh, amp);
657 c = ampr (get_byte, fh, amp);
662 while (*amp || (c && c != '>' && c != '/'))
664 wrbuf_putc (wrbuf, c);
665 c = ampr (get_byte, fh, amp);
668 wrbuf_putc (wrbuf, '\0');
669 len = wrbuf_len(wrbuf);
670 p->value = (char*) nmem_malloc (m, len);
671 strcpy (p->value, wrbuf_buf(wrbuf));
679 * Ugh. Sometimes functions just grow and grow on you. This one reads a
680 * 'node' and its children.
682 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
683 int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
685 data1_node *d1_stack[256];
692 c = ampr (get_byte, fh, &);
695 data1_node *parent = level ? d1_stack[level-1] : 0;
697 if (amp == 0 && c == '<') /* beginning of tag */
706 c = ampr (get_byte, fh, &);
707 if (amp == 0 && c == '/')
710 c = ampr (get_byte, fh, &);
712 else if (amp == 0 && c == '?')
715 while ((c = ampr(get_byte, fh, &)))
727 c = ampr(get_byte, fh, &);
733 if (amp == 0 && c == quote_mode)
739 else if (amp == 0 && c == '!')
745 c0 = ampr (get_byte, fh, &0);
746 if (amp0 == 0 && c0 == '\0')
748 c = ampr (get_byte, fh, &);
750 if (amp0 == 0 && c0 == '-' && amp == 0 && c == '-')
752 /* COMMENT: <!-- ... --> */
755 c = ampr (get_byte, fh, &);
758 if (amp == 0 && c == '-')
760 else if (amp == 0 && c == '>' && no_dash >= 2)
766 wrbuf_buf(wrbuf), wrbuf_len(wrbuf)-2,
768 c = ampr (get_byte, fh, &); /* skip > */
773 wrbuf_putc (wrbuf, c);
774 c = ampr (get_byte, fh, &);
779 { /* DIRECTIVE: <! .. > */
784 if (amp == 0 && c == '>' && blevel == 0)
786 c = ampr (get_byte, fh, &);
789 if (amp == 0 && c == '[')
791 if (amp == 0 && c == ']' && blevel > 0)
793 c = ampr (get_byte, fh, &);
798 while (amp || (c && c != '>' && c != '/' && !d1_isspace(c)))
800 if (i < (sizeof(tag)-1))
802 c = ampr (get_byte, fh, &);
805 xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c, &);
806 if (amp == 0 && c == '/')
807 { /* <tag attrs/> or <tag/> */
809 c = ampr (get_byte, fh, &);
813 yaz_log(YLOG_WARN, "d1: %d: Malformed tag", line);
817 c = ampr (get_byte, fh, &);
829 parent = d1_stack[--i];
830 if ((parent->which == DATA1N_root &&
831 !strcmp(tag, parent->u.root.type)) ||
832 (parent->which == DATA1N_tag &&
833 !strcmp(tag, parent->u.tag.tag)))
841 yaz_log (YLOG_WARN, "%d: no begin tag for %s",
846 if (data1_is_xmlmode(dh))
858 else if (!strcmp(tag, "var")
859 && xattr && xattr->next && xattr->next->next
861 && xattr->next->value == 0
862 && xattr->next->next->value == 0)
864 /* <var class type value> */
865 const char *tclass = xattr->name;
866 const char *type = xattr->next->name;
867 const char *value = xattr->next->name;
870 yaz_log(YLOG_LOG, "Variant class=%s type=%s value=%s",
871 tclass, type, value);
873 data1_getvartypebyct(dh,
874 parent->root->u.root.absyn->varset,
878 * If we're the first variant in this group, create a parent
879 * variant, and insert it before the current variant.
881 if (parent->which != DATA1N_variant)
883 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
888 * now determine if one of our ancestor triples is of
889 * same type. If so, we break here.
892 for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
893 if (d1_stack[i]->u.variant.type == tp)
898 res = data1_mk_node2 (dh, m, DATA1N_variant, parent);
899 res->u.variant.type = tp;
900 res->u.variant.value =
901 data1_insert_string (dh, res, m, value);
907 /* tag .. acquire our element in the abstract syntax */
910 parent = data1_mk_root (dh, m, tag);
911 res = d1_stack[level] = parent;
913 if (data1_is_xmlmode(dh))
916 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
917 res->u.tag.attributes = xattr;
922 res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
923 res->u.tag.attributes = xattr;
926 d1_stack[level] = res;
927 d1_stack[level+1] = 0;
928 if (level < 250 && !null_tag)
931 else /* != '<'... this is a body of text */
937 c = ampr (get_byte, fh, &);
940 res = data1_mk_node2 (dh, m, DATA1N_data, parent);
941 res->u.data.what = DATA1I_xmltext;
942 res->u.data.formatted_text = 0;
943 d1_stack[level] = res;
947 while (amp || (c && c != '<'))
949 wrbuf_putc (wrbuf, c);
950 c = ampr (get_byte, fh, &);
952 len = wrbuf_len(wrbuf);
954 /* use local buffer of nmem if too large */
955 if (len >= DATA1_LOCALDATA)
956 res->u.data.data = (char*) nmem_malloc (m, len);
958 res->u.data.data = res->lbuf;
961 memcpy (res->u.data.data, wrbuf_buf(wrbuf), len);
963 res->u.data.data = 0;
964 res->u.data.len = len;
970 int getc_mem (void *fh)
972 const char **p = (const char **) fh;
978 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
980 WRBUF wrbuf = wrbuf_alloc();
983 node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
984 wrbuf_free (wrbuf, 1);
989 * Read a record in the native syntax.
991 data1_node *data1_read_record(data1_handle dh,
992 int (*rf)(void *, char *, size_t), void *fh,
996 char **buf = data1_get_read_buf (dh, &size);
1001 *buf = (char *)xmalloc(*size = 4096);
1005 if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
1007 if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
1013 return data1_read_node(dh, &bp, m);
1022 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
1024 const char *bp = buf;
1025 return data1_read_node (dh, &bp, m);
1029 static int conv_item (NMEM m, yaz_iconv_t t,
1030 WRBUF wrbuf, char *inbuf, size_t inlen)
1032 wrbuf_rewind (wrbuf);
1033 if (wrbuf->size < 10)
1034 wrbuf_grow (wrbuf, 10);
1037 char *outbuf = wrbuf->buf + wrbuf->pos;
1038 size_t outlen = wrbuf->size - wrbuf->pos;
1039 if (yaz_iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
1040 (size_t)(-1) && yaz_iconv_error(t) != YAZ_ICONV_E2BIG)
1042 /* bad data. stop and skip conversion entirely */
1045 else if (inlen == 0)
1046 { /* finished converting */
1047 wrbuf->pos = wrbuf->size - outlen;
1052 /* buffer too small: make sure we expand buffer */
1053 wrbuf->pos = wrbuf->size - outlen;
1054 wrbuf_grow(wrbuf, 20);
1060 static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
1061 yaz_iconv_t t, WRBUF wrbuf, const char *tocode)
1063 for (; n; n = n->next)
1068 case DATA1N_comment:
1069 if (conv_item (m, t, wrbuf, n->u.data.data, n->u.data.len) == 0)
1072 data1_insert_string_n (dh, n, m, wrbuf->buf,
1074 n->u.data.len = wrbuf->pos;
1078 if (conv_item (m, t, wrbuf, n->u.tag.tag, strlen(n->u.tag.tag))
1082 data1_insert_string_n (dh, n, m,
1083 wrbuf->buf, wrbuf->pos);
1085 if (n->u.tag.attributes)
1088 for (p = n->u.tag.attributes; p; p = p->next)
1091 conv_item(m, t, wrbuf, p->value, strlen(p->value))
1094 wrbuf_puts (wrbuf, "");
1095 p->value = nmem_strdup (m, wrbuf->buf);
1100 case DATA1N_preprocess:
1101 if (strcmp(n->u.preprocess.target, "xml") == 0)
1103 data1_xattr *p = n->u.preprocess.attributes;
1104 for (; p; p = p->next)
1105 if (strcmp (p->name, "encoding") == 0)
1106 p->value = nmem_strdup (m, tocode);
1110 data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
1114 const char *data1_get_encoding (data1_handle dh, data1_node *n)
1116 /* see if we have an xml header that specifies encoding */
1117 if (n && n->child && n->child->which == DATA1N_preprocess &&
1118 strcmp (n->child->u.preprocess.target, "xml") == 0)
1120 data1_xattr *xp = n->child->u.preprocess.attributes;
1121 for (; xp; xp = xp->next)
1122 if (!strcmp (xp->name, "encoding") == 0)
1125 /* no encoding in header, so see if "encoding" was specified for abs */
1126 if (n && n->which == DATA1N_root &&
1127 n->u.root.absyn && n->u.root.absyn->encoding)
1128 return n->u.root.absyn->encoding;
1129 /* none of above, return a hard coded default */
1130 return "ISO-8859-1";
1133 int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
1135 const char *fromcode)
1137 if (yaz_matchstr (tocode, fromcode))
1139 WRBUF wrbuf = wrbuf_alloc();
1140 yaz_iconv_t t = yaz_iconv_open(tocode, fromcode);
1143 wrbuf_free(wrbuf, 1);
1146 data1_iconv_s(dh, m, n, t, wrbuf, tocode);
1148 wrbuf_free(wrbuf, 1);
1153 void data1_concat_text(data1_handle dh, NMEM m, data1_node *n)
1155 for (; n; n = n->next)
1157 if (n->which == DATA1N_data && n->next &&
1158 n->next->which == DATA1N_data)
1164 for (np = n; np && np->which == DATA1N_data; np=np->next)
1165 sz += np->u.data.len;
1166 ndata = nmem_malloc(m, sz);
1167 for (np = n; np && np->which == DATA1N_data; np=np->next)
1169 memcpy(ndata+off, np->u.data.data, np->u.data.len);
1170 off += np->u.data.len;
1172 n->u.data.data = ndata;
1175 if (!np && n->parent)
1176 n->parent->last_child = n;
1179 data1_concat_text(dh, m, n->child);
1185 * indent-tabs-mode: nil
1187 * vim: shiftwidth=4 tabstop=8 expandtab