-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.c
107 lines (79 loc) · 2.31 KB
/
vars.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
#include "vars.h"
TVarList *enhancer_varlist=NULL;
TVarList *enhancer_varlist_create()
{
TVarList *varl;
varl=(TVarList *) calloc(1, sizeof(TVarList));
varl->max=0;
return(varl);
}
void enhancer_setvarlist(TVarList *list, const char *name, const char *value)
{
char *ptr, *end;
TVar *Var;
int i;
if (name==NULL) return;
if (value==NULL) return;
for (i=0; i < list->max; i++)
{
Var=&(list->vars[i]);
if (Var && (strcmp(Var->name,name) ==0))
{
Var->value=enhancer_strcpy(Var->value, value);
return;
}
}
/*if we get to here, then we didn't find a variable in the list */
list->vars=realloc(list->vars, (list->max+10) * sizeof(TVar));
Var=&(list->vars[list->max]);
//these variables won't be allocated, nor set to NULL, so use enhancer_strcpy
//in light of that
Var->name=enhancer_strcpy(NULL, name);
Var->value=enhancer_strcpy(NULL, value);
list->max++;
}
void enhancer_setvar(const char *name, const char *value)
{
if (! enhancer_varlist) enhancer_varlist=enhancer_varlist_create();
enhancer_setvarlist(enhancer_varlist, name, value);
}
void enhancer_func_setvar(const char *Arg, const char *FuncName, const char *Str1, const char *Str2)
{
char *Name=NULL, *Value=NULL, *Tempstr=NULL;
const char *ptr;
ptr=enhancer_strtok(Arg, "=", &Tempstr);
Name=enhancer_format_str(Name, Tempstr, FuncName, Str1, Str2);
ptr=enhancer_strtok(ptr, "=", &Tempstr);
Value=enhancer_format_str(Value, Tempstr, FuncName, Str1, Str2);
enhancer_setvar(Name, Value);
destroy(Name);
destroy(Value);
destroy(Tempstr);
}
const char *enhancer_getvarlist(TVarList *list, const char *name)
{
TVar *Var;
int i;
for (i=0; i < list->max; i++)
{
Var=&(list->vars[i]);
if (Var && (strcmp(Var->name,name)==0) ) return(Var->value);
}
return("");
}
const char *enhancer_varlist_find_value(TVarList *list, const char *value)
{
TVar *Var;
int i;
for (i=0; i < list->max; i++)
{
Var=&(list->vars[i]);
if (Var && (strcmp(Var->value,value)==0) ) return(Var->name);
}
return("");
}
const char *enhancer_getvar(const char *name)
{
if (! enhancer_varlist) return("");
return(enhancer_getvarlist(enhancer_varlist, name));
}