From fd14f6fbf8206589d2a0ad8f0793845cb8faf1d7 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 26 Dec 2024 21:18:05 -0800 Subject: stuff --- src/hash_table.c | 11 +++++++++++ src/helpers.c | 6 ++++++ src/main.c | 4 ++++ src/opcodes.c | 6 ------ src/tsv.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/hash_table.c create mode 100644 src/tsv.c (limited to 'src') diff --git a/src/hash_table.c b/src/hash_table.c new file mode 100644 index 0000000..cced194 --- /dev/null +++ b/src/hash_table.c @@ -0,0 +1,11 @@ +#include +#include +#include + +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/helpers.c b/src/helpers.c index ad7228e..8485dc6 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -3,6 +3,12 @@ #include #include +void die_lz(int code, const char *msg) { + if (code < 0) { + die(msg); + } +} + void die(const char *msg) { fprintf(stderr, "panic: "); perror(msg); diff --git a/src/main.c b/src/main.c index 8555da1..7a17966 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,9 @@ int PORT = DEFAULT_PORT; int nfds = 1; struct pollfd fds[MAX_CONNECTIONS * 2]; +ht_t *USERS; +ht_t *CHAN; + void handle_sigint(int sig) { for (int i = 0; i < nfds; i++) { if (fds[i].fd >= 0) { diff --git a/src/opcodes.c b/src/opcodes.c index 66742db..8fc51f2 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -3,12 +3,6 @@ #include -void die_lz(int code, const char *msg) { - if (code < 0) { - die(msg); - } -} - char *decode_client_opcode(int op) { return "implement me"; } diff --git a/src/tsv.c b/src/tsv.c new file mode 100644 index 0000000..9b1c2be --- /dev/null +++ b/src/tsv.c @@ -0,0 +1,32 @@ +#include +#include + +#include +#include +#include + +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; +} -- cgit