diff options
author | David Robillard <d@drobilla.net> | 2011-02-11 23:37:12 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-02-11 23:37:12 +0000 |
commit | 0b6bdcce6cea21909553a334629a5b3d004bd553 (patch) | |
tree | 7fa15be6af1700e9169656467d3adcd0856fee05 /src | |
parent | 81d2d2f3abcec251d910358bb2751d5e1ee12985 (diff) | |
download | lilv-0b6bdcce6cea21909553a334629a5b3d004bd553.tar.gz lilv-0b6bdcce6cea21909553a334629a5b3d004bd553.tar.bz2 lilv-0b6bdcce6cea21909553a334629a5b3d004bd553.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/slv2_internal.h | 5 | ||||
-rw-r--r-- | src/value.c | 47 | ||||
-rw-r--r-- | src/world.c | 39 |
3 files changed, 73 insertions, 18 deletions
diff --git a/src/slv2_internal.h b/src/slv2_internal.h index f8ba415..e5aac33 100644 --- a/src/slv2_internal.h +++ b/src/slv2_internal.h @@ -214,8 +214,9 @@ struct _SLV2World { SLV2Node rdfs_subclassof_node; SLV2Node slv2_bundleuri_node; SLV2Node slv2_dmanifest_node; - SLV2Node xsd_integer_node; + SLV2Node xsd_boolean_node; SLV2Node xsd_decimal_node; + SLV2Node xsd_integer_node; SLV2Value doap_name_val; SLV2Value lv2_name_val; bool filter_language; @@ -256,6 +257,7 @@ typedef enum _SLV2ValueType { SLV2_VALUE_STRING, SLV2_VALUE_INT, SLV2_VALUE_FLOAT, + SLV2_VALUE_BOOL, SLV2_VALUE_BLANK } SLV2ValueType; @@ -265,6 +267,7 @@ struct _SLV2Value { union { int int_val; float float_val; + bool bool_val; SLV2Node uri_val; } val; }; 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; @@ -173,6 +180,15 @@ slv2_value_new_float(SLV2World world, float val) 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) { if (val == NULL) @@ -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; +} diff --git a/src/world.c b/src/world.c index f690909..a1ed8c9 100644 --- a/src/world.c +++ b/src/world.c @@ -83,8 +83,9 @@ slv2_world_new() world->rdfs_subclassof_node = NEW_URI(SLV2_NS_RDFS "subClassOf"); world->slv2_bundleuri_node = NEW_URI(SLV2_NS_SLV2 "bundleURI"); world->slv2_dmanifest_node = NEW_URI(SLV2_NS_SLV2 "dynamic-manifest"); - world->xsd_integer_node = NEW_URI(SLV2_NS_XSD "integer"); + world->xsd_boolean_node = NEW_URI(SLV2_NS_XSD "boolean"); world->xsd_decimal_node = NEW_URI(SLV2_NS_XSD "decimal"); + world->xsd_integer_node = NEW_URI(SLV2_NS_XSD "integer"); world->doap_name_val = NEW_URI_VAL(SLV2_NS_DOAP "name"); world->lv2_name_val = NEW_URI_VAL(SLV2_NS_LV2 "name"); @@ -138,20 +139,26 @@ slv2_world_free(SLV2World world) slv2_node_free(world->rdfs_class_node); slv2_node_free(world->slv2_bundleuri_node); slv2_node_free(world->slv2_dmanifest_node); - slv2_node_free(world->xsd_integer_node); + slv2_node_free(world->xsd_boolean_node); slv2_node_free(world->xsd_decimal_node); + slv2_node_free(world->xsd_integer_node); slv2_value_free(world->doap_name_val); slv2_value_free(world->lv2_name_val); - /* - for (unsigned i = 0; i < ((GPtrArray*)world->plugins)->len; ++i) - slv2_plugin_free(g_ptr_array_index((GPtrArray*)world->plugins, i)); - g_ptr_array_unref(world->plugins); - */ +#define SLV2_FOREACH(iter, seq) \ + for (GSequenceIter* (iter) = g_sequence_get_begin_iter(seq); \ + (iter) != g_sequence_get_end_iter(seq); \ + (iter) = g_sequence_iter_next(iter)) + + SLV2_FOREACH(i, world->plugins) { + SLV2Plugin p = g_sequence_get(i); + slv2_plugin_free(p); + } + g_sequence_free(world->plugins); world->plugins = NULL; - //g_ptr_array_unref(world->plugin_classes); + g_sequence_free(world->plugin_classes); world->plugin_classes = NULL; sord_free(world->model); @@ -164,12 +171,20 @@ slv2_world_free(SLV2World world) SLV2_API void -slv2_world_filter_language(SLV2World world, bool filter) +slv2_world_set_option(SLV2World world, + const char* option, + const SLV2Value value) { - world->filter_language = filter; + if (!strcmp(option, SLV2_OPTION_FILTER_LANG)) { + if (slv2_value_is_bool(value)) { + world->filter_language = slv2_value_as_bool(value); + return; + } + } else { + SLV2_WARNF("Unrecognized or invalid option `%s'\n", option); + } } - static SLV2Matches slv2_world_find_statements(SLV2World world, Sord model, @@ -434,6 +449,8 @@ slv2_world_load_bundle(SLV2World world, SLV2Value bundle_uri) sord_add(world->model, bundle_uri_tup); } slv2_match_end(spec_results); + + serd_node_free(&manifest_uri); } // Expand POSIX things in path (particularly ~) |