*** empty log message ***
[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.6  1995-05-16 08:50:53  quinn
8  * License, documentation, and memory fixes
9  *
10  * Revision 1.5  1995/03/18  12:16:31  quinn
11  * Minor changes.
12  *
13  * Revision 1.4  1995/03/14  16:59:38  quinn
14  * Added odr_constructed_more check
15  *
16  * Revision 1.3  1995/03/08  12:12:22  quinn
17  * Added better error checking.
18  *
19  * Revision 1.2  1995/02/09  15:51:48  quinn
20  * Works better now.
21  *
22  * Revision 1.1  1995/02/07  17:52:59  quinn
23  * A damn mess, but now things work, I think.
24  *
25  */
26
27 #include <odr.h>
28
29 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp)
30 {
31     int i, cl = -1, tg, cn, *which = whichp;
32
33     if (o->error)
34         return 0;
35     if (o->direction != ODR_DECODE && !*(char**)p)
36         return 0;
37     for (i = 0; arm[i].fun; i++)
38     {
39         if (o->direction == ODR_DECODE)
40             *which = arm[i].which;
41         else if (*which != arm[i].which)
42             continue;
43
44         if (arm[i].tagmode != ODR_NONE)
45         {
46             if (o->direction == ODR_DECODE && cl < 0)
47             {
48                 if (o->stackp > -1 && !odr_constructed_more(o))
49                     return 0;
50                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
51                     return 0;
52             }
53             else if (o->direction != ODR_DECODE)
54             {
55                 cl = arm[i].class;
56                 tg = arm[i].tag;
57             }
58             if (tg == arm[i].tag && cl == arm[i].class)
59             {
60                 if (arm[i].tagmode == ODR_IMPLICIT)
61                 {
62                     odr_implicit_settag(o, cl, tg);
63                     return (*arm[i].fun)(o, p, 0);
64                 }
65                 /* explicit */
66                 if (!odr_constructed_begin(o, p, cl, tg))
67                     return 0;
68                 return (*arm[i].fun)(o, p, 0) &&
69                     odr_constructed_end(o);
70             }
71         }
72         else  /* no tagging. Have to poll type */
73             if ((*arm[i].fun)(o, p, 0))
74                 return 1;
75     }
76     *which = -1;
77     *(char*)p = 0;
78     return 0;
79 }