Bug fixes, mostly.
[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.6  1995-02-10 15:55:29  quinn
8  * Bug fixes, mostly.
9  *
10  * Revision 1.5  1995/02/09  15:51:49  quinn
11  * Works better now.
12  *
13  * Revision 1.4  1995/02/07  17:53:00  quinn
14  * A damn mess, but now things work, I think.
15  *
16  * Revision 1.3  1995/02/07  14:13:46  quinn
17  * Bug fixes.
18  *
19  * Revision 1.2  1995/02/06  16:45:03  quinn
20  * Small mods.
21  *
22  * Revision 1.1  1995/02/02  16:21:54  quinn
23  * First kick.
24  *
25  */
26
27 #include <odr.h>
28 #include <assert.h>
29
30 int odr_sequence_begin(ODR o, void *p, int size)
31 {
32     char **pp = (char**) p;
33
34     if (o->t_class < 0)
35     {
36         o->t_class = ODR_UNIVERSAL;
37         o->t_tag = ODR_SEQUENCE;
38     }
39
40     if (o->direction == ODR_DECODE)
41         *pp = 0;
42     if (odr_constructed_begin(o, p, o->t_class, o->t_tag))
43     {
44         if (o->direction == ODR_DECODE && size)
45             *pp = nalloc(o, size);
46         if (o->direction == ODR_PRINT)
47         {
48             fprintf(o->print, "%s{\n", odr_indent(o));
49             o->indent++;
50         }
51         return 1;
52     }
53     else
54         return 0;
55 }
56
57 int odr_sequence_end(ODR o)
58 {
59     if (o->direction == ODR_PRINT)
60     {
61         assert(o->indent > 0);
62         o->indent--;
63         fprintf(o->print, "%s}\n", odr_indent(o));
64     }
65     return odr_constructed_end(o);    
66 }
67
68 int odr_sequence_more(ODR o)
69 {
70     return odr_constructed_more(o);
71 }
72
73 int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num)
74 {
75     char ***pp = (char***) p;  /* for dereferencing */
76     char **tmp;
77     char *dummy = "Nothing";
78     int size = 0, i;
79
80     if (!odr_sequence_begin(o, &dummy, 0))
81         return 0;
82
83     switch (o->direction)
84     {
85         case ODR_DECODE:
86             *num = 0;
87             while (odr_sequence_more(o))
88             {
89                 /* outgrown array? */
90                 if (*num * sizeof(void*) >= size)
91                 {
92                     /* double the buffer size */
93                     tmp = nalloc(o, sizeof(void*) * (size += size ? size :
94                         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))
106                     return 0;
107                 (*num)++;
108             }
109             break;
110         case ODR_ENCODE: case ODR_PRINT:
111             for (i = 0; i < *num; i++)
112                 if (!(*type)(o, *pp + i, 0))
113                     return 0;
114             break;
115         default: return 0;
116     }
117     return odr_sequence_end(o);
118 }