Client uses prefix query notation.
[yaz-moved-to-github.git] / odr / odr.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr.c,v $
7  * Revision 1.13  1995-05-22 11:32:02  quinn
8  * Fixing Interface to odr_null.
9  *
10  * Revision 1.12  1995/05/16  08:50:49  quinn
11  * License, documentation, and memory fixes
12  *
13  * Revision 1.11  1995/05/15  11:56:08  quinn
14  * More work on memory management.
15  *
16  * Revision 1.10  1995/04/18  08:15:20  quinn
17  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
18  * neater. We'll make the same change for decoding one day.
19  *
20  * Revision 1.9  1995/04/10  10:23:11  quinn
21  * Smallish changes.
22  *
23  * Revision 1.8  1995/03/17  10:17:43  quinn
24  * Added memory management.
25  *
26  * Revision 1.7  1995/03/10  11:44:41  quinn
27  * Fixed serious stack-bug in odr_cons_begin
28  *
29  * Revision 1.6  1995/03/08  12:12:15  quinn
30  * Added better error checking.
31  *
32  * Revision 1.5  1995/03/07  13:28:57  quinn
33  * *** empty log message ***
34  *
35  * Revision 1.4  1995/03/07  13:16:13  quinn
36  * Fixed bug in odr_reset
37  *
38  * Revision 1.3  1995/03/07  10:21:31  quinn
39  * odr_errno-->odr_error
40  *
41  * Revision 1.2  1995/03/07  10:19:05  quinn
42  * Addded some method functions to the ODR type.
43  *
44  * Revision 1.1  1995/03/07  09:23:15  quinn
45  * Installing top-level API and documentation.
46  *
47  *
48  */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52
53 #include <dmalloc.h>
54 #include <odr.h>
55
56 Odr_null *ODR_NULLVAL = "NULL";  /* the presence of a null value */
57
58 char *odr_errlist[] =
59 {
60     "No (unknown) error",
61     "Memory allocation failed",
62     "System error",
63     "No space in buffer",
64     "Required data element missing",
65     "Unexpected tag",
66     "Other error",
67     "Protocol error",
68     "Malformed data",
69     "Stack overflow"
70 };
71
72 void odr_perror(ODR o, char *message)
73 {
74     fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
75 }
76
77 int odr_geterror(ODR o)
78 {
79     return o->error;
80 }
81
82 void odr_setprint(ODR o, FILE *file)
83 {
84     o->print = file;
85 }
86
87 ODR odr_createmem(int direction)
88 {
89     struct odr *r;
90
91     if (!(r = malloc(sizeof(*r))))
92         return 0;
93     r->direction = direction;
94     r->print = stderr;
95     r->buf = 0;
96     r->ecb.buf = 0;
97     r->ecb.size = r->ecb.pos = r->ecb.top = 0;
98     r->ecb.can_grow = 1;
99     r->buflen = 0;
100     r->mem = 0;
101     odr_reset(r);
102     return r;
103 }
104
105 void odr_reset(ODR o)
106 {
107     o->error = ONONE;
108     o->bp = o->buf;
109     odr_seek(o, ODR_S_SET, 0);
110     o->ecb.top = 0;
111     o->left = o->buflen;
112     o->t_class = -1;
113     o->t_tag = -1;
114     o->indent = 0;
115     o->stackp = -1;
116     odr_release_mem(o->mem);
117     o->mem = 0;
118 }
119     
120 void odr_destroy(ODR o)
121 {
122     odr_release_mem(o->mem);
123     if (o->ecb.buf && o->ecb.can_grow)
124         free(o->ecb.buf);
125     if (o->print != stderr)
126         fclose(o->print);
127     free(o);
128 }
129
130 void odr_setbuf(ODR o, char *buf, int len, int can_grow)
131 {
132     o->buf = o->bp = (unsigned char *) buf;
133     o->buflen = o->left = len;
134
135     o->ecb.buf = (unsigned char *) buf;
136     o->ecb.can_grow = can_grow;
137     o->ecb.top = o->ecb.pos = 0;
138     o->ecb.size = len;
139 }
140
141 char *odr_getbuf(ODR o, int *len, int *size)
142 {
143     *len = o->ecb.top;
144     if (size)
145         *size = o->ecb.size;
146     return (char*) o->ecb.buf;
147 }