bfa833462c74be85ea9a9a57b8b3bffe0bf7dc22
[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.7  1995-03-10 11:44:41  quinn
8  * Fixed serious stack-bug in odr_cons_begin
9  *
10  * Revision 1.6  1995/03/08  12:12:15  quinn
11  * Added better error checking.
12  *
13  * Revision 1.5  1995/03/07  13:28:57  quinn
14  * *** empty log message ***
15  *
16  * Revision 1.4  1995/03/07  13:16:13  quinn
17  * Fixed bug in odr_reset
18  *
19  * Revision 1.3  1995/03/07  10:21:31  quinn
20  * odr_errno-->odr_error
21  *
22  * Revision 1.2  1995/03/07  10:19:05  quinn
23  * Addded some method functions to the ODR type.
24  *
25  * Revision 1.1  1995/03/07  09:23:15  quinn
26  * Installing top-level API and documentation.
27  *
28  *
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <odr.h>
35
36 char *odr_errlist[] =
37 {
38     "No (unknown) error",
39     "Memory allocation failed",
40     "System error",
41     "No space in buffer",
42     "Required data element missing",
43     "Unexpected tag",
44     "Other error",
45     "Protocol error",
46     "Malformed data",
47     "Stack overflow"
48 };
49
50 void odr_perror(ODR o, char *message)
51 {
52     fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
53 }
54
55 int odr_geterror(ODR o)
56 {
57     return o->error;
58 }
59
60 void odr_setprint(ODR o, FILE *file)
61 {
62     o->print = file;
63 }
64
65 ODR odr_createmem(int direction)
66 {
67     struct odr *r;
68
69     if (!(r = malloc(sizeof(*r))))
70         return 0;
71     r->direction = direction;
72     r->print = stdout;
73     r->buf = 0;
74     r->buflen = 0;
75     odr_reset(r);
76     return r;
77 }
78
79 void odr_reset(ODR o)
80 {
81     o->error = ONONE;
82     o->bp = o->buf;
83     o->left = o->buflen;
84     o->t_class = -1;
85     o->t_tag = -1;
86     o->indent = 0;
87     o->stackp = -1;
88 }
89     
90 void odr_destroy(ODR o)
91 {
92     free(o);
93 }
94
95 void odr_setbuf(ODR o, char *buf, int len)
96 {
97     o->buf = o->bp = (unsigned char *) buf;
98     o->buflen = o->left = len;
99 }
100
101 char *odr_getbuf(ODR o, int *len)
102 {
103     *len = o->bp - o->buf;
104     return (char *) o->buf;
105 }