diff options
Diffstat (limited to 'string_buf.h')
-rw-r--r-- | string_buf.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/string_buf.h b/string_buf.h new file mode 100644 index 0000000..160b7cb --- /dev/null +++ b/string_buf.h @@ -0,0 +1,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 */ |