Prettify usage()
[babeltrace.git] / formats / registry.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace
3 *
4 * Format Registry
5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
fc93b2bd 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
fc93b2bd
MD
17 */
18
4c8bfb7e 19#include <babeltrace/format.h>
fc93b2bd
MD
20#include <glib.h>
21#include <errno.h>
7fb21036 22#include <stdio.h>
fc93b2bd 23
d2b8ea6b
MD
24struct walk_data {
25 FILE *fp;
26 int iter;
27};
28
fc93b2bd
MD
29static int init_done;
30void __attribute__((constructor)) format_init(void);
31void __attribute__((destructor)) format_finalize(void);
32
33/*
34 * Format registry hash table contains the registered formats. Format
35 * registration is typically performed by a format plugin.
36 * TODO: support plugin unload (unregistration of formats).
37 */
38GHashTable *format_registry;
39
40struct format *bt_lookup_format(GQuark qname)
41{
42 if (!init_done)
43 return NULL;
44 return g_hash_table_lookup(format_registry,
4c8bfb7e 45 (gconstpointer) (unsigned long) qname);
fc93b2bd
MD
46}
47
7fb21036
MD
48static void show_format(gpointer key, gpointer value, gpointer user_data)
49{
d2b8ea6b 50 struct walk_data *data = user_data;
7fb21036 51
d2b8ea6b 52 fprintf(data->fp, "%s%s", data->iter ? ", " : "",
7fb21036 53 g_quark_to_string((GQuark) (unsigned long) key));
d2b8ea6b 54 data->iter++;
7fb21036
MD
55}
56
57void bt_fprintf_format_list(FILE *fp)
58{
d2b8ea6b
MD
59 struct walk_data data;
60
61 data.fp = fp;
62 data.iter = 0;
63
64 fprintf(fp, "Formats available: ");
7fb21036
MD
65 if (!init_done)
66 return;
d2b8ea6b
MD
67 g_hash_table_foreach(format_registry, show_format, &data);
68 if (data.iter == 0)
69 fprintf(fp, "<none>");
70 fprintf(fp, ".\n");
7fb21036
MD
71}
72
4c8bfb7e 73int bt_register_format(struct format *format)
fc93b2bd
MD
74{
75 if (!init_done)
76 format_init();
77
4c8bfb7e 78 if (bt_lookup_format(format->name))
fc93b2bd
MD
79 return -EEXIST;
80
81 g_hash_table_insert(format_registry,
4c8bfb7e 82 (gpointer) (unsigned long) format->name,
fc93b2bd
MD
83 format);
84 return 0;
85}
86
87void format_init(void)
88{
0860ab48
MD
89 if (init_done)
90 return;
fc93b2bd
MD
91 format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
92 assert(format_registry);
93 init_done = 1;
94}
95
4c8bfb7e 96void format_finalize(void)
fc93b2bd
MD
97{
98 g_hash_table_destroy(format_registry);
99}
This page took 0.039747 seconds and 4 git commands to generate.