Rename internal func yaz_use_attribute_create
[yaz-moved-to-github.git] / src / odr_seq.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file odr_seq.c
7  * \brief Implements ODR SEQUENCE codec
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include "odr-priv.h"
14
15 int odr_sequence_begin(ODR o, void *p, int size, const char *name)
16 {
17     char **pp = (char**) p;
18
19     if (o->error)
20         return 0;
21     if (o->op->t_class < 0)
22     {
23         o->op->t_class = ODR_UNIVERSAL;
24         o->op->t_tag = ODR_SEQUENCE;
25     }
26     if (o->direction == ODR_DECODE)
27         *pp = 0;
28     if (odr_constructed_begin(o, p, o->op->t_class, o->op->t_tag, name))
29     {
30         if (o->direction == ODR_DECODE && size)
31             *pp = (char *)odr_malloc(o, size);
32         return 1;
33     }
34     else
35         return 0;
36 }
37
38 int odr_set_begin(ODR o, void *p, int size, const char *name)
39 {
40     char **pp = (char**) p;
41
42     if (o->error)
43         return 0;
44     if (o->op->t_class < 0)
45     {
46         o->op->t_class = ODR_UNIVERSAL;
47         o->op->t_tag = ODR_SET;
48     }
49     if (o->direction == ODR_DECODE)
50         *pp = 0;
51     if (odr_constructed_begin(o, p, o->op->t_class, o->op->t_tag, name))
52     {
53         if (o->direction == ODR_DECODE && size)
54             *pp = (char *)odr_malloc(o, size);
55         return 1;
56     }
57     else
58         return 0;
59 }
60
61 int odr_sequence_end(ODR o)
62 {
63     return odr_constructed_end(o);    
64 }
65
66 int odr_set_end(ODR o)
67 {
68     return odr_constructed_end(o);    
69 }
70
71 static int odr_sequence_more(ODR o)
72 {
73     return odr_constructed_more(o);
74 }
75
76 static int odr_sequence_x (ODR o, Odr_fun type, void *p, int *num)
77 {
78     char ***pp = (char***) p;  /* for dereferencing */
79     char **tmp = 0;
80     int size = 0, i;
81
82     switch (o->direction)
83     {
84         case ODR_DECODE:
85             *num = 0;
86             *pp = (char **)odr_nullval();
87             while (odr_sequence_more(o))
88             {
89                 /* outgrown array? */
90                 if (*num * (int) sizeof(void*) >= size)
91                 {
92                     /* double the buffer size */
93                     tmp = (char **)odr_malloc(o, sizeof(void*) *
94                                               (size += size ? size : 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, 0))
106                     return 0;
107                 (*num)++;
108             }
109             break;
110         case ODR_ENCODE: case ODR_PRINT:
111             for (i = 0; i < *num; i++)
112             {
113                 if (!(*type)(o, *pp + i, 0, 0))
114                     return 0;
115             }
116             break;
117         default:
118             odr_seterror(o, OOTHER, 47);
119             return 0;
120     }
121     return odr_sequence_end(o);
122 }
123
124 int odr_set_of(ODR o, Odr_fun type, void *p, int *num, const char *name)
125 {
126     if (!odr_set_begin(o, p, 0, name)) {
127         if (o->direction == ODR_DECODE)
128             *num = 0;
129         return 0;
130     }
131     return odr_sequence_x (o, type, p, num);
132 }
133
134 int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
135                     const char *name)
136 {
137     if (!odr_sequence_begin(o, p, 0, name)) {
138         if (o->direction == ODR_DECODE)
139             *num = 0;
140         return 0;
141     }
142     return odr_sequence_x (o, type, p, num);
143 }
144
145 /*
146  * Local variables:
147  * c-basic-offset: 4
148  * c-file-style: "Stroustrup"
149  * indent-tabs-mode: nil
150  * End:
151  * vim: shiftwidth=4 tabstop=8 expandtab
152  */
153