38 lines
714 B
C
38 lines
714 B
C
|
|
#include <assert.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#include "buf.h"
|
||
|
|
|
||
|
|
void buf_init(struct buf *buf) {
|
||
|
|
buf->start = 0;
|
||
|
|
buf->length = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
ssize_t buf_fill(struct buf *buf, int fd) {
|
||
|
|
if (buf->start + buf->length == BUF_LEN_MAX) {
|
||
|
|
assert(buf->start > 0);
|
||
|
|
memmove(buf->buf, buf_at(buf, 0), buf->length);
|
||
|
|
buf->start = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
size_t space = BUF_LEN_MAX - buf->length - buf->start;
|
||
|
|
ssize_t in = read(fd, buf_at(buf, buf->length), space);
|
||
|
|
if (in <= 0) {
|
||
|
|
return in;
|
||
|
|
}
|
||
|
|
buf->length += (size_t) in;
|
||
|
|
return in;
|
||
|
|
}
|
||
|
|
|
||
|
|
void buf_consume(struct buf *buf, size_t length) {
|
||
|
|
assert(buf->length >= length);
|
||
|
|
|
||
|
|
buf->length -= length;
|
||
|
|
if (buf->length) {
|
||
|
|
buf->start += length;
|
||
|
|
} else {
|
||
|
|
buf->start = 0;
|
||
|
|
}
|
||
|
|
}
|