aboutsummaryrefslogtreecommitdiff
path: root/buffer.h
blob: 088248b03fa0e1a072eb9d4ee3f20a1b9397c9e5 (plain) (blame)
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
#ifndef BUFFER_H
#define BUFFER_H

#include <stddef.h>
#include <stdbool.h>
#include "text.h"

typedef struct {       /* a dynamically growing buffer storing arbitrary data */
	char *data;    /* NULL if empty */
	size_t len;    /* current length of data */
	size_t size;   /* maximal capacity of the buffer */
} Buffer;

/* initalize a (stack allocated) Buffer insteance */
void buffer_init(Buffer*);
/* relase/free all data stored in this buffer, reset size to zero */
void buffer_release(Buffer*);
/* reserve space to store at least size bytes in this buffer.*/
bool buffer_grow(Buffer*, size_t size);
/* truncate buffer, but keep associated memory region for further data */
void buffer_truncate(Buffer*);
/* replace buffer content with given data, growing the buffer if needed */
bool buffer_put(Buffer*, const void *data, size_t len);
/* same but with NUL-terminated data */
bool buffer_put0(Buffer*, const char *data);
/* append futher content to the end of the buffer data */
bool buffer_append(Buffer*, const void *data, size_t len);
/* append NUl-terminated data */
bool buffer_append0(Buffer*, const char *data);

#endif