diff options
Diffstat (limited to 'src/bvrvar.c')
-rw-r--r-- | src/bvrvar.c | 87 |
1 files changed, 56 insertions, 31 deletions
diff --git a/src/bvrvar.c b/src/bvrvar.c index 9e405ec..c5ead88 100644 --- a/src/bvrvar.c +++ b/src/bvrvar.c @@ -2,52 +2,77 @@ #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) { - 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]; + 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) { - 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) { + 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], BVR_UNDEFINED) == 0) { - strlcpy(bvr_variables[i], item, sizeof(bvr_variables[i])); - strlcpy(bvr_variables[i+1], value, sizeof(bvr_variables[i+1])); + 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, "[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); + fprintf(stderr, "undefined condition in bvr_var_set.\n"); 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])); + 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; } } |