-
Notifications
You must be signed in to change notification settings - Fork 0
/
xnr_var.c
134 lines (128 loc) · 3.47 KB
/
xnr_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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "xnr_var.h"
int xnr_var_init(xnr_var_tab_t *tab, xnr_var_t *var_buf, int num)
{
if (NULL == tab || NULL == var_buf || 0 == num)
return -1;
tab->capacity = num;
tab->var_buf = var_buf;
memset(var_buf, 0, sizeof(xnr_var_t) * num);
return 0;
}
int xnr_var_set(xnr_var_tab_t *tab, char *name, char *value)
{
if (NULL == tab ||
NULL == name ||
NULL == value ||
strlen(name) == 0 || strlen(name) >= XNR_VAR_NAME_LEN ||
strlen(value) == 0 || strlen(value) >= XNR_VAR_VAR_LEN)
return -1;
xnr_var_t *var = NULL; // 保存需要设置的槽位
xnr_var_t *var_empty = NULL; // 保存空闲槽位
for (int i = 0; i < tab->capacity; i++) // 查找name是否存在
{
if ((NULL == var_empty) && (tab->var_buf[i].name[0] == '\0'))
{
var_empty = &tab->var_buf[i];
}
else
{
if (strcmp(tab->var_buf[i].name, name) == 0)
{
var = &tab->var_buf[i];
}
}
}
if (NULL == var) // 获取一个空闲的var槽位
var = var_empty;
if (NULL == var) // 没有空余槽位
return -1;
if (strcmp(value, "nil") == 0) // 删除变量
{
var->name[0] = '\0';
return 0;
}
else
{
strncpy(var->value, value, XNR_VAR_VAR_LEN);
strncpy(var->name, name, XNR_VAR_NAME_LEN);
var->value[XNR_VAR_VAR_LEN - 1] = '\0';
var->name[XNR_VAR_NAME_LEN - 1] = '\0';
}
return 0;
}
char *xnr_var_value(xnr_var_tab_t *tab, char *name)
{
if (NULL == tab || NULL == name)
return "nil";
xnr_var_t *var = NULL;
for (int i = 0; i < tab->capacity; i++) // 查找name是否存在
{
if (strcmp(tab->var_buf[i].name, name) == 0)
{
var = &tab->var_buf[i];
}
}
if (NULL == var)
return "nil";
else
return var->value;
}
int xnr_var_repalce(xnr_var_tab_t *tab, char *in, char *out, int out_len)
{
if (NULL == tab || NULL == in || out == NULL || 0 == out_len)
return -1;
int len = strlen(in);
char name[XNR_VAR_NAME_LEN] = {0};
int out_i = 0;
for (int i = 0; (i < len) && (out_i < (out_len - 1)); i++)
{
if (in[i] == '$')
{
int k = 0;
char end = ' ';
int j = i + 1;
if (in[j] == '{')
{
end = '}';
j++;
}
while (isprint(in[j]) && // 可显示字符
in[j] != end // 变量结尾
)
{
if (k < (XNR_VAR_NAME_LEN - 1))
{
name[k++] = in[j++];
}
else
{
return -1; // name长度超限
}
}
name[k] = '\0';
char *value = xnr_var_value(tab, name); // 获取变量的值
if (value != NULL)
{
int val_len = strlen(value);
for (k = 0; (k < val_len) && (out_i < (out_len - 1)); k++)
{
out[out_i++] = value[k];
}
}
if (end == '}')
i = j;
else
i = j - 1;
}
else
{
out[out_i++] = in[i];
}
}
out[out_i] = '\0';
return out_i;
}