From 0b6bdcce6cea21909553a334629a5b3d004bd553 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 11 Feb 2011 23:37:12 +0000 Subject: Add support for boolean values. Replace slv2_world_filter_language with extensible option system (slv2_world_set_option). git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@2923 a436a847-0d15-0410-975c-d299462d15a1 --- src/value.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'src/value.c') diff --git a/src/value.c b/src/value.c index e08b7fd..dc75e07 100644 --- a/src/value.c +++ b/src/value.c @@ -57,13 +57,16 @@ slv2_value_set_numerics_from_string(SLV2Value val) setlocale(LC_NUMERIC, locale); free(locale); break; + case SLV2_VALUE_BOOL: + val->val.bool_val = (!strcmp(val->str_val, "true")); + break; } } -/** Note that if @a type is numeric, slv2_value_set_numerics_from_string MUST be - * called or the returned value will be corrupt! It is not automatically - * called from here to avoid the parsing overhead and imprecision when the - * true numeric value is already known. +/** Note that if @a type is numeric or boolean, the returned value is corrupt + * until slv2_value_set_numerics_from_string is called. It is not + * automatically called from here to avoid overhead and imprecision when the + * exact string value is known. */ SLV2Value slv2_value_new(SLV2World world, SLV2ValueType type, const char* str) @@ -82,6 +85,7 @@ slv2_value_new(SLV2World world, SLV2ValueType type, const char* str) case SLV2_VALUE_STRING: case SLV2_VALUE_INT: case SLV2_VALUE_FLOAT: + case SLV2_VALUE_BOOL: val->str_val = strdup(str); break; } @@ -108,10 +112,12 @@ slv2_value_new_from_node(SLV2World world, SordNode node) case SORD_LITERAL: datatype_uri = sord_literal_get_datatype(node); if (datatype_uri) { - if (sord_node_equals(datatype_uri, world->xsd_integer_node)) - type = SLV2_VALUE_INT; + if (sord_node_equals(datatype_uri, world->xsd_boolean_node)) + type = SLV2_VALUE_BOOL; else if (sord_node_equals(datatype_uri, world->xsd_decimal_node)) type = SLV2_VALUE_FLOAT; + else if (sord_node_equals(datatype_uri, world->xsd_integer_node)) + type = SLV2_VALUE_INT; else SLV2_ERRORF("Unknown datatype %s\n", sord_node_get_string(datatype_uri)); } @@ -119,6 +125,7 @@ slv2_value_new_from_node(SLV2World world, SordNode node) switch (result->type) { case SLV2_VALUE_INT: case SLV2_VALUE_FLOAT: + case SLV2_VALUE_BOOL: slv2_value_set_numerics_from_string(result); default: break; @@ -171,6 +178,15 @@ slv2_value_new_float(SLV2World world, float val) return ret; } +SLV2_API +SLV2Value +slv2_value_new_bool(SLV2World world, bool val) +{ + SLV2Value ret = slv2_value_new(world, SLV2_VALUE_BOOL, val ? "true" : "false"); + ret->val.bool_val = val; + return ret; +} + SLV2_API SLV2Value slv2_value_duplicate(SLV2Value val) @@ -228,6 +244,8 @@ slv2_value_equals(SLV2Value value, SLV2Value other) return (value->val.int_val == other->val.int_val); case SLV2_VALUE_FLOAT: return (value->val.float_val == other->val.float_val); + case SLV2_VALUE_BOOL: + return (value->val.bool_val == other->val.bool_val); } return false; /* shouldn't get here */ @@ -254,6 +272,7 @@ slv2_value_get_turtle_token(SLV2Value value) break; case SLV2_VALUE_STRING: case SLV2_VALUE_QNAME_UNUSED: + case SLV2_VALUE_BOOL: result = strdup(value->str_val); break; case SLV2_VALUE_INT: @@ -383,3 +402,19 @@ slv2_value_as_float(SLV2Value value) else // slv2_value_is_int(value) return (float)value->val.int_val; } + +SLV2_API +bool +slv2_value_is_bool(SLV2Value value) +{ + return (value && value->type == SLV2_VALUE_BOOL); +} + +SLV2_API +bool +slv2_value_as_bool(SLV2Value value) +{ + assert(value); + assert(slv2_value_is_bool(value)); + return value->val.bool_val; +} -- cgit v1.2.1