mcchunktools
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
buffer.h
Go to the documentation of this file.
1 /*
2  * -----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * Lukas Niederbremer <webmaster@flippeh.de> and Clark Gaebel <cg.wowus.cg@gmail.com>
5  * wrote this file. As long as you retain this notice you can do whatever you
6  * want with this stuff. If we meet some day, and you think this stuff is worth
7  * it, you can buy us a beer in return.
8  * -----------------------------------------------------------------------------
9  */
10 #ifndef NBT_BUFFER_H
11 #define NBT_BUFFER_H
12 
13 #include <stddef.h>
14 
15 /*
16  * A buffer is 'unlimited' storage for raw data. As long as buffer_append is
17  * used to add data, it will automatically resize to make room. To read the
18  * data, just access `data' directly.
19  */
20 struct buffer {
21  unsigned char* data; /* You can access the buffer's raw bytes through this pointer */
22  size_t len; /* Only accesses in the interval [data, data + len) are defined */
23  size_t cap; /* Internal use. The allocated size of the buffer. */
24 };
25 
26 /*
27  * Initialize a buffer with this macro.
28  *
29  * Usage:
30  * struct buffer b = BUFFER_INIT;
31  * OR
32  * struct buffer b;
33  * b = BUFFER_INIT;
34  */
35 #define BUFFER_INIT (struct buffer) { NULL, 0, 0 }
36 
37 /*
38  * Frees all memory associated with the buffer. The same buffer may be freed
39  * multiple times without consequence.
40  */
41 void buffer_free(struct buffer* b);
42 
43 /*
44  * Ensures there's enough room in the buffer for at least `reserved_amount'
45  * bytes. Returns non-zero on failure. If such a failure occurs, the buffer
46  * is deallocated and set to one which can be passed to buffer_free. Any other
47  * usage is undefined.
48  */
49 int buffer_reserve(struct buffer* b, size_t reserved_amount);
50 
51 /*
52  * Copies `n' bytes from `data' into the buffer. Returns non-zero if an
53  * out-of-memory failure occured. If such a failure occurs, further usage of the
54  * buffer results in undefined behavior.
55  */
56 int buffer_append(struct buffer* b, const void* data, size_t n);
57 
58 #endif
size_t cap
Definition: buffer.h:23
void buffer_free(struct buffer *b)
int buffer_reserve(struct buffer *b, size_t reserved_amount)
int buffer_append(struct buffer *b, const void *data, size_t n)
size_t len
Definition: buffer.h:22
Definition: buffer.h:20
unsigned char * data
Definition: buffer.h:21