1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include "parse.h"
unsigned char bs_peek(struct bytestream* bs) {
unsigned char r;
char c;
if (bs->cur>=bs->max) { // EOF or already error state?
bs->max=0; // signal error
bs->cur=1;
return 0; // return 0
}
switch (bs->type) {
case MEMBUF:
r=bs->u.base[bs->cur];
break;
case IOBUF:
{
int ret=buffer_peekc(bs->u.b, &c);
if (ret==1) {
r=c;
} else {
bs->max=0;
bs->cur=1;
return 0;
}
}
break;
case BSTREAM:
r=bs_peek(bs->u.bs);
break;
default:
r=0; // cannot happen
}
return r;
}
#ifdef UNITTEST
#include <assert.h>
#undef UNITTEST
#include "buffer/bs_err.c"
#include "buffer/bs_get.c"
#include "buffer/buffer_init_staticcontents.c"
#include "buffer/bs_init_iobuf.c"
#include "buffer/bs_init_iobuf_size.c"
#include "buffer/buffer_peekc.c"
#include "buffer/buffer_getc.c"
#include "buffer/buffer_feed.c"
#include "buffer/buffer_stubborn2.c"
int main() {
struct bytestream bs = BS_FROM_MEMBUF("fx", 1);
/* first test: membuf.
* See if we get all the bytes we put in and then error is signaled */
assert(bs_peek(&bs) == 'f');
assert(bs_peek(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_get(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_peek(&bs) == 0);
assert(bs_err(&bs));
/* second test: iobuf with no limit. Otherwise the same. */
struct buffer b;
buffer_init_staticcontents(&b, "fx", 1);
bs_init_iobuf(&bs, &b);
assert(bs_peek(&bs) == 'f');
assert(bs_peek(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_get(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_peek(&bs) == 0);
assert(bs_err(&bs));
/* third test: iobuf with limit. Otherwise the same. */
buffer_init_staticcontents(&b, "fx", 2);
bs_init_iobuf_size(&bs, &b, 1);
assert(bs_peek(&bs) == 'f');
assert(bs_peek(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_get(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_peek(&bs) == 0);
assert(bs_err(&bs));
/* fourth test: iobuf with EOF */
buffer_init_staticcontents(&b, "fx", 1);
bs_init_iobuf(&bs, &b); // bytestream has no limit but will hit EOF in backing buffer
assert(bs_peek(&bs) == 'f');
assert(bs_peek(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_get(&bs) == 'f');
assert(!bs_err(&bs));
assert(bs_peek(&bs) == 0);
assert(bs_err(&bs));
return 0;
}
#endif
|