diff options
author | David Robillard <d@drobilla.net> | 2016-03-15 23:21:34 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-10-27 13:13:58 +0100 |
commit | c63451092692c251cab874475d3540408ed5d8c2 (patch) | |
tree | 02a91a059723946c5db5f11977a79d1f1aed1a85 | |
parent | 203ca287b9fe7dc00f02ac5e354260cfedfeac93 (diff) | |
download | serd-c63451092692c251cab874475d3540408ed5d8c2.tar.gz serd-c63451092692c251cab874475d3540408ed5d8c2.tar.bz2 serd-c63451092692c251cab874475d3540408ed5d8c2.zip |
Add SerdBuffer type for mutable buffers
This avoids const violations from abusing SerdChunk as a mutable buffer
for string sinks.
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | serd/serd.h | 20 | ||||
-rw-r--r-- | src/node.c | 17 | ||||
-rw-r--r-- | src/uri.c | 10 | ||||
-rw-r--r-- | src/writer.c | 14 | ||||
-rw-r--r-- | tests/serd_test.c | 8 | ||||
-rw-r--r-- | wscript | 4 |
7 files changed, 47 insertions, 32 deletions
@@ -1,3 +1,9 @@ +serd (1.0.1) unstable; + + * Add SerdBuffer for mutable buffers to keep SerdChunk const-correct + + -- David Robillard <d@drobilla.net> Sat, 19 Jan 2019 12:31:12 +0000 + serd (0.30.6) stable; * Fix various minor warnings and other code quality issues diff --git a/serd/serd.h b/serd/serd.h index c1e8ba8e..08a5e151 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -234,6 +234,14 @@ typedef struct { } SerdChunk; /** + A mutable buffer in memory. +*/ +typedef struct { + void* buf; /**< Buffer */ + size_t len; /**< Size of buffer in bytes */ +} SerdBuffer; + +/** An error description. */ typedef struct { @@ -975,24 +983,24 @@ serd_file_sink(const void* buf, size_t len, void* stream); /** A convenience sink function for writing to a string. - This function can be used as a SerdSink to write to a SerdChunk which is + This function can be used as a SerdSink to write to a SerdBuffer which is resized as necessary with realloc(). The `stream` parameter must point to - an initialized SerdChunk. When the write is finished, the string should be - retrieved with serd_chunk_sink_finish(). + an initialized SerdBuffer. When the write is finished, the string should be + retrieved with serd_buffer_sink_finish(). */ SERD_API size_t -serd_chunk_sink(const void* buf, size_t len, void* stream); +serd_buffer_sink(const void* buf, size_t len, void* stream); /** - Finish a serialisation to a chunk with serd_chunk_sink(). + Finish a serialisation to a chunk with serd_buffer_sink(). The returned string is the result of the serialisation, which is NULL terminated (by this function) and owned by the caller. */ SERD_API uint8_t* -serd_chunk_sink_finish(SerdChunk* stream); +serd_buffer_sink_finish(SerdBuffer* stream); /** Set a function to be called when errors occur during writing. @@ -184,30 +184,31 @@ serd_node_new_file_uri(const uint8_t* path, evil ? "/" : ""); } - SerdChunk chunk = { uri, uri_len }; + SerdBuffer buffer = { uri, uri_len }; for (size_t i = 0; i < path_len; ++i) { if (evil && path[i] == '\\') { - serd_chunk_sink("/", 1, &chunk); + serd_buffer_sink("/", 1, &buffer); } else if (path[i] == '%') { - serd_chunk_sink("%%", 2, &chunk); + serd_buffer_sink("%%", 2, &buffer); } else if (!escape || is_uri_path_char(path[i])) { - serd_chunk_sink(path + i, 1, &chunk); + serd_buffer_sink(path + i, 1, &buffer); } else { char escape_str[4] = { '%', 0, 0, 0 }; snprintf(escape_str + 1, sizeof(escape_str) - 1, "%X", (unsigned)path[i]); - serd_chunk_sink(escape_str, 3, &chunk); + serd_buffer_sink(escape_str, 3, &buffer); } } - serd_chunk_sink_finish(&chunk); + serd_buffer_sink_finish(&buffer); if (out) { - serd_uri_parse(chunk.buf, out); + serd_uri_parse((const uint8_t*)buffer.buf, out); } - return serd_node_from_substring(SERD_URI, chunk.buf, chunk.len); + return serd_node_from_substring( + SERD_URI, (const uint8_t*)buffer.buf, buffer.len); } SerdNode @@ -74,25 +74,25 @@ serd_file_uri_parse(const uint8_t* uri, uint8_t** hostname) ++path; } - SerdChunk chunk = { NULL, 0 }; + SerdBuffer buffer = { NULL, 0 }; for (const uint8_t* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { - serd_chunk_sink("%", 1, &chunk); + serd_buffer_sink("%", 1, &buffer); ++s; } else if (is_hexdig(*(s + 1)) && is_hexdig(*(s + 2))) { const uint8_t code[3] = {*(s + 1), *(s + 2), 0}; const uint8_t c = (uint8_t)strtoul((const char*)code, NULL, 16); - serd_chunk_sink(&c, 1, &chunk); + serd_buffer_sink(&c, 1, &buffer); s += 2; } else { s += 2; // Junk escape, ignore } } else { - serd_chunk_sink(s, 1, &chunk); + serd_buffer_sink(s, 1, &buffer); } } - return serd_chunk_sink_finish(&chunk); + return serd_buffer_sink_finish(&buffer); } bool diff --git a/src/writer.c b/src/writer.c index 5f312c30..de37b984 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1003,18 +1003,18 @@ serd_file_sink(const void* buf, size_t len, void* stream) } size_t -serd_chunk_sink(const void* buf, size_t len, void* stream) +serd_buffer_sink(const void* buf, size_t len, void* stream) { - SerdChunk* chunk = (SerdChunk*)stream; - chunk->buf = (uint8_t*)realloc((uint8_t*)chunk->buf, chunk->len + len); - memcpy((uint8_t*)chunk->buf + chunk->len, buf, len); - chunk->len += len; + SerdBuffer* buffer = (SerdBuffer*)stream; + buffer->buf = (char*)realloc(buffer->buf, buffer->len + len); + memcpy((uint8_t*)buffer->buf + buffer->len, buf, len); + buffer->len += len; return len; } uint8_t* -serd_chunk_sink_finish(SerdChunk* stream) +serd_buffer_sink_finish(SerdBuffer* stream) { - serd_chunk_sink("", 1, stream); + serd_buffer_sink("", 1, stream); return (uint8_t*)stream->buf; } diff --git a/tests/serd_test.c b/tests/serd_test.c index efb38161..816a25cc 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -561,16 +561,16 @@ test_writer(const char* const path) serd_writer_free(writer); - // Test chunk sink - SerdChunk chunk = { NULL, 0 }; + // Test buffer sink + SerdBuffer buffer = { NULL, 0 }; writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); + SERD_TURTLE, (SerdStyle)0, env, NULL, serd_buffer_sink, &buffer); o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); assert(!serd_writer_set_base_uri(writer, &o)); serd_writer_free(writer); - uint8_t* out = serd_chunk_sink_finish(&chunk); + uint8_t* out = serd_buffer_sink_finish(&buffer); assert(!strcmp((const char*)out, "@base <http://example.org/base> .\n")); serd_free(out); @@ -11,8 +11,8 @@ from waflib.extras import autowaf # major increment <=> incompatible changes # minor increment <=> compatible changes (additions) # micro increment <=> no interface changes -SERD_VERSION = '0.30.6' -SERD_MAJOR_VERSION = '0' +SERD_VERSION = '1.0.1' +SERD_MAJOR_VERSION = '1' # Mandatory waf variables APPNAME = 'serd' # Package name for waf dist |