blob: 974668d61cb081e89ed581f9bce4fa7512381fe7 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include <stdlib.h>
2#include <string.h>
Lev Walkin1a49ced2017-11-06 00:07:00 -08003#include <genhash.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00004
5#include "asn1parser.h"
6
7/*
8 * Construct a new empty module.
9 */
10asn1p_module_t *
11asn1p_module_new() {
12 asn1p_module_t *mod;
13
14 mod = calloc(1, sizeof *mod);
15 if(mod) {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 TQ_INIT(&(mod->exports));
Lev Walkina9532f42006-09-17 04:52:50 +000017 TQ_INIT(&(mod->imports));
Lev Walkinf15320b2004-06-03 03:38:44 +000018 TQ_INIT(&(mod->members));
Lev Walkin1a49ced2017-11-06 00:07:00 -080019
20 mod->members_hash = genhash_new(cmpf_string, hashf_string, NULL, NULL);
21 assert(mod->members_hash);
22 }
Lev Walkinf15320b2004-06-03 03:38:44 +000023 return mod;
24}
25
26/*
27 * Destroy the module.
28 */
29void
30asn1p_module_free(asn1p_module_t *mod) {
31 if(mod) {
32 asn1p_expr_t *expr;
Bi-Ruei, Chiu3dcf05b2017-05-04 21:45:05 +080033 asn1p_xports_t *xports;
Lev Walkinf15320b2004-06-03 03:38:44 +000034
Lev Walkind8b83642016-03-14 02:00:27 -070035 free(mod->ModuleName);
Bi-Ruei, Chiu12021b52016-11-04 11:59:45 +080036 free(mod->source_file_name);
Lev Walkinf15320b2004-06-03 03:38:44 +000037
Lev Walkin13e57ef2016-03-14 02:05:23 -070038 asn1p_oid_free(mod->module_oid);
Lev Walkinf15320b2004-06-03 03:38:44 +000039
Bi-Ruei, Chiu3dcf05b2017-05-04 21:45:05 +080040 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
Lev Walkinf15320b2004-06-03 03:38:44 +000046 while((expr = TQ_REMOVE(&(mod->members), next)))
47 asn1p_expr_free(expr);
48
Lev Walkin1a49ced2017-11-06 00:07:00 -080049 genhash_destroy(mod->members_hash);
50 mod->members_hash = NULL;
51
Lev Walkinf15320b2004-06-03 03:38:44 +000052 free(mod);
53 }
54}
55
56asn1p_t *
57asn1p_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
67void
Lev Walkin9b5df842006-09-12 04:21:30 +000068asn1p_delete(asn1p_t *asn) {
Lev Walkinf15320b2004-06-03 03:38:44 +000069 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}
Lev Walkin1a49ced2017-11-06 00:07:00 -080076
77
78void
79asn1p_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
92void
93asn1p_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}