LMMS
Loading...
Searching...
No Matches
byte_sink.h
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#ifndef SERD_BYTE_SINK_H
18#define SERD_BYTE_SINK_H
19
20#include "serd_internal.h"
21#include "system.h"
22
23#include "serd/serd.h"
24
25#include <stddef.h>
26#include <stdint.h>
27#include <string.h>
28
36
37static inline SerdByteSink
39{
40 SerdByteSink bsink = {sink, stream, NULL, 0, block_size};
41
42 if (block_size > 1) {
44 }
45
46 return bsink;
47}
48
49static inline void
51{
52 if (bsink->block_size > 1 && bsink->size > 0) {
53 bsink->sink(bsink->buf, bsink->size, bsink->stream);
54 bsink->size = 0;
55 }
56}
57
58static inline void
60{
62 serd_free_aligned(bsink->buf);
63 bsink->buf = NULL;
64}
65
66static inline size_t
67serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink)
68{
69 if (len == 0) {
70 return 0;
71 }
72
73 if (bsink->block_size == 1) {
74 return bsink->sink(buf, len, bsink->stream);
75 }
76
77 const size_t orig_len = len;
78 while (len) {
79 const size_t space = bsink->block_size - bsink->size;
80 const size_t n = MIN(space, len);
81
82 // Write as much as possible into the remaining buffer space
83 memcpy(bsink->buf + bsink->size, buf, n);
84 bsink->size += n;
85 buf = (const uint8_t*)buf + n;
86 len -= n;
87
88 // Flush page if buffer is full
89 if (bsink->size == bsink->block_size) {
90 bsink->sink(bsink->buf, bsink->block_size, bsink->stream);
91 bsink->size = 0;
92 }
93 }
94
95 return orig_len;
96}
97
98#endif // SERD_BYTE_SINK_H
#define NULL
Definition CarlaBridgeFormat.cpp:30
static SerdByteSink serd_byte_sink_new(SerdSink sink, void *stream, size_t block_size)
Definition byte_sink.h:38
static size_t serd_byte_sink_write(const void *buf, size_t len, SerdByteSink *bsink)
Definition byte_sink.h:67
static void serd_byte_sink_flush(SerdByteSink *bsink)
Definition byte_sink.h:50
struct SerdByteSinkImpl SerdByteSink
static void serd_byte_sink_free(SerdByteSink *bsink)
Definition byte_sink.h:59
size_t(* SerdSink)(const void *SERD_NONNULL buf, size_t len, void *SERD_NONNULL stream)
Sink function for raw string output.
Definition serd.h:351
static size_t sink(const void *buf, size_t len, SerdWriter *writer)
Definition writer.c:177
unsigned char uint8_t
Definition mid.cpp:98
Definition byte_sink.h:29
size_t size
Definition byte_sink.h:33
size_t block_size
Definition byte_sink.h:34
void * stream
Definition byte_sink.h:31
SerdSink sink
Definition byte_sink.h:30
uint8_t * buf
Definition byte_sink.h:32
void * serd_allocate_buffer(const size_t size)
Allocate an aligned buffer for I/O.
Definition system.c:69
void serd_free_aligned(void *const ptr)
Free a buffer allocated with an aligned allocation function.
Definition system.c:75
static size_t block_size(const block_header_t *block)
Definition tlsf.c:173
int n
Definition crypt.c:458
memcpy(hh, h, RAND_HEAD_LEN)
#define MIN(a, b)
Definition unzpriv.h:2649