Commit | Line | Data |
---|---|---|
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 MD |
23 | |
24 | static int init_done; | |
25 | void __attribute__((constructor)) format_init(void); | |
26 | void __attribute__((destructor)) format_finalize(void); | |
27 | ||
28 | /* | |
29 | * Format registry hash table contains the registered formats. Format | |
30 | * registration is typically performed by a format plugin. | |
31 | * TODO: support plugin unload (unregistration of formats). | |
32 | */ | |
33 | GHashTable *format_registry; | |
34 | ||
35 | struct format *bt_lookup_format(GQuark qname) | |
36 | { | |
37 | if (!init_done) | |
38 | return NULL; | |
39 | return g_hash_table_lookup(format_registry, | |
4c8bfb7e | 40 | (gconstpointer) (unsigned long) qname); |
fc93b2bd MD |
41 | } |
42 | ||
7fb21036 MD |
43 | static void show_format(gpointer key, gpointer value, gpointer user_data) |
44 | { | |
45 | FILE *fp = user_data; | |
46 | ||
47 | fprintf(fp, "format: %s\n", | |
48 | g_quark_to_string((GQuark) (unsigned long) key)); | |
49 | } | |
50 | ||
51 | void bt_fprintf_format_list(FILE *fp) | |
52 | { | |
53 | fprintf(fp, "Formats available:\n"); | |
54 | if (!init_done) | |
55 | return; | |
56 | g_hash_table_foreach(format_registry, show_format, fp); | |
57 | fprintf(fp, "End of formats available.\n"); | |
58 | } | |
59 | ||
4c8bfb7e | 60 | int bt_register_format(struct format *format) |
fc93b2bd MD |
61 | { |
62 | if (!init_done) | |
63 | format_init(); | |
64 | ||
4c8bfb7e | 65 | if (bt_lookup_format(format->name)) |
fc93b2bd MD |
66 | return -EEXIST; |
67 | ||
68 | g_hash_table_insert(format_registry, | |
4c8bfb7e | 69 | (gpointer) (unsigned long) format->name, |
fc93b2bd MD |
70 | format); |
71 | return 0; | |
72 | } | |
73 | ||
74 | void format_init(void) | |
75 | { | |
0860ab48 MD |
76 | if (init_done) |
77 | return; | |
fc93b2bd MD |
78 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
79 | assert(format_registry); | |
80 | init_done = 1; | |
81 | } | |
82 | ||
4c8bfb7e | 83 | void format_finalize(void) |
fc93b2bd MD |
84 | { |
85 | g_hash_table_destroy(format_registry); | |
86 | } |