diff options
author | David Robillard <d@drobilla.net> | 2021-01-13 14:31:00 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 16:27:02 -0500 |
commit | 82f8f804773b10c42b17a30872a59ed76b062794 (patch) | |
tree | 73b6ebdde09b41126f37f3f4fc7fbe44b07ec3fb /src | |
parent | 84bab08585dec858807a6130bd2d71f304b889f0 (diff) | |
download | serd-82f8f804773b10c42b17a30872a59ed76b062794.tar.gz serd-82f8f804773b10c42b17a30872a59ed76b062794.tar.bz2 serd-82f8f804773b10c42b17a30872a59ed76b062794.zip |
Add SerdBuffer type for mutable buffers
This avoids const violations from abusing SerdChunk as a mutable buffer
for string sinks.
Diffstat (limited to 'src')
-rw-r--r-- | src/node.c | 14 | ||||
-rw-r--r-- | src/uri.c | 12 | ||||
-rw-r--r-- | src/writer.c | 16 |
3 files changed, 20 insertions, 22 deletions
@@ -200,29 +200,29 @@ serd_node_new_file_uri(const char* const path, } } - SerdChunk chunk = {uri, uri_len}; + SerdBuffer buffer = {uri, uri_len}; for (size_t i = 0; i < path_len; ++i) { if (path[i] == '%') { - serd_chunk_sink("%%", 2, &chunk); + serd_buffer_sink("%%", 2, &buffer); } else if (is_uri_path_char(path[i])) { - serd_chunk_sink(path + i, 1, &chunk); + serd_buffer_sink(path + i, 1, &buffer); #ifdef _WIN32 } else if (path[i] == '\\') { - serd_chunk_sink("/", 1, &chunk); + serd_buffer_sink("/", 1, &buffer); #endif } else { char escape_str[10] = {'%', 0, 0, 0, 0, 0, 0, 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); } } - const char* const string = serd_chunk_sink_finish(&chunk); + const char* const string = serd_buffer_sink_finish(&buffer); if (string && out) { serd_uri_parse(string, out); } - return serd_node_from_substring(SERD_URI, string, chunk.len); + return serd_node_from_substring(SERD_URI, string, buffer.len); } SerdNode @@ -27,7 +27,6 @@ serd_file_uri_parse(const char* const uri, char** const hostname) if (!(path = strchr(auth, '/'))) { return NULL; } - if (hostname) { const size_t len = (size_t)(path - auth); *hostname = (char*)calloc(len + 1, 1); @@ -40,27 +39,26 @@ serd_file_uri_parse(const char* const uri, char** const hostname) ++path; } - SerdChunk chunk = {NULL, 0}; + SerdBuffer buffer = {NULL, 0}; for (const char* 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 hi = hex_digit_value((const uint8_t)s[1]); const uint8_t lo = hex_digit_value((const uint8_t)s[2]); const char c = (char)((hi << 4U) | lo); - 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 751df38f..241e6fbf 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1256,21 +1256,21 @@ 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* const buf, const size_t len, void* const stream) { - SerdChunk* chunk = (SerdChunk*)stream; - char* new_buf = (char*)realloc((char*)chunk->buf, chunk->len + len); + SerdBuffer* buffer = (SerdBuffer*)stream; + char* new_buf = (char*)realloc((char*)buffer->buf, buffer->len + len); if (new_buf) { - memcpy(new_buf + chunk->len, buf, len); - chunk->buf = new_buf; - chunk->len += len; + memcpy(new_buf + buffer->len, buf, len); + buffer->buf = new_buf; + buffer->len += len; } return len; } char* -serd_chunk_sink_finish(SerdChunk* stream) +serd_buffer_sink_finish(SerdBuffer* const stream) { - serd_chunk_sink("", 1, stream); + serd_buffer_sink("", 1, stream); return (char*)stream->buf; } |