diff options
author | sijanec <anton@sijanec.eu> | 2021-01-21 08:19:19 +0100 |
---|---|---|
committer | sijanec <anton@sijanec.eu> | 2021-01-21 08:19:19 +0100 |
commit | 8f877a7a8a2bb81caeed24256baea4b54032b750 (patch) | |
tree | b8c08e77ff3d5cd14d9438a5b408c8e9c943aca8 /src/bvrvar.c | |
parent | tweaks (diff) | |
download | bverbose-master.tar bverbose-master.tar.gz bverbose-master.tar.bz2 bverbose-master.tar.lz bverbose-master.tar.xz bverbose-master.tar.zst bverbose-master.zip |
Diffstat (limited to 'src/bvrvar.c')
-rw-r--r-- | src/bvrvar.c | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/src/bvrvar.c b/src/bvrvar.c index 584611f..9ba2021 100644 --- a/src/bvrvar.c +++ b/src/bvrvar.c @@ -2,19 +2,34 @@ #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_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_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++) { @@ -32,23 +47,27 @@ int bvr_var_set(char * item, char * value) { int freevar = -69420; for(int i = 0; i < bvr_variables_count; i++) { // printf("loop here4\n"); - if (freevar != -69420 && strcmp(bvr_variables[i].v, BVR_UNDEFINED) == 0) { + 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 (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 (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)) { + 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(item)) { + 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); |