Skip to content

buffer.3

Manvendra Bhangui edited this page Feb 25, 2024 · 1 revision

NAME

buffer - generic read/write buffering

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_get copies data to x[0], x[1], ..., x[len-1] from the beginning of a string stored in preallocated space; removes these len bytes from the string; and returns len.

If, however, the string has fewer than len (but more than 0) bytes, buffer_get copies only that many bytes, and returns that number.

If the string is empty, buffer_get first uses a read operation to feed data into the string. The read operation may indicate end of input.

The preallocated space and the read operation are specified by b. You must initialize b using buffer_init before calling buffer_get (or use the pre-initialized buffer_0).

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.

MACROS

Apart from this basic usage, some helpful macro definitions are provided: BUFFER_INIT(op,fd,buf,len) uses op function to operate (read/write) on buf with len to file descriptor fd. buffer_GETC(buf,c) returns the upmost position of character c within buffer buf or 0. buffer_PUTC(buf,c) adds character c to buffer buf.

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.

REFERENCES

https://cr.yp.to/lib/buffer_get.html

https://cr.yp.to/lib/buffer_put.html

SEE ALSO

stdio(3)

Clone this wiki locally