aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/array.c47
-rw-r--r--src/common/better_string.c51
-rw-r--r--src/common/hash_table.c12
-rw-r--r--src/common/helpers.c36
-rw-r--r--src/common/opcodes.c20
-rw-r--r--src/common/tsv.c32
6 files changed, 198 insertions, 0 deletions
diff --git a/src/common/array.c b/src/common/array.c
new file mode 100644
index 0000000..4ebb67e
--- /dev/null
+++ b/src/common/array.c
@@ -0,0 +1,47 @@
+#include "../include/array.h"
+#include "../include/helpers.h"
+
+#include <stdlib.h>
+
+array_t *init_array() {
+ array_t *a = safe_calloc(1, sizeof(array_t));
+ a->capacity = DEFAULT_ARR_LEN;
+ a->size = 0;
+ a->items = safe_calloc(a->capacity, sizeof(void *));
+ return a;
+}
+
+void array_push(array_t *a, void *item) {
+ if (a->size >= a->capacity - 2) {
+ a->capacity *= 2;
+ a->items = realloc(a->items, a->capacity);
+ }
+ a->items[a->size] = item;
+ a->size++;
+}
+
+void *array_pop(array_t *a) {
+ if (a->size <= 0)
+ return NULL;
+ void *retval = a->items[a->size];
+ a->size--;
+ return retval;
+}
+
+array_t *array_reverse(array_t *a) {
+ array_t *reverse = init_array();
+ for (int i = 0; i < a->size; i ++) {
+ array_push(reverse, array_pop(a));
+ }
+ array_free(a, nothing);
+ return reverse;
+}
+
+void array_free(void *x, void (*freefunc)(void *)) {
+ array_t *a = (array_t *)x;
+ for (int i = 0; i < a->size; i++) {
+ freefunc(a->items[i]);
+ }
+ free(a->items);
+ free(a);
+}
diff --git a/src/common/better_string.c b/src/common/better_string.c
new file mode 100644
index 0000000..366dda6
--- /dev/null
+++ b/src/common/better_string.c
@@ -0,0 +1,51 @@
+#include "../include/better_string.h"
+#include "../include/helpers.h"
+#include <stdlib.h>
+#include <string.h>
+
+string_t *init_string(const char *src) {
+ string_t *s = safe_calloc(1, sizeof(string_t));
+ size_t len = src ? strlen(src) : DEFAULT_STR_SIZE;
+ size_t size = len * 2;
+ s->buf = safe_calloc(size, sizeof(char));
+ s->buf[0] = '\0';
+
+ if (src)
+ strcpy(s->buf, src);
+ s->len = len;
+ s->size = size;
+ return s;
+}
+
+void string_push(string_t *s, char c) {
+ if (s->len >= s->size - 2) {
+ s->size *= 2;
+ s->buf = safe_realloc(s->buf, s->size);
+ }
+ s->buf[s->len] = c;
+ s->len++;
+}
+
+char string_pop(string_t *s) {
+ char c = s->buf[s->len];
+ s->len--;
+ return c;
+}
+
+void string_concat_const(string_t *s1, const char *s2) {
+ for (int i = 0; i < strlen(s2); i++) {
+ string_push(s1, s2[i]);
+ }
+}
+
+void string_concat(string_t *s1, string_t *s2) {
+ for (int i = 0; i < s2->len; i++) {
+ string_push(s1, s2->buf[i]);
+ }
+}
+
+void string_free(void *x) {
+ string_t *s = x;
+ free(s->buf);
+ free(s);
+}
diff --git a/src/common/hash_table.c b/src/common/hash_table.c
new file mode 100644
index 0000000..2f66d45
--- /dev/null
+++ b/src/common/hash_table.c
@@ -0,0 +1,12 @@
+#include "../include/hash_table.h"
+#include "../include/helpers.h"
+
+#include <stdlib.h>
+
+ht_t *init_ht(size_t size) {
+ ht_t *ht = safe_calloc(1, sizeof(size));
+ size_t realsize = size == 0 ? DEFAULT_HT_SIZE : size;
+ ht->buckets = safe_calloc(realsize, sizeof(sll_t *));
+ ht->size = realsize;
+ return ht;
+}
diff --git a/src/common/helpers.c b/src/common/helpers.c
new file mode 100644
index 0000000..4bf7301
--- /dev/null
+++ b/src/common/helpers.c
@@ -0,0 +1,36 @@
+#include "../include/helpers.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void die_lz(int code, const char *msg) {
+ if (code < 0) {
+ die(msg);
+ }
+}
+
+void die(const char *msg) {
+ fprintf(stderr, "panic: ");
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
+void *safe_calloc(unsigned int i, size_t size) {
+ void *x = calloc(i, size);
+ if (x == NULL) {
+ die("abort: calloc()");
+ }
+ return x;
+}
+
+void *safe_realloc(void *x, size_t size) {
+ void *p = realloc(x, size);
+ if (x == NULL) {
+ die("abort: realloc()");
+ }
+ return p;
+}
+
+void alloc_zero(void *ptr, size_t size) { memset(ptr, 0, size); }
+
+void nothing(void *_) {}
diff --git a/src/common/opcodes.c b/src/common/opcodes.c
new file mode 100644
index 0000000..a489630
--- /dev/null
+++ b/src/common/opcodes.c
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#include "../include/opcodes.h"
+#include "../include/helpers.h"
+
+
+char *decode_client_opcode(int op) {
+ return "implement me";
+}
+
+char *decode_server_opcode(int op) {
+ return "implement me";
+}
+
+int encode_client_opcode(char *c) {
+ return -1;
+}
+
+int encode_server_opcode(char *c) {
+ return -1;
+}
diff --git a/src/common/tsv.c b/src/common/tsv.c
new file mode 100644
index 0000000..c06163b
--- /dev/null
+++ b/src/common/tsv.c
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "../include/tsv.h"
+#include "../include/helpers.h"
+#include "../include/better_string.h"
+
+tsv_t *init_tsv(char *source) {
+ tsv_t *tsv = safe_calloc(1, sizeof(tsv_t *));
+ tsv->source = source;
+ tsv->i = 0;
+ tsv->c = tsv->source[tsv->i];
+ return tsv;
+}
+
+void tsv_move(tsv_t *tsv) {
+ if (tsv->c != '\0') {
+ tsv->i++;
+ tsv->c = tsv->source[tsv->i];
+ }
+}
+
+string_t *tsv_next(tsv_t *tsv) {
+ string_t *s = init_string(NULL);
+ bool escape = false;
+ while (tsv->c != '\t' && !escape) {
+ if (tsv->c == '\0') break;
+ string_push(s, tsv->c);
+ tsv_move(tsv);
+ }
+ return s;
+}