summaryrefslogblamecommitdiffstats
path: root/src/bvrvar.c
blob: 6b80a6088290d6af85d836934f34d2d3ea3ba83d (plain) (tree)
1
2
3
4



                   












                                                                                          
                                 




                                                                                                
                 
                                           




                                            


                                                      
                                          























                                                                                                                                                                                                           


                                       
                                                                 



                                             
                             
                                                      






                                                                                              


                                       
                                                                             

                       
#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++) {
		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;
}