diff options
author | David Robillard <d@drobilla.net> | 2020-06-28 23:26:48 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:59 +0100 |
commit | a3dda1c84a9a258721e26ef57ac32d548a24c148 (patch) | |
tree | 911ee23fdc27a198e0428892d4e1ee844d10caeb /src/serdi.c | |
parent | ba2dc6b53c8dd840651fc9e2c10790989b9cee9f (diff) | |
download | serd-a3dda1c84a9a258721e26ef57ac32d548a24c148.tar.gz serd-a3dda1c84a9a258721e26ef57ac32d548a24c148.tar.bz2 serd-a3dda1c84a9a258721e26ef57ac32d548a24c148.zip |
WIP: Make Reader always read from a ByteSource
Diffstat (limited to 'src/serdi.c')
-rw-r--r-- | src/serdi.c | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/src/serdi.c b/src/serdi.c index 0fc30239..833dc0f0 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -145,18 +145,21 @@ on_filter_event(void* handle, const SerdEvent* event) static SerdSink* parse_filter(SerdWorld* world, const SerdSink* sink, const char* str) { - FilterPattern pat = {NULL, NULL, NULL, NULL}; - SerdSink* in_sink = serd_sink_new(&pat, NULL); - SerdReader* reader = + FilterPattern pat = {NULL, NULL, NULL, NULL}; + SerdSink* in_sink = serd_sink_new(&pat, NULL); + SerdByteSource* byte_source = serd_byte_source_new_string(str, NULL); + SerdReader* reader = serd_reader_new(world, SERD_NQUADS, SERD_READ_VARIABLES, in_sink, 4096); serd_sink_set_event_func(in_sink, on_filter_event); - SerdStatus st = serd_reader_start_string(reader, str, NULL); + + SerdStatus st = serd_reader_start(reader, byte_source); if (!st) { st = serd_reader_read_document(reader); } serd_reader_free(reader); + serd_byte_source_free(byte_source); serd_sink_free(in_sink); if (st) { @@ -184,28 +187,36 @@ read_file(SerdWorld* const world, syntax = syntax ? syntax : serd_guess_syntax(filename); syntax = syntax ? syntax : SERD_TRIG; - SerdStatus st = SERD_SUCCESS; - SerdReader* reader = - serd_reader_new(world, syntax, flags, sink, stack_size); - - serd_reader_add_blank_prefix(reader, add_prefix); - + SerdByteSource* byte_source = NULL; if (!strcmp(filename, "-")) { SerdNode* name = serd_new_string("stdin"); - st = serd_reader_start_stream(reader, - serd_file_read_byte, - (SerdStreamErrorFunc)ferror, - stdin, - name, - 1); + + byte_source = serd_byte_source_new_function( + serd_file_read_byte, (SerdStreamErrorFunc)ferror, stdin, name, 1); + serd_node_free(name); } else { - st = serd_reader_start_file(reader, filename, bulk_read); + byte_source = + serd_byte_source_new_filename(filename, + bulk_read ? SERD_PAGE_SIZE : 1u); + } + + if (!byte_source) { + SERDI_ERRORF("failed to open input file `%s'\n", filename); + return SERD_ERR_UNKNOWN; } + SerdReader* reader = + serd_reader_new(world, syntax, flags, sink, stack_size); + + serd_reader_add_blank_prefix(reader, add_prefix); + + SerdStatus st = serd_reader_start(reader, byte_source); + st = st ? st : serd_reader_read_document(reader); serd_reader_free(reader); + serd_byte_source_free(byte_source); return st; } @@ -439,19 +450,22 @@ main(int argc, char** argv) SerdStatus st = SERD_SUCCESS; SerdNode* input_name = NULL; if (input_string) { + SerdByteSource* byte_source = + serd_byte_source_new_string(input_string, NULL); + SerdReader* reader = serd_reader_new(world, input_syntax ? input_syntax : SERD_TRIG, reader_flags, sink, stack_size); + serd_reader_add_blank_prefix(reader, add_prefix); - SerdNode* name = serd_new_string("string"); - if (!(st = serd_reader_start_string(reader, input_string, name))) { + if (!(st = serd_reader_start(reader, byte_source))) { st = serd_reader_read_document(reader); } - serd_node_free(name); + serd_reader_free(reader); } |