/* Jungle Monkey * Copyright (C) 1999-2001 The Regents of the University of Michigan * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include "ggui_config.h" #define CONFIG_FILE ".jmconfig" #define MAX_VECTOR_ELEMENTS 1024 #define VECTOR_DELIMITOR_S ";" #define BUFFER_LEN 32000 #ifdef JM_ENABLE_GNOME #include void ggui_config_init (void) { /* do nothing */ } void ggui_config_shutdown (void) { ggui_config_sync (); } void ggui_config_sync (void) { gnome_config_sync (); } gchar* ggui_config_get_string (gchar* key, gchar* def) { gchar buffer[1024]; g_assert (strlen(key) < 1024); if (def) g_snprintf (buffer, sizeof(buffer), "%s=%s", key, def); else g_snprintf (buffer, sizeof(buffer), "%s", key); return gnome_config_get_string (buffer); } gint ggui_config_get_int (gchar* key, gint def) { gchar buffer[1024]; g_assert (strlen(key) < 1024); g_snprintf (buffer, sizeof(buffer), "%s=%d", key, def); return gnome_config_get_int (buffer); } gboolean ggui_config_get_bool (gchar* key, gboolean def) { gchar buffer[1024]; g_assert (strlen(key) < 1024); if (def) g_snprintf (buffer, sizeof(buffer), "%s=true", key); else g_snprintf (buffer, sizeof(buffer), "%s=false", key); return gnome_config_get_bool (buffer); } gchar** ggui_config_get_vector (gchar* key) { gint argc; gchar** argp; gchar** rv; gnome_config_get_vector (key, &argc, &argp); rv = g_new (gchar*, argc + 1); memcpy (rv, argp, argc * sizeof(gchar*)); rv[argc] = NULL; /* TODO: Copy strings in vector */ return rv; } void ggui_config_set_string (gchar* key, gchar* value) { gnome_config_set_string (key, value); } void ggui_config_set_int (gchar* key, gint value) { gnome_config_set_int (key, value); } void ggui_config_set_bool (gchar* key, gboolean value) { gnome_config_set_bool (key, value); } void ggui_config_set_vector (gchar* key, gchar** argp) { gint i = 0; g_return_if_fail (argp); while (argp[i]) ++i; gnome_config_set_vector (key, i, (const char**) argp); } #else typedef struct _ConfigEntry { gchar* key; gchar* value; } ConfigEntry; static GHashTable* entries = NULL; static void config_delete_hfunc (gpointer key, gpointer value, gpointer user_data); static void config_write_hfunc (gpointer key, gpointer value, gpointer user_data); void ggui_config_init (void) { gchar buffer[BUFFER_LEN]; gchar* config_file; FILE* file; entries = g_hash_table_new (g_str_hash, g_str_equal); config_file = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, CONFIG_FILE, NULL); file = fopen (config_file, "r"); g_free (config_file); if (!file) return; while (fgets(buffer, sizeof(buffer), file)) { gchar* key; key = strtok (buffer, "="); if (key) { gchar* value = strtok (NULL, "\n\r"); if (value) ggui_config_set_string (key, value); } } } void ggui_config_shutdown (void) { ggui_config_sync (); g_hash_table_foreach (entries, config_delete_hfunc, NULL); g_hash_table_destroy (entries); entries = NULL; } static void config_delete_hfunc (gpointer key, gpointer value, gpointer user_data) { ConfigEntry* entry = (ConfigEntry*) value; g_free (entry->key); g_free (entry->value); g_free (entry); } void ggui_config_sync (void) { gchar* config_file; FILE* file; g_return_if_fail (config_write_hfunc); config_file = g_strconcat(g_get_home_dir(), G_DIR_SEPARATOR_S, CONFIG_FILE, NULL); file = fopen (config_file, "w"); g_free (config_file); g_return_if_fail (file); g_hash_table_foreach (entries, config_write_hfunc, file); fclose (file); } static void config_write_hfunc (gpointer key, gpointer value, gpointer user_data) { gchar* ke = (gchar*) key; ConfigEntry* entry = (ConfigEntry*) value; FILE* file = (FILE*) user_data; g_return_if_fail (ke); g_return_if_fail (entry); g_return_if_fail (file); g_return_if_fail (entry->value); fprintf (file, "%s=%s\n", ke, entry->value); } gchar* ggui_config_get_string (gchar* key, gchar* def) { ConfigEntry* entry = (ConfigEntry*) g_hash_table_lookup (entries, key); if (!entry) return def; return g_strdup (entry->value); } gint ggui_config_get_int (gchar* key, gint def) { ConfigEntry* entry = (ConfigEntry*) g_hash_table_lookup (entries, key); if (!entry) return def; return atoi(entry->value); } gboolean ggui_config_get_bool (gchar* key, gboolean def) { ConfigEntry* entry = (ConfigEntry*) g_hash_table_lookup (entries, key); if (!entry) return def; if (!strcasecmp(entry->value, "false")) return FALSE; return TRUE; } gchar** ggui_config_get_vector (gchar* key) { ConfigEntry* entry = (ConfigEntry*) g_hash_table_lookup (entries, key); if (!entry) return NULL; return g_strsplit (entry->value, VECTOR_DELIMITOR_S, MAX_VECTOR_ELEMENTS); } void ggui_config_set_string (gchar* key, gchar* value) { ConfigEntry* entry; g_return_if_fail (key); entry = (ConfigEntry*) g_hash_table_lookup (entries, key); if (entry) { if (value) { g_free (entry->value); entry->value = g_strdup (value); } else { g_hash_table_remove (entries, key); g_free (entry->key); g_free (entry->value); g_free (entry); } } else { if (value) { entry = g_new (ConfigEntry, 1); entry->key = g_strdup (key); entry->value = g_strdup (value); g_hash_table_insert (entries, entry->key, entry); } } } void ggui_config_set_int (gchar* key, gint value) { gchar buf[16]; g_snprintf (buf, sizeof(buf), "%d", value); ggui_config_set_string (key, buf); } void ggui_config_set_bool (gchar* key, gboolean value) { ggui_config_set_string (key, value ? "true" : "false"); } void ggui_config_set_vector (gchar* key, gchar** argp) { gchar* str; str = g_strjoinv (VECTOR_DELIMITOR_S, argp); ggui_config_set_string (key, str); g_free (str); } #endif