Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / buffer.h / buffer_putescall.c

/* 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.h>
#include <limb/esc.h>

int
buffer_putescall(buffer *b, const char *s, size_t len, size_t *w, size_t *r)
{
    if (*r > len) return (errno = EINVAL, 0);

    *w = 0;
    while (*r < len) {
        char buf[64];
        size_t realw, realr;

        realr = *r;
        realw = 0;
        escall_fmt(buf, sizeof(buf), s, len, &realw, &realr);
        for (size_t p = 0; ; ) {
            p += buffer_putnoflush(b, buf + p, realw - p);
            if (p == realw)
                break;
            buffer_flush(b);
            if (buffer_isfull(b)) {
                buffer_unput(b, p);
                return 0;
            }
        }
        *r = realr;
        *w += realw;
    }
    return 1;
}