SYNTAX
#include "buffer.h"
buffer* buffer_0; /* like stdio's stdin */
buffer* buffer_1; /* like stdio's stdout */
buffer* buffer_2; /* like stdio's stderr */
void buffer_init(buffer &b,ssize_t (*op)(int,char*,size_t),
int fd, char *y, size_t ylen);
ssize_t buffer_get(buffer* b,char *x,size_t len);
int buffer_put(buffer* b,const char *x,size_t len);
int buffer_puts(buffer* b,const char *x);
int buffer_putalign(buffer* b,char *x,unsigned int len);
int buffer_putsalign(buffer* b,char *x);
int buffer_putflush(buffer* b,char *x,unsigned int len);
int buffer_putsflush(buffer* b,char *x);
int buffer_flush(buffer* b);
int buffer_copy(buffer* bo,buffer* bi);
int buffer_unixread(int fd,char* buf,size_t len);
int buffer_unixwrite(int fd,char* buf,size_t len);
DESCRIPTION
buffer.h describes a generic buffer interface that can be used for read
and write buffering. Buffers must be initialized with buffer_init.
A buffer can only be used for reading or writing at the same time, not
both.
Unlike stdio, these write buffers are not flushed automatically at
program termination; you must manually call buffer_flush,
buffer_putflush, or buffer_putsflush.
buffer_init prepares b to store a string in y[0], y[1], ..., y[ylen-1].
Initially the string is empty.
buffer_init also prepares b to use the read/write operation specified
by op and fd.
You can use
buffer b = BUFFER_INIT(op,fd,y,ylen);
to initialize b statically if op, fd, y, and ylen are compile-time
constants.
You can call buffer_init again at any time. Note that this discards the
currently buffered string.
buffer_put writes len bytes from x to b.
The difference to buffer_putalign is that, when there isn't enough
space for new data, buffer_put calls buffer_flush before copying any
data, while buffer_putalign fills all available space with data before
calling buffer_flush.
buffer_copy copies one buffer to other one. The output buffer needs to
have at least the preallocated size of the input buffer.
buffer_unixread and buffer_unixwrite perform the same operation like
standard Unix read or write.
EXAMPLE
#include <buffer.h>
#include <open.h>
char buf[BUFFER_INSIZE]; // defined in buffer.h
int fd = open_read("/etc/services");
buffer input;
if (fd >= 0) {
char x;
buffer_init(&input,read,fd,buf,sizeof buf);
while (buffer_get(&input,&x,1) == 1) {
buffer_put(buffer_1,&x,1);
if (x == '\n') break;
}
buffer_flush(buffer_1);
}
RETURN CODES
buffer_put and buffer_get return 0 if everything was fine, -1, on error
(setting errno). buffer_copy returns -2, if the input buffer can't be
read, and -3, if the data can't successfully copied to the output
buffer. On success buffer_copy returns 0.
REFRENCES
https://cr.yp.to/lib/buffer_get.html
https://cr.yp.to/lib/buffer_put.html
SEE ALSO
stdio(3)
buffer(3)
Man(1) output converted with
man2html