aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-04-28 17:54:00 +0200
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commit2ab2b5f2c7bd4d26aa99eee959f0e5192b3813dc (patch)
tree9fccf392d6eb69fb11b9bc2bbe24cf35eaa63ab2
parent01daa5914169d57a6a6c4925d3f8d76db80a2bd8 (diff)
downloadserd-2ab2b5f2c7bd4d26aa99eee959f0e5192b3813dc.tar.gz
serd-2ab2b5f2c7bd4d26aa99eee959f0e5192b3813dc.tar.bz2
serd-2ab2b5f2c7bd4d26aa99eee959f0e5192b3813dc.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--include/serd/serd.h24
-rw-r--r--src/reader.c9
-rw-r--r--src/serdi.c13
-rw-r--r--src/writer.c10
-rw-r--r--test/test_overflow.c3
-rw-r--r--test/test_read_chunk.c2
-rw-r--r--test/test_reader_writer.c28
7 files changed, 41 insertions, 48 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index c6300721..ba1b4ddc 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -293,6 +293,14 @@ typedef struct {
SerdStringView fragment; ///< Fragment
} SerdURIView;
+/// Reader options
+typedef enum {
+ SERD_READ_LAX = 1u << 0u ///< Tolerate invalid input where possible
+} SerdReaderFlag;
+
+/// Bitwise OR of SerdReaderFlag values
+typedef uint32_t SerdReaderFlags;
+
/**
Writer style options.
@@ -305,7 +313,7 @@ typedef enum {
SERD_WRITE_UNQUALIFIED = 1u << 1u, ///< Do not shorten URIs into CURIEs
SERD_WRITE_UNRESOLVED = 1u << 2u, ///< Do not make URIs relative
SERD_WRITE_TERSE = 1u << 3u, ///< Write terser output without newlines
- SERD_WRITE_STRICT = 1u << 4u ///< Abort with error on lossy output
+ SERD_WRITE_LAX = 1u << 4u ///< Tolerate lossy output
} SerdWriterFlag;
/// Bitwise OR of SerdWriterFlag values
@@ -1155,22 +1163,12 @@ SERD_API
SerdReader* SERD_ALLOCATED
serd_reader_new(SerdWorld* SERD_NONNULL world,
SerdSyntax syntax,
+ SerdReaderFlags flags,
const SerdSink* SERD_NONNULL 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* SERD_NONNULL reader, bool strict);
-
-/**
- Set a prefix to be added to all blank node identifiers.
+ 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 (a
model or a file). Since Serd preserves blank node IDs, this could cause
diff --git a/src/reader.c b/src/reader.c
index ceef5fb4..8a0b1a1f 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -156,6 +156,7 @@ serd_reader_read_document(SerdReader* reader)
SerdReader*
serd_reader_new(SerdWorld* const world,
const SerdSyntax syntax,
+ SerdReaderFlags flags,
const SerdSink* const sink,
const size_t stack_size)
{
@@ -170,7 +171,7 @@ serd_reader_new(SerdWorld* const world,
me->stack = serd_stack_new(stack_size, sizeof(SerdNode));
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
@@ -187,12 +188,6 @@ serd_reader_new(SerdWorld* const 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 64206d7f..72b7afe1 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -97,12 +97,12 @@ 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 lax = false;
bool quiet = false;
size_t stack_size = 4194304;
const char* add_prefix = NULL;
@@ -126,8 +126,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] == 'q') {
quiet = true;
} else if (argv[a][1] == 'v') {
@@ -232,10 +232,9 @@ main(int argc, char** argv)
(SerdWriteFunc)serd_byte_sink_write,
byte_sink);
- SerdReader* const reader =
- serd_reader_new(world, input_syntax, serd_writer_sink(writer), stack_size);
+ SerdReader* const reader = serd_reader_new(
+ world, input_syntax, reader_flags, serd_writer_sink(writer), stack_size);
- serd_reader_set_strict(reader, !lax);
if (quiet) {
serd_world_set_error_func(world, quiet_error_func, NULL);
}
diff --git a/src/writer.c b/src/writer.c
index 3ac8f4d9..71766d9d 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -343,7 +343,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;
}
@@ -366,7 +366,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
@@ -531,7 +531,7 @@ write_text(SerdWriter* writer,
// Write UTF-8 character
size_t size = 0;
len += write_character(writer, (const uint8_t*)utf8 + i - 1, &size, &st);
- if (st && (writer->flags & SERD_WRITE_STRICT)) {
+ if (st && !(writer->flags & SERD_WRITE_LAX)) {
return st;
}
@@ -545,7 +545,7 @@ write_text(SerdWriter* writer,
}
}
- return (writer->flags & SERD_WRITE_STRICT) ? st : SERD_SUCCESS;
+ return (writer->flags & SERD_WRITE_LAX) ? SERD_SUCCESS : st;
}
typedef struct {
@@ -796,7 +796,7 @@ write_uri_node(SerdWriter* const writer,
: serd_write_uri(serd_relative_uri(uri, base_uri), uri_sink, &ctx);
st = ctx.status;
- if (st && (writer->flags & SERD_WRITE_STRICT)) {
+ if (st && !(writer->flags & SERD_WRITE_LAX)) {
return st;
}
} else {
diff --git a/test/test_overflow.c b/test/test_overflow.c
index a4d59368..8d451f2a 100644
--- a/test/test_overflow.c
+++ b/test/test_overflow.c
@@ -24,7 +24,8 @@
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/test/test_read_chunk.c b/test/test_read_chunk.c
index 0f24089d..1bbe2709 100644
--- a/test/test_read_chunk.c
+++ b/test/test_read_chunk.c
@@ -94,7 +94,7 @@ main(void)
SerdWorld* world = serd_world_new();
SerdSink* sink = serd_sink_new(NULL, on_event, NULL);
- 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/test/test_reader_writer.c b/test/test_reader_writer.c
index fec09ff8..e0f636e9 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -77,7 +77,7 @@ test_read_chunks(void)
SerdSink* const sink = serd_sink_new(&n_statements, count_statements, NULL);
assert(sink);
- SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, sink, 4096);
+ SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096);
assert(reader);
SerdStatus st = serd_reader_start_stream(
@@ -148,12 +148,12 @@ test_get_blank(void)
static int
test_strict_write(void)
{
- SerdWorld* world = serd_world_new();
- const char* path = "serd_strict_write_test.ttl";
- FILE* fd = fopen(path, "wb");
- SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
- SerdWriter* writer = serd_writer_new(
- world, SERD_TURTLE, SERD_WRITE_STRICT, env, (SerdWriteFunc)fwrite, fd);
+ SerdWorld* world = serd_world_new();
+ const char* path = "serd_strict_write_test.ttl";
+ FILE* fd = fopen(path, "wb");
+ SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());
+ SerdWriter* writer =
+ serd_writer_new(world, SERD_TURTLE, 0, env, (SerdWriteFunc)fwrite, fd);
assert(fd);
assert(writer);
@@ -184,13 +184,13 @@ test_strict_write(void)
static void
test_read_string(void)
{
- SerdWorld* world = serd_world_new();
- size_t n_statements = 0;
+ SerdWorld* const world = serd_world_new();
+ size_t n_statements = 0;
- SerdSink* sink = serd_sink_new(&n_statements, count_statements, NULL);
+ SerdSink* const sink = serd_sink_new(&n_statements, count_statements, NULL);
assert(sink);
- SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096);
+ SerdReader* const reader = serd_reader_new(world, SERD_TURTLE, 0, sink, 4096);
assert(reader);
// Test reading a string that ends exactly at the end of input (no newline)
@@ -218,8 +218,8 @@ test_writer(const char* const path)
SerdWorld* world = serd_world_new();
- SerdWriter* writer =
- serd_writer_new(world, SERD_TURTLE, 0, env, (SerdWriteFunc)fwrite, fd);
+ SerdWriter* writer = serd_writer_new(
+ world, SERD_TURTLE, SERD_WRITE_LAX, env, (SerdWriteFunc)fwrite, fd);
assert(writer);
serd_writer_chop_blank_prefix(writer, "tmp");
@@ -324,7 +324,7 @@ test_reader(const char* path)
SerdSink* const sink = serd_sink_new(&n_statements, count_statements, NULL);
assert(sink);
- 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");