From 990c22b6d4bc48aad0a8b496b193b83e61de42ed Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 21 Jul 2010 13:42:02 +0200 Subject: [PATCH] src/graph_list.c: First take at writing the graph list to disk. --- src/graph_list.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/graph_list.c b/src/graph_list.c index d2416f4..26e182b 100644 --- a/src/graph_list.c +++ b/src/graph_list.c @@ -222,6 +222,51 @@ static int gl_clear_instances (void) /* {{{ */ return (0); } /* }}} int gl_clear_instances */ +static void gl_dump_cb (void *ctx, /* {{{ */ + const char *str, unsigned int len) +{ + FILE *fh = ctx; + + /* FIXME: Has everything been written? */ + fwrite ((void *) str, /* size = */ 1, /* nmemb = */ len, fh); +} /* }}} void gl_dump_cb */ + +static int gl_dump (void) /* {{{ */ +{ + FILE *fh; + yajl_gen handler; + yajl_gen_config handler_config = { /* pretty = */ 1, /* indent = */ " " }; + size_t i; + + /* FIXME: Lock the file */ + fh = fopen ("/tmp/collection4.json", "w"); + if (fh == NULL) + return (errno); + + handler = yajl_gen_alloc2 (gl_dump_cb, &handler_config, + /* alloc funcs = */ NULL, /* ctx = */ fh); + if (handler == NULL) + { + fclose (fh); + return (-1); + } + + yajl_gen_array_open (handler); + + for (i = 0; i < gl_active_num; i++) + graph_to_json (gl_active[i], handler); + + for (i = 0; i < gl_dynamic_num; i++) + graph_to_json (gl_dynamic[i], handler); + + yajl_gen_array_close (handler); + + yajl_gen_free (handler); + fclose (fh); + + return (0); +} /* }}} int gl_dump */ + /* * Global functions */ @@ -551,6 +596,8 @@ int gl_update (void) /* {{{ */ for (i = 0; i < gl_active_num; i++) graph_sort_instances (gl_active[i]); + gl_dump (); + return (status); } /* }}} int gl_update */ -- 2.30.2