diff options
author | David Robillard <d@drobilla.net> | 2023-05-04 14:58:45 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 551faf54e35c757144204bf7a7949c0f7d0a20a3 (patch) | |
tree | e8f761fc6361c75dd443d6e91806d27025d4102a /src | |
parent | c3bc111f7fb2a44e068e5250f7823352e44c76dc (diff) | |
download | serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.gz serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.tar.bz2 serd-551faf54e35c757144204bf7a7949c0f7d0a20a3.zip |
Make SerdBuffer an output stream
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 39 | ||||
-rw-r--r-- | src/node.c | 6 | ||||
-rw-r--r-- | src/uri.c | 9 | ||||
-rw-r--r-- | src/writer.c | 27 |
4 files changed, 47 insertions, 34 deletions
diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 00000000..12f343c2 --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,39 @@ +// Copyright 2011-2021 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "serd/serd.h" + +#include <assert.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +size_t +serd_buffer_write(const void* const buf, + const size_t size, + const size_t nmemb, + void* const stream) +{ + assert(buf); + assert(stream); + + SerdBuffer* const buffer = (SerdBuffer*)stream; + const size_t n_bytes = size * nmemb; + + char* const new_buf = (char*)realloc(buffer->buf, buffer->len + n_bytes); + if (new_buf) { + memcpy(new_buf + buffer->len, buf, n_bytes); + buffer->buf = new_buf; + buffer->len += nmemb; + } + + return new_buf ? nmemb : 0; +} + +int +serd_buffer_close(void* const stream) +{ + serd_buffer_write("", 1, 1, stream); // Write null terminator + + return 0; +} @@ -532,11 +532,11 @@ serd_new_file_uri(const SerdStringView path, const SerdStringView hostname) { SerdBuffer buffer = {NULL, 0U}; - serd_write_file_uri(path, hostname, serd_buffer_sink, &buffer); - serd_buffer_sink_finish(&buffer); + serd_write_file_uri(path, hostname, serd_buffer_write, &buffer); + serd_buffer_close(&buffer); const size_t length = buffer.len; - const char* const string = serd_buffer_sink_finish(&buffer); + const char* const string = (char*)buffer.buf; SerdNode* const node = serd_new_string(serd_substring(string, length)); free(buffer.buf); @@ -51,23 +51,24 @@ serd_parse_file_uri(const char* const uri, char** const hostname) for (const char* s = path; *s; ++s) { if (*s == '%') { if (*(s + 1) == '%') { - serd_buffer_sink("%", 1, 1, &buffer); + serd_buffer_write("%", 1, 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_buffer_sink(&c, 1, 1, &buffer); + serd_buffer_write(&c, 1, 1, &buffer); s += 2; } else { s += 2; // Junk escape, ignore } } else { - serd_buffer_sink(s, 1, 1, &buffer); + serd_buffer_write(s, 1, 1, &buffer); } } - return serd_buffer_sink_finish(&buffer); + serd_buffer_close(&buffer); + return (char*)buffer.buf; } /// RFC3986: scheme ::= ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) diff --git a/src/writer.c b/src/writer.c index 482721f9..c2008e2a 100644 --- a/src/writer.c +++ b/src/writer.c @@ -14,7 +14,6 @@ #include "world.h" #include "serd/attributes.h" -#include "serd/buffer.h" #include "serd/env.h" #include "serd/event.h" #include "serd/node.h" @@ -1490,29 +1489,3 @@ serd_writer_sink(SerdWriter* writer) assert(writer); return &writer->iface; } - -size_t -serd_buffer_sink(const void* const buf, - const size_t size, - const size_t nmemb, - void* const stream) -{ - assert(size == 1); - (void)size; - - SerdBuffer* buffer = (SerdBuffer*)stream; - char* new_buf = (char*)realloc(buffer->buf, buffer->len + nmemb); - if (new_buf) { - memcpy(new_buf + buffer->len, buf, nmemb); - buffer->buf = new_buf; - buffer->len += nmemb; - } - return nmemb; -} - -char* -serd_buffer_sink_finish(SerdBuffer* const stream) -{ - serd_buffer_sink("", 1, 1, stream); - return (char*)stream->buf; -} |