-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvar_table.c
158 lines (150 loc) · 4.14 KB
/
var_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "var_table.h"
static const unsigned INIT_HASH_SIZE_SCALE = 10;
HashMap *variable_table;
Variable *EMPTY_VAR;
void init_var_table()
{
if(!EMPTY_VAR) EMPTY_VAR = (Variable*)malloc(sizeof(Variable));
EMPTY_VAR->elec = 0;
EMPTY_VAR->elev = NULL;
if(!variable_table) variable_table = create_hash_map(INIT_HASH_SIZE_SCALE, BKDR_hash);
if(!variable_table) exit(VAR_HASH_TABLE_ERR_);
return;
}
Variable *destruct_var(Variable *var)
{
if(!var) return NULL;
if(var->elev)
{
int i;
for(i = 0; i < var->elec; i++)
{
if(var->elev[i]) free(var->elev[i]);
var->elev[i] = NULL;
}
free(var->elev);
}
return NULL;
}
void update_variable(const char *key_, Variable *var_)
{
const HashEntry *found;
// Empty key
if(!key_ || !*key_) return;
// Find key
found = forced_find_entry(variable_table, key_);
// If key not found
if(!found)
{
insert_entry(variable_table, key_, var_, sizeof(Variable));
}
// Else the key is found
else
{
Variable *found_variable = (Variable*)found->value;
// If the entry is empty
if(!found_variable)
{
insert_entry(variable_table, key_, var_, sizeof(Variable));
}
// If the entry is not empty
else
{
// If the entry
if(found_variable != EMPTY_VAR)
found_variable = destruct_var(found_variable);
insert_entry(variable_table, key_, var_, sizeof(Variable));
}
}
}
void delete_variable(const char *key_)
{
update_variable(key_, EMPTY_VAR);
}
char *get_variable(const char *key_, int index)
{
const HashEntry *found;
char buffer[MAX_PROMPT_LEN] = {};
if(!key_ || index < MIN_QUERY_OP)
{
if(index == ListAll)
{
char *result = (char*)malloc(2*sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
result[0] = '0';
result[1] = 0;
return result;
}
else
{
char *result = (char*)malloc(sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
result[0] = 0;
return result;
}
}
found = find_entry(variable_table, key_);
if(!found)
{
if(index == ElementCount)
{
char *result = (char*)malloc(2*sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
result[0] = '0';
result[1] = 0;
return result;
}
else
{
char *result = (char*)malloc(sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
result[0] = 0;
return result;
}
}
else
{
Variable *found_variable = (Variable*)found->value;
if(index >= found_variable->elec || index < MIN_QUERY_OP)
{
char *result = (char*)malloc(sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
result[0] = 0;
return result;
}
else if(index == ListAll)
{
int i;
char *result;
for(i = 0; i < found_variable->elec; i++)
{
strcat(buffer, found_variable->elev[i]);
if(i < found_variable->elec - 1)
strcat(buffer, " ");
}
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
return result;
}
else if(index == ElementCount)
{
char *result;
sprintf(buffer, "%d", found_variable->elec);
result = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, buffer);
return result;
}
else
{
char *result;
result
= (char*)malloc((strlen(found_variable->elev[index]) + 1)
* sizeof(char));
if(!result) exit(MEM_ALLOC_ERR_);
strcpy(result, found_variable->elev[0]);
return result;
}
}
}