diff options
author | David Robillard <d@drobilla.net> | 2021-10-27 14:15:31 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:24 -0500 |
commit | 30487c277ac5d4be5786733ca7b98adb4c810ae9 (patch) | |
tree | f1b00a7725d74a594fcd91de2aea924485356528 /tools | |
parent | 56cceb103dc633d6af957472945e792187a5dd4e (diff) | |
download | serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.gz serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.bz2 serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.zip |
Add custom allocator support
Diffstat (limited to 'tools')
-rw-r--r-- | tools/console.c | 20 | ||||
-rw-r--r-- | tools/serd-filter.c | 48 |
2 files changed, 44 insertions, 24 deletions
diff --git a/tools/console.c b/tools/console.c index c2166130..65b06b7b 100644 --- a/tools/console.c +++ b/tools/console.c @@ -49,7 +49,7 @@ serd_tool_setup(SerdTool* const tool, } // We have something to write to, so build the writing environment - if (!(tool->world = serd_world_new()) || + if (!(tool->world = serd_world_new(NULL)) || !(tool->env = serd_create_env( tool->world, program, options.base_uri, options.out_filename)) || !(tool->writer = serd_writer_new( @@ -390,7 +390,7 @@ serd_set_base_uri_from_path(SerdEnv* const env, const char* const path) return SERD_BAD_ARG; } - char* const real_path = serd_canonical_path(path); + char* const real_path = serd_canonical_path(NULL, path); if (!real_path) { return SERD_BAD_ARG; } @@ -399,18 +399,22 @@ serd_set_base_uri_from_path(SerdEnv* const env, const char* const path) SerdNode* base_node = NULL; if (path[path_len - 1] == '/' || path[path_len - 1] == '\\') { char* const base_path = (char*)calloc(real_path_len + 2, 1); + memcpy(base_path, real_path, real_path_len); base_path[real_path_len] = path[path_len - 1]; - base_node = serd_new_file_uri(SERD_STRING(base_path), SERD_EMPTY_STRING()); + base_node = + serd_new_file_uri(NULL, SERD_STRING(base_path), SERD_EMPTY_STRING()); + free(base_path); } else { - base_node = serd_new_file_uri(SERD_STRING(real_path), SERD_EMPTY_STRING()); + base_node = + serd_new_file_uri(NULL, SERD_STRING(real_path), SERD_EMPTY_STRING()); } serd_env_set_base_uri(env, serd_node_string_view(base_node)); - serd_node_free(base_node); - serd_free(real_path); + serd_node_free(NULL, base_node); + serd_free(NULL, real_path); return SERD_SUCCESS; } @@ -427,9 +431,9 @@ serd_read_source(SerdWorld* const world, SerdReader* const reader = serd_reader_new( world, syntax, opts.input.flags, env, sink, opts.stack_size); - SerdNode* const name_node = serd_new_string(SERD_STRING(name)); + SerdNode* const name_node = serd_new_string(NULL, SERD_STRING(name)); SerdStatus st = serd_reader_start(reader, in, name_node, opts.block_size); - serd_node_free(name_node); + serd_node_free(NULL, name_node); if (!st) { st = serd_reader_read_document(reader); } diff --git a/tools/serd-filter.c b/tools/serd-filter.c index 199125d5..adf2ab40 100644 --- a/tools/serd-filter.c +++ b/tools/serd-filter.c @@ -44,21 +44,30 @@ typedef struct { SerdNode* g; } FilterPattern; +// Context for the pattern event callback +typedef struct { + SerdAllocator* allocator; + FilterPattern pattern; +} PatternEventContext; + // Handler for events read from a pattern static SerdStatus on_pattern_event(void* const handle, const SerdEvent* const event) { + PatternEventContext* const ctx = (PatternEventContext*)handle; + SerdAllocator* const allocator = ctx->allocator; + if (event->type == SERD_STATEMENT) { - FilterPattern* const pat = (FilterPattern*)handle; + FilterPattern* const pat = &ctx->pattern; if (pat->s) { return SERD_BAD_PATTERN; } const SerdStatement* const statement = event->statement.statement; - pat->s = serd_node_copy(serd_statement_subject(statement)); - pat->p = serd_node_copy(serd_statement_predicate(statement)); - pat->o = serd_node_copy(serd_statement_object(statement)); - pat->g = serd_node_copy(serd_statement_graph(statement)); + pat->s = serd_node_copy(allocator, serd_statement_subject(statement)); + pat->p = serd_node_copy(allocator, serd_statement_predicate(statement)); + pat->o = serd_node_copy(allocator, serd_statement_object(statement)); + pat->g = serd_node_copy(allocator, serd_statement_graph(statement)); } return SERD_SUCCESS; @@ -71,10 +80,12 @@ parse_pattern(SerdWorld* const world, SerdInputStream* const in, const bool inclusive) { - SerdEnv* const env = serd_env_new(world, SERD_EMPTY_STRING()); - FilterPattern pat = {NULL, NULL, NULL, NULL}; - SerdSink* in_sink = serd_sink_new(world, &pat, on_pattern_event, NULL); - SerdReader* reader = serd_reader_new( + SerdAllocator* const allocator = serd_world_allocator(world); + SerdEnv* const env = serd_env_new(world, SERD_EMPTY_STRING()); + PatternEventContext ctx = {allocator, {NULL, NULL, NULL, NULL}}; + + SerdSink* in_sink = serd_sink_new(world, &ctx, on_pattern_event, NULL); + SerdReader* reader = serd_reader_new( world, SERD_NQUADS, SERD_READ_VARIABLES, env, in_sink, 4096); const SerdNode* pattern_name = @@ -98,13 +109,18 @@ parse_pattern(SerdWorld* const world, return NULL; } - SerdSink* filter = - serd_filter_new(world, sink, pat.s, pat.p, pat.o, pat.g, inclusive); - - serd_node_free(pat.s); - serd_node_free(pat.p); - serd_node_free(pat.o); - serd_node_free(pat.g); + SerdSink* filter = serd_filter_new(world, + sink, + ctx.pattern.s, + ctx.pattern.p, + ctx.pattern.o, + ctx.pattern.g, + inclusive); + + serd_node_free(allocator, ctx.pattern.s); + serd_node_free(allocator, ctx.pattern.p); + serd_node_free(allocator, ctx.pattern.o); + serd_node_free(allocator, ctx.pattern.g); return filter; } |