aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-02-10 18:58:35 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-15 20:03:43 +0100
commitf865f1554dec3d6fb71eab9c02ecbb6f0bfcb821 (patch)
treececc4eda62b8892828d67253b411ae5c1c79803f
parentMerge strbuf copy functions. (diff)
downloadcalp-f865f1554dec3d6fb71eab9c02ecbb6f0bfcb821.tar.gz
calp-f865f1554dec3d6fb71eab9c02ecbb6f0bfcb821.tar.xz
Replace key_val type with templatized PAIR(T, V).
-rw-r--r--graphs.c10
-rw-r--r--macro.h3
-rw-r--r--pair.h19
-rw-r--r--pair.inc.h33
-rw-r--r--parse.c24
-rw-r--r--vcal.c50
-rw-r--r--vcal.h18
7 files changed, 93 insertions, 64 deletions
diff --git a/graphs.c b/graphs.c
index 9bd52545..083bfea2 100644
--- a/graphs.c
+++ b/graphs.c
@@ -115,12 +115,12 @@ int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f ) {
/* Parameters */
- if (! EMPTY(LLIST(key_val))(&root->value->params)) {
+ if (! EMPTY(LLIST(PAIR(strbuf,strbuf)))(&root->value->params)) {
fprintf(f, "subgraph \"cluster_param_%p\"{\n color=blue;\n", root);
- FOR(LLIST(key_val), link, &root->value->params) {
+ FOR(LLIST(PAIR(strbuf,strbuf)), link, &root->value->params) {
fprintf(f, "\"%p\" [shape=rectangle, label=\"%s := %s\"];", link,
- link->value->key.mem,
- link->value->val.mem);
+ link->value->left.mem,
+ link->value->right.mem);
}
fputs("}", f);
@@ -133,7 +133,7 @@ int trie_to_dot_helper ( TRIE_NODE(T)* root, FILE* f ) {
*/
fprintf(f, "\"link_%p\" [label=params style=filled fillcolor=lightblue];\n", root);
fprintf(f, "\"%p\" -> \"link_%p\";\n", root, root);
- FOR(LLIST(key_val), link, &root->value->params) {
+ FOR(LLIST(PAIR(strbuf,strbuf)), link, &root->value->params) {
fprintf(f, "\"link_%p\" -> \"%p\";\n", root, link);
}
}
diff --git a/macro.h b/macro.h
index e1bff786..55bff012 100644
--- a/macro.h
+++ b/macro.h
@@ -31,7 +31,8 @@
*
* nameᐸTᐳ
*/
-#define TEMPL(name, T) TP4(name, \U00001438 , T, \U00001433 )
+#define TEMPL(name, T) TP4(name, \U00001438 , T, \U00001433 )
+#define TEMPL2(name, T, V) TP6(name, \U00001438\U00001438 , T , \U00001433_\U00001438 , V, \U00001433\U00001433)
#define TEMPL_N(name, T, argcount) TP6(name, \U00001438 , T, _, argcount, \U00001433 )
/* Constructor type name */
diff --git a/pair.h b/pair.h
new file mode 100644
index 00000000..e3822a9b
--- /dev/null
+++ b/pair.h
@@ -0,0 +1,19 @@
+#ifndef PAIR_H
+#define PAIR_H
+
+#define PAIR(T, V) TEMPL2(pair, T, V)
+
+#endif /* PAIR_H */
+#if defined(T) && defined(V)
+
+typedef struct {
+ T left;
+ V right;
+} PAIR(T, V);
+
+INIT_F(PAIR(T, V));
+FREE_F(PAIR(T, V));
+FMT_F(PAIR(T, V));
+int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src);
+
+#endif
diff --git a/pair.inc.h b/pair.inc.h
new file mode 100644
index 00000000..53f746bd
--- /dev/null
+++ b/pair.inc.h
@@ -0,0 +1,33 @@
+#if ! (defined(T) && defined(V))
+#error "Both T and V must be defiend here"
+#else
+
+INIT_F(PAIR(T, V)) {
+ INIT(T, &this->left);
+ INIT(V, &this->right);
+
+ return 0;
+}
+
+FREE_F(PAIR(T, V)) {
+ FREE(T)(&this->left);
+ FREE(V)(&this->right);
+ return 0;
+}
+
+FMT_F(PAIR(T, V)) {
+ char lbuf[100];
+ char rbuf[100];
+ FMT(T)(&this->left, lbuf);
+ FMT(V)(&this->right, rbuf);
+
+ return sprintf(buf, "<%s, %s>", lbuf, rbuf);
+}
+
+int DEEP_COPY(PAIR(T, V)) (PAIR(T, V)* dest, PAIR(T, V)* src) {
+ DEEP_COPY(T)(&dest->left, &src->left);
+ DEEP_COPY(V)(&dest->right, &src->right);
+ return 0;
+}
+
+#endif /* T & V */
diff --git a/parse.c b/parse.c
index 9605a163..a37c9874 100644
--- a/parse.c
+++ b/parse.c
@@ -13,10 +13,6 @@
#include "linked_list.inc.h"
#undef TYPE
-#define TYPE key_val
-#include "linked_list.inc.h"
-#undef TYPE
-
/*
* name *(";" param) ":" value CRLF
*/
@@ -30,7 +26,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
// keylen -+ |
// | |
SNEW(content_line, cline, 100, 100);
- SNEW(key_val, kv);
+ SNEW(PAIR(strbuf,strbuf), kv);
char c;
while ( (c = fgetc(f)) != EOF) {
@@ -85,8 +81,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
* Border between param {key, value}
*/
} else if (p_ctx == p_param_name && c == '=') {
- DEEP_COPY(strbuf)(&kv.key, &ctx.str);
- strbuf_cap(&kv.key);
+ DEEP_COPY(strbuf)(&kv.left, &ctx.str);
+ strbuf_cap(&kv.right);
strbuf_soft_reset(&ctx.str);
p_ctx = p_param_value;
@@ -101,7 +97,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
} else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) {
strbuf* dest;
if (p_ctx == p_key) dest = &cline.key;
- else if (p_ctx == p_param_value) dest = &kv.val;
+ else if (p_ctx == p_param_value) dest = &kv.right;
DEEP_COPY(strbuf)(dest, &ctx.str);
strbuf_cap(dest);
@@ -109,9 +105,9 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
if (p_ctx == p_param_value) {
/* push kv pair */
- NEW (key_val, _kv);
- DEEP_COPY(key_val)(_kv, &kv);
- PUSH(LLIST(key_val))(&cline.params, _kv);
+ NEW (PAIR(strbuf,strbuf), _kv);
+ DEEP_COPY(PAIR(strbuf,strbuf))(_kv, &kv);
+ PUSH(LLIST(PAIR(strbuf,strbuf)))(&cline.params, _kv);
}
if (c == ':') p_ctx = p_value;
@@ -146,7 +142,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) {
FREE(parse_ctx)(&ctx);
- FREE(key_val)(&kv);
+ FREE(PAIR(strbuf,strbuf))(&kv);
return 0;
}
@@ -196,8 +192,8 @@ int handle_kv (
&PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines,
c->key.mem, c);
- if ( SIZE(LLIST(key_val))(&c->params) != 0 ) {
- RESET(LLIST(key_val))(&cline->params);
+ if ( SIZE(LLIST(PAIR(strbuf,strbuf)))(&c->params) != 0 ) {
+ RESET(LLIST(PAIR(strbuf,strbuf)))(&cline->params);
}
}
diff --git a/vcal.c b/vcal.c
index e1df1f30..a36e584d 100644
--- a/vcal.c
+++ b/vcal.c
@@ -15,6 +15,17 @@
#include "vector.inc.h"
#undef TYPE
+#define T strbuf
+#define V strbuf
+#include "pair.inc.h"
+#undef T
+#undef V
+
+#define TYPE PAIR(strbuf, strbuf)
+#include "linked_list.inc.h"
+#undef TYPE
+
+
INIT_F(vcomponent) {
(void) this;
ERR("Do not use");
@@ -59,7 +70,7 @@ content_line* RESOLVE(content_line)
*/
APPEND(LLIST(strbuf)) (&dest->vals, &new->vals);
- APPEND(LLIST(key_val)) (&dest->params, &new->params);
+ APPEND(LLIST(PAIR(strbuf,strbuf))) (&dest->params, &new->params);
FREE(strbuf)(&new->key);
free(new);
@@ -75,7 +86,7 @@ INIT_F(content_line) {
INIT(strbuf, &this->key);
INIT( LLIST(strbuf), &this->vals );
- INIT( LLIST(key_val), &this->params );
+ INIT( LLIST(PAIR(strbuf,strbuf)), &this->params );
return 0;
}
@@ -86,7 +97,7 @@ INIT_F(content_line, int keylen, int vallen) {
NEW(strbuf, s, vallen);
PUSH(LLIST(strbuf))(&this->vals, s);
- INIT( LLIST(key_val), &this->params );
+ INIT( LLIST(PAIR(strbuf,strbuf)), &this->params );
return 0;
}
@@ -95,7 +106,7 @@ FREE_F(content_line) {
FREE(strbuf)(&this->key);
FREE(LLIST(strbuf))(&this->vals);
- FREE(LLIST(key_val))(&this->params);
+ FREE(LLIST(PAIR(strbuf,strbuf)))(&this->params);
return 0;
}
@@ -103,7 +114,7 @@ FREE_F(content_line) {
int content_line_copy (content_line* dest, content_line* src) {
DEEP_COPY(strbuf)(&dest->key, &src->key);
DEEP_COPY(LLIST(strbuf))(&dest->vals, &src->vals);
- DEEP_COPY(LLIST(key_val))(&dest->params, &src->params);
+ DEEP_COPY(LLIST(PAIR(strbuf,strbuf)))(&dest->params, &src->params);
return 0;
}
@@ -151,34 +162,7 @@ FMT_F(vcomponent) {
FMT_F(content_line) {
char str_a[100], str_b[100], str_c[100];;
FMT(strbuf)(&this->key, str_a);
- FMT(LLIST(key_val))(&this->params, str_b);
+ FMT(LLIST(PAIR(strbuf, strbuf)))(&this->params, str_b);
FMT(LLIST(strbuf))(&this->vals, str_c);
return sprintf(buf, "[[cl|%s] params := %s vals := %s]", str_a, str_b, str_c);
}
-
-INIT_F(key_val) {
- INIT(strbuf, &this->key, 100);
- INIT(strbuf, &this->val, 100);
- return 0;
-}
-
-FREE_F(key_val) {
- FREE(strbuf)(&this->key);
- FREE(strbuf)(&this->val);
- return 0;
-}
-
-FMT_F(key_val) {
- char keybuf[100];
- char valbuf[100];
- FMT(strbuf)(&this->key, keybuf);
- FMT(strbuf)(&this->val, valbuf);
-
- return sprintf(buf, "[[%s] := [%s]]", keybuf, valbuf);
-}
-
-int DEEP_COPY(key_val) (key_val* dest, key_val* src) {
- DEEP_COPY(strbuf)(&dest->key, &src->key);
- DEEP_COPY(strbuf)(&dest->val, &src->val);
- return 0;
-}
diff --git a/vcal.h b/vcal.h
index c86144f6..0451a037 100644
--- a/vcal.h
+++ b/vcal.h
@@ -10,17 +10,13 @@
// #include "trie.h"
#undef TYPE
-typedef struct {
- strbuf key;
- strbuf val;
-} key_val;
-
-INIT_F(key_val);
-FREE_F(key_val);
-FMT_F(key_val);
-int DEEP_COPY(key_val) (key_val* dest, key_val* src);
+#define T strbuf
+#define V strbuf
+#include "pair.h"
+#undef T
+#undef V
-#define TYPE key_val
+#define TYPE PAIR(strbuf, strbuf)
#include "linked_list.h"
#undef TYPE
@@ -29,7 +25,7 @@ typedef struct {
LLIST(strbuf) vals;
- LLIST(key_val) params;
+ LLIST(PAIR(strbuf, strbuf)) params;
} content_line;
INIT_F(content_line);