Mercurial > libjeffpc > experimental
changeset 985:013adc5c54dd
buffer: add function to copy between two buffers
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Wed, 02 Sep 2020 21:21:18 -0400 |
parents | 8421a95e0d89 |
children | 4e86cf312d6c |
files | buffer.c include/jeffpc/buffer.h mapfile-vers |
diffstat | 3 files changed, 31 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/buffer.c Tue Sep 15 20:36:58 2020 -0400 +++ b/buffer.c Wed Sep 02 21:21:18 2020 -0400 @@ -157,6 +157,34 @@ return 0; } +int buffer_copy(struct buffer *dst, struct buffer *src, size_t len) +{ + /* + * TODO: we could make this more efficient by growing the + * destination buffer only once + */ + + while (len) { + uint8_t tmp[4096]; + ssize_t ret; + + /* read in a bunch */ + ret = buffer_read(src, tmp, MIN(sizeof(tmp), len)); + if (ret < 0) + return ret; + + /* decrement now, so we can reuse ret below */ + len -= ret; + + /* and append whatever we read */ + ret = buffer_append(dst, tmp, ret); + if (ret) + return ret; + } + + return 0; +} + ssize_t buffer_seek(struct buffer *buffer, off_t offset, int whence) { size_t newoff;
--- a/include/jeffpc/buffer.h Tue Sep 15 20:36:58 2020 -0400 +++ b/include/jeffpc/buffer.h Wed Sep 02 21:21:18 2020 -0400 @@ -80,6 +80,8 @@ extern void buffer_init_stdio(struct buffer *buffer, FILE *f); extern int buffer_append(struct buffer *buffer, const void *data, size_t size); +/* append specified number of bytes from src to dst */ +extern int buffer_copy(struct buffer *dst, struct buffer *src, size_t len); extern ssize_t buffer_seek(struct buffer *buffer, off_t offset, int whence); extern int buffer_truncate(struct buffer *buffer, size_t size); extern ssize_t buffer_pread(struct buffer *buffer, void *data, size_t len,