vis: reject invalid register name when recording a macro
[vis.git] / buffer.h
blob1730b05f013b77745517e452710e5c44e1c03ac2
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_reserve(Buffer*, size_t size);
23 /* reserve space for at least `len` more bytes in this buffer */
24 bool buffer_grow(Buffer*, size_t len);
25 /* if buffer is not empty, make sure it is NUL terminated */
26 bool buffer_terminate(Buffer*);
27 /* replace buffer content with given data, growing the buffer if needed */
28 bool buffer_put(Buffer*, const void *data, size_t len);
29 /* same but with NUL-terminated data */
30 bool buffer_put0(Buffer*, const char *data);
31 /* remove len bytes from the buffer starting at pos */
32 bool buffer_remove(Buffer*, size_t pos, size_t len);
33 /* insert arbitrary data of length len at pos (in [0, buf->len]) */
34 bool buffer_insert(Buffer*, size_t pos, const void *data, size_t len);
35 /* insert NUL-terminate data at pos (in [0, buf->len]) */
36 bool buffer_insert0(Buffer*, size_t pos, const char *data);
37 /* append futher content to the end of the buffer data */
38 bool buffer_append(Buffer*, const void *data, size_t len);
39 /* append NUl-terminated data */
40 bool buffer_append0(Buffer*, const char *data);
41 /* insert new data at the start of the buffer */
42 bool buffer_prepend(Buffer*, const void *data, size_t len);
43 /* prepend NUL-terminated data */
44 bool buffer_prepend0(Buffer*, const char *data);
45 /* set formatted buffer content, ensures NUL termination on success */
46 bool buffer_printf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
47 /* append formatted buffer content, ensures NUL termination on success */
48 bool buffer_appendf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
49 /* return length of a buffer without trailing NUL byte */
50 size_t buffer_length0(Buffer*);
51 /* return length of a buffer including possible NUL byte */
52 size_t buffer_length(Buffer*);
53 /* return current maximal capacity in bytes of this buffer */
54 size_t buffer_capacity(Buffer*);
55 /* pointer to buffer data, guaranteed to return a NUL terminated
56 * string even if buffer is empty */
57 const char *buffer_content0(Buffer*);
58 /* pointer to buffer data, might be NULL if empty, might not be NUL terminated */
59 const char *buffer_content(Buffer*);
60 /* steal underlying buffer data, caller is responsible to free(3) it,
61 * similar to move semantics in some programming languages */
62 char *buffer_move(Buffer*);
64 #endif