Modified function heads & prototypes.
[yaz-moved-to-github.git] / odr / odr_seq.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_seq.c,v $
7  * Revision 1.15  1995-09-27 15:03:00  quinn
8  * Modified function heads & prototypes.
9  *
10  * Revision 1.14  1995/08/15  11:16:39  quinn
11  * Fixed pretty-printers.
12  * CV:e ----------------------------------------------------------------------
13  * CV:e ----------------------------------------------------------------------
14  *
15  * Revision 1.13  1995/05/22  14:56:57  quinn
16  * Fixed problem in decoding empty sequence.
17  *
18  * Revision 1.12  1995/05/18  13:06:32  quinn
19  * Smallish.
20  *
21  * Revision 1.11  1995/05/17  08:41:54  quinn
22  * Small, hopefully insignificant change.
23  *
24  * Revision 1.10  1995/05/16  08:50:59  quinn
25  * License, documentation, and memory fixes
26  *
27  * Revision 1.9  1995/03/17  10:17:57  quinn
28  * Added memory management.
29  *
30  * Revision 1.8  1995/03/15  11:18:05  quinn
31  * Fixed serious bug in odr_cons
32  *
33  * Revision 1.7  1995/03/08  12:12:30  quinn
34  * Added better error checking.
35  *
36  * Revision 1.6  1995/02/10  15:55:29  quinn
37  * Bug fixes, mostly.
38  *
39  * Revision 1.5  1995/02/09  15:51:49  quinn
40  * Works better now.
41  *
42  * Revision 1.4  1995/02/07  17:53:00  quinn
43  * A damn mess, but now things work, I think.
44  *
45  * Revision 1.3  1995/02/07  14:13:46  quinn
46  * Bug fixes.
47  *
48  * Revision 1.2  1995/02/06  16:45:03  quinn
49  * Small mods.
50  *
51  * Revision 1.1  1995/02/02  16:21:54  quinn
52  * First kick.
53  *
54  */
55
56 #include <odr.h>
57 #include <assert.h>
58
59 int MDF odr_sequence_begin(ODR o, void *p, int size)
60 {
61     char **pp = (char**) p;
62
63     if (o->error)
64         return 0;
65     if (o->t_class < 0)
66     {
67         o->t_class = ODR_UNIVERSAL;
68         o->t_tag = ODR_SEQUENCE;
69     }
70     if (o->direction == ODR_DECODE)
71         *pp = 0;
72     if (odr_constructed_begin(o, p, o->t_class, o->t_tag))
73     {
74         if (o->direction == ODR_DECODE && size)
75             *pp = odr_malloc(o, size);
76         return 1;
77     }
78     else
79         return 0;
80 }
81
82 int MDF odr_sequence_end(ODR o)
83 {
84     return odr_constructed_end(o);    
85 }
86
87 int odr_sequence_more(ODR o)
88 {
89     return odr_constructed_more(o);
90 }
91
92 int MDF odr_sequence_of(ODR o, Odr_fun type, void *p, int *num)
93 {
94     char ***pp = (char***) p;  /* for dereferencing */
95     char **tmp = 0;
96     int size = 0, i;
97
98     if (!odr_sequence_begin(o, p, 0))
99         return 0;
100
101     switch (o->direction)
102     {
103         case ODR_DECODE:
104             *num = 0;
105             *pp = ODR_NULLVAL;
106             while (odr_sequence_more(o))
107             {
108                 /* outgrown array? */
109                 if (*num * sizeof(void*) >= size)
110                 {
111                     /* double the buffer size */
112                     tmp = odr_malloc(o, sizeof(void*) * (size += size ? size :
113                         128));
114                     if (*num)
115                     {
116                         memcpy(tmp, *pp, *num * sizeof(void*));
117                         /*
118                          * For now, we just throw the old *p away, since we use
119                          * nibble memory anyway (disgusting, isn't it?).
120                          */
121                     }
122                     *pp = tmp;
123                 }
124                 if (!(*type)(o, (*pp) + *num, 0))
125                     return 0;
126                 (*num)++;
127             }
128             break;
129         case ODR_ENCODE: case ODR_PRINT:
130 #ifdef ODR_DEBUG
131             fprintf(stderr, "[seqof: num=%d]", *num);
132 #endif
133             for (i = 0; i < *num; i++)
134             {
135 #ifdef ODR_DEBUG
136                 fprintf(stderr, "[seqof: elem #%d]", i);
137 #endif
138                 if (!(*type)(o, *pp + i, 0))
139                     return 0;
140             }
141             break;
142         default:
143             o->error = OOTHER;
144             return 0;
145     }
146     return odr_sequence_end(o);
147 }