f24f51a2ec204e02cca96c6b18c2da35b2c20c79
[yaz-moved-to-github.git] / odr / odr_enum.c
1 /*
2  * Copyright (c) 1995-1999, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_enum.c,v $
7  * Revision 1.4  1999-11-30 13:47:11  adam
8  * Improved installation. Moved header files to include/yaz.
9  *
10  * Revision 1.3  1999/04/20 09:56:48  adam
11  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
12  * Modified all encoders/decoders to reflect this change.
13  *
14  * Revision 1.2  1999/01/08 11:23:27  adam
15  * Added const modifier to some of the BER/ODR encoding routines.
16  *
17  * Revision 1.1  1998/03/20 14:45:01  adam
18  * Implemented odr_enum and odr_set_of.
19  *
20  */
21
22 #include <yaz/odr.h>
23
24 /*
25  * Top level enum en/decoder.
26  * Returns 1 on success, 0 on error.
27  */
28 int odr_enum(ODR o, int **p, int opt, const char *name)
29 {
30     int res, cons = 0;
31
32     if (o->error)
33         return 0;
34     if (o->t_class < 0)
35     {
36         o->t_class = ODR_UNIVERSAL;
37         o->t_tag = ODR_ENUM;
38     }
39     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt)) < 0)
40         return 0;
41     if (!res)
42         return opt;
43     if (o->direction == ODR_PRINT)
44     {
45         odr_prname(o, name);
46         fprintf(o->print, "%d\n", **p);
47         return 1;
48     }
49     if (cons)
50     {
51         o->error = OPROTO;
52         return 0;
53     }
54     if (o->direction == ODR_DECODE)
55         *p = (int *)odr_malloc(o, sizeof(int));
56     return ber_integer(o, *p);
57 }