* See the file LICENSE for details.
* Sebastian Hammer, Adam Dickmeiss
*
- * $Id: d1_read.c,v 1.48 2002-08-19 21:09:10 adam Exp $
+ * $Id: d1_read.c,v 1.52 2002-09-24 07:58:59 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/yaz-util.h>
#include <yaz/log.h>
#include <yaz/data1.h>
}
data1_node *data1_mk_preprocess (data1_handle dh, NMEM nmem,
- const char *target, const char **attr,
- data1_node *at)
+ const char *target,
+ const char **attr, data1_node *at)
+{
+ return data1_mk_preprocess_n (dh, nmem, target, strlen(target),
+ attr, at);
+}
+
+data1_node *data1_mk_preprocess_n (data1_handle dh, NMEM nmem,
+ const char *target, size_t len,
+ const char **attr, data1_node *at)
{
data1_xattr **p;
data1_node *res = data1_mk_node2 (dh, nmem, DATA1N_preprocess, at);
- res->u.preprocess.target = data1_insert_string (dh, res, nmem, target);
-
+ res->u.preprocess.target = data1_insert_string_n (dh, res, nmem,
+ target, len);
+
p = &res->u.preprocess.attributes;
while (attr && *attr)
{
{
char *b;
if (len >= DATA1_LOCALDATA)
- b = nmem_malloc (m, len+1);
+ b = (char *) nmem_malloc (m, len+1);
else
b = res->lbuf;
memcpy (b, str, len);
}
}
+static int ampr (int (*get_byte)(void *fh), void *fh, int *amp)
+{
+ int c = (*get_byte)(fh);
+ *amp = 0;
+ if (c == '&')
+ {
+ char ent[20];
+ int i = 0;
+
+ while (1)
+ {
+ c = (*get_byte)(fh);
+ if (c == ';')
+ {
+ ent[i] = 0;
+
+ c = ' ';
+ if (!strcmp (ent, "quot"))
+ c = '"';
+ if (!strcmp (ent, "apos"))
+ c = '\'';
+ if (!strcmp (ent, "gt"))
+ c = '>';
+ if (!strcmp (ent, "lt"))
+ c = '<';
+ if (!strcmp (ent, "amp"))
+ c = '&';
+ *amp = 1;
+ break;
+ }
+ else if (c == 0 || d1_isspace(c))
+ break;
+ if (i < 19)
+ ent[i++] = c;
+ }
+ }
+ return c;
+}
data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
int (*get_byte)(void *fh), void *fh,
- WRBUF wrbuf, int *ch)
+ WRBUF wrbuf, int *ch, int *amp)
{
data1_xattr *p_first = 0;
data1_xattr **pp = &p_first;
{
data1_xattr *p;
int len;
- while (c && d1_isspace(c))
- c = (*get_byte)(fh);
- if (!c || c == '>' || c == '/')
+ while (*amp || (c && d1_isspace(c)))
+ c = ampr (get_byte, fh, amp);
+ if (*amp == 0 && (c == 0 || c == '>' || c == '/'))
break;
*pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
p->next = 0;
while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
wrbuf_putc (wrbuf, '\0');
len = wrbuf_len(wrbuf);
strcpy (p->name, wrbuf_buf(wrbuf));
if (c == '=')
{
- c = (*get_byte)(fh);
- if (c == '"')
+ c = ampr (get_byte, fh, amp);
+ if (*amp == 0 && c == '"')
{
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
wrbuf_rewind(wrbuf);
- while (c && c != '"')
+ while (*amp || (c && c != '"'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
if (c)
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
+ }
+ else if (*amp == 0 && c == '\'')
+ {
+ c = ampr (get_byte, fh, amp);
+ wrbuf_rewind(wrbuf);
+ while (*amp || (c && c != '\''))
+ {
+ wrbuf_putc (wrbuf, c);
+ c = ampr (get_byte, fh, amp);
+ }
+ if (c)
+ c = ampr (get_byte, fh, amp);
}
else
{
wrbuf_rewind(wrbuf);
- while (c && c != '>' && c != '/')
+ while (*amp || (c && c != '>' && c != '/'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, amp);
}
}
wrbuf_putc (wrbuf, '\0');
{
data1_node *d1_stack[256];
data1_node *res;
- int c;
+ int c, amp;
int level = 0;
int line = 1;
d1_stack[level] = 0;
- c = (*get_byte)(fh);
- while (1)
+ c = ampr (get_byte, fh, &);
+ while (c != '\0')
{
data1_node *parent = level ? d1_stack[level-1] : 0;
- while (c != '\0' && d1_isspace(c))
- {
- if (c == '\n')
- line++;
- c = (*get_byte)(fh);
- }
- if (c == '\0')
- break;
-
- if (c == '<') /* beginning of tag */
+
+ if (amp == 0 && c == '<') /* beginning of tag */
{
data1_xattr *xattr;
int end_tag = 0;
size_t i = 0;
- c = (*get_byte)(fh);
- if (c == '/')
+ c = ampr (get_byte, fh, &);
+ if (amp == 0 && c == '/')
{
end_tag = 1;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
- else if (c == '!') /* tags/comments that we don't deal with yet */
+ else if (amp == 0 && c == '!')
{
- while (c && c != '>')
- c = (*get_byte)(fh);
- if (c)
- c = (*get_byte)(fh);
- continue;
+ int c0, amp0;
+
+ wrbuf_rewind(wrbuf);
+
+ c0 = ampr (get_byte, fh, &0);
+ if (amp0 == 0 && c0 == '\0')
+ break;
+ c = ampr (get_byte, fh, &);
+
+ if (amp0 == 0 && c0 == '-' && amp == 0 && c == '-')
+ {
+ /* COMMENT: <!-- ... --> */
+ int no_dash = 0;
+
+ c = ampr (get_byte, fh, &);
+ while (amp || c)
+ {
+ if (amp == 0 && c == '-')
+ no_dash++;
+ else if (amp == 0 && c == '>' && no_dash >= 2)
+ {
+ if (level > 0)
+ d1_stack[level] =
+ data1_mk_comment_n (
+ dh, m,
+ wrbuf_buf(wrbuf), wrbuf_len(wrbuf)-2,
+ d1_stack[level-1]);
+ c = ampr (get_byte, fh, &); /* skip > */
+ break;
+ }
+ else
+ no_dash = 0;
+ wrbuf_putc (wrbuf, c);
+ c = ampr (get_byte, fh, &);
+ }
+ continue;
+ }
+ else
+ { /* DIRECTIVE: <! .. > */
+
+ int blevel = 0;
+ while (amp || c)
+ {
+ if (amp == 0 && c == '>' && blevel == 0)
+ {
+ c = ampr (get_byte, fh, &);
+ break;
+ }
+ if (amp == 0 && c == '[')
+ blevel++;
+ if (amp == 0 && c == ']' && blevel > 0)
+ blevel--;
+ c = ampr (get_byte, fh, &);
+ }
+ continue;
+ }
}
- while (c && c != '>' && c != '/' && !d1_isspace(c))
+ while (amp || (c && c != '>' && c != '/' && !d1_isspace(c)))
{
if (i < (sizeof(tag)-1))
tag[i++] = c;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
tag[i] = '\0';
- xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c);
+ xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c, &);
args[0] = '\0';
- if (c == '/')
+ if (amp == 0 && c == '/')
{ /* <tag attrs/> or <tag/> */
null_tag = 1;
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
- if (c != '>')
+ if (amp || c != '>')
{
yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
return 0;
}
else
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
/* End tag? */
if (end_tag)
}
else /* != '<'... this is a body of text */
{
- const char *src;
- char *dst;
- int len, prev_char = 0;
+ int len;
if (level == 0)
{
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
continue;
}
res = data1_mk_node2 (dh, m, DATA1N_data, parent);
wrbuf_rewind(wrbuf);
- while (c && c != '<')
+ while (amp || (c && c != '<'))
{
wrbuf_putc (wrbuf, c);
- c = (*get_byte)(fh);
+ c = ampr (get_byte, fh, &);
}
len = wrbuf_len(wrbuf);
res->u.data.data = (char*) nmem_malloc (m, len);
else
res->u.data.data = res->lbuf;
-
- /* read "data" and transfer while removing white space */
- dst = res->u.data.data;
- for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
- {
- if (*src == '\n')
- line++;
- if (d1_isspace (*src))
- prev_char = ' ';
- else
- {
- if (prev_char)
- {
- *dst++ = prev_char;
- prev_char = 0;
- }
- *dst++ = *src;
- }
- }
- res->u.data.len = dst - res->u.data.data;
+
+ if (len)
+ memcpy (res->u.data.data, wrbuf_buf(wrbuf), len);
+ else
+ res->u.data.data = 0;
+ res->u.data.len = len;
}
}
return 0;
}
-#if HAVE_ICONV_H
-
-static int conv_item (NMEM m, iconv_t t,
+static int conv_item (NMEM m, yaz_iconv_t t,
WRBUF wrbuf, char *inbuf, size_t inlen)
{
wrbuf_rewind (wrbuf);
{
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)
+ if (yaz_iconv (t, &inbuf, &inlen, &outbuf, &outlen) ==
+ (size_t)(-1) && yaz_iconv_error(t) != YAZ_ICONV_E2BIG)
{
/* bad data. stop and skip conversion entirely */
return -1;
}
static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n,
- iconv_t t, WRBUF wrbuf, const char *tocode)
+ yaz_iconv_t t, WRBUF wrbuf, const char *tocode)
{
for (; n; n = n->next)
{
data1_iconv_s (dh, m, n->child, t, wrbuf, tocode);
}
}
-#endif
const char *data1_get_encoding (data1_handle dh, 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))
+ yaz_iconv_t t = yaz_iconv_open (tocode, fromcode);
+ if (!t)
return -1;
data1_iconv_s (dh, m, n, t, wrbuf, tocode);
- iconv_close (t);
+ yaz_iconv_close (t);
wrbuf_free (wrbuf, 1);
}
return 0;
-#else
- return -2;
-#endif
}