32 lines
782 B
C
32 lines
782 B
C
#ifndef STRING_BUFFER_H
|
|
#define STRING_BUFFER_H
|
|
|
|
#include <stddef.h>
|
|
|
|
/* "move" underlying contents of buffer to str shrinking it */
|
|
/* TODO: handle realloc fail */
|
|
#define SB_MOVE(buffer, str) do { \
|
|
buffer->capacity = buffer->size + 1; \
|
|
buffer->buf = realloc(buffer->buf, buffer->capacity); \
|
|
buffer->buf[buffer->size++] = '\0'; \
|
|
str = buffer->buf; \
|
|
} while (0)
|
|
|
|
typedef struct {
|
|
char *buf;
|
|
size_t size, capacity;
|
|
} StringBuffer;
|
|
|
|
void sb_add_char(StringBuffer *buffer, char c);
|
|
void sb_add_string(StringBuffer *buffer, const char *str);
|
|
|
|
size_t sb_length(const StringBuffer *buffer);
|
|
|
|
void sb_to_c_str(const StringBuffer *buffer, char *str);
|
|
|
|
void sb_init_empty(StringBuffer *buffer);
|
|
|
|
void sb_free(StringBuffer *buffer);
|
|
|
|
#endif /* STRING_BUFFER_H */
|