X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fodr.c;h=645317e93effab63eca05f03010ed2498a60b7d4;hp=964193e9be666fb48d7754e4a5af14589d35e2e5;hb=021f5586a328c6600460aa9f9be664ba19ba20d4;hpb=3b96525a40981e162b959f3e842e0ff20e314320 diff --git a/src/odr.c b/src/odr.c index 964193e..645317e 100644 --- a/src/odr.c +++ b/src/odr.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 1995-2004, Index Data + * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: odr.c,v 1.9 2004-11-18 15:18:13 heikki Exp $ + * $Id: odr.c,v 1.15 2007-02-23 10:15:01 adam Exp $ * */ @@ -15,12 +15,14 @@ #include #endif +#include #include #include #include #include -#include +#include +#include #include "odr-priv.h" static int log_level=0; @@ -58,13 +60,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 +97,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) @@ -106,34 +135,34 @@ void odr_setelement(ODR o, const char *element) } void odr_FILE_write(ODR o, void *handle, int type, - const char *buf, int len) + const char *buf, int len) { int i; #if 0 if (type == ODR_OCTETSTRING) { - const char **stack_names = odr_get_element_path(o); - for (i = 0; stack_names[i]; i++) - fprintf((FILE*) handle, "[%s]", stack_names[i]); - fputs("\n", (FILE*) handle); + const char **stack_names = odr_get_element_path(o); + for (i = 0; stack_names[i]; i++) + fprintf((FILE*) handle, "[%s]", stack_names[i]); + fputs("\n", (FILE*) handle); } #endif for (i = 0; i 3100) - { - fputs(" ..... ", (FILE*) handle); - i = len - 1000; - } - if (strchr("\r\n\f\t", c) || (c >= ' ' && c <= 126)) - putc(c, (FILE*) handle); - else - { - char x[5]; - sprintf(x, "\\X%02X", c); - fputs(x, (FILE*) handle); - } + unsigned c = ((const unsigned char *) buf)[i]; + if (i == 2000 && len > 3100) + { + fputs(" ..... ", (FILE*) handle); + i = len - 1000; + } + if (strchr("\r\n\f\t", c) || (c >= ' ' && c <= 126)) + putc(c, (FILE*) handle); + else + { + char x[5]; + sprintf(x, "\\X%02X", c); + fputs(x, (FILE*) handle); + } } } @@ -141,7 +170,7 @@ void odr_FILE_close(void *handle) { FILE *f = (FILE *) handle; if (f && f != stderr && f != stdout) - fclose(f); + fclose(f); } void odr_setprint(ODR o, FILE *file) @@ -150,10 +179,10 @@ void odr_setprint(ODR o, FILE *file) } void odr_set_stream(ODR o, void *handle, - void (*stream_write)(ODR o, - void *handle, int type, - const char *buf, int len), - void (*stream_close)(void *handle)) + void (*stream_write)(ODR o, + void *handle, int type, + const char *buf, int len), + void (*stream_close)(void *handle)) { o->print = (FILE*) handle; o->op->stream_write = stream_write; @@ -216,7 +245,10 @@ void odr_reset(ODR o) o->t_class = -1; o->t_tag = -1; o->indent = 0; - o->op->stackp = -1; + 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; @@ -231,7 +263,7 @@ void odr_destroy(ODR o) if (o->buf && o->can_grow) xfree(o->buf); if (o->op->stream_close) - o->op->stream_close(o->print); + o->op->stream_close(o->print); if (o->op->iconv_handle != 0) yaz_iconv_close (o->op->iconv_handle); xfree(o->op); @@ -264,15 +296,15 @@ 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 + yaz_vsnprintf(buf, sizeof(buf), fmt, ap); o->op->stream_write(o, o->print, ODR_VISIBLESTRING, buf, strlen(buf)); va_end(ap); } +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +