X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fodr.c;h=7e376c114282449986f0fb8dd34c80bbc74880f7;hp=acc7b1969d7fbefbf5fa0cb3e938eb224483b813;hb=b6aa93dcb5c07ed09b664c006b5f33b0135a0854;hpb=c6e47cbbff56f39f6d81b079ebaeac41d793d4d9 diff --git a/src/odr.c b/src/odr.c index acc7b19..7e376c1 100644 --- a/src/odr.c +++ b/src/odr.c @@ -1,20 +1,30 @@ -/* - * Copyright (c) 1995-2003, Index Data +/* 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.1 2003-10-27 12:21:33 adam Exp $ - * */ + +/** + * \file odr.c + * \brief Implements fundamental ODR functionality + */ + #if HAVE_CONFIG_H #include #endif +#include #include #include +#include #include +#include +#include #include "odr-priv.h" +static int log_level=0; +static int log_level_initialized=0; + Odr_null *ODR_NULLVAL = (Odr_null *) "NULL"; /* the presence of a null value */ Odr_null *odr_nullval (void) @@ -47,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) @@ -68,11 +87,34 @@ int odr_geterrorx(ODR o, int *x) return o->error; } -char *odr_getelement(ODR o) +const char *odr_getelement(ODR o) { return o->op->element; } +const char **odr_get_element_path(ODR o) +{ + 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) { o->error = error; @@ -89,9 +131,59 @@ void odr_setelement(ODR o, const char *element) } } +void odr_FILE_write(ODR o, void *handle, int type, + 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); + } +#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); + } + } +} + +void odr_FILE_close(void *handle) +{ + FILE *f = (FILE *) handle; + if (f && f != stderr && f != stdout) + fclose(f); +} + void odr_setprint(ODR o, FILE *file) { - o->print = file; + odr_set_stream(o, file, odr_FILE_write, odr_FILE_close); +} + +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)) +{ + o->op->print = (FILE*) handle; + o->op->stream_write = stream_write; + o->op->stream_close = stream_close; } int odr_set_charset(ODR o, const char *to, const char *from) @@ -108,67 +200,81 @@ int odr_set_charset(ODR o, const char *to, const char *from) return 0; } -#include ODR odr_createmem(int direction) { - ODR r; + ODR o; + if (!log_level_initialized) + { + log_level=yaz_log_module_level("odr"); + log_level_initialized=1; + } - if (!(r = (ODR)xmalloc(sizeof(*r)))) + if (!(o = (ODR)xmalloc(sizeof(*o)))) return 0; - r->direction = direction; - r->print = stderr; - r->buf = 0; - r->size = r->pos = r->top = 0; - r->can_grow = 1; - r->mem = nmem_create(); - r->enable_bias = 1; - r->op = (struct Odr_private *) xmalloc (sizeof(*r->op)); - r->op->odr_ber_tag.lclass = -1; - r->op->iconv_handle = 0; - odr_reset(r); - yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, r); - return r; + o->op = (struct Odr_private *) xmalloc (sizeof(*o->op)); + o->direction = direction; + o->buf = 0; + o->size = o->pos = o->top = 0; + o->op->can_grow = 1; + o->mem = nmem_create(); + o->op->enable_bias = 1; + o->op->odr_ber_tag.lclass = -1; + o->op->iconv_handle = 0; + odr_setprint(o, stderr); + odr_reset(o); + yaz_log (log_level, "odr_createmem dir=%d o=%p", direction, o); + return o; } void odr_reset(ODR o) { + if (!log_level_initialized) + { + log_level=yaz_log_module_level("odr"); + log_level_initialized=1; + } + odr_seterror(o, ONONE, 0); 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_DEBUG, "odr_reset o=%p", o); + yaz_log (log_level, "odr_reset o=%p", 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->print && o->print != stderr) - fclose(o->print); + if (o->op->stream_close) + o->op->stream_close(o->op->print); if (o->op->iconv_handle != 0) yaz_iconv_close (o->op->iconv_handle); xfree(o->op); xfree(o); - yaz_log (LOG_DEBUG, "odr_destroy o=%p", o); + yaz_log (log_level, "odr_destroy o=%p", o); } void odr_setbuf(ODR o, char *buf, int len, int can_grow) { + odr_seterror(o, ONONE, 0); 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; } @@ -181,3 +287,22 @@ char *odr_getbuf(ODR o, int *len, int *size) return (char*) o->buf; } +void odr_printf(ODR o, const char *fmt, ...) +{ + va_list ap; + char buf[4096]; + + va_start(ap, fmt); + 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 + */ +