Works better now.
[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.2  1995-02-09 15:51:48  quinn
8  * Works better now.
9  *
10  * Revision 1.1  1995/02/07  17:52:59  quinn
11  * A damn mess, but now things work, I think.
12  *
13  */
14
15 #include <odr.h>
16
17 int odr_choice(ODR o, Odr_arm arm[], void *p, int *which)
18 {
19     int i, cl = -1, tg, cn;
20
21     if (o->direction != ODR_DECODE && !*(char**)p)
22         return 0;
23     for (i = 0; arm[i].fun; i++)
24     {
25         if (o->direction == ODR_DECODE)
26             *which = arm[i].which;
27         else if (*which != arm[i].which)
28             continue;
29
30         if (arm[i].tagmode != ODR_NONE)
31         {
32             if (o->direction == ODR_DECODE && cl < 0)
33             {
34                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
35                     return 0;
36             }
37             else if (o->direction != ODR_DECODE)
38             {
39                 cl = arm[i].class;
40                 tg = arm[i].tag;
41             }
42             if (tg == arm[i].tag && cl == arm[i].class)
43             {
44                 if (arm[i].tagmode == ODR_IMPLICIT)
45                 {
46                     odr_implicit_settag(o, cl, tg);
47                     return (*arm[i].fun)(o, p, 0);
48                 }
49                 /* explicit */
50                 if (!odr_constructed_begin(o, p, cl, tg))
51                     return 0;
52                 return (*arm[i].fun)(o, p, 0) &&
53                     odr_constructed_end(o);
54             }
55         }
56         else  /* no tagging. Have to poll type */
57             if ((*arm[i].fun)(o, p, 0))
58                 return 1;
59     }
60     *which = -1;
61     *(char*)p = 0;
62     return 0;
63 }