aboutsummaryrefslogtreecommitdiffstats
path: root/src/writer.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2016-03-16 16:21:20 -0400
committerDavid Robillard <d@drobilla.net>2022-01-13 15:33:54 -0500
commit97258f0e85834d71b17e3c1997a5c7dc136e0b98 (patch)
treed0508236b48d292e59b9ae4404dc23cb258c2107 /src/writer.c
parentf078026b3c5d63c494381d3573a8107ddd7d78f7 (diff)
downloadserd-97258f0e85834d71b17e3c1997a5c7dc136e0b98.tar.gz
serd-97258f0e85834d71b17e3c1997a5c7dc136e0b98.tar.bz2
serd-97258f0e85834d71b17e3c1997a5c7dc136e0b98.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.c67
1 files changed, 31 insertions, 36 deletions
diff --git a/src/writer.c b/src/writer.c
index 4b4d480b..84c704ee 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 bool
-uri_must_escape(const uint8_t c)
+uri_must_escape(const int 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,23 +250,22 @@ 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, write percent-encoded bytes and scan to next start
char escape[4] = {0, 0, 0, 0};
for (; i < n_bytes && (utf8[i] & 0x80); ++i) {
- snprintf(escape, sizeof(escape), "%%%02X", utf8[i]);
+ snprintf(escape, sizeof(escape), "%%%02X", (uint8_t)utf8[i]);
len += sink(escape, 3, writer);
}
}
}
-
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
@@ -303,7 +302,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) {
@@ -329,10 +328,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;
@@ -355,7 +354,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;
@@ -419,7 +418,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, write replacement character and scan to the next start
len += sink(replacement_char, sizeof(replacement_char), writer);
@@ -429,14 +429,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
@@ -509,7 +508,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"))) {
@@ -519,8 +518,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.
@@ -552,7 +550,7 @@ write_literal(SerdWriter* writer,
// Return true iff `buf` is a valid prefixed name prefix or suffix
static 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) {
@@ -574,12 +572,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;
}
@@ -623,7 +620,6 @@ write_uri_node(SerdWriter* const writer,
} else {
write_uri(writer, node->buf, node->n_bytes);
}
-
write_sep(writer, SEP_URI_END);
return true;
}
@@ -688,9 +684,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);
@@ -753,13 +748,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);
}
@@ -981,16 +976,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);
}
}
@@ -1095,9 +1090,9 @@ serd_buffer_sink(const void* const buf, const size_t len, void* const stream)
return len;
}
-uint8_t*
+char*
serd_buffer_sink_finish(SerdBuffer* const stream)
{
serd_buffer_sink("", 1, stream);
- return (uint8_t*)stream->buf;
+ return (char*)stream->buf;
}