2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr.c,v 1.1 2003-10-27 12:21:33 adam Exp $
15 #include <yaz/xmalloc.h>
18 Odr_null *ODR_NULLVAL = (Odr_null *) "NULL"; /* the presence of a null value */
20 Odr_null *odr_nullval (void)
28 "Memory allocation failed",
31 "Required data element missing",
37 "Length of constructed type different from sum of members",
38 "Overflow writing definite length of constructed type",
42 char *odr_errmsg(int n)
44 return odr_errlist[n];
47 void odr_perror(ODR o, const char *message)
49 const char *e = odr_getelement(o);
52 err = odr_geterrorx(o, &x);
53 fprintf(stderr, "%s: %s (code %d:%d)", message, odr_errlist[err], err, x);
55 fprintf (stderr, " element %s", e);
56 fprintf(stderr, "\n");
59 int odr_geterror(ODR o)
64 int odr_geterrorx(ODR o, int *x)
71 char *odr_getelement(ODR o)
73 return o->op->element;
76 void odr_seterror(ODR o, int error, int id)
80 o->op->element[0] = '\0';
83 void odr_setelement(ODR o, const char *element)
87 strncpy(o->op->element, element, sizeof(o->op->element)-1);
88 o->op->element[sizeof(o->op->element)-1] = '\0';
92 void odr_setprint(ODR o, FILE *file)
97 int odr_set_charset(ODR o, const char *to, const char *from)
99 if (o->op->iconv_handle)
100 yaz_iconv_close (o->op->iconv_handle);
101 o->op->iconv_handle = 0;
104 o->op->iconv_handle = yaz_iconv_open (to, from);
105 if (o->op->iconv_handle == 0)
113 ODR odr_createmem(int direction)
117 if (!(r = (ODR)xmalloc(sizeof(*r))))
119 r->direction = direction;
122 r->size = r->pos = r->top = 0;
124 r->mem = nmem_create();
126 r->op = (struct Odr_private *) xmalloc (sizeof(*r->op));
127 r->op->odr_ber_tag.lclass = -1;
128 r->op->iconv_handle = 0;
130 yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, r);
134 void odr_reset(ODR o)
136 odr_seterror(o, ONONE, 0);
138 odr_seek(o, ODR_S_SET, 0);
147 if (o->op->iconv_handle != 0)
148 yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
149 yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
152 void odr_destroy(ODR o)
154 nmem_destroy(o->mem);
155 if (o->buf && o->can_grow)
157 if (o->print && o->print != stderr)
159 if (o->op->iconv_handle != 0)
160 yaz_iconv_close (o->op->iconv_handle);
163 yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
166 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
168 o->bp = (unsigned char *) buf;
170 o->buf = (unsigned char *) buf;
171 o->can_grow = can_grow;
176 char *odr_getbuf(ODR o, int *len, int *size)
181 return (char*) o->buf;