/* This file is part of limb https://lila.oss/limb
* Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/buffer-shldata.h>
#include <limb/shldata.h>
#include <limb/siovec.h>
#include "buffer-shldata.h"
int
buffer_shldata_getfull(buffer *b, shldata_getfull fn, void *args,
stralloc *sa, buffer_shldata_ctx *ctx)
{
struct iovec v[2];
ssize_t o;
size_t salen, l;
salen = sa->len;
for(;;) {
buffer_rpeek(b, v);
/* make sure we can put the whole buffer into our sa */
if (!stralloc_readyplus(sa, siov_len(v, 2)))
return (sa->len = salen, 0);
/* and gather it */
l = siov_gather(sa->s + sa->len, sa->a - sa->len, v, 2);
/* try the getfull fn() */
o = fn(sa->s + salen, sa->len - salen + l, args, ctx);
/* in case of ENODATA we "appropriate" the buffer's content & refill */
if (o < 0 && errno == ENODATA) {
sa->len += l;
buffer_rseek(b, l);
o = buffer_fill(b);
if (o > 0)
continue;
}
break;
}
if (o <= 0) {
sa->len = salen;
return (!o) ? (errno = EPIPE, 0) : 0;
}
buffer_rseek(b, o - (sa->len - salen));
sa->len = salen;
return 1;
}