You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.3 KiB
104 lines
2.3 KiB
5 years ago
|
#ifndef VARS_C
|
||
|
#define VARS_C
|
||
|
#include "vars.h"
|
||
|
|
||
|
/*
|
||
|
Author: xerox
|
||
|
Date: 1/20/2020
|
||
|
|
||
|
add var to hashmap of variables.
|
||
|
*/
|
||
|
static void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map)
|
||
|
{
|
||
|
if (!p_name || !p_values)
|
||
|
return;
|
||
|
map_set(p_var_map, p_name, p_values);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
Author: xerox
|
||
|
Date: 1/20/2020
|
||
|
|
||
|
get value given name and hashmap.
|
||
|
*/
|
||
|
static variable_values* get_value(char *p_name, map_void_t* p_var_map)
|
||
|
{
|
||
|
if (!p_name)
|
||
|
return NULL;
|
||
|
return *map_get(&*((map_void_t* ) p_var_map), p_name);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Author: xerox
|
||
|
Date: 1/20/2020
|
||
|
|
||
|
makes a variable struct and define it.
|
||
|
*/
|
||
|
static variable* make_variable(char *p_name, void* value, variable_type var_type)
|
||
|
{
|
||
|
variable *new_var = malloc(sizeof(variable));
|
||
|
variable_values *new_value = malloc(sizeof(variable_values));
|
||
|
if (!new_var || !new_value || !p_name || !value)
|
||
|
return NULL;
|
||
|
|
||
|
memset(new_var, NULL, sizeof(*new_var));
|
||
|
memset(new_value, NULL, sizeof(*new_value));
|
||
|
new_value->type = var_type;
|
||
|
new_value->data_ptr = value;
|
||
|
new_var->name = p_name;
|
||
|
new_var->value = new_value;
|
||
|
return new_var;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Author: xerox
|
||
|
Date: 1/20/2020
|
||
|
|
||
|
allocate memory for values that are 8 bytes or less.
|
||
|
*/
|
||
|
static void* alloc_value(void* value, variable_type var_type)
|
||
|
{
|
||
|
switch (var_type)
|
||
|
{
|
||
|
case VAR_INT:
|
||
|
{
|
||
|
int* p_int = (int*)malloc(sizeof(int));
|
||
|
if (!p_int)
|
||
|
return NULL;
|
||
|
memset(p_int, NULL, sizeof(*p_int));
|
||
|
*p_int = *(int*)value;
|
||
|
return p_int;
|
||
|
}
|
||
|
case VAR_SHORT:
|
||
|
{
|
||
|
short* p_short = (short*)malloc(sizeof(short));
|
||
|
if (!p_short)
|
||
|
return NULL;
|
||
|
memset(p_short, NULL, sizeof(*p_short));
|
||
|
*p_short = *(short*)value;
|
||
|
return p_short;
|
||
|
}
|
||
|
case VAR_BYTE:
|
||
|
{
|
||
|
char* p_char = malloc(sizeof(char));
|
||
|
if (!p_char)
|
||
|
return NULL;
|
||
|
memset(p_char, NULL, sizeof(*p_char));
|
||
|
*p_char = *(char*)value;
|
||
|
return p_char;
|
||
|
}
|
||
|
case VAR_DOUBLE:
|
||
|
{
|
||
|
double* p_double = malloc(sizeof(double));
|
||
|
if (!p_double)
|
||
|
return NULL;
|
||
|
memset(p_double, NULL, sizeof(*p_double));
|
||
|
*p_double = *(double*)value;
|
||
|
return p_double;
|
||
|
}
|
||
|
default:
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
#endif
|