aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-04-26 19:14:40 +0000
committerJoursoir <chat@joursoir.net>2021-04-26 19:14:40 +0000
commit60ac7dc79aeb8cb09a52be2e2510e158644fc344 (patch)
tree758e89fec222d168e1f9f5e41dc4b5fba26a526a
downloadctimeline-60ac7dc79aeb8cb09a52be2e2510e158644fc344.tar.gz
ctimeline-60ac7dc79aeb8cb09a52be2e2510e158644fc344.tar.bz2
ctimeline-60ac7dc79aeb8cb09a52be2e2510e158644fc344.zip
init project
-rw-r--r--xstring.c59
-rw-r--r--xstring.h17
2 files changed, 76 insertions, 0 deletions
diff --git a/xstring.c b/xstring.c
new file mode 100644
index 0000000..98d91f9
--- /dev/null
+++ b/xstring.c
@@ -0,0 +1,59 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "xstring.h"
+
+string *string_alloc(const char *text)
+{
+ string *str = malloc(sizeof(string));
+ if(text) {
+ int length = strlen(text);
+ str->s = malloc(sizeof(char) * (length + 1));
+ strcpy(str->s, text);
+ str->capacity = length;
+ str->len = length;
+ }
+ else {
+ str->s = malloc(sizeof(char) * (INIT_LEN_STRING + 1));
+ str->len = 0;
+ str->capacity = INIT_LEN_STRING;
+ str->s[0] = '\0';
+ }
+ return str;
+}
+
+void string_release(string *str)
+{
+ if(str->s) {
+ free(str->s);
+ str->s = NULL;
+ }
+ free(str);
+}
+
+void string_reset(string *str)
+{
+ if(str->s) {
+ str->s[0] = '\0';
+ str->len = 0;
+ }
+}
+
+void string_addch(string *str, int ch)
+{
+ if(str->capacity <= (str->len + 1)) {
+ int i;
+ int updated_cap = str->capacity + INIT_LEN_STRING;
+ char *tmp_s = str->s;
+ str->s = malloc(sizeof(char) * (updated_cap + 1));
+ for(i = 0; i < updated_cap; i++)
+ str->s[i] = (i >= str->capacity) ? '\0' : tmp_s[i];
+
+ str->capacity = updated_cap;
+ free(tmp_s);
+ }
+
+ str->s[str->len] = ch;
+ str->len++;
+ str->s[str->len] = '\0';
+}
diff --git a/xstring.h b/xstring.h
new file mode 100644
index 0000000..3e98ec9
--- /dev/null
+++ b/xstring.h
@@ -0,0 +1,17 @@
+#ifndef CTIMELINE_XSTRING_H
+#define CTIMELINE_XSTRING_H
+
+#define INIT_LEN_STRING 32
+
+typedef struct tag_string {
+ char *s;
+ int len;
+ int capacity;
+} string;
+
+string *string_alloc(const char *text);
+void string_release(string *str);
+void string_reset(string *str);
+void string_addch(string *str, int ch);
+
+#endif /* CTIMELINE_XSTRING_H */