diff --git a/buffer.h b/buffer.h index 7432ba8..48da797 100644 --- a/buffer.h +++ b/buffer.h @@ -125,6 +125,7 @@ __writememsz__(2,3) ssize_t buffer_get(buffer* b,char* x,size_t len); ssize_t buffer_feed(buffer* b); ssize_t buffer_getc(buffer* b,char* x); +ssize_t buffer_peekc(buffer* b,char* x); __writememsz__(2,3) ssize_t buffer_getn(buffer* b,char* x,size_t len); diff --git a/buffer/buffer_peekc.3 b/buffer/buffer_peekc.3 new file mode 100644 index 0000000..35e56b1 --- /dev/null +++ b/buffer/buffer_peekc.3 @@ -0,0 +1,17 @@ +.TH buffer_peekc 3 +.SH NAME +buffer_peekc \- read one char from buffer +.SH SYNTAX +.B #include + +int \fBbuffer_peekc\fP(buffer* \fIb\fR,char* \fIx\fR); +.SH DESCRIPTION +buffer_peekc(b,x) is like buffer_get(b,x,1) but without +advancing the buffer pointer. If you call it again, or call +\fIbuffer_getc\fR after it, you will get the same data again. + +.SH "RETURN VALUE" +\fIbuffer_peekc\fR returns 1 on success, 0 when at the end of the file, +or -1 if there was an I/O error (setting \fIerrno\fR appropriately). +.SH "SEE ALSO" +buffer_init(3), buffer_get(3), buffer_getc(3), buffer(3) diff --git a/buffer/buffer_peekc.c b/buffer/buffer_peekc.c new file mode 100644 index 0000000..9935bcd --- /dev/null +++ b/buffer/buffer_peekc.c @@ -0,0 +1,11 @@ +#include "byte.h" +#include "buffer.h" + +ssize_t buffer_peekc(buffer* b,char* x) { + if (b->p==b->n) { + register ssize_t blen; + if ((blen=buffer_feed(b))<=0) return blen; + } + *x=b->x[b->p]; + return 1; +}