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