From f80f005c602654abe137f152a5128c8abd81f9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Sun, 3 May 2020 21:00:05 +0200 Subject: variables setting and getting now works from bvr scripts note to future self: don't make this a turing complete language --- src/bvrvar.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/bvrvar.c (limited to 'src/bvrvar.c') diff --git a/src/bvrvar.c b/src/bvrvar.c new file mode 100644 index 0000000..97b54b7 --- /dev/null +++ b/src/bvrvar.c @@ -0,0 +1,56 @@ +#pragma once +#include +#include +#include +char * bvr_var_get(char * item) { + for(int i = 0; i < sizeof(bvr_variables)/sizeof(bvr_variables[0]); i=i+2) { + // printf("%s, %s, %d, %d\n", bvr_variables[i], item, sizeof(bvr_variables)/sizeof(bvr_variables[0]), i); + if(strcmp(bvr_variables[i], item) == 0) { + return bvr_variables[i+1]; + } + } + return BVR_UNDEFINED; +} + +int bvr_var_set(char * item, char * value) { + if(bvr_bvrvar_first_time_set == 1) { + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here1\n"); + strlcpy(bvr_variables[i], BVR_UNDEFINED, sizeof(bvr_variables[i])); + } + bvr_bvrvar_first_time_set = 0; + } + if(strlen(value) >= BVR_MAX_VARIABLE_SIZE) { // >=, ker je še \0, ki ga strlen ne prišteje! + value[BVR_MAX_VARIABLE_SIZE-1] = '\0'; + fprintf(stderr, "[bvrvar.c] bvr_set: value of variable %s too long, chopped to \"%s\"; increase BVR_MAX_VARIABLE_SIZE (%d). Returning FAILURE and setting anyways.\n", + item, value, BVR_MAX_VARIABLE_SIZE); + } + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here2\n"); + if(strcmp(bvr_variables[i], item) == 0) { + strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1])); + return SUCCESS; + } + } // could already search for BVR_UNDEFINED here, but idc + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + // printf("loop here4\n"); + if(strcmp(bvr_variables[i], BVR_UNDEFINED) == 0) { + strlcpy(bvr_variables[i], item, sizeof(bvr_variables[i])); + strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1])); + return SUCCESS; + } + } + fprintf(stderr, "[bvrvar.c] bvr_set: no more space on the variable stack for %s. Increase BVR_INITIAL_VARIABLES_COUNT (%d).", item, BVR_INITIAL_VARIABLES_COUNT); + return FAILURE; +} + +int bvr_var_mv(char * item, char * newname) { + for(int i = 0; i < (sizeof(bvr_variables)/sizeof(bvr_variables[0])); i=i+2) { + if(strcmp(bvr_variables[i], item) == 0) { + strlcpy(bvr_variables[i], newname, sizeof(bvr_variables[i])); + return SUCCESS; + } + } + fprintf(stderr, "[bvrvar.c] bvr_mv: variable %s not found!", item); + return FAILURE; +} -- cgit v1.2.3