Miroslav Lichvar <mlichvar(a)redhat.com> writes:
+/* Returns false if write failed */
+bool save_conf_file(const char *path, map_string_h *settings)
+{
+ bool ret;
+ FILE *out;
+ char *temp_path, *name, *value;
+ GHashTableIter iter;
+
+ ret = false, out = NULL;
+
+ if (!(temp_path = xasprintf("%s.tmp", path)) ||
xasprintf always returns non-null value, otherwise xasprintf dies
+ !create_parentdir(temp_path) ||
+ !(out = fopen(temp_path, "w")))
+ goto cleanup;
coding style violation; put || at the beginning like
if (!(temp_path = xasprintf("%s.tmp", path))
|| !create_parentdir(temp_path)
|| !(out = fopen(temp_path, "w")))
+
+ g_hash_table_iter_init(&iter, settings);
+ while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
+ fprintf(out, "%s = \"%s\"\n", name, value);
do we have plan to store strings with spaces or something that needs
quotes?
I'd prefer key = value not key = "value"
+ fclose(out);
+ out = NULL;
+
+ if (!rename(temp_path, path))
+ goto cleanup;
+
+ ret = true; /* success */
+
+cleanup:
+ if (out)
+ fclose(out);
+ free(temp_path);
+
+ return ret;
+}
+
+static char *get_conf_path(const char *name)
+{
+ char *HOME = getenv("HOME"), *s, *conf;
+
+ s = xasprintf("%s/%s", ".abrt/settings", name);
+ conf = concat_path_file(HOME, s);
+ free(s);
+ return conf;
+}
+
+bool save_user_settings()
+{
+ if (!conf_path || !user_settings)
+ return true;
+
+ return save_conf_file(conf_path, user_settings);
+}
+
+bool load_user_settings(const char *application_name)
+{
+ if (conf_path)
+ free(conf_path);
+ conf_path = get_conf_path(application_name);
+
+ if (user_settings)
+ g_hash_table_destroy(user_settings);
+ user_settings = g_hash_table_new_full(
+ /*hash_func*/ g_str_hash,
+ /*key_equal_func:*/ g_str_equal,
+ /*key_destroy_func:*/ free,
+ /*value_destroy_func:*/ free);
+
+ return load_conf_file(conf_path, user_settings, false);
+}
+
+void set_user_setting(const char *name, const char *value)
+{
+ if (value)
+ g_hash_table_replace(user_settings, xstrdup(name), xstrdup(value));
+ else
+ g_hash_table_remove(user_settings, name);
+}
+
+const char *get_user_setting(const char *name)
+{
+ return g_hash_table_lookup(user_settings, name);
+}
rest is ok
--
Save the whales. Feed the hungry. Free the mallocs