standalone: update lua to version 5.3.4
[vis.git] / buffer.h
blob55ebb7d5fe3cea161a049556ec65d22b429ed6e9
1 #ifndef BUFFER_H
2 #define BUFFER_H
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include "text.h"
8 /* a dynamically growing buffer storing arbitrary data, used for registers/macros */
9 typedef struct {
10 char *data; /* NULL if empty */
11 size_t len; /* current length of data */
12 size_t size; /* maximal capacity of the buffer */
13 } Buffer;
15 /* initalize a (stack allocated) Buffer instance */
16 void buffer_init(Buffer*);
17 /* release/free all data stored in this buffer, reset size to zero */
18 void buffer_release(Buffer*);
19 /* set buffer size to zero, keep allocated memory */
20 void buffer_clear(Buffer*);
21 /* reserve space to store at least size bytes in this buffer.*/
22 bool buffer_grow(Buffer*, size_t size);
23 /* if buffer is not empty, make sure it is NUL terminated */
24 bool buffer_terminate(Buffer*);
25 /* replace buffer content with given data, growing the buffer if needed */
26 bool buffer_put(Buffer*, const void *data, size_t len);
27 /* same but with NUL-terminated data */
28 bool buffer_put0(Buffer*, const char *data);
29 /* remove len bytes from the buffer starting at pos */
30 bool buffer_remove(Buffer*, size_t pos, size_t len);
31 /* insert arbitrary data of length len at pos (in [0, buf->len]) */
32 bool buffer_insert(Buffer*, size_t pos, const void *data, size_t len);
33 /* insert NUL-terminate data at pos (in [0, buf->len]) */
34 bool buffer_insert0(Buffer*, size_t pos, const char *data);
35 /* append futher content to the end of the buffer data */
36 bool buffer_append(Buffer*, const void *data, size_t len);
37 /* append NUl-terminated data */
38 bool buffer_append0(Buffer*, const char *data);
39 /* insert new data at the start of the buffer */
40 bool buffer_prepend(Buffer*, const void *data, size_t len);
41 /* prepend NUL-terminated data */
42 bool buffer_prepend0(Buffer*, const char *data);
43 /* set formatted buffer content, ensures NUL termination on success */
44 bool buffer_printf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
45 /* append formatted buffer content, ensures NUL termination on success */
46 bool buffer_appendf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
47 /* return length of a buffer without trailing NUL byte */
48 size_t buffer_length0(Buffer*);
49 /* return length of a buffer including possible NUL byte */
50 size_t buffer_length(Buffer*);
51 /* return current maximal capacity in bytes of this buffer */
52 size_t buffer_capacity(Buffer*);
53 /* pointer to buffer data, guaranteed to return a NUL terminated
54 * string even if buffer is empty */
55 const char *buffer_content0(Buffer*);
56 /* pointer to buffer data, might be NULL if empty, might not be NUL terminated */
57 const char *buffer_content(Buffer*);
58 /* steal underlying buffer data, caller is responsible to free(3) it,
59 * similar to move semantics in some programming languages */
60 char *buffer_move(Buffer*);
62 #endif