aboutsummaryrefslogtreecommitdiff
path: root/src/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c
new file mode 100644
index 0000000..5cce400
--- /dev/null
+++ b/src/string.c
@@ -0,0 +1,86 @@
+#include <errno.h>
+#include <stdlib.h>
+
+#include <ct/debug.h>
+#include <ct/string.h>
+
+
+struct string *new_string(const char *s)
+{
+ struct string *n = malloc(sizeof(struct string));
+ if (!n) {
+ error("allocating new string: %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if (!s) {
+ n->len = 1;
+ n->buf = calloc(1, 1);
+ return n;
+ }
+
+ n->len = strlen(s) + 1;
+ n->buf = strdup(s);
+ return n;
+}
+
+void destroy_string(struct string *s)
+{
+ free(s->buf);
+ free(s);
+}
+
+int str_append(struct string *s, char c)
+{
+ s->len += 1;
+ s->buf = realloc(s->buf, s->len);
+ if (!s->buf) {
+ error("appending to string: %s\n", strerror(errno));
+ return -1;
+ }
+
+ s->buf[s->len - 2] = c;
+ s->buf[s->len - 1] = 0;
+ return 0;
+}
+
+int str_concat(struct string *s, const char *c)
+{
+ size_t c_len = strlen(c);
+ size_t len = s->len + c_len;
+ s->buf = realloc(s->buf, len);
+ if (!s->buf) {
+ error("concatenating strings: %s\n", strerror(errno));
+ return -1;
+ }
+
+ strncat(s->buf + s->len - 1, c, c_len);
+ s->len = len;
+ return 0;
+}
+
+int str_add(struct string *s, struct string *c)
+{
+ size_t len = s->len + c->len - 1;
+ s->buf = realloc(s->buf, len);
+ if (!s->buf) {
+ error("adding to string: %s\n", strerror(errno));
+ return -1;
+ }
+
+ strncat(s->buf + s->len - 1, c->buf, c->len);
+ s->len = len;
+ return 0;
+}
+
+bool str_compare(struct string *s, const char *c)
+{
+ return strncmp(s->buf, c, s->len - 1) == 0;
+}
+
+void str_clear(struct string *s)
+{
+ s->len = 1;
+ if (s->buf)
+ s->buf[0] = 0;
+}