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
|
#pragma once
#include <bvr.h>
#include <string.h>
#include <stdlib.h>
#define BVR_VAR_FIRST_TIME() \
if(bvr_bvrvar_first_time_set == 1) { \
bvr_variables = malloc(sizeof(struct bvr_variable)*bvr_variables_count); \
for(int i = 0; i < bvr_variables_count; i++) { \
bvr_variables[i].v = malloc(sizeof(char)*128); \
strcpy(bvr_variables[i].v, BVR_UNDEFINED); \
bvr_variables[i].sv = 128; \
bvr_variables[i].k = malloc(sizeof(char)*128); \
strcpy(bvr_variables[i].k, BVR_UNDEFINED); \
bvr_variables[i].sk = 128; \
} \
bvr_bvrvar_first_time_set = 0; \
}
char * bvr_var_get(char * item) {
BVR_VAR_FIRST_TIME();
for(int i = 0; i < bvr_variables_count; i++) {
// printf("%s, %s, %d, %d\n", bvr_variables[i].v, item, bvr_variables_count, i);
if(strcmp(bvr_variables[i].k, item) == 0) {
return bvr_variables[i].v;
}
// fprintf(stderr, "wa\n");
}
return BVR_UNDEFINED;
}
int bvr_var_set(char * item, char * value) {
BVR_VAR_FIRST_TIME();
int freevar = -69420;
for(int i = 0; i < bvr_variables_count; i++) {
// printf("loop here4\n");
if (strcmp(bvr_variables[i].v, BVR_UNDEFINED) == 0) {
freevar = i;
}
if(strcmp(bvr_variables[i].k, item) == 0 || i+1 == bvr_variables_count) {
if (i+1 == bvr_variables_count && strcmp(bvr_variables[i].k, item) != 0) {
i = freevar;
if (i == -69420) {
fprintf(stderr, "[bvrvar.c] bvr_set: no more space on the variable stack for %s. Increase BVR_INITIAL_VARIABLES_COUNT (%d).\n", item, BVR_INITIAL_VARIABLES_COUNT);
return FAILURE;
}
}
if (bvr_variables[i].sk > strlen(item)) {
bvr_variables[i].sk = strlen(item)+128;
free(bvr_variables[i].k);
bvr_variables[i].k = malloc(sizeof(char)*bvr_variables[i].sk);
}
if (bvr_variables[i].sv > strlen(item)) {
bvr_variables[i].sv = strlen(value)+128;
free(bvr_variables[i].v);
bvr_variables[i].v = malloc(sizeof(char)*bvr_variables[i].sv);
}
strlcpy(bvr_variables[i].k, item, bvr_variables[i].sk);
strlcpy(bvr_variables[i].v, value, bvr_variables[i].sv);
// fprintf(stderr, "debug: %s\n", bvr_variables[i].v);
return SUCCESS;
}
}
fprintf(stderr, "undefined condition in bvr_var_set.\n");
return FAILURE;
}
int bvr_var_mv(char * item, char * newname) {
BVR_VAR_FIRST_TIME();
for(int i = 0; i < bvr_variables_count; i=i+2) {
if(strcmp(bvr_variables[i].k, item) == 0) {
if (bvr_variables[i].sk > strlen(newname)) {
bvr_variables[i].sk = strlen(newname)+128;
free(bvr_variables[i].k);
bvr_variables[i].k = malloc(sizeof(char)*bvr_variables[i].sk);
}
strlcpy(bvr_variables[i].k, newname, bvr_variables[i].sk);
return SUCCESS;
}
}
fprintf(stderr, "[bvrvar.c] bvr_mv: variable %s not found!\n", item);
return FAILURE;
}
|