X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=data1%2Fd1_read.c;h=fe95dd0ce93e73f863a24c39891a428361fff3de;hb=60b519816aefda59ed16a7b484f13f144cd1fcac;hp=56fdffdd38990e28410f59f33a52211adef64777;hpb=89d3a004b7c651fd5673abfc192e1472dc4d4197;p=idzebra-moved-to-github.git diff --git a/data1/d1_read.c b/data1/d1_read.c index 56fdffd..fe95dd0 100644 --- a/data1/d1_read.c +++ b/data1/d1_read.c @@ -1,4 +1,4 @@ -/* $Id: d1_read.c,v 1.22 2007-01-15 15:10:14 adam Exp $ +/* $Id: d1_read.c,v 1.26 2007-05-21 11:53:49 adam Exp $ Copyright (C) 1995-2007 Index Data ApS @@ -174,6 +174,7 @@ data1_node *data1_mk_root (data1_handle dh, NMEM nmem, const char *name) { data1_absyn *absyn = data1_get_absyn(dh, name, 1); data1_node *res; + if (!absyn) { yaz_log(YLOG_WARN, "Unable to acquire abstract syntax " "for '%s'", @@ -592,7 +593,6 @@ data1_xattr *data1_read_xattr (data1_handle dh, NMEM m, for (;;) { data1_xattr *p; - int len; while (*amp || (c && d1_isspace(c))) c = ampr (get_byte, fh, amp); if (*amp == 0 && (c == 0 || c == '>' || c == '/')) @@ -609,10 +609,7 @@ data1_xattr *data1_read_xattr (data1_handle dh, NMEM m, wrbuf_putc (wrbuf, c); c = ampr (get_byte, fh, amp); } - wrbuf_putc (wrbuf, '\0'); - len = wrbuf_len(wrbuf); - p->name = (char*) nmem_malloc (m, len); - strcpy (p->name, wrbuf_buf(wrbuf)); + p->name = nmem_strdup (m, wrbuf_cstr(wrbuf)); if (c == '=') { c = ampr (get_byte, fh, amp); @@ -649,10 +646,7 @@ data1_xattr *data1_read_xattr (data1_handle dh, NMEM m, c = ampr (get_byte, fh, amp); } } - wrbuf_putc (wrbuf, '\0'); - len = wrbuf_len(wrbuf); - p->value = (char*) nmem_malloc (m, len); - strcpy (p->value, wrbuf_buf(wrbuf)); + p->value = nmem_strdup(m, wrbuf_cstr(wrbuf)); } } *ch = c; @@ -965,7 +959,7 @@ data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m) data1_node *node; node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf); - wrbuf_free (wrbuf, 1); + wrbuf_destroy(wrbuf); return node; } @@ -1010,34 +1004,12 @@ data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf) } -static int conv_item (NMEM m, yaz_iconv_t t, - WRBUF wrbuf, char *inbuf, size_t inlen) +static int conv_item(NMEM m, yaz_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 (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; - } - 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); - } - } + wrbuf_rewind(wrbuf); + wrbuf_iconv_write(wrbuf, t, inbuf, inlen); + wrbuf_iconv_reset(wrbuf, t); return 0; } @@ -1075,8 +1047,7 @@ static void data1_iconv_s (data1_handle dh, NMEM m, data1_node *n, conv_item(m, t, wrbuf, p->value, strlen(p->value)) == 0) { - wrbuf_puts (wrbuf, ""); - p->value = nmem_strdup (m, wrbuf->buf); + p->value = nmem_strdup(m, wrbuf_cstr(wrbuf)); } } } @@ -1115,8 +1086,8 @@ const char *data1_get_encoding (data1_handle dh, data1_node *n) } int data1_iconv (data1_handle dh, NMEM m, data1_node *n, - const char *tocode, - const char *fromcode) + const char *tocode, + const char *fromcode) { if (yaz_matchstr (tocode, fromcode)) { @@ -1124,12 +1095,12 @@ int data1_iconv (data1_handle dh, NMEM m, data1_node *n, yaz_iconv_t t = yaz_iconv_open(tocode, fromcode); if (!t) { - wrbuf_free(wrbuf, 1); + wrbuf_destroy(wrbuf); return -1; } data1_iconv_s(dh, m, n, t, wrbuf, tocode); yaz_iconv_close(t); - wrbuf_free(wrbuf, 1); + wrbuf_destroy(wrbuf); } return 0; }