Fix sample PQF
[yaz-moved-to-github.git] / odr / odr_seq.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_seq.c,v 1.30 2003-03-11 11:03:31 adam Exp $
7  */
8
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include "odr-priv.h"
14
15 int odr_sequence_begin(ODR o, void *p, int size, const char *name)
16 {
17     char **pp = (char**) p;
18
19     if (o->error)
20         return 0;
21     if (o->t_class < 0)
22     {
23         o->t_class = ODR_UNIVERSAL;
24         o->t_tag = ODR_SEQUENCE;
25     }
26     if (o->direction == ODR_DECODE)
27         *pp = 0;
28     if (odr_constructed_begin(o, p, o->t_class, o->t_tag, name))
29     {
30         if (o->direction == ODR_DECODE && size)
31             *pp = (char *)odr_malloc(o, size);
32         return 1;
33     }
34     else
35         return 0;
36 }
37
38 int odr_set_begin(ODR o, void *p, int size, const char *name)
39 {
40     char **pp = (char**) p;
41
42     if (o->error)
43         return 0;
44     if (o->t_class < 0)
45     {
46         o->t_class = ODR_UNIVERSAL;
47         o->t_tag = ODR_SET;
48     }
49     if (o->direction == ODR_DECODE)
50         *pp = 0;
51     if (odr_constructed_begin(o, p, o->t_class, o->t_tag, name))
52     {
53         if (o->direction == ODR_DECODE && size)
54             *pp = (char *)odr_malloc(o, size);
55         return 1;
56     }
57     else
58         return 0;
59 }
60
61 int odr_sequence_end(ODR o)
62 {
63     return odr_constructed_end(o);    
64 }
65
66 int odr_set_end(ODR o)
67 {
68     return odr_constructed_end(o);    
69 }
70
71 static int odr_sequence_more(ODR o)
72 {
73     return odr_constructed_more(o);
74 }
75
76 static int odr_sequence_x (ODR o, Odr_fun type, void *p, int *num)
77 {
78     char ***pp = (char***) p;  /* for dereferencing */
79     char **tmp = 0;
80     int size = 0, i;
81
82     switch (o->direction)
83     {
84         case ODR_DECODE:
85             *num = 0;
86             *pp = (char **)odr_nullval();
87             while (odr_sequence_more(o))
88             {
89                 /* outgrown array? */
90                 if (*num * (int) sizeof(void*) >= size)
91                 {
92                     /* double the buffer size */
93                     tmp = (char **)odr_malloc(o, sizeof(void*) *
94                                               (size += size ? size : 128));
95                     if (*num)
96                     {
97                         memcpy(tmp, *pp, *num * sizeof(void*));
98                         /*
99                          * For now, we just throw the old *p away, since we use
100                          * nibble memory anyway (disgusting, isn't it?).
101                          */
102                     }
103                     *pp = tmp;
104                 }
105                 if (!(*type)(o, (*pp) + *num, 0, 0))
106                     return 0;
107                 (*num)++;
108             }
109             break;
110         case ODR_ENCODE: case ODR_PRINT:
111 #ifdef ODR_DEBUG
112             fprintf(stderr, "[seqof: num=%d]", *num);
113 #endif
114             for (i = 0; i < *num; i++)
115             {
116 #ifdef ODR_DEBUG
117                 fprintf(stderr, "[seqof: elem #%d]", i);
118 #endif
119                 if (!(*type)(o, *pp + i, 0, 0))
120                     return 0;
121             }
122             break;
123         default:
124             odr_seterror(o, OOTHER, 47);
125             return 0;
126     }
127     return odr_sequence_end(o);
128 }
129
130 int odr_set_of(ODR o, Odr_fun type, void *p, int *num, const char *name)
131 {
132     if (!odr_set_begin(o, p, 0, name)) {
133         if (o->direction == ODR_DECODE)
134             *num = 0;
135         return 0;
136     }
137     return odr_sequence_x (o, type, p, num);
138 }
139
140 int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
141                     const char *name)
142 {
143     if (!odr_sequence_begin(o, p, 0, name)) {
144         if (o->direction == ODR_DECODE)
145             *num = 0;
146         return 0;
147     }
148     return odr_sequence_x (o, type, p, num);
149 }
150