Addded some method functions to the ODR type.
[yaz-moved-to-github.git] / odr / odr.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr.c,v $
7  * Revision 1.2  1995-03-07 10:19:05  quinn
8  * Addded some method functions to the ODR type.
9  *
10  * Revision 1.1  1995/03/07  09:23:15  quinn
11  * Installing top-level API and documentation.
12  *
13  *
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <odr.h>
20
21 char *odr_errlist[] =
22 {
23     "No (unknown) error",
24     "Memoy allocation failed",
25     "System error",
26     "No space in buffer",
27     "Required data element missing",
28     "Unexpected tag",
29     "Other error"
30 };
31
32 void odr_perror(ODR o, char *message)
33 {
34     fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
35 }
36
37 int odr_errno(ODR o)
38 {
39     return o->error;
40 }
41
42 void odr_setprint(ODR o, FILE *file)
43 {
44     o->print = file;
45 }
46
47 ODR odr_createmem(int direction)
48 {
49     struct odr *r;
50
51     if (!(r = malloc(sizeof(*r))))
52         return 0;
53     r->direction = direction;
54     r->print = stdout;
55     r->buf = 0;
56     r->buflen = 0;
57     odr_reset(r);
58     return r;
59 }
60
61 void odr_reset(ODR o)
62 {
63     o->error = ONONE;
64     o->bp = 0;
65     o->left = o->buflen;
66     o->t_class = -1;
67     o->t_tag = -1;
68     o->indent = 0;
69     o->stackp = 0;
70 }
71     
72 void odr_destroy(ODR o)
73 {
74     free(o);
75 }
76
77 void odr_setbuf(ODR o, char *buf, int len)
78 {
79     o->buf = o->bp = (unsigned char *) buf;
80     o->buflen = o->left = len;
81 }
82
83 char *odr_getbuf(ODR o, int *len)
84 {
85     *len = o->bp - o->buf;
86     return (char *) o->buf;
87 }