aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 15:38:03 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 18:16:19 +0100
commit447e42b4fbf5567b3cf96ddee0186f76f0c7ebe9 (patch)
tree349566b092f4d6f566cad3411f4e926534c5b304
parentAdd single file mode. (diff)
downloadcalp-447e42b4fbf5567b3cf96ddee0186f76f0c7ebe9.tar.gz
calp-447e42b4fbf5567b3cf96ddee0186f76f0c7ebe9.tar.xz
Improve templating macros.
-rw-r--r--Makefile2
-rw-r--r--hash.h12
-rw-r--r--hash.inc.h8
-rw-r--r--linked_list.h8
-rw-r--r--linked_list.inc.h4
-rw-r--r--macro.h28
-rw-r--r--trie.h15
-rw-r--r--trie.inc.h4
-rw-r--r--vcal.c11
-rw-r--r--vcal.h2
10 files changed, 49 insertions, 45 deletions
diff --git a/Makefile b/Makefile
index f8e07e8a..51e27750 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ OBJDIR = obj
CPPFLAGS = -DSAFE_STR # -DSAFE_HASH
CFLAGS = $(CPPFLAGS) \
- -std=gnu99 -Wall -Wextra -pedantic \
+ -std=gnu11 -Wall -Wextra -pedantic \
-ggdb -fPIC \
$(shell guile-config compile)
LDFLAGS = -fPIC $(shell guile-config link)
diff --git a/hash.h b/hash.h
index 02741642..e1ff7385 100644
--- a/hash.h
+++ b/hash.h
@@ -7,9 +7,7 @@
unsigned long hash(char*);
-#define TABLE(T) TP(table__, T)
-#define HASH_PUT(T) TP(hash_put_, T)
-#define HASH_GET(T) TP(hash_get_, T)
+#define HASHT(T) TEMPL(hash_t, T)
#endif /* HASH_H */
#ifdef TYPE
@@ -24,16 +22,16 @@ typedef struct {
TYPE** values;
} TABLE(TYPE);
-int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value );
+int PUSH(HASHT(TYPE)) ( HASHT(TYPE)* table, TYPE* value );
-INIT_F(HASH(TYPE), int init_size );
+INIT_F(HASHT(TYPE), int init_size );
-TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key );
+TYPE* GET(HASHT(TYPE)) ( HASHT(TYPE)* table, char* key );
/*
* Free's all item's stored in table.
* And finally frees table.
*/
-FREE_F(HASH(TYPE));
+FREE_F(HASHT(TYPE));
#endif /* HASH_H */
diff --git a/hash.inc.h b/hash.inc.h
index 6ded0628..0aee8ef4 100644
--- a/hash.inc.h
+++ b/hash.inc.h
@@ -4,7 +4,7 @@
#include "err.h"
-int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) {
+int PUT(HASHT(TYPE)) ( HASHT(TYPE)* table, TYPE* value) {
// TODO genicify the hash function
unsigned long h = hash(value->key.mem) % table->size;
TYPE* mem = table->values[h];
@@ -17,7 +17,7 @@ int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) {
return 0;
}
-INIT_F(HASH(TYPE), int init_size ) {
+INIT_F(HASHT(TYPE), int init_size ) {
/*
* TODO parts of table might not get properly initialized to 0
*/
@@ -27,7 +27,7 @@ INIT_F(HASH(TYPE), int init_size ) {
return 0;
}
-TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) {
+TYPE* GET(HASHT(TYPE)) ( HASHT(TYPE)* table, char* key ) {
unsigned long h = hash(key) % table->size;
TYPE* mem = table->values[h];
if (mem == NULL) {
@@ -43,7 +43,7 @@ TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) {
}
}
-FREE(HASH(TYPE)) {
+FREE(HASHT(TYPE)) {
/*
* TODO an early return is possible by checking if all items have
* been found. table->item_count
diff --git a/linked_list.h b/linked_list.h
index 90a110bf..5bf3db15 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -3,10 +3,8 @@
#include "macro.h"
-#define LLIST(T) TP(llist__, T)
-#define LINK(T) TP(llist_link__, T)
-
-#define LLIST_CONS(T) TP(llist_cons__, T)
+#define LLIST(T) TEMPL(llist, T)
+#define LINK(T) TEMPL(llist_link, T)
#endif /* LINKED_LIST_H */
#ifdef TYPE
@@ -31,7 +29,7 @@ INIT_F ( LINK(TYPE) );
INIT_F ( LINK(TYPE), TYPE* val );
FREE_F ( LINK(TYPE) );
-int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val);
+int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val);
int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src );
diff --git a/linked_list.inc.h b/linked_list.inc.h
index be51e582..7d9452e0 100644
--- a/linked_list.inc.h
+++ b/linked_list.inc.h
@@ -52,7 +52,7 @@ INIT_F ( LINK(TYPE), TYPE* val ) {
return 0;
}
-int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val) {
+int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) {
NEW(LINK(TYPE), link, val);
link->after = lst->head->after;
@@ -73,7 +73,7 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
while (n->after != NULL) {
NEW(TYPE, cpy);
DEEP_COPY(TYPE)(cpy, n->value);
- LLIST_CONS(TYPE) ( dest, cpy );
+ PUSH(LLIST(TYPE)) ( dest, cpy );
n = n->after;
}
diff --git a/macro.h b/macro.h
index 67901ebb..dbf4e355 100644
--- a/macro.h
+++ b/macro.h
@@ -1,6 +1,7 @@
#ifndef MACRO_H
#define MACRO_H
+
/*
* NOTE This file uses __VA_OPT__. This is not standard compliant.
*/
@@ -12,6 +13,7 @@
#define TP3(a, b, c) a ## b ## c
#define TP4(a, b, c, d) a ## b ## c ## d
#define TP5(a, b, c, d, e) a ## b ## c ## d ## e
+#define TP6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f
/*
* Get length of __VA_ARGS__
@@ -21,8 +23,16 @@
#define VA_ARGS_NUM_PRIV(P1, P2, P3, P4, P5, P6, Pn, ...) Pn
#define VA_ARGS_NUM(...) VA_ARGS_NUM_PRIV(-1, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0)
+/*
+ * Templetize
+ * TODO actuall documentatino
+ * ᐸElementTᐳ
+ */
+#define TEMPL(name, T) TP4(name, \U00001438 , T, \U00001433 )
+#define TEMPL_N(name, T, argcount) TP6(name, \U00001438 , T, _, argcount, \U00001433 )
+
/* Constructor type name */
-#define __INIT_T(T, C) TP3(T, __init__, C)
+#define __INIT_T(T, C) TEMPL_N(init, T, C)
/* Returns full type of constructor */
#define INIT_F(T, ...) \
@@ -49,18 +59,20 @@
INIT(T, & N, __VA_ARGS__);
/* Destructor for type */
-#define FREE(T) TP(T, __free)
+#define FREE(T) TEMPL(free, T)
/* Call destructor for type, and free object */
#define FFREE(T, N) do { FREE(T)(N); free(N); } while (0)
/* Declare destructor */
-#define FREE_F(T) int TP(T, __free) (T* this)
+#define FREE_F(T) int FREE(T) (T* this)
-#define DEEP_COPY(T) TP(deep_copy__, T)
-#define RESOLVE(T) TP(resolve__, T)
-#define APPEND(T) TP(append__, T)
-#define SIZE(T) TP(size__, T)
-#define EMPTY(T) TP(empty__, T)
+#define DEEP_COPY(T) TEMPL(deep_copy , T)
+#define RESOLVE(T) TEMPL(resolve , T)
+#define APPEND(T) TEMPL(append , T)
+#define SIZE(T) TEMPL(size , T)
+#define EMPTY(T) TEMPL(empty , T)
+#define PUSH(T) TEMPL(push , T)
+#define GET(T) TEMPL(get , T)
#endif /* MACRO_H */
diff --git a/trie.h b/trie.h
index 4fe26df5..916f672f 100644
--- a/trie.h
+++ b/trie.h
@@ -5,13 +5,10 @@
#include "macro.h"
-#define TRIE(T) TP(trie__, T)
-#define TRIE_NODE(T) TP(trie_node__, T)
+#define TRIE(T) TEMPL(trie, T)
+#define TRIE_NODE(T) TEMPL(trie_node, T)
-#define TRIE_PUT(T) TP(trie_put__, T)
-#define TRIE_GET(T) TP(trie_get__, T)
-
-#define TRIE_DOT(T) TP(trie_to_dot__, T)
+#define TRIE_DOT(T) TP(trie_to_dot__, T)
#define TRIE_DOT_HELP(T) TP(trie_to_dot_helper__, T)
#endif /* TRIE_H */
@@ -36,14 +33,12 @@ INIT_F (TRIE_NODE(TYPE), char c);
INIT_F (TRIE_NODE(TYPE),
char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child );
-int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val );
+int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val );
-TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key );
+TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key );
-// int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node );
FREE_F(TRIE_NODE(TYPE));
-// int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie );
FREE_F(TRIE(TYPE));
int TRIE_DOT(TYPE) ( TRIE(TYPE)*, FILE* );
diff --git a/trie.inc.h b/trie.inc.h
index 8a817ab6..50b8ec5b 100644
--- a/trie.inc.h
+++ b/trie.inc.h
@@ -29,7 +29,7 @@ INIT_F (TRIE_NODE(TYPE),
return 0;
}
-int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
+int PUSH(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
TRIE_NODE(TYPE) *cur, *last;
last = trie->root;
@@ -80,7 +80,7 @@ int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
/*
* TODO what happens when I give an invalid key?
*/
-TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) {
+TYPE* GET(TRIE(TYPE)) ( TRIE(TYPE)* trie, char* key ) {
TRIE_NODE(TYPE)* n = trie->root->child;
char* subkey = key;
diff --git a/vcal.c b/vcal.c
index a62c99a4..448a0a34 100644
--- a/vcal.c
+++ b/vcal.c
@@ -40,7 +40,7 @@ content_line* RESOLVE(content_line)
for (LINK(strbuf)* s = new->vals.head->after;
s->after != NULL;
s = s->after) {
- LLIST_CONS(strbuf) (&dest->vals, s->value);
+ PUSH(LLIST(strbuf)) (&dest->vals, s->value);
}
FREE(content_line)(new);
@@ -49,12 +49,12 @@ content_line* RESOLVE(content_line)
}
content_line* get_property (vevent* ev, char* key) {
- return TRIE_GET(content_line)(&ev->clines, key);
+ return GET(TRIE(content_line))(&ev->clines, key);
}
int add_content_line (vevent* ev, content_line* c) {
// TODO memmory safety on strbuf?
- return TRIE_PUT(content_line)(&ev->clines, c->key.mem, c);
+ return PUSH(TRIE(content_line))(&ev->clines, c->key.mem, c);
}
INIT_F(content_line) {
@@ -72,14 +72,15 @@ INIT_F(content_line, int keylen, int vallen) {
// INIT(strbuf, &this->val, vallen);
INIT( LLIST(strbuf), &this->vals );
NEW(strbuf, s, vallen);
- LLIST_CONS(strbuf)(&this->vals, s);
+ PUSH(LLIST(strbuf))(&this->vals, s);
// TODO remaining fields
return 0;
}
int content_line_copy (content_line* dest, content_line* src) {
- strbuf_init_copy(&dest->key, &src->key);
+ //strbuf_init_copy(&dest->key, &src->key);
+ DEEP_COPY(strbuf)(&dest->key, &src->key);
// strbuf_init_copy(&dest->val, &src->val);
DEEP_COPY(LLIST(strbuf))(&dest->vals, &src->vals);
// TODO remaining fields
diff --git a/vcal.h b/vcal.h
index 9123e3fa..74ea819b 100644
--- a/vcal.h
+++ b/vcal.h
@@ -56,7 +56,7 @@ content_line* get_property (vevent* ev, char* key);
int add_content_line (vevent* ev, content_line* c);
-int free_vevent(vevent* ev);
+FREE_F(vevent);
typedef struct s_vcalendar {
size_t n_events;