YAZ  5.23.1
odr_seq.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
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 
62 {
63  return odr_constructed_end(o);
64 }
65 
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 
void odr_seterror(ODR o, int error, int id)
Definition: odr.c:118
int t_class
Definition: odr-priv.h:107
#define ODR_UNIVERSAL
Definition: odr.h:65
#define ODR_PRINT
Definition: odr.h:97
#define OOTHER
Definition: odr.h:156
#define ODR_ENCODE
Definition: odr.h:96
int odr_set_begin(ODR o, void *p, int size, const char *name)
Definition: odr_seq.c:38
int odr_sequence_begin(ODR o, void *p, int size, const char *name)
Definition: odr_seq.c:15
int error
Definition: odr.h:128
char * name
Definition: initopt.c:18
static int odr_sequence_x(ODR o, Odr_fun type, void *p, int *num)
Definition: odr_seq.c:76
int odr_constructed_begin(ODR o, void *xxp, int zclass, int tag, const char *name)
Definition: odr_cons.c:24
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
Internal ODR definitions.
int direction
Definition: odr.h:126
int odr_constructed_more(ODR o)
Definition: odr_cons.c:136
#define ODR_SET
Definition: odr.h:84
Odr_null * odr_nullval(void)
Definition: odr.c:30
enum l_file_type type
Definition: log.c:45
int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num, const char *name)
Definition: odr_seq.c:134
int(* Odr_fun)(ODR, char **, int, const char *)
Definition: odr.h:135
int odr_set_end(ODR o)
Definition: odr_seq.c:66
struct Odr_private * op
Definition: odr.h:132
Definition: odr.h:124
int odr_sequence_end(ODR o)
Definition: odr_seq.c:61
int odr_set_of(ODR o, Odr_fun type, void *p, int *num, const char *name)
Definition: odr_seq.c:124
static int odr_sequence_more(ODR o)
Definition: odr_seq.c:71
#define ODR_SEQUENCE
Definition: odr.h:83
int odr_constructed_end(ODR o)
Definition: odr_cons.c:148
#define ODR_DECODE
Definition: odr.h:95