diff options
author | David Robillard <d@drobilla.net> | 2018-12-29 20:05:00 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 41c4935f03caef37baff3d22310b90abd300fd02 (patch) | |
tree | 69bea30ba4292d94f187659837cf8f204f7ab8c2 | |
parent | 7a179e568a3576819d0e600cf35c94051e107d33 (diff) | |
download | serd-41c4935f03caef37baff3d22310b90abd300fd02.tar.gz serd-41c4935f03caef37baff3d22310b90abd300fd02.tar.bz2 serd-41c4935f03caef37baff3d22310b90abd300fd02.zip |
Rename SerdStyle to SerdWriterFlags
-rw-r--r-- | include/serd/serd.h | 21 | ||||
-rw-r--r-- | src/serdi.c | 24 | ||||
-rw-r--r-- | src/writer.c | 52 | ||||
-rw-r--r-- | test/test_reader_writer.c | 15 | ||||
-rw-r--r-- | test/test_writer.c | 12 |
5 files changed, 64 insertions, 60 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h index c2308b16..c7ff1358 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -815,25 +815,28 @@ serd_reader_free(SerdReader* SERD_NULLABLE reader); typedef struct SerdWriterImpl SerdWriter; /** - Syntax style options. + Writer style options. These flags allow more precise control of writer output style. Note that some options are only supported for some syntaxes, for example, NTriples does not support abbreviation and is always ASCII. */ typedef enum { - SERD_STYLE_ABBREVIATED = 1U << 0U, ///< Abbreviate triples when possible. - SERD_STYLE_ASCII = 1U << 1U, ///< Escape all non-ASCII characters. - SERD_STYLE_RESOLVED = 1U << 2U, ///< Resolve URIs against base URI. - SERD_STYLE_CURIED = 1U << 3U, ///< Shorten URIs into CURIEs. - SERD_STYLE_BULK = 1U << 4U, ///< Write output in pages. - SERD_STYLE_STRICT = 1U << 5U, ///< Abort with error on lossy output -} SerdStyle; + SERD_WRITE_ABBREVIATED = 1U << 0U, ///< Abbreviate triples when possible + SERD_WRITE_ASCII = 1U << 1U, ///< Escape all non-ASCII characters + SERD_WRITE_RESOLVED = 1U << 2U, ///< Resolve URIs against base URI + SERD_WRITE_CURIED = 1U << 3U, ///< Shorten URIs into CURIEs + SERD_WRITE_BULK = 1U << 4U, ///< Write output in pages + SERD_WRITE_STRICT = 1U << 5U, ///< Abort with error on lossy output +} SerdWriterFlag; + +/// Bitwise OR of SerdWriterFlag values +typedef uint32_t SerdWriterFlags; /// Create a new RDF writer SERD_API SerdWriter* SERD_ALLOCATED serd_writer_new(SerdSyntax syntax, - SerdStyle style, + SerdWriterFlags flags, SerdEnv* SERD_NONNULL env, const SerdURIView* SERD_NULLABLE base_uri, SerdSink SERD_NONNULL ssink, diff --git a/src/serdi.c b/src/serdi.c index 0ec6366f..0a7cfc56 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -138,7 +138,7 @@ serd_fopen(const char* const path, const char* const mode) return fd; } -static SerdStyle +static SerdWriterFlags choose_style(const SerdSyntax input_syntax, const SerdSyntax output_syntax, const bool ascii, @@ -146,31 +146,31 @@ choose_style(const SerdSyntax input_syntax, const bool full_uris, const bool lax) { - unsigned output_style = 0U; + SerdWriterFlags writer_flags = 0U; if (output_syntax == SERD_NTRIPLES || ascii) { - output_style |= SERD_STYLE_ASCII; + writer_flags |= SERD_WRITE_ASCII; } else if (output_syntax == SERD_TURTLE) { - output_style |= SERD_STYLE_ABBREVIATED; + writer_flags |= SERD_WRITE_ABBREVIATED; if (!full_uris) { - output_style |= SERD_STYLE_CURIED; + writer_flags |= SERD_WRITE_CURIED; } } if ((input_syntax == SERD_TURTLE || input_syntax == SERD_TRIG) || - (output_style & SERD_STYLE_CURIED)) { + (writer_flags & SERD_WRITE_CURIED)) { // Base URI may change and/or we're abbreviating URIs, so must resolve - output_style |= SERD_STYLE_RESOLVED; + writer_flags |= SERD_WRITE_RESOLVED; } if (bulk_write) { - output_style |= SERD_STYLE_BULK; + writer_flags |= SERD_WRITE_BULK; } if (!lax) { - output_style |= SERD_STYLE_STRICT; + writer_flags |= SERD_WRITE_STRICT; } - return (SerdStyle)output_style; + return writer_flags; } int @@ -313,7 +313,7 @@ main(int argc, char** argv) : SERD_NQUADS); } - const SerdStyle output_style = choose_style( + const SerdWriterFlags writer_flags = choose_style( input_syntax, output_syntax, ascii, bulk_write, full_uris, lax); SerdURIView base_uri = SERD_URI_NULL; @@ -328,7 +328,7 @@ main(int argc, char** argv) SerdEnv* const env = serd_env_new(&base); SerdWriter* const writer = serd_writer_new( - output_syntax, output_style, env, &base_uri, serd_file_sink, out_fd); + output_syntax, writer_flags, env, &base_uri, serd_file_sink, out_fd); SerdReader* const reader = serd_reader_new(input_syntax, diff --git a/src/writer.c b/src/writer.c index 19f5d269..e8225a41 100644 --- a/src/writer.c +++ b/src/writer.c @@ -114,21 +114,21 @@ static const SepRule rules[] = { #undef SEP_EACH struct SerdWriterImpl { - SerdSyntax syntax; - SerdStyle style; - SerdEnv* env; - SerdNode root_node; - SerdURIView root_uri; - SerdURIView base_uri; - SerdStack anon_stack; - SerdByteSink byte_sink; - SerdErrorSink error_sink; - void* error_handle; - WriteContext context; - char* bprefix; - size_t bprefix_len; - Sep last_sep; - int indent; + SerdSyntax syntax; + SerdWriterFlags flags; + SerdEnv* env; + SerdNode root_node; + SerdURIView root_uri; + SerdURIView base_uri; + SerdStack anon_stack; + SerdByteSink byte_sink; + SerdErrorSink error_sink; + void* error_handle; + WriteContext context; + char* bprefix; + size_t bprefix_len; + Sep last_sep; + int indent; }; typedef enum { WRITE_STRING, WRITE_LONG_STRING } TextContext; @@ -271,7 +271,7 @@ write_character(SerdWriter* writer, break; } - if (!(writer->style & SERD_STYLE_ASCII)) { + if (!(writer->flags & SERD_WRITE_ASCII)) { // Write UTF-8 character directly to UTF-8 output return sink(utf8, *size, writer); } @@ -333,7 +333,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->style & SERD_STYLE_STRICT)) { + if (*st && (writer->flags & SERD_WRITE_STRICT)) { break; } @@ -356,7 +356,7 @@ ewrite_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) SerdStatus st = SERD_SUCCESS; write_uri(writer, utf8, n_bytes, &st); - return (st == SERD_ERR_BAD_WRITE || (writer->style & SERD_STYLE_STRICT)) + return (st == SERD_ERR_BAD_WRITE || (writer->flags & SERD_WRITE_STRICT)) ? st : SERD_SUCCESS; } @@ -522,7 +522,7 @@ write_text(SerdWriter* writer, // Write UTF-8 character size_t size = 0; write_character(writer, (const uint8_t*)utf8 + i - 1, &size, &st); - if (st && (writer->style & SERD_STYLE_STRICT)) { + if (st && (writer->flags & SERD_WRITE_STRICT)) { return st; } @@ -536,7 +536,7 @@ write_text(SerdWriter* writer, } } - return (writer->style & SERD_STYLE_STRICT) ? st : SERD_SUCCESS; + return (writer->flags & SERD_WRITE_STRICT) ? st : SERD_SUCCESS; } typedef struct { @@ -731,7 +731,7 @@ write_uri_node(SerdWriter* const writer, return esink("()", 2, writer); } - if (has_scheme && (writer->style & SERD_STYLE_CURIED) && + if (has_scheme && (writer->flags & SERD_WRITE_CURIED) && serd_env_qualify(writer->env, node, &prefix, &suffix) && is_name(prefix.buf, prefix.n_bytes) && is_name(suffix.buf, suffix.len)) { @@ -751,7 +751,7 @@ write_uri_node(SerdWriter* const writer, TRY(st, esink("<", 1, writer)); - if (writer->style & SERD_STYLE_RESOLVED) { + if (writer->flags & SERD_WRITE_RESOLVED) { SerdURIView in_base_uri; SerdURIView uri; SerdURIView abs_uri; @@ -784,7 +784,7 @@ write_curie(SerdWriter* const writer, const SerdNode* const node) // In fast-and-loose Turtle/TriG mode CURIEs are simply passed through const bool fast = - !(writer->style & (SERD_STYLE_CURIED | SERD_STYLE_RESOLVED)); + !(writer->flags & (SERD_WRITE_CURIED | SERD_WRITE_RESOLVED)); if (!supports_abbrev(writer) || !fast) { if ((st = serd_env_expand(writer->env, node, &prefix, &suffix))) { @@ -1119,7 +1119,7 @@ serd_writer_finish(SerdWriter* writer) SerdWriter* serd_writer_new(SerdSyntax syntax, - SerdStyle style, + SerdWriterFlags flags, SerdEnv* env, const SerdURIView* base_uri, SerdSink ssink, @@ -1129,7 +1129,7 @@ serd_writer_new(SerdSyntax syntax, SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter)); writer->syntax = syntax; - writer->style = style; + writer->flags = flags; writer->env = env; writer->root_node = SERD_NODE_NULL; writer->root_uri = SERD_URI_NULL; @@ -1137,7 +1137,7 @@ serd_writer_new(SerdSyntax syntax, writer->anon_stack = serd_stack_new(SERD_PAGE_SIZE); writer->context = context; writer->byte_sink = serd_byte_sink_new( - ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1); + ssink, stream, (flags & SERD_WRITE_BULK) ? SERD_PAGE_SIZE : 1); return writer; } diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index bb27cf3e..6c759d8d 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -394,8 +394,9 @@ quiet_error_sink(void* const handle, const SerdError* const e) static void test_write_errors(void) { - ErrorContext ctx = {0U, 0U}; - const SerdStyle style = (SerdStyle)(SERD_STYLE_STRICT | SERD_STYLE_CURIED); + ErrorContext ctx = {0U, 0U}; + const SerdWriterFlags style = + (SerdWriterFlags)(SERD_WRITE_STRICT | SERD_WRITE_CURIED); const size_t max_offsets[] = {0, 386, 1911, 2003, 386}; @@ -440,7 +441,7 @@ test_writer(const char* const path) assert(fd); SerdWriter* writer = - serd_writer_new(SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); + serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_file_sink, fd); assert(writer); serd_writer_chop_blank_prefix(writer, "tmp"); @@ -516,8 +517,8 @@ test_writer(const char* const path) // Test buffer sink SerdBuffer buffer = {NULL, 0}; - writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); + writer = + serd_writer_new(SERD_TURTLE, 0, env, NULL, serd_buffer_sink, &buffer); o = serd_node_from_string(SERD_URI, "http://example.org/base"); assert(!serd_writer_set_base_uri(writer, &o)); @@ -533,8 +534,8 @@ test_writer(const char* const path) buffer.buf = NULL; buffer.len = 0; - writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); + writer = + serd_writer_new(SERD_TURTLE, 0U, env, NULL, serd_buffer_sink, &buffer); assert(!serd_writer_write_statement( writer, 0, NULL, &s, &p, ¬hing, NULL, NULL)); diff --git a/test/test_writer.c b/test/test_writer.c index aa1b05e7..783bf3b0 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -15,8 +15,8 @@ test_write_long_literal(void) { SerdEnv* env = serd_env_new(NULL); SerdBuffer buffer = {NULL, 0}; - SerdWriter* writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, 0U, env, NULL, serd_buffer_sink, &buffer); assert(writer); @@ -54,7 +54,7 @@ test_writer_cleanup(void) SerdStatus st = SERD_SUCCESS; SerdEnv* env = serd_env_new(NULL); SerdWriter* writer = - serd_writer_new(SERD_TURTLE, (SerdStyle)0U, env, NULL, null_sink, NULL); + serd_writer_new(SERD_TURTLE, 0U, env, NULL, null_sink, NULL); SerdNode s = serd_node_from_string(SERD_URI, "http://example.org/s"); SerdNode p = serd_node_from_string(SERD_URI, "http://example.org/p"); @@ -98,7 +98,7 @@ test_strict_write(void) SerdEnv* const env = serd_env_new(NULL); SerdWriter* const writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)SERD_STYLE_STRICT, env, NULL, null_sink, fd); + SERD_TURTLE, (SerdWriterFlags)SERD_WRITE_STRICT, env, NULL, null_sink, fd); assert(writer); @@ -141,8 +141,8 @@ test_write_error(void) SerdNode u = serd_node_from_string(SERD_URI, "http://example.com/u"); - writer = - serd_writer_new(SERD_TURTLE, (SerdStyle)0, env, NULL, error_sink, NULL); + writer = serd_writer_new( + SERD_TURTLE, (SerdWriterFlags)0, env, NULL, error_sink, NULL); assert(writer); st = serd_writer_write_statement(writer, 0U, NULL, &u, &u, &u, NULL, NULL); assert(st == SERD_ERR_BAD_WRITE); |