#pragma once
#include <bvr.h>
#include <string.h>
#include <stdlib.h>
#define BVR_VAR_MALLOC_VAR(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;
#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_VAR_MALLOC_VAR(i) \
} \
bvr_bvrvar_first_time_set = 0; \
}
char * bvr_var_remove_orphans() {
BVR_VAR_FIRST_TIME();
for (int i = 0; i < bvr_variables_count; i++) {
if (strncmp("BVR_STRING_", bvr_variables[i].k, strlen("BVR_STRING_")) == 0) {
for (int j = 0; j < bvr_variables_count; j++) {
if (strcmp(bvr_variables[i].k, bvr_variables[j].v) == 0 && strcmp(bvr_variables[j].k, BVR_UNDEFINED) != 0)
break;
}
strcpy(bvr_variables[i].k, BVR_UNDEFINED);
}
}
return SUCCESS;
}
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 (freevar == -69420 && strcmp(bvr_variables[i].k, 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 (freevar == -69420) {
freevar = i = bvr_variables_count;
bvr_variables_count += BVR_INITIAL_VARIABLES_COUNT;
bvr_variables = realloc(bvr_variables, sizeof(struct bvr_variable)*bvr_variables_count);
for (int j = freevar; j < bvr_variables_count; j++) {
BVR_VAR_MALLOC_VAR(j);
}
}
}
if (bvr_variables[i].sk < strlen(item)+64) {
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(value)+64) {
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++) {
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;
}