NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / libasn1parser / asn1p_module.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <genhash.h>
4
5 #include "asn1parser.h"
6
7 /*
8  * Construct a new empty module.
9  */
10 asn1p_module_t *
11 asn1p_module_new() {
12         asn1p_module_t *mod;
13
14         mod = calloc(1, sizeof *mod);
15         if(mod) {
16                 TQ_INIT(&(mod->exports));
17                 TQ_INIT(&(mod->imports));
18                 TQ_INIT(&(mod->members));
19
20         mod->members_hash = genhash_new(cmpf_string, hashf_string, NULL, NULL);
21         assert(mod->members_hash);
22     }
23         return mod;
24 }
25
26 /*
27  * Destroy the module.
28  */
29 void
30 asn1p_module_free(asn1p_module_t *mod) {
31         if(mod) {
32                 asn1p_expr_t *expr;
33                 asn1p_xports_t *xports;
34
35                 free(mod->ModuleName);
36                 free(mod->source_file_name);
37
38                 asn1p_oid_free(mod->module_oid);
39
40                 while((xports = TQ_REMOVE(&(mod->exports), xp_next)))
41                         asn1p_xports_free(xports);
42
43                 while((xports = TQ_REMOVE(&(mod->imports), xp_next)))
44                         asn1p_xports_free(xports);
45
46                 while((expr = TQ_REMOVE(&(mod->members), next)))
47                         asn1p_expr_free(expr);
48
49         genhash_destroy(mod->members_hash);
50         mod->members_hash = NULL;
51
52                 free(mod);
53         }
54 }
55
56 asn1p_t *
57 asn1p_new() {
58         asn1p_t *asn;
59         asn = calloc(1, sizeof(*asn));
60         if(asn) {
61                 TQ_INIT(&(asn->modules));
62         }
63         return asn;
64 }
65
66
67 void
68 asn1p_delete(asn1p_t *asn) {
69         if(asn) {
70                 asn1p_module_t *mod;
71                 while((mod = TQ_REMOVE(&(asn->modules), mod_next)))
72                         asn1p_module_free(mod);
73                 free(asn);
74         }
75 }
76
77
78 void
79 asn1p_module_move_members(asn1p_module_t *to, asn1p_module_t *from) {
80     if(from) {
81         while(TQ_FIRST(&(from->members))) {
82             asn1p_expr_t *expr = TQ_REMOVE(&from->members, next);
83             TQ_ADD(&to->members, expr, next);
84             genhash_add(to->members_hash, expr->Identifier, expr);
85         }
86         assert(TQ_FIRST(&from->members) == 0);
87
88         genhash_empty(from->members_hash, 0, 0);
89     }
90 }
91
92 void
93 asn1p_module_member_add(asn1p_module_t *mod, asn1p_expr_t *expr) {
94     if(expr) {
95         TQ_ADD(&mod->members, expr, next);
96         genhash_add(mod->members_hash, expr->Identifier, expr);
97     }
98 }