/* * Copyright 2015 (c) Anna Schumaker. */ #include #include static GHashTable *gui_settings = NULL; static struct file gui_settings_file = FILE_INIT("settings", 0, 0); static void __settings_save_item(gpointer key, gpointer value, gpointer data) { file_writef(&gui_settings_file, "%s %u\n", (const gchar *)key, GPOINTER_TO_UINT(value)); } static void __settings_save() { file_open(&gui_settings_file, OPEN_WRITE); file_writef(&gui_settings_file, "%u\n", g_hash_table_size(gui_settings)); g_hash_table_foreach(gui_settings, __settings_save_item, NULL); file_close(&gui_settings_file); } static void __settings_read() { unsigned int num, i, value; gchar *key; file_readf(&gui_settings_file, "%u\n", &num); for (i = 0; i < num; i++) { file_readf(&gui_settings_file, "%m[^ ] %u\n", &key, &value); g_hash_table_insert(gui_settings, key, GUINT_TO_POINTER(value)); } } void gui_settings_init() { gui_settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); if (file_open(&gui_settings_file, OPEN_READ)) __settings_read(); file_close(&gui_settings_file); } void gui_settings_deinit() { g_hash_table_destroy(gui_settings); gui_settings = NULL; } void gui_settings_set(const gchar *key, unsigned int value) { if (gui_settings) { g_hash_table_replace(gui_settings, g_strdup(key), GUINT_TO_POINTER(value)); __settings_save(); } } unsigned int gui_settings_get(const gchar *key) { if (gui_settings) return GPOINTER_TO_UINT(g_hash_table_lookup(gui_settings, key)); return 0; } bool gui_settings_has(const gchar *key) { if (gui_settings) return g_hash_table_contains(gui_settings, key); return false; } #ifdef CONFIG_TESTING GHashTable *test_get_gui_settings() { return gui_settings; } #endif /* CONFIG_TESTING */