Update README.md
[puttycyg-ng.git] / windows / cthelper / buffer.h
blob8df562d6bcfdc7324251383f4d99ecbbe9c261da
1 #ifndef BUFFER_H
2 #define BUFFER_H
4 #include <sys/types.h>
5 #include <alloca.h>
7 typedef struct buffer_tag *Buffer;
8 struct buffer_tag {
9 size_t avail, len;
10 unsigned char data[1];
13 /* Initialize a Buffer */
14 Buffer buffer_init(size_t size);
16 /* Free a buffer; sets *pb to NULL */
17 void buffer_free(Buffer *pb);
19 /* Initialize a Buffer with alloca() */
20 #define BUFFER_ALLOCA(b,s) do{\
21 b = alloca(sizeof(b)+(s)); b->avail = s; b->len = 0;\
22 }while(0)
24 /* Returns true if buffer is full */
25 #define buffer_isfull(b) ((b)->avail == 0)
27 /* Returns true if buffer is empty */
28 #define buffer_isempty(b) ((b)->len == 0)
30 /* Reads up to buffer_avail(b) bytes from descriptor `des' */
31 ssize_t buffer_read(Buffer b, int des);
33 /* Writes as many bytes in the buffer as possible to descriptor `des' */
34 ssize_t buffer_write(Buffer b, int des);
36 /* Appends data from data to end of buffer returning number of bytes
37 * appended (which may be less than `len') */
38 size_t buffer_append(Buffer b, const char *data, size_t len);
40 /* Clears len bytes from start of buffer, returning number of bytes removed */
41 size_t buffer_consumed(Buffer b, size_t len);
43 #endif /* BUFFER_H */