Added odr_set_stream which is is a more generic to odr_setprint.
[yaz-moved-to-github.git] / src / odr_choice.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: odr_choice.c,v 1.2 2004-08-11 12:15:38 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "odr-priv.h"
13
14 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
15                const char *name)
16 {
17     int i, cl = -1, tg, cn, *which = (int *)whichp, bias = o->choice_bias;
18
19     if (o->error)
20         return 0;
21     if (o->direction != ODR_DECODE && !*(char**)p)
22         return 0;
23
24     if (o->direction == ODR_DECODE)
25     {
26         *which = -1;
27         *(char**)p = 0;
28     }
29     o->choice_bias = -1;
30
31     if (o->direction == ODR_PRINT)
32     {
33         if (name)
34         {
35             odr_prname(o, name);
36             odr_printf(o, "choice\n");
37         }
38     }
39     for (i = 0; arm[i].fun; i++)
40     {
41         if (o->direction == ODR_DECODE)
42         {
43             if (bias >= 0 && bias != arm[i].which)
44                 continue;
45             *which = arm[i].which;
46         }
47         else if (*which != arm[i].which)
48             continue;
49
50         if (arm[i].tagmode != ODR_NONE)
51         {
52             if (o->direction == ODR_DECODE && cl < 0)
53             {
54                 if (o->op->stackp > -1 && !odr_constructed_more(o))
55                     return 0;
56                 if (ber_dectag(o->bp, &cl, &tg, &cn, odr_max(o)) <= 0)
57                     return 0;
58             }
59             else if (o->direction != ODR_DECODE)
60             {
61                 cl = arm[i].zclass;
62                 tg = arm[i].tag;
63             }
64             if (tg == arm[i].tag && cl == arm[i].zclass)
65             {
66                 if (arm[i].tagmode == ODR_IMPLICIT)
67                 {
68                     odr_implicit_settag(o, cl, tg);
69                     return (*arm[i].fun)(o, (char **)p, 0, arm[i].name);
70                 }
71                 /* explicit */
72                 if (!odr_constructed_begin(o, p, cl, tg, 0))
73                     return 0;
74                 return (*arm[i].fun)(o, (char **)p, 0, arm[i].name) &&
75                     odr_constructed_end(o);
76             }
77         }
78         else  /* no tagging. Have to poll type */
79         {
80             if ((*arm[i].fun)(o, (char **)p, 1, arm[i].name) && *(char**)p)
81                 return 1;
82         }
83     }
84     return 0;
85 }
86
87 void odr_choice_bias(ODR o, int what)
88 {
89     if (o->enable_bias)
90         o->choice_bias = what;
91 }
92
93 void odr_choice_enable_bias (ODR o, int mode)
94 {
95     o->enable_bias = mode;
96 }