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