aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/better_string.c15
-rw-r--r--src/common/bsv.c33
-rw-r--r--src/common/hash_table.c19
-rw-r--r--src/common/protocol.c20
-rw-r--r--src/common/stdobj.c12
-rw-r--r--src/common/tsv.c33
6 files changed, 96 insertions, 36 deletions
diff --git a/src/common/better_string.c b/src/common/better_string.c
index 7863da0..0788aab 100644
--- a/src/common/better_string.c
+++ b/src/common/better_string.c
@@ -1,6 +1,7 @@
#include "../include/better_string.h"
#include "../include/helpers.h"
#include <stdlib.h>
+#include <stdbool.h>
#include <string.h>
string_t *init_string(const char *src) {
@@ -17,6 +18,16 @@ string_t *init_string(const char *src) {
return s;
}
+string_t *string_copy(string_t *s) {
+ return init_string(s->buf);
+}
+
+char *string_copy_raw(string_t *s) {
+ char *raw = calloc(s->len + 1, sizeof(char));
+ strncpy(raw, s->buf, s->len);
+ return raw;
+}
+
void string_push(string_t *s, char c) {
if (s->len >= s->size - 3) {
s->size *= 2;
@@ -44,6 +55,10 @@ void string_concat(string_t *s1, string_t *s2) {
}
}
+bool string_cmp(string_t *s1, string_t *s2) {
+ return strcmp(s1->buf, s2->buf) == 0;
+}
+
void string_free(void *x) {
string_t *s = x;
diff --git a/src/common/bsv.c b/src/common/bsv.c
new file mode 100644
index 0000000..4a3d000
--- /dev/null
+++ b/src/common/bsv.c
@@ -0,0 +1,33 @@
+#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, char delim) {
+ tsv_t *tsv = safe_calloc(1, sizeof(tsv_t *));
+ tsv->source = source;
+ tsv->i = 0;
+ tsv->c = tsv->source[tsv->i];
+ tsv->delim = delim;
+ 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 != tsv->delim && !escape) {
+ if (tsv->c == '\0') break;
+ string_push(s, tsv->c);
+ tsv_move(tsv);
+ }
+ return s;
+}
diff --git a/src/common/hash_table.c b/src/common/hash_table.c
index 6b4c014..caefae6 100644
--- a/src/common/hash_table.c
+++ b/src/common/hash_table.c
@@ -111,6 +111,17 @@ void ht_insert(ht_t *ht, char *key, void *value) {
list_push_back(bucket, init_pair(key, value));
}
+void *ht_modify(ht_t *ht, char *key, void *newval) {
+ void *oldval = ht_pop(ht, key);
+ ht_insert(ht, key, newval);
+ return oldval;
+}
+
+void ht_overwrite(ht_t *ht, char *key, void *newval, void (*freefunc)(void *)) {
+ void *oldval = ht_modify(ht, key, newval);
+ freefunc(oldval);
+}
+
void *ht_pop(ht_t *ht, char *key) {
unsigned long bn = hash(key) % ht->size;
list_t *bucket = ht->buckets[bn];
@@ -136,6 +147,14 @@ void ht_free(void *x, void (*freefunc)(void *)) {
free(ht);
}
+ht_t *init_map(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(list_t *));
+ ht->is_map = true;
+ return ht;
+}
+
/* DJB2 hash function */
unsigned long hash(char *str) {
unsigned long hash = 5381;
diff --git a/src/common/protocol.c b/src/common/protocol.c
deleted file mode 100644
index 9be3bcf..0000000
--- a/src/common/protocol.c
+++ /dev/null
@@ -1,20 +0,0 @@
-#include "../include/protocol.h"
-#include "../include/better_string.h"
-
-#include <stdlib.h>
-#include <stdbool.h>
-#include <time.h>
-
-string_t *date_str() {
- char dateStr[11];
- time_t t = time(NULL);
- struct tm *tm_info = localtime(&t);
-
- strftime(dateStr, sizeof(dateStr), "%d-%m-%Y", tm_info);
- return init_string(dateStr);
-}
-
-bool same_day(struct tm *date1, struct tm *date2) {
- return (date1->tm_year == date2->tm_year && date1->tm_mon == date2->tm_mon &&
- date1->tm_mday == date2->tm_mday);
-}
diff --git a/src/common/stdobj.c b/src/common/stdobj.c
new file mode 100644
index 0000000..c7657ed
--- /dev/null
+++ b/src/common/stdobj.c
@@ -0,0 +1,12 @@
+#include "../include/stdobj.h"
+#include "../include/better_string.h"
+#include <stdbool.h>
+#include <stdlib.h>
+
+bool array_in(array_t *a, string_t *s) {
+ for (int i = 0; i < a->size; i++) {
+ if (string_cmp(a->items[i], s))
+ return true;
+ }
+ return false;
+}
diff --git a/src/common/tsv.c b/src/common/tsv.c
index c06163b..290dc43 100644
--- a/src/common/tsv.c
+++ b/src/common/tsv.c
@@ -1,32 +1,33 @@
#include <stdlib.h>
#include <stdbool.h>
-#include "../include/tsv.h"
+#include "../include/bsv.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;
+bsv_t *init_bsv(char *source, char delim) {
+ bsv_t *bsv = safe_calloc(1, sizeof(bsv_t *));
+ bsv->source = source;
+ bsv->i = 0;
+ bsv->c = bsv->source[bsv->i];
+ bsv->delim = delim;
+ return bsv;
}
-void tsv_move(tsv_t *tsv) {
- if (tsv->c != '\0') {
- tsv->i++;
- tsv->c = tsv->source[tsv->i];
+void bsv_move(bsv_t *bsv) {
+ if (bsv->c != '\0') {
+ bsv->i++;
+ bsv->c = bsv->source[bsv->i];
}
}
-string_t *tsv_next(tsv_t *tsv) {
+string_t *bsv_next(bsv_t *bsv) {
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);
+ while (bsv->c != bsv->delim && !escape) {
+ if (bsv->c == '\0') break;
+ string_push(s, bsv->c);
+ bsv_move(bsv);
}
return s;
}