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