A damn mess, but now things work, I think.
[yaz-moved-to-github.git] / odr / odr_choice.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_choice.c,v $
7  * Revision 1.1  1995-02-07 17:52:59  quinn
8  * A damn mess, but now things work, I think.
9  *
10  */
11
12 #include <odr.h>
13
14 int odr_choice(ODR o, Odr_arm arm[], void *p, int *which)
15 {
16     int i, cl = -1, tg, cn;
17
18     if (o->direction != ODR_DECODE && !*(char*)p)
19         return 0;
20     for (i = 0; arm[i].fun; i++)
21     {
22         if (o->direction == ODR_DECODE)
23             *which = arm[i].which;
24         else if (*which != arm[i].which)
25             continue;
26
27         if (arm[i].tagmode != ODR_NONE)
28         {
29             if (o->direction == ODR_DECODE && cl < 0)
30             {
31                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
32                     return 0;
33             }
34             else if (o->direction != ODR_DECODE)
35             {
36                 cl = arm[i].class;
37                 tg = arm[i].tag;
38             }
39             if (tg == arm[i].tag && cl == arm[i].class)
40             {
41                 if (arm[i].tagmode == ODR_IMPLICIT)
42                 {
43                     odr_implicit_settag(o, cl, tg);
44                     return (*arm[i].fun)(o, p, 0);
45                 }
46                 /* explicit */
47                 if (!odr_constructed_begin(o, p, cl, tg))
48                     return 0;
49                 return (*arm[i].fun)(o, p, 0) &&
50                     odr_constructed_end(o);
51             }
52         }
53         else  /* no tagging. Have to poll type */
54             if ((*arm[i].fun)(o, p, 0))
55                 return 1;
56     }
57     *which = -1;
58     *(char*)p = 0;
59     return 0;
60 }