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