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