Files
adsb-tools/common.c
2016-02-14 20:35:43 +00:00

36 lines
668 B
C

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
ssize_t buf_fill(struct buf *buf, int fd) {
if (buf->start + buf->length == BUF_LEN) {
assert(buf->start > 0);
memmove(buf->buf, buf_at(buf, 0), buf->length);
buf->start = 0;
}
size_t space = BUF_LEN - buf->length - buf->start;
ssize_t in = read(fd, buf_at(buf, buf->length), space);
if (in < 0) {
return in;
}
buf->length += 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;
}
}