Small mods.
[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.2  1995-02-06 16:45:03  quinn
8  * Small mods.
9  *
10  * Revision 1.1  1995/02/02  16:21:54  quinn
11  * First kick.
12  *
13  */
14
15 #include <odr.h>
16
17 int odr_sequence_begin(ODR o, void *p, int size)
18 {
19     char **pp = (char**) p;
20
21     if (o->t_class < 0)
22     {
23         o->t_class = ODR_UNIVERSAL;
24         o->t_tag = ODR_SEQUENCE;
25     }
26
27     if (odr_constructed_begin(o, p, o->t_class, o->t_tag, 0))
28     {
29         if (o->direction == ODR_DECODE && size)
30             *pp = nalloc(o, size);
31         return 1;
32     }
33     else
34         return 0;
35 }
36
37 int odr_sequence_end(ODR o)
38 {
39     return odr_constructed_end(o);    
40 }
41
42 int odr_sequence_more(ODR o)
43 {
44     if (o->stackp < 0)
45         return 0;
46     if (o->stack[o->stackp].len >= 0)
47         return o->bp - o->stack[o->stackp].base < o->stack[o->stackp].len;
48     else
49         return (!(*o->bp == 0 && *(o->bp + 1) == 0));
50 }
51
52 int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num)
53 {
54     char **pp = (char**) p;  /* for dereferencing */
55     char *tmp;
56     int size = 0;
57
58     if (!odr_sequence_begin(o, p, 0))
59         return 0;
60
61     if (o->direction = ODR_DECODE)
62         *num = 0;
63     while (odr_sequence_more(o))
64     {
65         /* outgrown array? */
66         if (*num * sizeof(void*) >= size)
67         {
68             /* double the buffer size */
69             tmp = nalloc(o, sizeof(void*) * (size += size ? size :
70                 128));
71             if (*num)
72             {
73                 memcpy(tmp, *pp, *num * sizeof(void*));
74                 /*
75                  * For now, we just throw the old *p away, since we use
76                  * nibble memory anyway (disgusting, isn't it?).
77                  */
78                 *pp = tmp;
79             }
80         }
81         if (!(*type)(o, (*pp)[*num++], 0))
82             return 0;
83         *num++;
84     }
85     return odr_sequence_end(o);
86 }