From a01c33c6b38dd764a776294267fa0f5fc9fb0d3e Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 25 Apr 2014 21:15:17 +0800 Subject: [PATCH] Added variable_set() --- include/System/variable.h | 1 + src/variable.c | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/System/variable.h b/include/System/variable.h index 655c9ed..254b3b1 100644 --- a/include/System/variable.h +++ b/include/System/variable.h @@ -61,6 +61,7 @@ void variable_delete(Variable * variable); int variable_get_as(Variable * variable, VariableType type, void * result); VariableType variable_get_type(Variable * variable); +int variable_set(Variable * variable, Variable * from); int variable_set_from(Variable * variable, VariableType type, void * value); /* useful */ diff --git a/src/variable.c b/src/variable.c index 825beb8..f1f8267 100644 --- a/src/variable.c +++ b/src/variable.c @@ -523,6 +523,50 @@ VariableType variable_get_type(Variable * variable) } +/* variable_set */ +int variable_set(Variable * variable, Variable * from) +{ + Buffer * b; + String * s; + + switch(from->type) + { + case VT_NULL: + case VT_BOOL: + case VT_INT8: + case VT_UINT8: + case VT_INT16: + case VT_UINT16: + case VT_INT32: + case VT_UINT32: + case VT_INT64: + case VT_UINT64: + case VT_FLOAT: + case VT_DOUBLE: + _variable_destroy(variable); + memcpy(&variable->u, &from->u, sizeof(from->u)); + break; + case VT_BUFFER: + if((b = buffer_new_copy(from->u.buffer)) == NULL) + return -1; + _variable_destroy(variable); + variable->u.buffer = b; + break; + case VT_STRING: + if((s = string_new(from->u.string)) == NULL) + return -1; + _variable_destroy(variable); + variable->u.string = s; + break; + default: + /* FIXME implement */ + return -error_set_code(-ENOSYS, "%s", strerror(ENOSYS)); + } + variable->type = from->type; + return 0; +} + + /* variable_set_from */ int variable_set_from(Variable * variable, VariableType type, void * value) {