add functions for capacity checking and to skip bytes

master
leitner 4 years ago
parent 03feeb7ef2
commit 302026f1b2

@ -0,0 +1,22 @@
#include "parse.h"
size_t bs_capacityleft(struct bytestream* bs) {
if (bs->cur>=bs->max) // if EOF or error, return 0
return 0;
return bs->max - bs->cur;
}
#ifdef UNITTEST
#include <assert.h>
int main() {
struct bytestream bs;
bs.cur=0; bs.max=100;
assert(bs_capacityleft(&bs) == 100);
bs.cur=1;
assert(bs_capacityleft(&bs) == 99);
bs.max=0;
assert(bs_capacityleft(&bs) == 0);
}
#endif

@ -0,0 +1,9 @@
#include "parse.h"
size_t bs_consumeleftovers(struct bytestream* bs) {
if (bs->cur >= bs->max)
return 0;
return bs_skip(bs, bs->max - bs->cur);
}
/* bs_skip unit tests cover this too */

@ -0,0 +1,34 @@
#include "parse.h"
int bs_nomoredataassert(struct bytestream* bs) {
if (bs->cur == bs->max)
return 1;
bs->cur = 1; /* otherwise set error state */
bs->max = 0;
return 0;
}
#ifdef UNITTEST
#include <assert.h>
#undef UNITTEST
#include "buffer/bs_err.c"
int main() {
struct bytestream bs;
bs.cur=0; bs.max=100; // full buffer
assert(bs_nomoredataassert(&bs) == 0);
assert(bs_err(&bs));
bs.cur=0; bs.max=0; // empty buffer
assert(bs_nomoredataassert(&bs) == 1);
assert(bs_err(&bs) == 0);
bs.cur=1; bs.max=0; // error state buffer
assert(bs_nomoredataassert(&bs) == 0);
assert(bs_err(&bs));
return 0;
}
#endif

@ -0,0 +1,104 @@
#include "parse.h"
size_t bs_skip(struct bytestream* bs, size_t len) {
size_t n;
if (bs->cur >= bs->max)
return 0;
n = bs->max - bs->cur;
if (len > n) {
bs->max = 0; // set error
bs->cur = 1;
return 0;
}
switch (bs->type) {
case MEMBUF:
bs->cur += len;
break;
case IOBUF:
return buffer_seek(bs->u.b, len);
case BSTREAM:
return bs_skip(bs->u.bs, len);
}
return len;
}
#ifdef UNITTEST
#include <assert.h>
#include <signal.h>
#undef UNITTEST
#include "buffer/bs_init_membuf.c"
#include "buffer/bs_err.c"
#include "buffer/bs_get.c"
#include "buffer/buffer_getc.c"
#include "buffer/buffer_seek.c"
#include "buffer/buffer_feed.c"
#include "buffer/buffer_stubborn2.c"
#include "buffer/bs_init_iobuf.c"
#include "open/open_read.c"
#include "buffer/prs_readblob.c"
#include "buffer/buffer_get.c"
#include "buffer/bs_capacityassert.c"
#include "byte/byte_copy.c"
#include "buffer/buffer_init.c"
int main() {
struct bytestream bs;
// test basic functionality inside buffer with known length
bs_init_membuf(&bs, "fnord\n", 7);
assert(bs_skip(&bs, 0) == 0);
assert(bs_err(&bs) == 0);
assert(bs_skip(&bs, 5) == 5);
assert(bs_err(&bs) == 0);
assert(bs_get(&bs) == '\n');
assert(bs_skip(&bs, 100) == 0);
assert(bs_err(&bs));
// test basic functionality when the backend is a file
int fd = open_read("GNUmakefile");
assert(fd != -1);
unsigned char iobuf[42];
buffer b = BUFFER_INIT(read, fd, iobuf+32, 10);
bs_init_iobuf(&bs, &b);
assert(bs_skip(&bs, 100) == 100);
assert(prs_readblob(&bs, iobuf, 10) == 10);
assert(pread(fd, iobuf+10, 10, 100) == 10);
assert(!memcmp(iobuf, iobuf+10, 10));
// test basic functionality when backend is non-seekable file
int pipes[2];
assert(pipe(pipes)==0);
int r=fork();
assert(r!=-1);
if (r==0) {
lseek(fd, 0, SEEK_SET);
for (;;) {
r=read(fd, iobuf, 32);
if (r <= 0) return 0;
write(pipes[1], iobuf, 32);
}
}
buffer_init(&b, read, pipes[0], iobuf+32, 10);
bs_init_iobuf(&bs, &b);
assert(bs_skip(&bs, 20) == 20);
assert(prs_readblob(&bs, iobuf, 10) == 10);
assert(pread(fd, iobuf+10, 10, 20) == 10);
assert(!memcmp(iobuf, iobuf+10, 10));
kill(r, 15);
close(fd);
}
#endif

@ -105,6 +105,9 @@ int bs_nomoredataassert(struct bytestream* bs);
/* Return number of bytes consumed */ /* Return number of bytes consumed */
size_t bs_consumeleftovers(struct bytestream* bs); size_t bs_consumeleftovers(struct bytestream* bs);
/* Skip n bytes, return number skipped (or 0 if error) */
size_t bs_skip(struct bytestream* bs, size_t n);
/* Read n bytes from stream. Return n. /* Read n bytes from stream. Return n.
* Set stream to error state if not enough space or I/O error. */ * Set stream to error state if not enough space or I/O error. */
ssize_t prs_readblob(struct bytestream* bs,unsigned char* dest,size_t destlen); ssize_t prs_readblob(struct bytestream* bs,unsigned char* dest,size_t destlen);

Loading…
Cancel
Save