2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr.c,v 1.4 2004-08-13 08:58:59 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 const char *odr_getelement(ODR o)
74 return o->op->element;
77 const char **odr_get_element_path(ODR o)
79 return o->op->stack_names;
82 void odr_seterror(ODR o, int error, int id)
86 o->op->element[0] = '\0';
89 void odr_setelement(ODR o, const char *element)
93 strncpy(o->op->element, element, sizeof(o->op->element)-1);
94 o->op->element[sizeof(o->op->element)-1] = '\0';
98 void odr_FILE_write(ODR o, void *handle, int type,
99 const char *buf, int len)
103 if (type == ODR_OCTETSTRING)
105 const char **stack_names = odr_get_element_path(o);
106 for (i = 0; stack_names[i]; i++)
107 fprintf((FILE*) handle, "[%s]", stack_names[i]);
108 fputs("\n", (FILE*) handle);
111 for (i = 0; i<len; i++)
113 unsigned c = ((const unsigned char *) buf)[i];
114 if (i == 2000 && len > 3100)
116 fputs(" ..... ", (FILE*) handle);
119 if (strchr("\r\n\f\t", c) || c >= ' ' && c <= 126)
120 putc(c, (FILE*) handle);
124 sprintf(x, "\\X%02X", c);
125 fputs(x, (FILE*) handle);
130 void odr_FILE_close(void *handle)
132 FILE *f = (FILE *) handle;
133 if (f && f != stderr && f != stdout)
137 void odr_setprint(ODR o, FILE *file)
139 odr_set_stream(o, file, odr_FILE_write, odr_FILE_close);
142 void odr_set_stream(ODR o, void *handle,
143 void (*stream_write)(ODR o,
144 void *handle, int type,
145 const char *buf, int len),
146 void (*stream_close)(void *handle))
149 o->op->stream_write = stream_write;
150 o->op->stream_close = stream_close;
153 int odr_set_charset(ODR o, const char *to, const char *from)
155 if (o->op->iconv_handle)
156 yaz_iconv_close (o->op->iconv_handle);
157 o->op->iconv_handle = 0;
160 o->op->iconv_handle = yaz_iconv_open (to, from);
161 if (o->op->iconv_handle == 0)
169 ODR odr_createmem(int direction)
173 if (!(o = (ODR)xmalloc(sizeof(*o))))
175 o->direction = direction;
177 o->size = o->pos = o->top = 0;
179 o->mem = nmem_create();
181 o->op = (struct Odr_private *) xmalloc (sizeof(*o->op));
182 o->op->odr_ber_tag.lclass = -1;
183 o->op->iconv_handle = 0;
184 odr_setprint(o, stderr);
186 yaz_log (LOG_DEBUG, "odr_createmem dir=%d o=%p", direction, o);
190 void odr_reset(ODR o)
192 odr_seterror(o, ONONE, 0);
194 odr_seek(o, ODR_S_SET, 0);
203 if (o->op->iconv_handle != 0)
204 yaz_iconv(o->op->iconv_handle, 0, 0, 0, 0);
205 yaz_log (LOG_DEBUG, "odr_reset o=%p", o);
208 void odr_destroy(ODR o)
210 nmem_destroy(o->mem);
211 if (o->buf && o->can_grow)
213 if (o->op->stream_close)
214 o->op->stream_close(o->print);
215 if (o->op->iconv_handle != 0)
216 yaz_iconv_close (o->op->iconv_handle);
219 yaz_log (LOG_DEBUG, "odr_destroy o=%p", o);
222 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
224 o->bp = (unsigned char *) buf;
226 o->buf = (unsigned char *) buf;
227 o->can_grow = can_grow;
232 char *odr_getbuf(ODR o, int *len, int *size)
237 return (char*) o->buf;
240 void odr_printf(ODR o, const char *fmt, ...)
247 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
250 vsnprintf(buf, sizeof(buf), fmt, ap);
252 vsprintf(buf, fmt, ap);
255 o->op->stream_write(o, o->print, ODR_VISIBLESTRING, buf, strlen(buf));