vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
buffer.h
(2018B)
1 #ifndef BUFFER_H
2 #define BUFFER_H
3
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include "text.h"
7
8 /**
9 * @file
10 * A dynamically growing buffer storing arbitrary data.
11 * @rst
12 * .. note:: Used for Register, *not* Text content.
13 * @endrst
14 */
15
16 /** A dynamically growing buffer storing arbitrary data. */
17 typedef struct {
18 char *data; /**< Data pointer, ``NULL`` if empty. */
19 size_t len; /**< Current length of data. */
20 size_t size; /**< Maximal capacity of the buffer. */
21 } Buffer;
22
23 /** Release all resources, reinitialize buffer. */
24 void buffer_release(Buffer*);
25 /** Reserve space to store at least ``size`` bytes.*/
26 bool buffer_reserve(Buffer*, size_t size);
27 /** Reserve space for at least ``len`` *more* bytes. */
28 bool buffer_grow(Buffer*, size_t len);
29 /** If buffer is non-empty, make sure it is ``NUL`` terminated. */
30 bool buffer_terminate(Buffer*);
31 /** Set buffer content, growing the buffer as needed. */
32 bool buffer_put(Buffer*, const void *data, size_t len);
33 /** Set buffer content to ``NUL`` terminated data. */
34 bool buffer_put0(Buffer*, const char *data);
35 /** Remove ``len`` bytes starting at ``pos``. */
36 bool buffer_remove(Buffer*, size_t pos, size_t len);
37 /** Insert NUL-terminated data at pos. */
38 bool buffer_insert0(Buffer*, size_t pos, const char *data);
39 /** Append further content to the end. */
40 bool buffer_append(Buffer*, const void *data, size_t len);
41 /** Append NUL-terminated data. */
42 bool buffer_append0(Buffer*, const char *data);
43 /** Append formatted buffer content, ensures NUL termination on success. */
44 bool buffer_appendf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
45 /** Return length of a buffer without trailing NUL byte. */
46 size_t buffer_length0(Buffer*);
47 /**
48 * Get pointer to buffer data.
49 * Guaranteed to return a NUL terminated string even if buffer is empty.
50 */
51 const char *buffer_content0(Buffer*);
52
53 /** ``read(3p)`` like interface for reading into a Buffer (``context``) */
54 ssize_t read_into_buffer(void *context, char *data, size_t len);
55
56 #endif