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