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