blob: 23ab4cb365bde43916dcd0146175343c8c20356e [file] [log] [blame]
Lev Walkinf15320b2004-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);
60 CLCLONE(params, asn1p_paramlist_clone);
61 CLCLONE(value, asn1p_value_clone);
62 CLCLONE(with_syntax, asn1p_wsyntx_clone);
63
64 /*
65 * Copy all the children of this expr.
66 */
67 TQ_FOR(tcmemb, &(expr->members), next) {
68 asn1p_expr_t *cmemb = asn1p_expr_clone(tcmemb);
69 if(cmemb == NULL) {
70 asn1p_expr_free(clone);
71 return NULL;
72 }
73 TQ_ADD(&(clone->members), cmemb, next);
74 }
75
76 return clone;
77}
78
79/*
80 * Destruct the types collection structure.
81 */
82void
83asn1p_expr_free(asn1p_expr_t *expr) {
84 if(expr) {
85 asn1p_expr_t *tm;
86
87 if(expr->Identifier)
88 free(expr->Identifier);
89 if(expr->reference)
90 asn1p_ref_free(expr->reference);
91 if(expr->constraints)
92 asn1p_constraint_free(expr->constraints);
93 if(expr->params)
94 asn1p_paramlist_free(expr->params);
95 if(expr->value)
96 asn1p_value_free(expr->value);
97 if(expr->with_syntax)
98 asn1p_wsyntx_free(expr->with_syntax);
99
100 /* Remove all children */
101 while((tm = TQ_REMOVE(&(expr->members), next))) {
102 asn1p_expr_free(tm);
103 }
104
105 if(expr->data && expr->data_free)
106 expr->data_free(expr->data);
107
108 memset(expr, 0, sizeof(*expr));
109 free(expr);
110 }
111}
112