summaryrefslogtreecommitdiffhomepage
path: root/mixlib
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2001-07-02 22:39:39 +0000
committerJose Antonio Ortega Ruiz <jao@gnu.org>2001-07-02 22:39:39 +0000
commit25829b27687166867b5f0a82838b48ffe5010723 (patch)
tree9a181fb1c5b40e9211742b9467d19bd6e256bbf9 /mixlib
parentebefa5584f9a6fc29bdcd85d38c790cda42306b8 (diff)
downloadmdk-25829b27687166867b5f0a82838b48ffe5010723.tar.gz
mdk-25829b27687166867b5f0a82838b48ffe5010723.tar.bz2
shared config handler
Diffstat (limited to 'mixlib')
-rw-r--r--mixlib/Makefile.am4
-rw-r--r--mixlib/mix_config.c186
-rw-r--r--mixlib/mix_config.h62
3 files changed, 251 insertions, 1 deletions
diff --git a/mixlib/Makefile.am b/mixlib/Makefile.am
index 5091e88..2c1f818 100644
--- a/mixlib/Makefile.am
+++ b/mixlib/Makefile.am
@@ -28,6 +28,8 @@ libmix_a_SOURCES = mix.h mix.c \
mix_eval.h mix_eval.c xmix_eval.h mix_eval_scanner.l \
mix_src_file.c mix_src_file.h \
mix_vm_clock.c mix_vm_clock.h \
- mix_vm_command.c mix_vm_command.h
+ mix_vm_command.c mix_vm_command.h \
+ mix_config.c mix_config.h
+
diff --git a/mixlib/mix_config.c b/mixlib/mix_config.c
new file mode 100644
index 0000000..721e88e
--- /dev/null
+++ b/mixlib/mix_config.c
@@ -0,0 +1,186 @@
+/* -*-c-*- -------------- mix_config.c :
+ * Implementation of the functions declared in mix_config.h
+ * ------------------------------------------------------------------
+ * Last change: Time-stamp: "01/02/12 00:35:28 jose"
+ * ------------------------------------------------------------------
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * 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 <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include "mix_config.h"
+
+static const gchar COMMENT_PREFIX_ = '#';
+static const gchar *DEF_DIR_ = ".mdk";
+static const gchar *AUTOSAVE_KEY_ = "Autosave";
+static const gchar *AUTOSAVE_YES_ = "True";
+static const gchar *AUTOSAVE_NO_ = "False";
+
+/* the config type */
+struct mix_config_t
+{
+ gchar *filename; /* full path to configuration file */
+ gboolean autosave; /* whether save on destroy */
+ GHashTable *items; /* configuration items */
+};
+
+/* create a new config handler, giving the dir and name of the config file */
+mix_config_t *
+mix_config_new (const gchar *dirname, const gchar *filename)
+{
+ static const gchar *DEF_DIRNAME_ = NULL;
+ static const gchar *DEF_FILENAME_ = "config";
+
+ const gchar *autosave;
+ FILE *f;
+ int k;
+ mix_config_t *result = g_new (mix_config_t, 1);
+
+ if (DEF_DIRNAME_ == NULL)
+ DEF_DIRNAME_ = g_strdup_printf ("%s/%s", g_get_home_dir (), DEF_DIR_);
+
+ if (dirname == NULL) dirname = DEF_DIRNAME_;
+ if (filename == NULL) filename = DEF_FILENAME_;
+
+ k = mkdir (dirname, S_IRWXU | S_IRWXG | S_IRWXO);
+ if (!k) g_message ("Configuration directory %s created\n", dirname);
+
+ result->filename = g_strdup_printf ("%s/%s", dirname, filename);
+ result->items = g_hash_table_new (g_str_hash, g_str_equal);
+
+ f = fopen (result->filename, "r");
+ if (f != NULL)
+ {
+ enum {LEN = 256};
+ gchar buffer[LEN];
+ gchar *line = buffer;
+ while (!feof (f))
+ {
+ line = fgets (line, LEN, f);
+ if (line) line = g_strstrip (line);
+ if (line && line[0] != COMMENT_PREFIX_)
+ {
+ gchar **vals = g_strsplit (line, "=", 2);
+ g_hash_table_insert (result->items,
+ (gpointer)g_strdup (g_strstrip (vals[0])),
+ (gpointer)g_strdup (g_strstrip (vals[1])));
+ g_strfreev (vals);
+ }
+ }
+ fclose (f);
+ }
+
+ autosave = mix_config_get (result, AUTOSAVE_KEY_);
+ result->autosave = autosave && !g_strcasecmp (autosave, AUTOSAVE_YES_);
+
+ return result;
+}
+
+
+/* delete a config handler, saving the configuration if needed */
+void
+mix_config_delete (mix_config_t *config)
+{
+ g_return_if_fail (config != NULL);
+ if (mix_config_is_autosave (config)) mix_config_save (config);
+ g_free (config->filename);
+ g_hash_table_destroy (config->items);
+ g_free (config);
+}
+
+/* get a config item's value from its key */
+const gchar *
+mix_config_get (const mix_config_t *config, const gchar *key)
+{
+ g_return_val_if_fail (config != NULL, NULL);
+ g_return_val_if_fail (key != NULL, NULL);
+
+ return (const gchar*)g_hash_table_lookup (config->items, key);
+}
+
+
+/* update (or create if it does not exist) a new config item */
+void
+mix_config_update (mix_config_t *config, const gchar *key, const gchar *value)
+{
+ gpointer tmp = NULL;
+
+ g_return_if_fail (config != NULL);
+ g_return_if_fail (key != NULL);
+ g_return_if_fail (value != NULL);
+
+ tmp = g_hash_table_lookup (config->items, key);
+ if (tmp)
+ {
+ g_free (tmp);
+ tmp = (gpointer)key;
+ }
+ else
+ {
+ tmp = (gpointer)g_strdup (key);
+ }
+
+ g_hash_table_insert (config->items, tmp, g_strdup (value));
+}
+
+/* save the current configuration */
+static void
+save_ (gpointer key, gpointer value, gpointer file)
+{
+ fprintf ((FILE *)file, "%s=%s\n", (char *)key, (char *)value);
+}
+
+void
+mix_config_save (const mix_config_t *config)
+{
+ FILE *f;
+
+ g_return_if_fail (config != NULL);
+
+ f = fopen (config->filename, "w");
+ if (!f)
+ {
+ g_warning ("Unable to open config file %s (%s)",
+ config->filename, g_strerror (errno));
+ return;
+ }
+ g_hash_table_foreach (config->items, save_, (gpointer)f);
+ fclose (f);
+}
+
+/* set autosave on delete flag */
+void
+mix_config_set_autosave (mix_config_t *config, gboolean autosave)
+{
+ mix_config_update (config, AUTOSAVE_KEY_,
+ autosave? AUTOSAVE_YES_ : AUTOSAVE_NO_);
+ config->autosave = autosave;
+}
+
+
+gboolean
+mix_config_is_autosave (const mix_config_t *config)
+{
+ g_return_val_if_fail (config != NULL, FALSE);
+ return config->autosave;
+}
+
diff --git a/mixlib/mix_config.h b/mixlib/mix_config.h
new file mode 100644
index 0000000..b763160
--- /dev/null
+++ b/mixlib/mix_config.h
@@ -0,0 +1,62 @@
+/* -*-c-*- ---------------- mix_config.h :
+ * Basic config storage utility.
+ * ------------------------------------------------------------------
+ * Last change: Time-stamp: <2001-07-02 01:21:11 jao>
+ * ------------------------------------------------------------------
+ * Copyright (C) 2001 Free Software Foundation, Inc.
+ *
+ * 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.
+ *
+ */
+
+
+#ifndef MIX_CONFIG_H
+#define MIX_CONFIG_H
+
+#include "mix.h"
+
+/* the config type */
+typedef struct mix_config_t mix_config_t;
+
+/* create a new config handler, giving the dir and name of the config file */
+extern mix_config_t *
+mix_config_new (const gchar *dirname, const gchar *filename);
+
+/* delete a config handler, saving the configuration if needed */
+extern void
+mix_config_delete (mix_config_t *config);
+
+/* get a config item's value from its key */
+extern const gchar *
+mix_config_get (const mix_config_t *config, const gchar *key);
+
+/* update (or create if it does not exist) a new config item */
+extern void
+mix_config_update (mix_config_t *config, const gchar *key, const gchar *value);
+
+/* save the current configuration */
+extern void
+mix_config_save (const mix_config_t *config);
+
+/* set autosave on delete flag */
+extern void
+mix_config_set_autosave (mix_config_t *config, gboolean autosave);
+
+extern gboolean
+mix_config_is_autosave (const mix_config_t *config);
+
+
+#endif /* MIX_CONFIG_H */
+