Code

src/graph_list.c: First take at writing the graph list to disk.
[collection4.git] / src / graph_list.c
index a33697f4c084b6b49ef5ef19b034fe871a8c5b95..26e182bdc7bd8b6a9f89db82d6395ec50919bfbe 100644 (file)
@@ -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
  */
@@ -391,18 +436,25 @@ int gl_search (search_info_t *si, /* {{{ */
   if ((si == NULL) || (callback == NULL))
     return (EINVAL);
 
-  ident = search_to_ident (si);
-  if (ident == NULL)
+  if (search_has_selector (si))
   {
-    fprintf (stderr, "gl_search: search_to_ident failed\n");
-    return (-1);
+    ident = search_to_ident (si);
+    if (ident == NULL)
+    {
+      fprintf (stderr, "gl_search: search_to_ident failed\n");
+      return (-1);
+    }
+  }
+  else
+  {
+    ident = NULL;
   }
 
   for (i = 0; i < gl_active_num; i++)
   {
     int status;
 
-    if (!graph_matches_ident (gl_active[i], ident))
+    if ((ident != NULL) && !graph_ident_intersect (gl_active[i], ident))
       continue;
 
     status = graph_search_inst (gl_active[i], si,
@@ -416,7 +468,7 @@ int gl_search (search_info_t *si, /* {{{ */
   {
     int status;
 
-    if (!graph_matches_ident (gl_dynamic[i], ident))
+    if ((ident != NULL) && !graph_ident_intersect (gl_dynamic[i], ident))
       continue;
 
     status = graph_search_inst (gl_dynamic[i], si,
@@ -544,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 */