Old versions of GILS tables
[yaz-moved-to-github.git] / odr / odr_choice.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_choice.c,v $
7  * Revision 1.14  1997-05-14 06:53:57  adam
8  * C++ support.
9  *
10  * Revision 1.13  1997/04/30 08:52:10  quinn
11  * Null
12  *
13  * Revision 1.12  1996/10/08  12:58:17  adam
14  * New ODR function, odr_choice_enable_bias, to control behaviour of
15  * odr_choice_bias.
16  *
17  * Revision 1.11  1995/09/29  17:12:23  quinn
18  * Smallish
19  *
20  * Revision 1.10  1995/09/27  15:02:58  quinn
21  * Modified function heads & prototypes.
22  *
23  * Revision 1.9  1995/08/15  12:00:23  quinn
24  * Updated External
25  *
26  * Revision 1.8  1995/06/19  17:01:51  quinn
27  * This should bring us in sync with the version distributed as 1.0b
28  *
29  * Revision 1.7  1995/06/19  13:06:50  quinn
30  * Fixed simple bug in the code to handle untagged choice elements.
31  *
32  * Revision 1.6  1995/05/16  08:50:53  quinn
33  * License, documentation, and memory fixes
34  *
35  * Revision 1.5  1995/03/18  12:16:31  quinn
36  * Minor changes.
37  *
38  * Revision 1.4  1995/03/14  16:59:38  quinn
39  * Added odr_constructed_more check
40  *
41  * Revision 1.3  1995/03/08  12:12:22  quinn
42  * Added better error checking.
43  *
44  * Revision 1.2  1995/02/09  15:51:48  quinn
45  * Works better now.
46  *
47  * Revision 1.1  1995/02/07  17:52:59  quinn
48  * A damn mess, but now things work, I think.
49  *
50  */
51
52 #include <odr.h>
53
54 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp)
55 {
56     int i, cl = -1, tg, cn, *which = whichp, bias = o->choice_bias;
57
58     if (o->error)
59         return 0;
60     if (o->direction != ODR_DECODE && !*(char**)p)
61         return 0;
62     o->choice_bias = -1;
63     for (i = 0; arm[i].fun; i++)
64     {
65         if (o->direction == ODR_DECODE)
66         {
67             if (bias >= 0 && bias != arm[i].which)
68                 continue;
69             *which = arm[i].which;
70         }
71         else if (*which != arm[i].which)
72             continue;
73
74         if (arm[i].tagmode != ODR_NONE)
75         {
76             if (o->direction == ODR_DECODE && cl < 0)
77             {
78                 if (o->stackp > -1 && !odr_constructed_more(o))
79                     return 0;
80                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
81                     return 0;
82             }
83             else if (o->direction != ODR_DECODE)
84             {
85                 cl = arm[i].zclass;
86                 tg = arm[i].tag;
87             }
88             if (tg == arm[i].tag && cl == arm[i].zclass)
89             {
90                 if (arm[i].tagmode == ODR_IMPLICIT)
91                 {
92                     odr_implicit_settag(o, cl, tg);
93                     return (*arm[i].fun)(o, p, 0);
94                 }
95                 /* explicit */
96                 if (!odr_constructed_begin(o, p, cl, tg))
97                     return 0;
98                 return (*arm[i].fun)(o, p, 0) &&
99                     odr_constructed_end(o);
100             }
101         }
102         else  /* no tagging. Have to poll type */
103         {
104             if ((*arm[i].fun)(o, p, 1) && *(char**)p)
105                 return 1;
106         }
107     }
108     *which = -1;
109     *(char*)p = 0;
110     return 0;
111 }
112
113 void odr_choice_bias(ODR o, int what)
114 {
115     if (o->enable_bias)
116         o->choice_bias = what;
117 }
118
119 void odr_choice_enable_bias (ODR o, int mode)
120 {
121     o->enable_bias = mode;
122 }