blob: be9da7734f80e791b5ab9cba016aee7c402cfd6e [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
5#include <assert.h>
6
7#include "asn1parser.h"
8
9/*
10 * Construct a new empty types collection.
11 */
12asn1p_expr_t *
13asn1p_expr_new(int _lineno) {
14 asn1p_expr_t *expr;
15
16 expr = calloc(1, sizeof *expr);
17 if(expr) {
18 TQ_INIT(&(expr->members));
19 expr->_lineno = _lineno;
20 }
21
22 return expr;
23}
24
25asn1p_expr_t *
26asn1p_expr_clone(asn1p_expr_t *expr) {
27 asn1p_expr_t *clone;
28 asn1p_expr_t *tcmemb; /* Child of tc */
29
30 clone = asn1p_expr_new(expr->_lineno);
31 if(clone == NULL) return NULL;
32
33#define CLCOPY(field) do { clone->field = expr->field; } while(0)
34#define CLCLONE(field, func) do { if(expr->field) { \
35 clone->field = func(expr->field); \
36 if(clone->field == NULL) { \
37 asn1p_expr_free(clone); \
38 return NULL; \
39 } \
40 } } while(0)
41
42 /*
43 * Copy simple fields.
44 */
45 CLCOPY(meta_type);
46 CLCOPY(expr_type);
47 CLCOPY(tag);
48 CLCOPY(marker);
49 CLCOPY(_mark);
50
51 clone->data = 0; /* Do not clone this */
52 clone->data_free = 0; /* Do not clone this */
53
54 /*
55 * Clone complex fields.
56 */
57 CLCLONE(Identifier, strdup);
58 CLCLONE(reference, asn1p_ref_clone);
59 CLCLONE(constraints, asn1p_constraint_clone);
vlm9283dbe2004-08-18 04:59:12 +000060 CLCLONE(combined_constraints, asn1p_constraint_clone);
vlmfa67ddc2004-06-03 03:38:44 +000061 CLCLONE(params, asn1p_paramlist_clone);
62 CLCLONE(value, asn1p_value_clone);
63 CLCLONE(with_syntax, asn1p_wsyntx_clone);
64
65 /*
66 * Copy all the children of this expr.
67 */
68 TQ_FOR(tcmemb, &(expr->members), next) {
69 asn1p_expr_t *cmemb = asn1p_expr_clone(tcmemb);
70 if(cmemb == NULL) {
71 asn1p_expr_free(clone);
72 return NULL;
73 }
74 TQ_ADD(&(clone->members), cmemb, next);
75 }
76
77 return clone;
78}
79
80/*
81 * Destruct the types collection structure.
82 */
83void
84asn1p_expr_free(asn1p_expr_t *expr) {
85 if(expr) {
86 asn1p_expr_t *tm;
87
88 if(expr->Identifier)
89 free(expr->Identifier);
90 if(expr->reference)
91 asn1p_ref_free(expr->reference);
92 if(expr->constraints)
93 asn1p_constraint_free(expr->constraints);
vlm9283dbe2004-08-18 04:59:12 +000094 if(expr->combined_constraints)
95 asn1p_constraint_free(expr->combined_constraints);
vlmfa67ddc2004-06-03 03:38:44 +000096 if(expr->params)
97 asn1p_paramlist_free(expr->params);
98 if(expr->value)
99 asn1p_value_free(expr->value);
100 if(expr->with_syntax)
101 asn1p_wsyntx_free(expr->with_syntax);
102
103 /* Remove all children */
104 while((tm = TQ_REMOVE(&(expr->members), next))) {
105 asn1p_expr_free(tm);
106 }
107
108 if(expr->data && expr->data_free)
109 expr->data_free(expr->data);
110
111 memset(expr, 0, sizeof(*expr));
112 free(expr);
113 }
114}
115