aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-03 13:27:20 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-03 13:27:20 +0100
commit1fd1232450a215a37915e3657964f2dad7ce72d6 (patch)
tree907cb87e027c12ffd550081513340db5aef3589a
parentMade .x files secoundary in makefile. (diff)
downloadcalp-1fd1232450a215a37915e3657964f2dad7ce72d6.tar.gz
calp-1fd1232450a215a37915e3657964f2dad7ce72d6.tar.xz
Add graphviz output for TRIE's.
Add simple output in dot format for trie structures. Along with a slightly updated main which handles a few more command line arguments. Also updated makefile to generate pdf's from dot files and dot-files from parse.
-rw-r--r--Makefile8
-rw-r--r--calendar.c1
-rw-r--r--graphs.c17
-rw-r--r--graphs.h8
-rw-r--r--main.c41
-rw-r--r--trie.h8
-rw-r--r--trie.inc.h30
-rw-r--r--vcal.h1
8 files changed, 107 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index e1448eee..cea10f18 100644
--- a/Makefile
+++ b/Makefile
@@ -41,6 +41,14 @@ $(OBJDIR):
libguile-calendar.so: $(O_FILES)
$(CC) -shared -o $@ $^ $(LDFLAGS)
+CALDIR = cal
+.SECONDARY += %.dot
+%.dot: parse
+ ./parse $(CALDIR) -g $@
+
+%.pdf: %.dot
+ dot -Tpdf -o $@ $<
+
clean:
-rm parse
-rm $(OBJDIR)/*.o
diff --git a/calendar.c b/calendar.c
index f99eb775..abecef89 100644
--- a/calendar.c
+++ b/calendar.c
@@ -46,6 +46,7 @@ int read_vcalendar(vcalendar* cal, char* path) {
* Preferably, a special case is made for vdir structures
* which can assume that all headers are equal. */
parse_file(f, cal);
+ cal->events[cal->n_events - 1]->filename = fname;
fclose(f);
}
diff --git a/graphs.c b/graphs.c
new file mode 100644
index 00000000..931b181b
--- /dev/null
+++ b/graphs.c
@@ -0,0 +1,17 @@
+#include "graphs.h"
+
+#include <stdio.h>
+
+int create_graph (vevent* ev, char* filename) {
+ FILE* f = fopen(filename, "w");
+
+ fputs("digraph {\n rankdir=LR;", f);
+ TRIE_DOT(content_line)(&ev->clines, f);
+ fputs("}", f);
+
+ fclose(f);
+
+ printf("Wrote '%s' to '%s'\n", ev->filename, filename);
+
+ return 0;
+}
diff --git a/graphs.h b/graphs.h
new file mode 100644
index 00000000..399079b6
--- /dev/null
+++ b/graphs.h
@@ -0,0 +1,8 @@
+#ifndef GRAPHS_H
+#define GRAPHS_H
+
+#include "vcal.h"
+
+int create_graph (vevent* ev, char* filename);
+
+#endif /* GRAPHS_H */
diff --git a/main.c b/main.c
index 9ee1288b..27385b55 100644
--- a/main.c
+++ b/main.c
@@ -1,25 +1,52 @@
#include <errno.h>
+#include <stdio.h>
+#include <string.h>
#include "calendar.h"
#include "macro.h"
#include "vcal.h"
-#include "stdio.h"
+#include "graphs.h"
+
+typedef struct {
+ int argc;
+ char** argv;
+} arg;
+
+int arg_shift (arg* a) {
+ if (a->argc == 0) return 0;
+
+ ++a->argv;
+ return --a->argc;
+
+}
int main (int argc, char* argv[argc]) {
- if (argc < 2) {
+ arg args = { .argc = argc, .argv = argv };
+
+ if (arg_shift(&args) == 0) {
puts("Please give vdir as first argument");
exit (1);
}
SNEW(vcalendar, cal);
- read_vcalendar(&cal, argv[1]);
+ read_vcalendar(&cal, args.argv[0]);
+ arg_shift(&args);
- printf("\nParsed calendar file containing [%lu] events\n",
- cal.n_events);
- for (size_t i = 0; i < cal.n_events; i++) {
- printf("%3lu. %s\n", i + 1, get_property(cal.events[i], "SUMMARY")->val.mem);
+ if (args.argc == 0 || strcmp(args.argv[0], "-p") == 0) {
+ printf("\nParsed calendar file containing [%lu] events\n",
+ cal.n_events);
+ for (size_t i = 0; i < cal.n_events; i++) {
+ printf("%3lu. %s\n", i + 1, get_property(cal.events[i], "SUMMARY")->val.mem);
+ }
+ } else if (strcmp(args.argv[0], "-g") == 0) {
+ if (arg_shift(&args) == 0) {
+ create_graph(cal.events[0], "graph.dot");
+ } else {
+ create_graph(cal.events[0], args.argv[0]);
+ }
}
+
free_vcalendar(&cal);
}
diff --git a/trie.h b/trie.h
index 52e1072d..20b08ec9 100644
--- a/trie.h
+++ b/trie.h
@@ -1,6 +1,8 @@
#ifndef TRIE_H
#define TRIE_H
+#include <stdio.h>
+
#include "macro.h"
#define TRIE(T) TP(trie__, T)
@@ -15,6 +17,9 @@
#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_HELP(T) TP(trie_to_dot_helper__, T)
+
#endif /* TRIE_H */
#ifdef TYPE
@@ -45,4 +50,7 @@ int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node );
int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie );
+int TRIE_DOT(TYPE) ( TRIE(TYPE)*, FILE* );
+int TRIE_DOT_HELP(TYPE) ( TRIE_NODE(TYPE)*, FILE* );
+
#endif /* TYPE */
diff --git a/trie.inc.h b/trie.inc.h
index 50aeff29..316b54b2 100644
--- a/trie.inc.h
+++ b/trie.inc.h
@@ -110,4 +110,34 @@ int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ) {
return TRIE_NODE_FREE(TYPE)(trie->root);
}
+int TRIE_DOT(TYPE) ( TRIE(TYPE)* trie, FILE* f ) {
+ TRIE_NODE(TYPE)* n = trie->root->child;
+ fputs("root;", f);
+ while (n != NULL) {
+ fprintf(f, "root -> \"%p\"\n",
+ (void*) n);
+ fprintf(f, "subgraph \"cluster_%c\" {\n",
+ n->c);
+ TRIE_DOT_HELP(TYPE) ( n, f );
+ fputs("}", f);
+ n = n->next;
+ }
+ return 0;
+}
+
+int TRIE_DOT_HELP(TYPE) ( TRIE_NODE(TYPE)* root, FILE* f ) {
+ fprintf(f, "\"%p\"[label = \"%c\" style=filled fillcolor=\"%s\"];\n",
+ (void*) root, root->c,
+ root->value == NULL ? "white" : "green");
+ TRIE_NODE(TYPE)* child = root->child;
+
+ while (child != NULL) {
+ fprintf(f, "\"%p\" -> \"%p\";\n",
+ (void*) root, (void*) child);
+ TRIE_DOT_HELP(TYPE)(child, f);
+ child = child->next;
+ }
+ return 0;
+}
+
#endif /* TYPE */
diff --git a/vcal.h b/vcal.h
index d6218399..32564cb2 100644
--- a/vcal.h
+++ b/vcal.h
@@ -32,6 +32,7 @@ int CONSTRUCTOR_DECL(content_line, int keylen, int vallen);
#undef TYPE
typedef struct s_vevent {
+ char* filename;
TRIE(content_line) clines;
} vevent;