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
|
#include <config.h>
#include <c.h>
#include "variables.h"
#include <stdlib.h>
#include <assert.h>
VariableSpace CreateVariableSpace(void)
{
struct _variable *ptr;
ptr = calloc(1, sizeof *ptr);
if (!ptr) return NULL;
ptr->name = strdup("@");
ptr->value = strdup("");
if (!ptr->name || !ptr->value) {
free(ptr->name);
free(ptr->value);
free(ptr);
return NULL;
}
return ptr;
}
const char * GetVariable(VariableSpace space, const char * name)
{
struct _variable *current;
if (!space)
return NULL;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return NULL;
for (current = space; current; current = current->next) {
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0)
return current->value;
}
return NULL;
}
bool GetVariableBool(VariableSpace space, const char * name)
{
return GetVariable(space, name)!=NULL ? true : false;
}
bool SetVariable(VariableSpace space, const char * name, const char * value)
{
struct _variable *current, *previous;
if (!space)
return false;
if (!value)
return DeleteVariable(space, name);
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
for (current = space; current; previous = current, current = current->next) {
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0) {
free (current->value);
current->value = strdup(value);
return current->value ? true : false;
}
}
previous->next = calloc(1, sizeof *(previous->next));
if (!previous->next)
return false;
previous->next->name = strdup(name);
if (!previous->next->name)
return false;
previous->next->value = strdup(value);
return previous->next->value ? true : false;
}
bool DeleteVariable(VariableSpace space, const char * name)
{
struct _variable *current, *previous;
if (!space)
return false;
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
for (current = space, previous = NULL; current; previous = current, current = current->next) {
#ifdef USE_ASSERT_CHECKING
assert(current->name);
assert(current->value);
#endif
if (strcmp(current->name, name)==0) {
free (current->name);
free (current->value);
if (previous)
previous->next = current->next;
free(current);
return true;
}
}
return true;
}
void DestroyVariableSpace(VariableSpace space)
{
if (!space)
return;
DestroyVariableSpace(space->next);
free(space);
}
|