diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 52 | ||||
-rw-r--r-- | src/byte_sink.c | 10 | ||||
-rw-r--r-- | src/uri.c | 9 | ||||
-rw-r--r-- | src/writer.c | 28 |
4 files changed, 58 insertions, 41 deletions
diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 00000000..85ec09de --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,52 @@ +/* + Copyright 2011-2021 David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#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; +} diff --git a/src/byte_sink.c b/src/byte_sink.c index febd11cd..b5b4039e 100644 --- a/src/byte_sink.c +++ b/src/byte_sink.c @@ -30,21 +30,13 @@ # include <fcntl.h> #endif -static int -close_buffer(void* const stream) -{ - serd_buffer_sink("", 1, 1, stream); // Write null terminator - - return 0; -} - SerdByteSink* serd_byte_sink_new_buffer(SerdBuffer* const buffer) { assert(buffer); return serd_byte_sink_new_function( - serd_buffer_sink, close_buffer, buffer, 1u); + serd_buffer_write, serd_buffer_close, buffer, 1u); } SerdByteSink* @@ -60,22 +60,23 @@ 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 char code[3] = {*(s + 1), *(s + 2), 0}; const char c = (char)strtoul(code, NULL, 16); - 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 b7814158..305c72e1 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1436,31 +1436,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(buf); - assert(size == 1); - assert(stream); - - (void)size; - - SerdBuffer* buffer = (SerdBuffer*)stream; - buffer->buf = (char*)realloc(buffer->buf, buffer->len + nmemb); - memcpy((uint8_t*)buffer->buf + buffer->len, buf, nmemb); - buffer->len += nmemb; - return nmemb; -} - -char* -serd_buffer_sink_finish(SerdBuffer* const stream) -{ - assert(stream); - - serd_buffer_sink("", 1, 1, stream); - return (char*)stream->buf; -} |