diff options
author | David Robillard <d@drobilla.net> | 2019-04-28 17:54:00 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:58 +0100 |
commit | 44e792d8da5ef93a88dac76b7b191e588203c42b (patch) | |
tree | 654d6767998c0a444f8eff8fd3de2461295e1fa3 | |
parent | 02f77d71211825b590e43f571b1d65bcf56cde4d (diff) | |
download | serd-44e792d8da5ef93a88dac76b7b191e588203c42b.tar.gz serd-44e792d8da5ef93a88dac76b7b191e588203c42b.tar.bz2 serd-44e792d8da5ef93a88dac76b7b191e588203c42b.zip |
Replace serd_reader_set_strict() with SerdReaderFlags
This makes reader options extensible and should hopefully prevent the need for
grafting on more similar functions in the future.
-rw-r--r-- | serd/serd.h | 22 | ||||
-rw-r--r-- | src/reader.c | 9 | ||||
-rw-r--r-- | src/serdi.c | 13 | ||||
-rw-r--r-- | src/writer.c | 6 | ||||
-rw-r--r-- | tests/overflow_test.c | 4 | ||||
-rw-r--r-- | tests/read_chunk_test.c | 2 | ||||
-rw-r--r-- | tests/serd_test.c | 10 |
7 files changed, 31 insertions, 35 deletions
diff --git a/serd/serd.h b/serd/serd.h index bad9a4ac..c48ace10 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -255,6 +255,14 @@ typedef struct { SerdStringView fragment; ///< Fragment } SerdURI; +/// Reader options +typedef enum { + SERD_READ_LAX = 1 << 0 ///< Tolerate invalid input where possible +} SerdReaderFlag; + +/// Bitwise OR of SerdReaderFlag values +typedef uint32_t SerdReaderFlags; + /** Writer style options @@ -268,7 +276,7 @@ typedef enum { SERD_WRITE_UNQUALIFIED = 1 << 1, ///< Do not shorten URIs into CURIEs SERD_WRITE_UNRESOLVED = 1 << 2, ///< Do not make URIs relative SERD_WRITE_TERSE = 1 << 3, ///< Write terser output without newlines - SERD_WRITE_STRICT = 1 << 4 ///< Abort with error on lossy output + SERD_WRITE_LAX = 1 << 4 ///< Tolerate lossy output } SerdWriterFlag; /// Bitwise OR of SerdWriterFlag values @@ -1145,21 +1153,11 @@ SERD_API SerdReader* serd_reader_new(SerdWorld* world, SerdSyntax syntax, + SerdReaderFlags flags, const SerdSink* sink, size_t stack_size); /** - Enable or disable strict parsing - - The reader is non-strict (lax) by default, which will tolerate URIs with - invalid characters. Setting strict will fail when parsing such files. An - error is printed for invalid input in either case. -*/ -SERD_API -void -serd_reader_set_strict(SerdReader* reader, bool strict); - -/** Set a prefix to be added to all blank node identifiers This is useful when multiple files are to be parsed into the same output diff --git a/src/reader.c b/src/reader.c index f4a874cb..6a4b0dc2 100644 --- a/src/reader.c +++ b/src/reader.c @@ -158,6 +158,7 @@ serd_reader_read_document(SerdReader* reader) SerdReader* serd_reader_new(SerdWorld* world, SerdSyntax syntax, + SerdReaderFlags flags, const SerdSink* sink, size_t stack_size) { @@ -172,7 +173,7 @@ serd_reader_new(SerdWorld* world, me->stack = serd_stack_new(stack_size, serd_node_align); me->syntax = syntax; me->next_id = 1; - me->strict = true; + me->strict = !(flags & SERD_READ_LAX); /* Reserve a bit of space at the end of the stack to zero pad nodes. This particular kind of overflow could be detected (in emit_statement), but @@ -189,12 +190,6 @@ serd_reader_new(SerdWorld* world, } void -serd_reader_set_strict(SerdReader* reader, bool strict) -{ - reader->strict = strict; -} - -void serd_reader_free(SerdReader* reader) { if (!reader) { diff --git a/src/serdi.c b/src/serdi.c index ec4b0384..14b3e6e5 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -87,13 +87,13 @@ main(int argc, char** argv) SerdSyntax input_syntax = (SerdSyntax)0; SerdSyntax output_syntax = (SerdSyntax)0; - SerdWriterFlags writer_flags = SERD_WRITE_STRICT; + SerdReaderFlags reader_flags = 0; + SerdWriterFlags writer_flags = 0; bool from_string = false; bool from_stdin = false; bool bulk_read = true; bool bulk_write = false; bool no_inline = false; - bool lax = false; bool use_model = false; bool quiet = false; size_t stack_size = 4194304; @@ -116,8 +116,8 @@ main(int argc, char** argv) } else if (argv[a][1] == 'h') { return print_usage(argv[0], false); } else if (argv[a][1] == 'l') { - writer_flags &= ~(unsigned)SERD_WRITE_STRICT; - lax = true; + reader_flags |= SERD_READ_LAX; + writer_flags |= SERD_WRITE_LAX; } else if (argv[a][1] == 'm') { use_model = true; } else if (argv[a][1] == 'q') { @@ -236,8 +236,9 @@ main(int argc, char** argv) sink = serd_writer_get_sink(writer); } - reader = serd_reader_new(world, input_syntax, sink, stack_size); - serd_reader_set_strict(reader, !lax); + reader = serd_reader_new( + world, input_syntax, reader_flags, sink, stack_size); + if (quiet) { serd_world_set_log_func(world, serd_quiet_error_func, NULL); } diff --git a/src/writer.c b/src/writer.c index aa882a31..a6c5f8c7 100644 --- a/src/writer.c +++ b/src/writer.c @@ -326,7 +326,7 @@ write_uri(SerdWriter* writer, const char* utf8, size_t n_bytes, SerdStatus* st) size_t size = 0; len += write_character(writer, (const uint8_t*)utf8 + i, &size, st); i += size; - if (*st && (writer->flags & SERD_WRITE_STRICT)) { + if (*st && !(writer->flags & SERD_WRITE_LAX)) { break; } else if (size == 0) { // Corrupt input, scan to start of next character @@ -342,7 +342,7 @@ ewrite_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) SerdStatus st = SERD_SUCCESS; write_uri(writer, utf8, n_bytes, &st); - return (writer->flags & SERD_WRITE_STRICT) ? st : SERD_SUCCESS; + return (writer->flags & SERD_WRITE_LAX) ? SERD_SUCCESS : st; } SERD_WARN_UNUSED_RESULT static SerdStatus @@ -471,7 +471,7 @@ write_text(SerdWriter* writer, } } - return (writer->flags & SERD_WRITE_STRICT) ? st : SERD_SUCCESS; + return (writer->flags & SERD_WRITE_LAX) ? SERD_SUCCESS : st; } SERD_WARN_UNUSED_RESULT static size_t diff --git a/tests/overflow_test.c b/tests/overflow_test.c index 917d02bf..918f1cc4 100644 --- a/tests/overflow_test.c +++ b/tests/overflow_test.c @@ -24,7 +24,9 @@ static SerdStatus test(SerdWorld* world, SerdSink* sink, const char* str, size_t stack_size) { - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, stack_size); + SerdReader* reader = + serd_reader_new(world, SERD_TURTLE, 0, sink, stack_size); + serd_reader_start_string(reader, str, NULL); const SerdStatus st = serd_reader_read_document(reader); serd_reader_free(reader); diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c index 56c6abdc..31b11603 100644 --- a/tests/read_chunk_test.c +++ b/tests/read_chunk_test.c @@ -80,7 +80,7 @@ main(void) serd_sink_set_statement_func(sink, on_statement); serd_sink_set_end_func(sink, on_end); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); assert(!serd_reader_start_string(reader, diff --git a/tests/serd_test.c b/tests/serd_test.c index a42fe02a..599e948a 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -124,7 +124,7 @@ test_read_chunks(void) assert(sink); serd_sink_set_statement_func(sink, count_statements); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); assert(f); @@ -206,7 +206,7 @@ test_strict_write(void) SerdEnv* env = serd_env_new(NULL); SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, - SERD_WRITE_STRICT, + 0, env, (SerdWriteFunc)fwrite, fd); @@ -239,7 +239,7 @@ test_read_string(void) SerdWorld* world = serd_world_new(); size_t n_statements = 0; SerdSink* sink = serd_sink_new(&n_statements, NULL); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); serd_sink_set_statement_func(sink, count_statements); @@ -657,7 +657,7 @@ test_writer(const char* const path) SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, - 0, + SERD_WRITE_LAX, env, (SerdWriteFunc)fwrite, fd); @@ -772,7 +772,7 @@ test_reader(const char* path) SerdSink* sink = serd_sink_new(&n_statements, NULL); serd_sink_set_statement_func(sink, count_statements); - SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096); + SerdReader* reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096); assert(reader); serd_reader_add_blank_prefix(reader, "tmp"); |