diff options
author | David Robillard <d@drobilla.net> | 2016-03-16 16:21:20 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-03-07 15:32:23 -0500 |
commit | a29581d3ba664175c459e20e6c86be09707fde6e (patch) | |
tree | d11253ca88b09d7a768740b00332a4d780e3852d /src/writer.c | |
parent | 6e856d3e7a9c3162b9af350d5cec8a3f6bb94ee2 (diff) | |
download | serd-a29581d3ba664175c459e20e6c86be09707fde6e.tar.gz serd-a29581d3ba664175c459e20e6c86be09707fde6e.tar.bz2 serd-a29581d3ba664175c459e20e6c86be09707fde6e.zip |
Use char* for strings in public API
The constant casting just makes user code a mess, for no benefit.
Diffstat (limited to 'src/writer.c')
-rw-r--r-- | src/writer.c | 66 |
1 files changed, 30 insertions, 36 deletions
diff --git a/src/writer.c b/src/writer.c index 2c483c4e..dce47599 100644 --- a/src/writer.c +++ b/src/writer.c @@ -106,7 +106,7 @@ struct SerdWriterImpl { SerdNode list_subj; unsigned list_depth; unsigned indent; - uint8_t* bprefix; + char* bprefix; size_t bprefix_len; Sep last_sep; bool empty; @@ -145,7 +145,7 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...) va_list args; va_start(args, fmt); - const SerdError e = {st, (const uint8_t*)"", 0, 0, fmt, &args}; + const SerdError e = {st, "", 0, 0, fmt, &args}; serd_error(writer->error_sink, writer->error_handle, &e); va_end(args); } @@ -162,7 +162,7 @@ static void copy_node(SerdNode* dst, const SerdNode* src) { if (src) { - dst->buf = (uint8_t*)realloc((char*)dst->buf, src->n_bytes + 1); + dst->buf = (char*)realloc((char*)dst->buf, src->n_bytes + 1); dst->n_bytes = src->n_bytes; dst->flags = src->flags; dst->type = src->type; @@ -211,7 +211,7 @@ write_character(SerdWriter* writer, const uint8_t* utf8, size_t* size) } static inline bool -uri_must_escape(const uint8_t c) +uri_must_escape(const char c) { switch (c) { case ' ': @@ -231,7 +231,7 @@ uri_must_escape(const uint8_t c) } static size_t -write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +write_uri(SerdWriter* writer, const char* utf8, size_t n_bytes) { size_t len = 0; for (size_t i = 0; i < n_bytes;) { @@ -250,7 +250,7 @@ write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) // Write UTF-8 character size_t size = 0; - len += write_character(writer, utf8 + i, &size); + len += write_character(writer, (const uint8_t*)utf8 + i, &size); i += size; if (size == 0) { // Corrupt input, scan to start of next character @@ -258,12 +258,11 @@ write_uri(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) } } } - return len; } static bool -lname_must_escape(const uint8_t c) +lname_must_escape(const char c) { /* This arbitrary list of characters, most of which have nothing to do with Turtle, must be handled as special cases here because the RDF and SPARQL @@ -300,7 +299,7 @@ lname_must_escape(const uint8_t c) } static size_t -write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) +write_lname(SerdWriter* writer, const char* utf8, size_t n_bytes) { size_t len = 0; for (size_t i = 0; i < n_bytes; ++i) { @@ -326,10 +325,10 @@ write_lname(SerdWriter* writer, const uint8_t* utf8, size_t n_bytes) } static size_t -write_text(SerdWriter* writer, - TextContext ctx, - const uint8_t* utf8, - size_t n_bytes) +write_text(SerdWriter* writer, + TextContext ctx, + const char* utf8, + size_t n_bytes) { size_t len = 0; size_t n_consecutive_quotes = 0; @@ -352,7 +351,7 @@ write_text(SerdWriter* writer, break; // Reached end } - const uint8_t in = utf8[i++]; + const char in = utf8[i++]; if (ctx == WRITE_LONG_STRING) { n_consecutive_quotes = (in == '\"') ? (n_consecutive_quotes + 1) : 0; @@ -416,7 +415,8 @@ write_text(SerdWriter* writer, // Write UTF-8 character size_t size = 0; - len += write_character(writer, utf8 + i - 1, &size); + len += write_character(writer, (const uint8_t*)utf8 + i - 1, &size); + if (size == 0) { // Corrupt input, scan to start of next character for (; i < n_bytes && (utf8[i] & 0x80); ++i) { @@ -425,14 +425,13 @@ write_text(SerdWriter* writer, i += size - 1; } } - return len; } static size_t uri_sink(const void* buf, size_t len, void* stream) { - return write_uri((SerdWriter*)stream, (const uint8_t*)buf, len); + return write_uri((SerdWriter*)stream, (const char*)buf, len); } static void @@ -505,7 +504,7 @@ write_literal(SerdWriter* writer, SerdStatementFlags flags) { if (supports_abbrev(writer) && datatype && datatype->buf) { - const char* type_uri = (const char*)datatype->buf; + const char* type_uri = datatype->buf; if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && (!strcmp(type_uri + sizeof(NS_XSD) - 1, "boolean") || !strcmp(type_uri + sizeof(NS_XSD) - 1, "integer"))) { @@ -515,8 +514,7 @@ write_literal(SerdWriter* writer, if (!strncmp(type_uri, NS_XSD, sizeof(NS_XSD) - 1) && !strcmp(type_uri + sizeof(NS_XSD) - 1, "decimal") && - strchr((const char*)node->buf, '.') && - node->buf[node->n_bytes - 1] != '.') { + strchr(node->buf, '.') && node->buf[node->n_bytes - 1] != '.') { /* xsd:decimal literals without trailing digits, e.g. "5.", can not be written bare in Turtle. We could add a 0 which is prettier, but changes the text and breaks round tripping. @@ -548,7 +546,7 @@ write_literal(SerdWriter* writer, // Return true iff `buf` is a valid prefixed name suffix static inline bool -is_name(const uint8_t* buf, const size_t len) +is_name(const char* buf, const size_t len) { // TODO: This is more strict than it should be for (size_t i = 0; i < len; ++i) { @@ -577,12 +575,11 @@ write_uri_node(SerdWriter* const writer, const bool has_scheme = serd_uri_string_has_scheme(node->buf); if (supports_abbrev(writer)) { - if (field == FIELD_PREDICATE && - !strcmp((const char*)node->buf, NS_RDF "type")) { + if (field == FIELD_PREDICATE && !strcmp(node->buf, NS_RDF "type")) { return sink("a", 1, writer) == 1; } - if (!strcmp((const char*)node->buf, NS_RDF "nil")) { + if (!strcmp(node->buf, NS_RDF "nil")) { return sink("()", 2, writer) == 2; } @@ -625,13 +622,11 @@ write_uri_node(SerdWriter* const writer, } else { write_uri(writer, node->buf, node->n_bytes); } - write_sep(writer, SEP_URI_END); if (is_inline_start(writer, field, flags)) { sink(" ;", 2, writer); write_newline(writer); } - return true; } @@ -707,9 +702,8 @@ write_blank(SerdWriter* const writer, } sink("_:", 2, writer); - if (writer->bprefix && !strncmp((const char*)node->buf, - (const char*)writer->bprefix, - writer->bprefix_len)) { + if (writer->bprefix && + !strncmp(node->buf, writer->bprefix, writer->bprefix_len)) { sink(node->buf + writer->bprefix_len, node->n_bytes - writer->bprefix_len, writer); @@ -772,13 +766,13 @@ write_list_obj(SerdWriter* writer, const SerdNode* datatype, const SerdNode* lang) { - if (!strcmp((const char*)object->buf, NS_RDF "nil")) { + if (!strcmp(object->buf, NS_RDF "nil")) { --writer->indent; write_sep(writer, SEP_LIST_END); return true; } - if (!strcmp((const char*)predicate->buf, NS_RDF "first")) { + if (!strcmp(predicate->buf, NS_RDF "first")) { write_sep(writer, SEP_LIST_SEP); write_node(writer, object, datatype, lang, FIELD_OBJECT, flags); } @@ -1000,16 +994,16 @@ serd_writer_set_error_sink(SerdWriter* writer, } void -serd_writer_chop_blank_prefix(SerdWriter* writer, const uint8_t* prefix) +serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) { free(writer->bprefix); writer->bprefix_len = 0; writer->bprefix = NULL; - const size_t prefix_len = prefix ? strlen((const char*)prefix) : 0; + const size_t prefix_len = prefix ? strlen(prefix) : 0; if (prefix_len) { writer->bprefix_len = prefix_len; - writer->bprefix = (uint8_t*)malloc(writer->bprefix_len + 1); + writer->bprefix = (char*)malloc(writer->bprefix_len + 1); memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); } } @@ -1114,9 +1108,9 @@ serd_buffer_sink(const void* buf, size_t len, void* stream) return len; } -uint8_t* +char* serd_buffer_sink_finish(SerdBuffer* stream) { serd_buffer_sink("", 1, stream); - return (uint8_t*)stream->buf; + return (char*)stream->buf; } |