aboutsummaryrefslogtreecommitdiff
path: root/src/string_buf.h
blob: 160b7cb7d2eac41fa1ab55016faa7a6054a6ac18 (about) (plain)
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
/**
 * C string buffers for easy construction of complex strings
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

#ifndef STRING_BUF_H
#define STRING_BUF_H

#include <stdio.h>
#include <stdarg.h>

struct stringbuf {
	char *buf;
	size_t buf_len;
	size_t buf_filled;
};

void stringbuf_init(struct stringbuf *sb);
void stringbuf_destroy(struct stringbuf *sb);
void stringbuf_truncate(struct stringbuf *sb, size_t len);

#define _RAW_BUF_ARGS char **buf, size_t *buf_len, size_t *buf_filled

#define _SB_HEAD(name, ...)					\
	int sb_##name(struct stringbuf *sb, __VA_ARGS__)

#define _SB_RAW_HEAD(name, ...)				\
	int sb_raw_##name(_RAW_BUF_ARGS, __VA_ARGS__)

#define _SB_DEFINE_2(name, ...)			\
	_SB_HEAD(name, __VA_ARGS__);		\
	_SB_RAW_HEAD(name, __VA_ARGS__)

int extend_buf_raw(_RAW_BUF_ARGS, size_t extend_len);
int extend_buf(struct stringbuf *sb, size_t extend_len);

int crop_buf_raw(_RAW_BUF_ARGS);
int crop_buf(struct stringbuf *sb);

_SB_DEFINE_2(bytes, const void *bytes, size_t bytes_len);
_SB_DEFINE_2(string, const char *string);
_SB_DEFINE_2(char, char c);
_SB_DEFINE_2(long, long num);
_SB_DEFINE_2(file, FILE *file);
_SB_DEFINE_2(filepath, const char *path);
_SB_DEFINE_2(vsprintf, const char *fmt, va_list ap);
_SB_DEFINE_2(sprintf, const char *fmt, ...);

#undef _SB_DEFINE_2

#ifndef STRING_BUF_C
#undef _RAW_BUF_ARGS
#undef _SB_HEAD
#undef _SB_RAW_HEAD
#endif

#endif /* STRING_BUF_H */