Read token from buffer
#include <stralloc.h> #include <buffer.h> int buffer_get_token_sa_pred(buffer* b,stralloc* sa, int (*predicate)(stralloc* * sa));
buffer_get_token_sa_pred appends data from b to sa until predicate(sa) returns 1 or -1.
If predicate returns 1 once a '\n' was read, that new-line character is still appended to sa -- use stralloc_chop or stralloc_chomp to get rid of it. predicate can also return 0 (indicating further input is required to complete the token) or -1 (abort and return -1; use this if predicate 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 errno appropriately. At that point sa may already contain a partial token.
On success, buffer_get_token_sa_pred returns 0.
buffer_get_token_sa_pred appends instead of overwriting so it can be used on non-blocking sockets (these signal error and set errno to EAGAIN; in this case you can simply call buffer_get_token_sa again when select or poll indicate more data is available).