From 7c7dd1a8b18b101e093df5eff6247acd94f25422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sun, 3 Feb 2019 00:00:05 +0100 Subject: Rework makefile, made .inc into .inc.h. --- Makefile | 36 +++++++++++-------- hash.inc | 63 --------------------------------- hash.inc.h | 61 ++++++++++++++++++++++++++++++++ trie.inc | 115 ------------------------------------------------------------- trie.inc.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vcal.c | 2 +- 6 files changed, 196 insertions(+), 194 deletions(-) delete mode 100644 hash.inc create mode 100644 hash.inc.h delete mode 100644 trie.inc create mode 100644 trie.inc.h diff --git a/Makefile b/Makefile index 12045831..041f0432 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,42 @@ .PHONY: all clean CC := gcc -LEX := flex -DIRS := obj +OBJDIR = obj -CFLAGS = -std=gnu99 -Wall -Wextra -pedantic \ - -DSAFE_STR -DSAFE_HASH -ggdb \ - -fPIC \ +CPPFLAGS = -DSAFE_STR -DSAFE_HASH +CFLAGS = $(CPPFLAGS) \ + -std=gnu99 -Wall -Wextra -pedantic \ + -ggdb -fPIC \ $(shell guile-config compile) -# LFLAGS = LDFLAGS = -fPIC $(shell guile-config link) -C_FILES = $(wildcard *.c) -INC_FILES = $(wildcard *.inc) -O_FILES = $(addprefix obj/,$(C_FILES:.c=.o)) H_FILES = $(wildcard *.h) -$(shell mkdir -p $(DIRS)) +C_FILES = $(wildcard *.c) all: parse libguile-calendar.so -obj/%.o : %.c $(H_FILES) $(INC_FILES) - $(CC) -c -o $@ $< ${CFLAGS} +O_FILES = $(addprefix obj/,$(C_FILES:.c=.o)) -libguile-calendar.so: $(O_FILES) - $(CC) -shared -o $@ $^ $(LDFLAGS) +all: parse libguile-calendar.so parse: $(O_FILES) $(CC) -o $@ $^ $(LDFLAGS) +$(O_FILES): | $(OBJDIR) + +$(OBJDIR)/%.o : %.c $(H_FILES) + $(CC) -c -o $@ $< $(CFLAGS) + +$(OBJDIR): + mkdir -p $(OBJDIR) + +libguile-calendar.so: $(O_FILES) + $(CC) -shared -o $@ $^ $(LDFLAGS) + clean: -rm parse - -rm obj/*.o + -rm $(OBJDIR)/*.o + -rmdir $(OBJDIR) -rm *.so diff --git a/hash.inc b/hash.inc deleted file mode 100644 index 81da6218..00000000 --- a/hash.inc +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef TYPE -#error "Set TYPE to something before including this header" -#else - -#include "err.h" - -int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) { - // TODO genicify the hash function - unsigned long h = hash(value->key.mem) % table->size; - TYPE* mem = table->values[h]; - - /* TODO conflict resolution */ - if (mem != NULL) ERR("Hash collision"); - mem = value; - - ++table->item_count; - return 0; -} - -int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) { - /* - * TODO parts of table might not get properly initialized to 0 - */ - table->values = calloc(sizeof(table->values), init_size); - table->size = init_size; - table->item_count = 0; - return 0; -} - -TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) { - unsigned long h = hash(key) % table->size; - TYPE* mem = table->values[h]; - if (mem == NULL) { - fprintf(stderr, "Trying to access %s\n", key); - ERR("Nothing in field"); - return 0; - } else if (strcmp(mem->key.mem, key) == 0) { - return mem; - } else { - /* TODO fix retrival on invalid key */ - ERR("Other error"); - return 0; - } -} - -int HASH_FREE(TYPE) ( TABLE(TYPE)* table ) { - /* - * TODO an early return is possible by checking if all items have - * been found. table->item_count - */ - for (int i = 0; i < table->size; i++) { - TYPE* mem = table->values[i]; - if (mem == NULL) continue; - - free(mem); - } - free(table->values); - return 0; -} - -#endif /* TYPE */ - -// vim: ft=c diff --git a/hash.inc.h b/hash.inc.h new file mode 100644 index 00000000..0b07629f --- /dev/null +++ b/hash.inc.h @@ -0,0 +1,61 @@ +#ifndef TYPE +#error "Set TYPE to something before including this header" +#else + +#include "err.h" + +int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) { + // TODO genicify the hash function + unsigned long h = hash(value->key.mem) % table->size; + TYPE* mem = table->values[h]; + + /* TODO conflict resolution */ + if (mem != NULL) ERR("Hash collision"); + mem = value; + + ++table->item_count; + return 0; +} + +int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) { + /* + * TODO parts of table might not get properly initialized to 0 + */ + table->values = calloc(sizeof(table->values), init_size); + table->size = init_size; + table->item_count = 0; + return 0; +} + +TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) { + unsigned long h = hash(key) % table->size; + TYPE* mem = table->values[h]; + if (mem == NULL) { + fprintf(stderr, "Trying to access %s\n", key); + ERR("Nothing in field"); + return 0; + } else if (strcmp(mem->key.mem, key) == 0) { + return mem; + } else { + /* TODO fix retrival on invalid key */ + ERR("Other error"); + return 0; + } +} + +int HASH_FREE(TYPE) ( TABLE(TYPE)* table ) { + /* + * TODO an early return is possible by checking if all items have + * been found. table->item_count + */ + for (int i = 0; i < table->size; i++) { + TYPE* mem = table->values[i]; + if (mem == NULL) continue; + + free(mem); + } + free(table->values); + return 0; +} + +#endif /* TYPE */ diff --git a/trie.inc b/trie.inc deleted file mode 100644 index 532251b3..00000000 --- a/trie.inc +++ /dev/null @@ -1,115 +0,0 @@ -#ifndef TYPE -#error "Set TYPE before including this file" -#else - -#include "err.h" - -int CONSTRUCTOR_DECL ( TRIE(TYPE) ) { - NEW(TRIE_NODE(TYPE), t, '\0'); - this->root = t; - return 0; -} - -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c) { - this->c = c; - this->value = NULL; - this->next = NULL; - this->child = NULL; - return 0; -} - -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), - char c, - TRIE_NODE(TYPE)* next, - TRIE_NODE(TYPE)* child ) -{ - this->c = c; - this->next = next; - this->child = child; - return 0; -} - -int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) { - TRIE_NODE(TYPE) *cur, *last; - - last = trie->root; - cur = last->child; - - char* subkey = key; - // -------------------------------------------------- - - while (1) { - if (cur == NULL) { - /* Build direct LL for remaining subkey */ - for (char* c = subkey; c[0] != '\0'; c++) { - NEW(TRIE_NODE(TYPE), t, *c); - last->child = t; - last = t; - } - last->value = val; - return 0; - } else if (cur->c == subkey[0]) { - /* This node belongs to the key, - * Decend further */ - last = cur; - cur = cur->child; - subkey++; - } else if (subkey[0] == '\0') { - /* Key finished */ - last->value = val; - return 0; - } else if (cur->next != NULL) { - /* This node was not part of the set, but it's sibling might */ - cur = cur->next; - /* `last` not set since we aren't moving down */ - } else { - /* No node on this level was part of the set, create a new - * sibling and follow down that parse */ - NEW(TRIE_NODE(TYPE), t, *subkey); - cur->next = t; - last = cur; - cur = t; - } - } - - return 0; -} - -/* - * TODO what happens when I give an invalid key? - */ -TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) { - TRIE_NODE(TYPE)* n = trie->root->child; - char* subkey = key; - - while (n != NULL) { - if (subkey[1] == '\0') { - return n->value; - } else if (subkey[0] == n->c) { - n = n->child; - subkey++; - } else { - n = n->next; - } - } - - ERR("Position not found"); - return 0; -} - -int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ) { - if (node == NULL) return 0; - if (node->value != NULL) FFREE(TYPE, node->value); - if (node->next != NULL) TRIE_NODE_FREE(TYPE)(node->next); - if (node->child != NULL) TRIE_NODE_FREE(TYPE)(node->child); - free (node); - return 0; -} - -int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ) { - return TRIE_NODE_FREE(TYPE)(trie->root); -} - -#endif /* TYPE */ - -// vim: ft=c diff --git a/trie.inc.h b/trie.inc.h new file mode 100644 index 00000000..50aeff29 --- /dev/null +++ b/trie.inc.h @@ -0,0 +1,113 @@ +#ifndef TYPE +#error "Set TYPE before including this file" +#else + +#include "err.h" + +int CONSTRUCTOR_DECL ( TRIE(TYPE) ) { + NEW(TRIE_NODE(TYPE), t, '\0'); + this->root = t; + return 0; +} + +int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c) { + this->c = c; + this->value = NULL; + this->next = NULL; + this->child = NULL; + return 0; +} + +int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), + char c, + TRIE_NODE(TYPE)* next, + TRIE_NODE(TYPE)* child ) +{ + this->c = c; + this->next = next; + this->child = child; + return 0; +} + +int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) { + TRIE_NODE(TYPE) *cur, *last; + + last = trie->root; + cur = last->child; + + char* subkey = key; + // -------------------------------------------------- + + while (1) { + if (cur == NULL) { + /* Build direct LL for remaining subkey */ + for (char* c = subkey; c[0] != '\0'; c++) { + NEW(TRIE_NODE(TYPE), t, *c); + last->child = t; + last = t; + } + last->value = val; + return 0; + } else if (cur->c == subkey[0]) { + /* This node belongs to the key, + * Decend further */ + last = cur; + cur = cur->child; + subkey++; + } else if (subkey[0] == '\0') { + /* Key finished */ + last->value = val; + return 0; + } else if (cur->next != NULL) { + /* This node was not part of the set, but it's sibling might */ + cur = cur->next; + /* `last` not set since we aren't moving down */ + } else { + /* No node on this level was part of the set, create a new + * sibling and follow down that parse */ + NEW(TRIE_NODE(TYPE), t, *subkey); + cur->next = t; + last = cur; + cur = t; + } + } + + return 0; +} + +/* + * TODO what happens when I give an invalid key? + */ +TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) { + TRIE_NODE(TYPE)* n = trie->root->child; + char* subkey = key; + + while (n != NULL) { + if (subkey[1] == '\0') { + return n->value; + } else if (subkey[0] == n->c) { + n = n->child; + subkey++; + } else { + n = n->next; + } + } + + ERR("Position not found"); + return 0; +} + +int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ) { + if (node == NULL) return 0; + if (node->value != NULL) FFREE(TYPE, node->value); + if (node->next != NULL) TRIE_NODE_FREE(TYPE)(node->next); + if (node->child != NULL) TRIE_NODE_FREE(TYPE)(node->child); + free (node); + return 0; +} + +int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ) { + return TRIE_NODE_FREE(TYPE)(trie->root); +} + +#endif /* TYPE */ diff --git a/vcal.c b/vcal.c index 65caaa26..2310c3fb 100644 --- a/vcal.c +++ b/vcal.c @@ -4,7 +4,7 @@ #define TYPE content_line // #include "hash.inc" -#include "trie.inc" +#include "trie.inc.h" #undef TYPE int CONSTRUCTOR_DECL(vevent) { -- cgit v1.2.3