2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr.c,v 1.43 2003-05-20 19:55:29 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, char *message)
49 fprintf(stderr, "%s: %s: %s\n", message, odr_errlist[o->error],
53 int odr_geterror(ODR o)
58 int odr_geterrorx(ODR o, int *x)
65 char *odr_getaddinfo(ODR o)
67 return o->op->addinfo;
70 void odr_seterror(ODR o, int error, int id)
74 o->op->addinfo[0] = '\0';
77 void odr_setaddinfo(ODR o, const char *addinfo)
81 strncpy(o->op->addinfo, addinfo, sizeof(o->op->addinfo)-1);
82 o->op->addinfo[sizeof(o->op->addinfo)-1] = '\0';
86 void odr_setprint(ODR o, FILE *file)
91 int odr_set_charset(ODR o, const char *to, const char *from)
93 if (o->op->iconv_handle)
94 yaz_iconv_close (o->op->iconv_handle);
95 o->op->iconv_handle = 0;
98 o->op->iconv_handle = yaz_iconv_open (to, from);
99 if (o->op->iconv_handle == 0)
107 ODR odr_createmem(int direction)
111 if (!(r = (ODR)xmalloc(sizeof(*r))))
113 r->direction = direction;
116 r->size = r->pos = r->top = 0;
118 r->mem = nmem_create();
120 r->op = (struct Odr_private *) xmalloc (sizeof(*r->op));
121 r->op->odr_ber_tag.lclass = -1;
122 r->op->iconv_handle = 0;
124 yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, r);
128 void odr_reset(ODR o)
130 odr_seterror(o, ONONE, 0);
132 odr_seek(o, ODR_S_SET, 0);
141 if (o->op->iconv_handle != 0)
142 yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
143 yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
146 void odr_destroy(ODR o)
148 nmem_destroy(o->mem);
149 if (o->buf && o->can_grow)
151 if (o->print && o->print != stderr)
153 if (o->op->iconv_handle != 0)
154 yaz_iconv_close (o->op->iconv_handle);
157 yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
160 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
162 o->bp = (unsigned char *) buf;
164 o->buf = (unsigned char *) buf;
165 o->can_grow = can_grow;
170 char *odr_getbuf(ODR o, int *len, int *size)
175 return (char*) o->buf;