better and more predication, improved documentation
parent
b36f12a121
commit
558c6128fe
@ -0,0 +1,23 @@
|
|||||||
|
.TH buffer_get_token_pred 3
|
||||||
|
.SH NAME
|
||||||
|
buffer_get_token_pred \- read token from buffer
|
||||||
|
.SH SYNTAX
|
||||||
|
.B #include <buffer.h>
|
||||||
|
|
||||||
|
int \fBbuffer_get_token_pred\fP(buffer* \fIb\fR,char* \fIx\fR,unsigned int \fIlen\fR,
|
||||||
|
int (*\fIpredicate\fR)(const char* \fIs\fR,unsigned int \fIlen\fR));
|
||||||
|
.SH DESCRIPTION
|
||||||
|
buffer_get_token_pred copies data from \fIb\fR to \fIx\fR[0],
|
||||||
|
\fIx\fR[1], ..., \fIx\fR[\fIlen\fR-1] until \fIlen\fR bytes have been
|
||||||
|
read or \fIpredicate\fR called on the destination string returns
|
||||||
|
nonzero.
|
||||||
|
|
||||||
|
\fIpredicate\fR can also return 0 (indicating further input is required
|
||||||
|
to complete the token) or -1 (abort and return -1; use this if
|
||||||
|
\fIpredicate\fR wants to enfore a maximum message size or does timeout
|
||||||
|
handling or detects a malformed message).
|
||||||
|
|
||||||
|
buffer_get_token_pred returns the number of bytes copied or -1 on
|
||||||
|
\fIerrno\fR (setting errno appropriately).
|
||||||
|
.SH "SEE ALSO"
|
||||||
|
buffer_init(3), buffer_feed(3), buffer_peek(3), buffer_seek(3), buffer(3)
|
@ -0,0 +1,16 @@
|
|||||||
|
#include "byte.h"
|
||||||
|
#include "buffer.h"
|
||||||
|
#include "scan.h"
|
||||||
|
|
||||||
|
int buffer_get_token_pred(buffer* b,char* x,unsigned int len,string_predicate p) {
|
||||||
|
int blen;
|
||||||
|
|
||||||
|
for (blen=0;blen<len;++blen) {
|
||||||
|
register int r;
|
||||||
|
if ((r=buffer_getc(b,x))<0) return r;
|
||||||
|
if (r==0) break;
|
||||||
|
if (p(x-blen,blen+1)) break;
|
||||||
|
++x;
|
||||||
|
}
|
||||||
|
return blen;
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
.TH buffer_get_token_sa_pred 3
|
||||||
|
.SH NAME
|
||||||
|
buffer_get_token_sa_pred \- read token from buffer
|
||||||
|
.SH SYNTAX
|
||||||
|
.nf
|
||||||
|
.B #include <stralloc.h>
|
||||||
|
.B #include <buffer.h>
|
||||||
|
|
||||||
|
int \fBbuffer_get_token_sa_pred\fP(buffer* \fIb\fR,stralloc* \fIsa\fR,
|
||||||
|
int (*\fIpredicate\fR)(stralloc* * \fIsa\fR));
|
||||||
|
.SH DESCRIPTION
|
||||||
|
buffer_get_token_sa_pred appends data from the \fIb\fR to \fIsa\fR until
|
||||||
|
\fIpredicate\fR(\fIsa\fR) returns 1 or -1.
|
||||||
|
|
||||||
|
If \fIpredicate\fR returns 1 once a '\\n' was read, that new-line
|
||||||
|
character is still appended to \fIsa\fR -- use stralloc_chop or
|
||||||
|
stralloc_chomp to get rid of it. \fIpredicate\fR can also return 0
|
||||||
|
(indicating further input is required to complete the token) or -1
|
||||||
|
(abort and return -1; use this if \fIpredicate\fR wants to enfore a
|
||||||
|
maximum message size or does timeout handling or detects a malformed
|
||||||
|
message).
|
||||||
|
|
||||||
|
If reading from the buffer or allocating memory fails,
|
||||||
|
buffer_get_token_sa_pred returns -1 and sets \fIerrno\fR appropriately. At
|
||||||
|
that point \fIsa\fR may already contain a partial token.
|
||||||
|
|
||||||
|
On success, buffer_get_token_sa_pred returns 0.
|
||||||
|
.SH RATIONALE
|
||||||
|
buffer_get_token_sa_pred appends instead of overwriting so it can be
|
||||||
|
used on non-blocking sockets (these signal error and set \fIerrno\fR to
|
||||||
|
EAGAIN; in this case you can simply call buffer_get_token_sa again when
|
||||||
|
\fBselect\fR or \fBpoll\fR indicate more data is available).
|
||||||
|
.SH "SEE ALSO"
|
||||||
|
buffer_getline_sa(3), buffer_get_token(3), buffer(3)
|
@ -0,0 +1,21 @@
|
|||||||
|
#include "byte.h"
|
||||||
|
#include "stralloc.h"
|
||||||
|
#include "buffer.h"
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p) {
|
||||||
|
for (;;) {
|
||||||
|
char x;
|
||||||
|
if (!stralloc_readyplus(sa,1)) return -1;
|
||||||
|
switch (buffer_getc(b,&x)) {
|
||||||
|
case -1: return -1;
|
||||||
|
case 0: return 0;
|
||||||
|
}
|
||||||
|
stralloc_append(sa,&x);
|
||||||
|
switch (p(sa)) {
|
||||||
|
case -1: return -1;
|
||||||
|
case 0: break;
|
||||||
|
case 1: return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue