X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fodr.c;h=7e376c114282449986f0fb8dd34c80bbc74880f7;hp=8d4cff84730d292a04589754037196583d2b6600;hb=b6aa93dcb5c07ed09b664c006b5f33b0135a0854;hpb=fb6d99a0c7e07d9cc4a315c447deaf6564a85505 diff --git a/src/odr.c b/src/odr.c index 8d4cff8..7e376c1 100644 --- a/src/odr.c +++ b/src/odr.c @@ -1,9 +1,6 @@ -/* - * Copyright (C) 1995-2005, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data * See the file LICENSE for details. - * - * $Id: odr.c,v 1.12 2005-06-25 15:46:04 adam Exp $ - * */ /** @@ -15,12 +12,14 @@ #include #endif +#include #include #include #include #include #include +#include #include "odr-priv.h" static int log_level=0; @@ -58,13 +57,22 @@ char *odr_errmsg(int n) void odr_perror(ODR o, const char *message) { const char *e = odr_getelement(o); + const char **element_path = odr_get_element_path(o); int err, x; err = odr_geterrorx(o, &x); fprintf(stderr, "%s: %s (code %d:%d)", message, odr_errlist[err], err, x); if (e && *e) - fprintf (stderr, " element %s", e); + fprintf(stderr, " element %s", e); + fprintf(stderr, "\n"); + if (element_path) + { + fprintf(stderr, "Element path:"); + while (*element_path) + fprintf(stderr, " %s", *element_path++); + fprintf(stderr, "\n"); + } } int odr_geterror(ODR o) @@ -86,7 +94,25 @@ const char *odr_getelement(ODR o) const char **odr_get_element_path(ODR o) { - return o->op->stack_names; + int cur_sz = 0; + struct odr_constack *st; + + for (st = o->op->stack_top; st; st = st->prev) + cur_sz++; + if (o->op->tmp_names_sz < cur_sz + 1) + { + o->op->tmp_names_sz = 2 * cur_sz + 5; + o->op->tmp_names_buf = (const char **) + odr_malloc(o, o->op->tmp_names_sz * sizeof(char*)); + } + o->op->tmp_names_buf[cur_sz] = 0; + for (st = o->op->stack_top; st; st = st->prev) + { + cur_sz--; + o->op->tmp_names_buf[cur_sz] = st->name; + } + assert(cur_sz == 0); + return o->op->tmp_names_buf; } void odr_seterror(ODR o, int error, int id) @@ -155,7 +181,7 @@ void odr_set_stream(ODR o, void *handle, const char *buf, int len), void (*stream_close)(void *handle)) { - o->print = (FILE*) handle; + o->op->print = (FILE*) handle; o->op->stream_write = stream_write; o->op->stream_close = stream_close; } @@ -186,13 +212,13 @@ ODR odr_createmem(int direction) if (!(o = (ODR)xmalloc(sizeof(*o)))) return 0; + o->op = (struct Odr_private *) xmalloc (sizeof(*o->op)); o->direction = direction; o->buf = 0; o->size = o->pos = o->top = 0; - o->can_grow = 1; + o->op->can_grow = 1; o->mem = nmem_create(); - o->enable_bias = 1; - o->op = (struct Odr_private *) xmalloc (sizeof(*o->op)); + o->op->enable_bias = 1; o->op->odr_ber_tag.lclass = -1; o->op->iconv_handle = 0; odr_setprint(o, stderr); @@ -213,13 +239,16 @@ void odr_reset(ODR o) o->bp = o->buf; odr_seek(o, ODR_S_SET, 0); o->top = 0; - o->t_class = -1; - o->t_tag = -1; - o->indent = 0; - o->op->stackp = -1; + o->op->t_class = -1; + o->op->t_tag = -1; + o->op->indent = 0; + o->op->stack_first = 0; + o->op->stack_top = 0; + o->op->tmp_names_sz = 0; + o->op->tmp_names_buf = 0; nmem_reset(o->mem); - o->choice_bias = -1; - o->lenlen = 1; + o->op->choice_bias = -1; + o->op->lenlen = 1; if (o->op->iconv_handle != 0) yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0); yaz_log (log_level, "odr_reset o=%p", o); @@ -228,10 +257,10 @@ void odr_reset(ODR o) void odr_destroy(ODR o) { nmem_destroy(o->mem); - if (o->buf && o->can_grow) + if (o->buf && o->op->can_grow) xfree(o->buf); if (o->op->stream_close) - o->op->stream_close(o->print); + o->op->stream_close(o->op->print); if (o->op->iconv_handle != 0) yaz_iconv_close (o->op->iconv_handle); xfree(o->op); @@ -245,7 +274,7 @@ void odr_setbuf(ODR o, char *buf, int len, int can_grow) o->bp = (unsigned char *) buf; o->buf = (unsigned char *) buf; - o->can_grow = can_grow; + o->op->can_grow = can_grow; o->top = o->pos = 0; o->size = len; } @@ -264,21 +293,14 @@ void odr_printf(ODR o, const char *fmt, ...) char buf[4096]; va_start(ap, fmt); -#ifdef WIN32 - _vsnprintf(buf, sizeof(buf)-1, fmt, ap); -#else -#if HAVE_VSNPRINTF - vsnprintf(buf, sizeof(buf), fmt, ap); -#else - vsprintf(buf, fmt, ap); -#endif -#endif - o->op->stream_write(o, o->print, ODR_VISIBLESTRING, buf, strlen(buf)); + yaz_vsnprintf(buf, sizeof(buf), fmt, ap); + o->op->stream_write(o, o->op->print, ODR_VISIBLESTRING, buf, strlen(buf)); va_end(ap); } /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab