diff options
author | Leandro Pereira <leandro@hardinfo.org> | 2012-01-26 22:14:43 -0200 |
---|---|---|
committer | Leandro Pereira <leandro@hardinfo.org> | 2012-01-26 22:14:43 -0200 |
commit | d3b9aa19f04e6be78afb8497e7808a3d42042bf2 (patch) | |
tree | 340e9be8f863e1ad29e1da91cd87a66409d6f706 /hardinfo | |
parent | d9e242117b7a73dd3057af25956027a361f7999f (diff) |
Better manage moreinfo hashes
Have only one for the whole program and use per-module namespaces/prefixes
so that each module can clear its own information easily.
Diffstat (limited to 'hardinfo')
-rw-r--r-- | hardinfo/hardinfo.c | 5 | ||||
-rw-r--r-- | hardinfo/util.c | 98 |
2 files changed, 103 insertions, 0 deletions
diff --git a/hardinfo/hardinfo.c b/hardinfo/hardinfo.c index 784a3c06..ffe35920 100644 --- a/hardinfo/hardinfo.c +++ b/hardinfo/hardinfo.c @@ -115,6 +115,9 @@ int main(int argc, char **argv) /* initialize vendor database */ vendor_init(); + /* initialize moreinfo */ + moreinfo_init(); + if (params.run_xmlrpc_server) { g_type_init(); @@ -156,6 +159,8 @@ int main(int argc, char **argv) g_error("Don't know what to do. Exiting."); } + moreinfo_shutdown(); + DEBUG("finished"); return 0; } diff --git a/hardinfo/util.c b/hardinfo/util.c index 0f88c388..1640be8b 100644 --- a/hardinfo/util.c +++ b/hardinfo/util.c @@ -1288,3 +1288,101 @@ h_sysfs_read_string(gchar *endpoint, gchar *entry) return return_value; } +static GHashTable *_moreinfo = NULL; + +void +moreinfo_init(void) +{ + if (G_UNLIKELY(_moreinfo)) { + DEBUG("moreinfo already initialized"); + return; + } + DEBUG("initializing moreinfo"); + _moreinfo = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); +} + +void +moreinfo_shutdown(void) +{ + if (G_UNLIKELY(!_moreinfo)) { + DEBUG("moreinfo not initialized"); + return; + } + DEBUG("shutting down moreinfo"); + g_hash_table_destroy(_moreinfo); + _moreinfo = NULL; +} + +void +moreinfo_add_with_prefix(gchar *prefix, gchar *key, gchar *value) +{ + if (G_UNLIKELY(!_moreinfo)) { + DEBUG("moreinfo not initialized"); + return; + } + + if (prefix) { + gchar *hashkey = g_strconcat(prefix, ":", key, NULL); + g_hash_table_insert(_moreinfo, hashkey, value); + return; + } + + g_hash_table_insert(_moreinfo, g_strdup(key), value); +} + +void +moreinfo_add(gchar *key, gchar *value) +{ + moreinfo_add_with_prefix(NULL, key, value); +} + +static gboolean +_moreinfo_del_cb(gpointer key, gpointer value, gpointer data) +{ + return g_str_has_prefix(key, data); +} + +void +moreinfo_del_with_prefix(gchar *prefix) +{ + if (G_UNLIKELY(!_moreinfo)) { + DEBUG("moreinfo not initialized"); + return; + } + + g_hash_table_foreach_remove(_moreinfo, _moreinfo_del_cb, prefix); +} + +void +moreinfo_clear(void) +{ + if (G_UNLIKELY(!_moreinfo)) { + DEBUG("moreinfo not initialized"); + return; + } + h_hash_table_remove_all(_moreinfo); +} + +gchar * +moreinfo_lookup_with_prefix(gchar *prefix, gchar *key) +{ + if (G_UNLIKELY(!_moreinfo)) { + DEBUG("moreinfo not initialized"); + return; + } + + if (prefix) { + gchar *lookup_key = g_strconcat(prefix, ":", key, NULL); + gchar *result = g_hash_table_lookup(_moreinfo, lookup_key); + g_free(lookup_key); + return result; + } + + return g_hash_table_lookup(_moreinfo, key); +} + +gchar * +moreinfo_lookup(gchar *key) +{ + return moreinfo_lookup_with_prefix(NULL, key); +} |