Prettify usage()
[babeltrace.git] / formats / registry.c
index 6b28cbeb0b1cad5354fd64bb688bbf5e1b2aa750..656d6360aca0030856c3a08830998cea1ea4119f 100644 (file)
  * all copies or substantial portions of the Software.
  */
 
+#include <babeltrace/format.h>
 #include <glib.h>
 #include <errno.h>
+#include <stdio.h>
+
+struct walk_data {
+       FILE *fp;
+       int iter;
+};
 
 static int init_done;
 void __attribute__((constructor)) format_init(void);
@@ -35,31 +42,58 @@ struct format *bt_lookup_format(GQuark qname)
        if (!init_done)
                return NULL;
        return g_hash_table_lookup(format_registry,
-                                  (gconstpointer) (unsigned long) qname)
+                                  (gconstpointer) (unsigned long) qname);
+}
+
+static void show_format(gpointer key, gpointer value, gpointer user_data)
+{
+       struct walk_data *data = user_data;
+
+       fprintf(data->fp, "%s%s", data->iter ? ", " : "",
+               g_quark_to_string((GQuark) (unsigned long) key));
+       data->iter++;
+}
+
+void bt_fprintf_format_list(FILE *fp)
+{
+       struct walk_data data;
+
+       data.fp = fp;
+       data.iter = 0;
+
+       fprintf(fp, "Formats available: ");
+       if (!init_done)
+               return;
+       g_hash_table_foreach(format_registry, show_format, &data);
+       if (data.iter == 0)
+               fprintf(fp, "<none>");
+       fprintf(fp, ".\n");
 }
 
-int bt_register_format(const struct format *format)
+int bt_register_format(struct format *format)
 {
        if (!init_done)
                format_init();
 
-       if (bt_lookup_format(qname))
+       if (bt_lookup_format(format->name))
                return -EEXIST;
 
        g_hash_table_insert(format_registry,
-                           (gconstpointer) (unsigned long) format->name,
+                           (gpointer) (unsigned long) format->name,
                            format);
        return 0;
 }
 
 void format_init(void)
 {
+       if (init_done)
+               return;
        format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
        assert(format_registry);
        init_done = 1;
 }
 
-int format_finalize(void)
+void format_finalize(void)
 {
        g_hash_table_destroy(format_registry);
 }
This page took 0.023228 seconds and 4 git commands to generate.