diff options
author | David Robillard <d@drobilla.net> | 2021-08-14 01:51:55 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | b3892cb6e4963e1bbeb346a8124101b7c3cf379b (patch) | |
tree | 4800918b6f4db5ce0d0f4802988c1935996d6ba3 /tools | |
parent | 0e739f34801ff6810064a8fac570f6be2b61ae70 (diff) | |
download | serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.tar.gz serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.tar.bz2 serd-b3892cb6e4963e1bbeb346a8124101b7c3cf379b.zip |
Simplify input stream API
More or less the same rationale as the previous commit, but for reading. This
makes for nice symmetry with writing, at the cost of a slightly more annoying
reader interface since the source doesn't know its block size or name.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/console.c | 40 | ||||
-rw-r--r-- | tools/console.h | 9 | ||||
-rw-r--r-- | tools/serd-filter.c | 34 | ||||
-rw-r--r-- | tools/serd-pipe.c | 9 |
4 files changed, 50 insertions, 42 deletions
diff --git a/tools/console.c b/tools/console.c index 5cfeb56c..30c57575 100644 --- a/tools/console.c +++ b/tools/console.c @@ -40,7 +40,7 @@ serd_tool_setup(SerdTool* const tool, // Open the output first, since if that fails we have nothing to do const char* const out_path = options.out_filename; - if (!((tool->out = serd_open_output(out_path)).stream)) { + if (!((tool->out = serd_open_tool_output(out_path)).stream)) { fprintf(stderr, "%s: failed to open output file (%s)\n", program, @@ -352,28 +352,22 @@ serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream) return 1; } -SerdByteSource* -serd_open_input(const char* const filename, const size_t block_size) +SerdInputStream +serd_open_tool_input(const char* const filename) { - SerdByteSource* byte_source = NULL; if (!strcmp(filename, "-")) { - serd_set_stream_utf8_mode(stdin); - - SerdNode* name = serd_new_string(SERD_STRING("stdin")); - - byte_source = serd_byte_source_new_function( - serd_file_read_byte, (SerdStreamErrorFunc)ferror, NULL, stdin, name, 1); + const SerdInputStream in = serd_open_input_stream( + serd_file_read_byte, (SerdStreamErrorFunc)ferror, NULL, stdin); - serd_node_free(name); - } else { - byte_source = serd_byte_source_new_filename(filename, block_size); + serd_set_stream_utf8_mode(stdin); + return in; } - return byte_source; + return serd_open_input_file(filename); } SerdOutputStream -serd_open_output(const char* const filename) +serd_open_tool_output(const char* const filename) { if (!filename || !strcmp(filename, "-")) { serd_set_stream_utf8_mode(stdout); @@ -422,13 +416,16 @@ serd_read_source(SerdWorld* const world, const SerdCommonOptions opts, SerdEnv* const env, const SerdSyntax syntax, - SerdByteSource* const in, + SerdInputStream* const in, + const char* const name, const SerdSink* const sink) { SerdReader* const reader = serd_reader_new( world, syntax, opts.input.flags, env, sink, opts.stack_size); - SerdStatus st = serd_reader_start(reader, in); + SerdNode* const name_node = serd_new_string(SERD_STRING(name)); + SerdStatus st = serd_reader_start(reader, in, name_node, opts.block_size); + serd_node_free(name_node); if (!st) { st = serd_reader_read_document(reader); } @@ -455,8 +452,8 @@ serd_read_inputs(SerdWorld* const world, } // Open the input stream - SerdByteSource* const in = serd_open_input(in_path, opts.block_size); - if (!in) { + SerdInputStream in = serd_open_tool_input(in_path); + if (!in.stream) { return SERD_ERR_BAD_ARG; } @@ -466,10 +463,11 @@ serd_read_inputs(SerdWorld* const world, opts, env, serd_choose_syntax(world, opts.input, in_path, SERD_TRIG), - in, + &in, + !strcmp(in_path, "-") ? "stdin" : in_path, sink); - serd_byte_source_free(in); + serd_close_input(&in); } return st; diff --git a/tools/console.h b/tools/console.h index 97251f68..43bc7a12 100644 --- a/tools/console.h +++ b/tools/console.h @@ -119,11 +119,11 @@ serd_choose_syntax(SerdWorld* world, const char* filename, SerdSyntax fallback); -SerdByteSource* -serd_open_input(const char* filename, size_t block_size); +SerdInputStream +serd_open_tool_input(const char* filename); SerdOutputStream -serd_open_output(const char* filename); +serd_open_tool_output(const char* filename); SerdStatus serd_set_base_uri_from_path(SerdEnv* env, const char* path); @@ -133,7 +133,8 @@ serd_read_source(SerdWorld* world, SerdCommonOptions opts, SerdEnv* env, SerdSyntax syntax, - SerdByteSource* in, + SerdInputStream* in, + const char* name, const SerdSink* sink); SerdStatus diff --git a/tools/serd-filter.c b/tools/serd-filter.c index a6f9b484..147bf51d 100644 --- a/tools/serd-filter.c +++ b/tools/serd-filter.c @@ -66,10 +66,10 @@ on_pattern_event(void* const handle, const SerdEvent* const event) // Parse a pattern from some input and return a new filter for it static SerdSink* -parse_pattern(SerdWorld* const world, - const SerdSink* const sink, - SerdByteSource* const byte_source, - const bool inclusive) +parse_pattern(SerdWorld* const world, + const SerdSink* const sink, + SerdInputStream* const in, + const bool inclusive) { SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); FilterPattern pat = {NULL, NULL, NULL, NULL}; @@ -77,11 +77,15 @@ parse_pattern(SerdWorld* const world, SerdReader* reader = serd_reader_new( world, SERD_NQUADS, SERD_READ_VARIABLES, env, in_sink, 4096); - SerdStatus st = serd_reader_start(reader, byte_source); + const SerdNode* pattern_name = + serd_nodes_string(serd_world_nodes(world), SERD_STRING("pattern")); + + SerdStatus st = serd_reader_start(reader, in, pattern_name, 1); if (!st) { st = serd_reader_read_document(reader); } + serd_close_input(in); serd_reader_free(reader); serd_env_free(env); serd_sink_free(in_sink); @@ -135,24 +139,29 @@ run(Options opts) const SerdSink* const target = serd_writer_sink(app.writer); // Open the pattern input (either a string or filename) - SerdByteSource* const pattern = - opts.pattern ? serd_byte_source_new_string(opts.pattern, NULL) - : opts.pattern_file - ? serd_byte_source_new_filename(opts.pattern_file, opts.common.block_size) - : NULL; - if (!pattern) { + SerdInputStream pattern = {NULL, NULL, NULL, NULL}; + const char* position = opts.pattern; + if (opts.pattern) { + pattern = serd_open_input_string(&position); + } else if (opts.pattern_file) { + pattern = serd_open_input_file(opts.pattern_file); + } + + if (!pattern.stream) { log_error(app.world, "failed to open pattern"); return SERD_ERR_UNKNOWN; } // Set up the output pipeline: filter -> writer SerdSink* const filter = - parse_pattern(app.world, target, pattern, !opts.invert); + parse_pattern(app.world, target, &pattern, !opts.invert); if (!filter) { log_error(app.world, "failed to set up filter"); return SERD_ERR_UNKNOWN; } + serd_close_input(&pattern); + // Read all the inputs, which drives the writer to emit the output if (!(st = serd_read_inputs(app.world, opts.common, @@ -168,7 +177,6 @@ run(Options opts) } serd_sink_free(filter); - serd_byte_source_free(pattern); const SerdStatus cst = serd_tool_cleanup(app); return st ? st : cst; diff --git a/tools/serd-pipe.c b/tools/serd-pipe.c index 08479a74..25607d15 100644 --- a/tools/serd-pipe.c +++ b/tools/serd-pipe.c @@ -64,18 +64,19 @@ run(const Options opts) } if (opts.input_string) { - SerdByteSource* const in = - serd_byte_source_new_string(opts.input_string, NULL); + const char* position = opts.input_string; + SerdInputStream in = serd_open_input_string(&position); st = serd_read_source( app.world, opts.common, app.env, serd_choose_syntax(app.world, opts.common.input, NULL, SERD_TRIG), - in, + &in, + "string", sink); - serd_byte_source_free(in); + serd_close_input(&in); } // Read all the inputs, which drives the writer to emit the output |