2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr.c,v 1.2 2004-08-11 12:15:38 adam Exp $
16 #include <yaz/xmalloc.h>
19 Odr_null *ODR_NULLVAL = (Odr_null *) "NULL"; /* the presence of a null value */
21 Odr_null *odr_nullval (void)
29 "Memory allocation failed",
32 "Required data element missing",
38 "Length of constructed type different from sum of members",
39 "Overflow writing definite length of constructed type",
43 char *odr_errmsg(int n)
45 return odr_errlist[n];
48 void odr_perror(ODR o, const char *message)
50 const char *e = odr_getelement(o);
53 err = odr_geterrorx(o, &x);
54 fprintf(stderr, "%s: %s (code %d:%d)", message, odr_errlist[err], err, x);
56 fprintf (stderr, " element %s", e);
57 fprintf(stderr, "\n");
60 int odr_geterror(ODR o)
65 int odr_geterrorx(ODR o, int *x)
72 char *odr_getelement(ODR o)
74 return o->op->element;
77 void odr_seterror(ODR o, int error, int id)
81 o->op->element[0] = '\0';
84 void odr_setelement(ODR o, const char *element)
88 strncpy(o->op->element, element, sizeof(o->op->element)-1);
89 o->op->element[sizeof(o->op->element)-1] = '\0';
93 void odr_FILE_puts(void *handle, const char *strz)
95 fputs(strz, (FILE*) handle);
98 void odr_FILE_close(void *handle)
100 FILE *f = (FILE *) handle;
101 if (f && f != stderr && f != stdout)
105 void odr_setprint(ODR o, FILE *file)
107 odr_set_stream(o, file, odr_FILE_puts, odr_FILE_close);
110 void odr_set_stream(ODR o, void *handle,
111 void (*stream_puts)(void *handle, const char *strz),
112 void (*stream_close)(void *handle))
115 o->op->stream_puts = stream_puts;
116 o->op->stream_close = stream_close;
119 int odr_set_charset(ODR o, const char *to, const char *from)
121 if (o->op->iconv_handle)
122 yaz_iconv_close (o->op->iconv_handle);
123 o->op->iconv_handle = 0;
126 o->op->iconv_handle = yaz_iconv_open (to, from);
127 if (o->op->iconv_handle == 0)
135 ODR odr_createmem(int direction)
139 if (!(o = (ODR)xmalloc(sizeof(*o))))
141 o->direction = direction;
143 o->size = o->pos = o->top = 0;
145 o->mem = nmem_create();
147 o->op = (struct Odr_private *) xmalloc (sizeof(*o->op));
148 o->op->odr_ber_tag.lclass = -1;
149 o->op->iconv_handle = 0;
150 odr_setprint(o, stderr);
152 yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, o);
156 void odr_reset(ODR o)
158 odr_seterror(o, ONONE, 0);
160 odr_seek(o, ODR_S_SET, 0);
169 if (o->op->iconv_handle != 0)
170 yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
171 yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
174 void odr_destroy(ODR o)
176 nmem_destroy(o->mem);
177 if (o->buf && o->can_grow)
179 if (o->op->stream_close)
180 o->op->stream_close(o->print);
181 if (o->op->iconv_handle != 0)
182 yaz_iconv_close (o->op->iconv_handle);
185 yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
188 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
190 o->bp = (unsigned char *) buf;
192 o->buf = (unsigned char *) buf;
193 o->can_grow = can_grow;
198 char *odr_getbuf(ODR o, int *len, int *size)
203 return (char*) o->buf;
206 void odr_printf(ODR o, const char *fmt, ...)
213 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
216 vsnprintf(buf, sizeof(buf), fmt, ap);
218 vsprintf(buf, fmt, ap);
221 o->op->stream_puts(o->print, buf);