forked frommartanne/vis
- Notifications
You must be signed in to change notification settings - Fork0
/
Copy pathbuffer.h
83 lines (78 loc) · 2.95 KB
/
buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndefBUFFER_H
#defineBUFFER_H
#include<stddef.h>
#include<stdbool.h>
#include"text.h"
/**
* @file
* A dynamically growing buffer storing arbitrary data.
* @rst
* .. note:: Used for Register, *not* Text content.
* @endrst
*/
/** A dynamically growing buffer storing arbitrary data. */
typedefstruct {
char*data;/**< Data pointer, ``NULL`` if empty. */
size_tlen;/**< Current length of data. */
size_tsize;/**< Maximal capacity of the buffer. */
}Buffer;
/** Initialize a Buffer object. */
voidbuffer_init(Buffer*);
/** Release all resources, reinitialize buffer. */
voidbuffer_release(Buffer*);
/** Set buffer length to zero, keep allocated memory. */
voidbuffer_clear(Buffer*);
/** Reserve space to store at least ``size`` bytes.*/
boolbuffer_reserve(Buffer*,size_tsize);
/** Reserve space for at least ``len`` *more* bytes. */
boolbuffer_grow(Buffer*,size_tlen);
/** If buffer is non-empty, make sure it is ``NUL`` terminated. */
boolbuffer_terminate(Buffer*);
/** Set buffer content, growing the buffer as needed. */
boolbuffer_put(Buffer*,constvoid*data,size_tlen);
/** Set buffer content to ``NUL`` terminated data. */
boolbuffer_put0(Buffer*,constchar*data);
/** Remove ``len`` bytes starting at ``pos``. */
boolbuffer_remove(Buffer*,size_tpos,size_tlen);
/** Insert ``len`` bytes of ``data`` at ``pos``. */
boolbuffer_insert(Buffer*,size_tpos,constvoid*data,size_tlen);
/** Insert NUL-terminated data at pos. */
boolbuffer_insert0(Buffer*,size_tpos,constchar*data);
/** Append further content to the end. */
boolbuffer_append(Buffer*,constvoid*data,size_tlen);
/** Append NUL-terminated data. */
boolbuffer_append0(Buffer*,constchar*data);
/** Insert ``len`` bytes of ``data`` at the start. */
boolbuffer_prepend(Buffer*,constvoid*data,size_tlen);
/** Insert NUL-terminated data at the start. */
boolbuffer_prepend0(Buffer*,constchar*data);
/** Set formatted buffer content, ensures NUL termination on success. */
boolbuffer_printf(Buffer*,constchar*fmt, ...) __attribute__((format(printf,2,3)));
/** Append formatted buffer content, ensures NUL termination on success. */
boolbuffer_appendf(Buffer*,constchar*fmt, ...) __attribute__((format(printf,2,3)));
/** Return length of a buffer without trailing NUL byte. */
size_tbuffer_length0(Buffer*);
/** Return length of a buffer including possible NUL byte. */
size_tbuffer_length(Buffer*);
/** Return current maximal capacity in bytes of this buffer. */
size_tbuffer_capacity(Buffer*);
/**
* Get pointer to buffer data.
* Guaranteed to return a NUL terminated string even if buffer is empty.
*/
constchar*buffer_content0(Buffer*);
/**
* Get pointer to buffer data.
* @rst
* .. warning:: Might be NULL, if empty. Might not be NUL terminated.
* @endrst
*/
constchar*buffer_content(Buffer*);
/**
* Borrow underlying buffer data.
* @rst
* .. warning:: The caller is responsible to ``free(3)`` it.
* @endrst
*/
char*buffer_move(Buffer*);
#endif