Prettify usage()
[babeltrace.git] / formats / registry.c
... / ...
CommitLineData
1/*
2 * BabelTrace
3 *
4 * Format Registry
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <babeltrace/format.h>
20#include <glib.h>
21#include <errno.h>
22#include <stdio.h>
23
24struct walk_data {
25 FILE *fp;
26 int iter;
27};
28
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,
45 (gconstpointer) (unsigned long) qname);
46}
47
48static void show_format(gpointer key, gpointer value, gpointer user_data)
49{
50 struct walk_data *data = user_data;
51
52 fprintf(data->fp, "%s%s", data->iter ? ", " : "",
53 g_quark_to_string((GQuark) (unsigned long) key));
54 data->iter++;
55}
56
57void bt_fprintf_format_list(FILE *fp)
58{
59 struct walk_data data;
60
61 data.fp = fp;
62 data.iter = 0;
63
64 fprintf(fp, "Formats available: ");
65 if (!init_done)
66 return;
67 g_hash_table_foreach(format_registry, show_format, &data);
68 if (data.iter == 0)
69 fprintf(fp, "<none>");
70 fprintf(fp, ".\n");
71}
72
73int bt_register_format(struct format *format)
74{
75 if (!init_done)
76 format_init();
77
78 if (bt_lookup_format(format->name))
79 return -EEXIST;
80
81 g_hash_table_insert(format_registry,
82 (gpointer) (unsigned long) format->name,
83 format);
84 return 0;
85}
86
87void format_init(void)
88{
89 if (init_done)
90 return;
91 format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
92 assert(format_registry);
93 init_done = 1;
94}
95
96void format_finalize(void)
97{
98 g_hash_table_destroy(format_registry);
99}
This page took 0.022496 seconds and 4 git commands to generate.