aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-01-17 00:20:06 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-01-17 00:20:06 +0100
commit649ade4fb2c588355e89aa060b4d5954f77579e5 (patch)
tree337943cf7e23069bcbfac4a973dee0a59784a0ba /strbuf.h
parentFix memory leak. (diff)
downloadcalp-649ade4fb2c588355e89aa060b4d5954f77579e5.tar.gz
calp-649ade4fb2c588355e89aa060b4d5954f77579e5.tar.xz
Further work.
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h65
1 files changed, 65 insertions, 0 deletions
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 <stdlib.h>
+
+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 */