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