* See the file LICENSE for details.
* Sebastian Hammer, Adam Dickmeiss
*
- * $Id: d1_read.c,v 1.44 2002-07-03 10:04:04 adam Exp $
+ * $Id: d1_read.c,v 1.47 2002-07-29 20:04:08 adam Exp $
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
+
+#if HAVE_ICONV_H
+#include <iconv.h>
+#endif
+
#include <yaz/xmalloc.h>
#include <yaz/log.h>
#include <yaz/data1.h>
{
if (!n)
return 0;
- n = n->child;
- while (n && n->which != DATA1N_tag)
- n = n->next;
+ if (data1_is_xmlmode(dh))
+ {
+ n = n->child;
+ while (n && n->which != DATA1N_tag)
+ n = n->next;
+ }
return n;
}
break;
}
}
- if (level <= 1)
- return d1_stack[0];
+ if (data1_is_xmlmode(dh))
+ {
+ if (level <= 1)
+ return d1_stack[0];
+ }
+ else
+ {
+ if (level <= 0)
+ return d1_stack[0];
+ }
continue;
}
else if (!strcmp(tag, "var"))
if (level == 0)
{
parent = data1_mk_root (dh, m, tag);
- d1_stack[level++] = parent;
+ res = d1_stack[level] = parent;
+
+ if (data1_is_xmlmode(dh))
+ {
+ level++;
+ res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+ res->u.tag.attributes = xattr;
+ }
+ }
+ else
+ {
+ res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
+ res->u.tag.attributes = xattr;
}
- res = data1_mk_tag (dh, m, tag, 0 /* attr */, parent);
- res->u.tag.attributes = xattr;
}
d1_stack[level] = res;
d1_stack[level+1] = 0;
return data1_read_node (dh, &bp, m);
}
+
+#if HAVE_ICONV_H
+
+static int conv_item (NMEM m, iconv_t t,
+ WRBUF wrbuf, char *inbuf, size_t inlen)
+{
+ wrbuf_rewind (wrbuf);
+ if (wrbuf->size < 10)
+ wrbuf_grow (wrbuf, 10);
+ for (;;)
+ {
+ char *outbuf = wrbuf->buf + wrbuf->pos;
+ size_t outlen = wrbuf->size - wrbuf->pos;
+ if (iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
+ (size_t)(-1) && errno != E2BIG)
+ {
+ /* bad data. stop and skip conversion entirely */
+ return -1;
+ }
+ else if (inlen == 0)
+ { /* finished converting */
+ wrbuf->pos = wrbuf->size - outlen;
+ break;
+ }
+ else
+ {
+ /* buffer too small: make sure we expand buffer */
+ wrbuf->pos = wrbuf->size - outlen;
+ wrbuf_grow(wrbuf, 20);
+ }
+ }
+ return 0;
+}
+
+static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
+ iconv_t t, WRBUF wrbuf, const char *tocode)
+{
+ for (; n; n = n->next)
+ {
+ switch (n->which)
+ {
+ case DATA1N_data:
+ case DATA1N_comment:
+ if (conv_item (m, t, wrbuf, n->u.data.data, n->u.data.len) == 0)
+ {
+ n->u.data.data =
+ data1_insert_string_n (dh, n, m, wrbuf->buf,
+ wrbuf->pos);
+ n->u.data.len = wrbuf->pos;
+ }
+ break;
+ case DATA1N_tag:
+ if (conv_item (m, t, wrbuf, n->u.tag.tag, strlen(n->u.tag.tag))
+ == 0)
+ {
+ n->u.tag.tag =
+ data1_insert_string_n (dh, n, m,
+ wrbuf->buf, wrbuf->pos);
+ }
+ if (n->u.tag.attributes)
+ {
+ data1_xattr *p;
+ for (p = n->u.tag.attributes; p; p = p->next)
+ {
+ if (conv_item(m, t, wrbuf, p->value, strlen(p->value))
+ == 0)
+ {
+ wrbuf_puts (wrbuf, "");
+ p->value = nmem_strdup (m, wrbuf->buf);
+ }
+ }
+ }
+ break;
+ case DATA1N_preprocess:
+ if (strcmp(n->u.preprocess.target, "xml") == 0)
+ {
+ data1_xattr *p = n->u.preprocess.attributes;
+ for (; p; p = p->next)
+ if (strcmp (p->name, "encoding") == 0)
+ p->value = nmem_strdup (m, tocode);
+ }
+ break;
+ }
+ data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
+ }
+}
+#endif
+
+const char *data1_get_encoding (data1_handle dh, data1_node *n)
+{
+ /* see if we have an xml header that specifies encoding */
+ if (n && n->child && n->child->which == DATA1N_preprocess &&
+ strcmp (n->child->u.preprocess.target, "xml") == 0)
+ {
+ data1_xattr *xp = n->child->u.preprocess.attributes;
+ for (; xp; xp = xp->next)
+ if (!strcmp (xp->name, "encoding") == 0)
+ return xp->value;
+ }
+ /* no encoding in header, so see if "encoding" was specified for abs */
+ if (n && n->which == DATA1N_root &&
+ n->u.root.absyn && n->u.root.absyn->encoding)
+ return n->u.root.absyn->encoding;
+ /* none of above, return a hard coded default */
+ return "ISO-8859-1";
+}
+
+int data1_iconv (data1_handle dh, NMEM m, data1_node *n,
+ const char *tocode,
+ const char *fromcode)
+{
+#if HAVE_ICONV_H
+ if (strcmp (tocode, fromcode))
+ {
+ WRBUF wrbuf = wrbuf_alloc();
+ iconv_t t = iconv_open (tocode, fromcode);
+ if (t == (iconv_t) (-1))
+ return -1;
+ data1_iconv_s (dh, m, n, t, wrbuf, tocode);
+ iconv_close (t);
+ wrbuf_free (wrbuf, 1);
+ }
+ return 0;
+#else
+ return -2;
+#endif
+}