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