-
Notifications
You must be signed in to change notification settings - Fork 0
/
var.c
212 lines (166 loc) · 3.37 KB
/
var.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tl2/list.h"
#include "var.h"
#include "scope.h"
static list *var_list;
extern list *var_scope;
extern int current_scope_tag;
//extern char *current_scope;
void vl_var_list_init() {
var_list = list_init_node(NULL);
}
void vl_var_add(char *name, char *value, int tag) {
list *last = NULL;
var *new_var = NULL;
// init
new_var = (var*)malloc(sizeof(var));
if (!new_var) {
perror("forp");
exit(0);
}
new_var->name = (char*)malloc(strlen(name) + 1);
new_var->value = (char*)malloc(strlen(value) + 1);
new_var->tag = tag;
// copy to fields
strcpy(new_var->name, name);
strcpy(new_var->value, value);
// add var to list
list_add_node(var_list);
last = list_get_last(var_list);
list_set_data(last, new_var);
}
var *vl_var_get(char *name, int tag) {
list *node = NULL;
var *vptr = NULL;
node = var_list;
while (node) {
if (!node->data) {
node = node->next;
continue;
}
vptr = (var*)node->data;
if (vptr)
if (vptr->tag == tag)
if (!strcmp(vptr->name, name))
return vptr;
node = node->next;
}
return NULL;
}
char *vl_var_get_value(char *name, int tag) {
var *vptr = NULL;
vptr = vl_var_get(name, tag);
if (vptr)
return vptr->value;
return NULL;
}
void vl_var_list_free() {
list *node = NULL, *next = NULL;
var *vptr = NULL;
node = var_list;
while (node) {
next = node->next;
if (!node->data) {
node = node->next;
continue;
}
// free var
vptr = (var*)node->data;
if (vptr) {
if (vptr->name)
free(vptr->name);
if (vptr->value)
free(vptr->value);
free(vptr);
}
// free node
free(node);
node = next;
}
}
void vl_var_list_show() {
list *node = NULL;
var *vptr = NULL;
node = var_list;
while (node) {
if (node->data) {
vptr = (var*)node->data;
if (vptr)
printf("[%d] %s = %s\n", vptr->tag, vptr->name, vptr->value);
}
node = node->next;
}
}
int vl_var_get_exist(char *name, int tag) {
if (!vl_var_get(name, tag))
return 0;
return 1;
}
void vl_var_change_value(char *name, char *value, int tag) {
var *vptr = NULL;
vptr = vl_var_get(name, tag);
if (vptr) {
vptr->value = (char*)realloc(vptr->value, strlen(value) + 1);
if (!vptr->value) {
perror("forp");
exit(0);
}
strcpy(vptr->value, value);
}
}
var *vl_var_get_exist_with_syntax(char *name) {
char *scope_name, *variable_name;
char *cpy;
int tag;
cpy = (char*)malloc(strlen(name) + 1);
if (!cpy) {
perror("forp");
exit(0);
}
strcpy(cpy, name);
if (strchr(cpy, ':')) {
scope_name = strtok(cpy, ":");
variable_name = strtok(NULL, ":");
}
else {
tag = current_scope_tag;
variable_name = cpy;
}
if (!variable_name || !scope_name)
return NULL;
return vl_var_get(variable_name, tag);
}
void vl_var_free(list *node, var *vptr) {
// remove node fomr list
if (node->prev)
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
// free var
if (vptr->name)
free(vptr->name);
if (vptr->value)
free(vptr->value);
free(vptr);
// free node
free(node);
}
void vl_var_remove(char *name, int tag) {
list *node = NULL, *next = NULL;
var *vptr = NULL;
node = var_list;
while (node) {
next = node->next;
vptr = (var*)node->data;
if (!vptr) {
node = node->next;
continue;
}
if (vptr->tag == tag)
if (!strcmp(vptr->name, name))
vl_var_free(node, vptr);
node = next;
}
}