@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe M.
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include "cescal/readbuf.h"
|
||||
|
||||
int
|
||||
readbuf_init(struct readbuf *rb)
|
||||
{
|
||||
if (rb == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rb->size = 0;
|
||||
rb->tail = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char
|
||||
readbuf_read(struct readbuf *rb, int fd)
|
||||
{
|
||||
ssize_t nread = -1;
|
||||
|
||||
if (rb == NULL || fd < 0) {
|
||||
errno = EINVAL;
|
||||
return '\0';
|
||||
}
|
||||
|
||||
#define N_RESIDUAL(rb) ((rb)->size - (rb)->tail)
|
||||
if (N_RESIDUAL(rb) == 0 || rb->size == 0) {
|
||||
if ((nread = read(fd, rb->buf, READBUF_CAP)) <= 0)
|
||||
return nread;
|
||||
|
||||
rb->size = nread;
|
||||
rb->tail = 0;
|
||||
}
|
||||
#undef N_RESIDUAL
|
||||
|
||||
return rb->buf[rb->tail++];
|
||||
}
|
||||
Reference in New Issue
Block a user