From 649ade4fb2c588355e89aa060b4d5954f77579e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 17 Jan 2019 00:20:06 +0100 Subject: Further work. --- strbuf.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 strbuf.h (limited to 'strbuf.h') diff --git a/strbuf.h b/strbuf.h new file mode 100644 index 00000000..ef6ea61f --- /dev/null +++ b/strbuf.h @@ -0,0 +1,65 @@ +#ifndef STRBUF_H +#define STRBUF_H + +#include + +typedef struct { + char* mem; + size_t ptr; + size_t alloc; + size_t len; +} string; + +/* + * TODO rename everything to be on the form + * strbuf_.* + * + * TODO Check memmory allocation for last +1 byte for null. + */ + +/* + * Constructor + */ +int init_string(string* str, size_t len); + +/* + * Like realloc, but for strbuf + */ +int realloc_string(string* str, size_t len); + +/* + * Free's contents of str, but keeps str. + */ +int free_string(string* str); + +/* + * Copy contents from src to dest. + * Assumes that dest is already initialized. + * + * also see: strbuf_alloc_copy + */ +int copy_strbuf(string* dest, string* src); +int strbuf_cmp(string* a, string* b); +int strbuf_c(string* a, char* b); +int strbuf_append(string* a, char c); +int strbuf_reset(string* s); +char* charat(string* s, int idx); +char* strbuf_cur(string* s); + +/* + * Sets the length and seek ptr to 0, but doesn't touch the memmory. + */ +int strbuf_soft_reset(string* s); + +/* + * Returns the character after the last, so where null hopefully is. + */ +char* strbuf_end(string* s); + +/* + * Copies contents from src to dest, also allocating dest in the + * process. dest should not be initialized before this call. + */ +int strbuf_init_copy(string* dest, string* src); + +#endif /* STRBUF_H */ -- cgit v1.2.3