From 3de5486aac600f2b06368b8f66d7070468381c2e Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Wed, 20 May 2009 09:33:53 +0300 Subject: [PATCH] mcconfig: added module for work with ini-files --- src/mcconfig/Makefile.am | 13 +++ src/mcconfig/common.c | 228 +++++++++++++++++++++++++++++++++++++++ src/mcconfig/get.c | 174 ++++++++++++++++++++++++++++++ src/mcconfig/mcconfig.h | 97 +++++++++++++++++ src/mcconfig/set.c | 113 +++++++++++++++++++ 5 files changed, 625 insertions(+) create mode 100644 src/mcconfig/Makefile.am create mode 100644 src/mcconfig/common.c create mode 100644 src/mcconfig/get.c create mode 100644 src/mcconfig/mcconfig.h create mode 100644 src/mcconfig/set.c diff --git a/src/mcconfig/Makefile.am b/src/mcconfig/Makefile.am new file mode 100644 index 000000000..2d82b2246 --- /dev/null +++ b/src/mcconfig/Makefile.am @@ -0,0 +1,13 @@ + +noinst_LTLIBRARIES = libmcconfig.la + +libmcconfig_la_SOURCES = \ + ../mcconfig.h \ + common.c \ + get.c \ + set.c + +libmcconfig_la_CFLAGS=-I../ -I$(top_srcdir)/src \ + $(GLIB_CFLAGS) \ + -DDATADIR=\""$(pkgdatadir)/"\" -DLOCALEDIR=\""$(localedir)"\" + diff --git a/src/mcconfig/common.c b/src/mcconfig/common.c new file mode 100644 index 000000000..75ba8f8bc --- /dev/null +++ b/src/mcconfig/common.c @@ -0,0 +1,228 @@ +/* Configure module for the Midnight Commander + Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include + +#include "global.h" + +#include "mcconfig.h" + +/*** global variables **************************************************/ + +mc_config_t *mc_main_config; +mc_config_t *mc_panels_config; + +/*** file scope macro definitions **************************************/ + +/*** file scope type declarations **************************************/ + +/*** file scope variables **********************************************/ + +/*** file scope functions **********************************************/ + +/*** public functions **************************************************/ + +mc_config_t * +mc_config_init (const gchar * ini_path) +{ + mc_config_t *mc_config; + + mc_config = g_try_malloc0 (sizeof (mc_config_t)); + + if (mc_config == NULL) + return NULL; + + mc_config->handle = g_key_file_new (); + if (mc_config->handle == NULL) + { + g_free (mc_config); + return NULL; + } + if (!ini_path || !exist_file (ini_path)) + { + return mc_config; + } + + if (!g_key_file_load_from_file + (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL)) + { + g_key_file_free (mc_config->handle); + g_free (mc_config); + return NULL; + } + mc_config->ini_path = g_strdup (ini_path); + return mc_config; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +void +mc_config_deinit (mc_config_t * mc_config) +{ + if (!mc_config) + return; + + g_free (mc_config->ini_path); + g_key_file_free (mc_config->handle); + g_free (mc_config); + +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_has_param (mc_config_t * mc_config, const char *group, + const gchar * param) +{ + if (!mc_config || !group || !param) + return FALSE; + + return g_key_file_has_key (mc_config->handle, group, param, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_has_group (mc_config_t * mc_config, const char *group) +{ + if (!mc_config || !group) + return FALSE; + + return g_key_file_has_group (mc_config->handle, group); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + +gboolean +mc_config_del_param (mc_config_t * mc_config, const char *group, + const gchar * param) +{ + if (!mc_config || !group || !param) + return FALSE; +#if GLIB_CHECK_VERSION (2, 15, 0) + return g_key_file_remove_key (mc_config->handle, group, param, NULL); +#else + g_key_file_remove_key (mc_config->handle, group, param, NULL); + return TRUE; +#endif +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_del_group (mc_config_t * mc_config, const char *group) +{ + if (!mc_config || !group) + return FALSE; + +#if GLIB_CHECK_VERSION (2, 15, 0) + return g_key_file_remove_group (mc_config->handle, group, NULL); +#else + g_key_file_remove_group (mc_config->handle, group, NULL); + return TRUE; +#endif +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path) +{ + mc_config_t *tmp_config; + gchar **groups, **curr_grp; + gchar **keys, **curr_key; + gchar *value; + + if (mc_config == NULL){ + return FALSE; + } + + tmp_config = mc_config_init(ini_path); + if (tmp_config == NULL) + return FALSE; + + groups = mc_config_get_groups (tmp_config, NULL); + + for (curr_grp = groups; *curr_grp != NULL; curr_grp++) + { + keys = mc_config_get_keys (tmp_config, *curr_grp ,NULL); + for (curr_key = keys; *curr_key != NULL; curr_key++) + { + value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL); + if (value == NULL) + continue; + + g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value); + g_free(value); + } + g_strfreev(keys); + } + g_strfreev(groups); + mc_config_deinit(tmp_config); + return TRUE; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_save_file (mc_config_t * mc_config) +{ + gchar *data; + gsize len; + gboolean ret; + + if (mc_config == NULL || mc_config->ini_path == NULL){ + return FALSE; + } + data = g_key_file_to_data (mc_config->handle,&len,NULL); + if (exist_file(mc_config->ini_path)) + { + mc_unlink (mc_config->ini_path); + } + + ret = g_file_set_contents(mc_config->ini_path,data,len,NULL); + g_free(data); + return ret; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path) +{ + gchar *data; + gsize len; + gboolean ret; + + if (mc_config == NULL){ + return FALSE; + } + data = g_key_file_to_data (mc_config->handle,&len,NULL); + if (exist_file(ini_path)) + { + mc_unlink (ini_path); + } +mc_log("ini_path = %s\n", ini_path); + ret = g_file_set_contents(ini_path,data,len,NULL); + g_free(data); + return ret; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ diff --git a/src/mcconfig/get.c b/src/mcconfig/get.c new file mode 100644 index 000000000..a8a84d547 --- /dev/null +++ b/src/mcconfig/get.c @@ -0,0 +1,174 @@ +/* Configure module for the Midnight Commander + Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include + +#include "global.h" + +#include "mcconfig.h" +/*** global variables **************************************************/ + +/*** file scope macro definitions **************************************/ + +/*** file scope type declarations **************************************/ + +/*** file scope variables **********************************************/ + +/*** file scope functions **********************************************/ + +/*** public functions **************************************************/ + +gchar ** +mc_config_get_groups (mc_config_t * mc_config, gsize * len) +{ + gchar **ret; + + if (!mc_config) + { + ret = g_try_malloc0 (sizeof (gchar **)); + return ret; + } + ret = g_key_file_get_groups (mc_config->handle, len); + if (ret == NULL) + { + ret = g_try_malloc0 (sizeof (gchar **)); + } + return ret; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gchar ** +mc_config_get_keys (mc_config_t * mc_config, const gchar * group, gsize * len) +{ + gchar **ret; + + if (!mc_config || !group) + { + ret = g_try_malloc0 (sizeof (gchar **)); + return ret; + } + ret = g_key_file_get_keys (mc_config->handle, group, len, NULL); + if (ret == NULL) + { + ret = g_try_malloc0 (sizeof (gchar **)); + } + return ret; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gchar * +mc_config_get_string (mc_config_t * mc_config, const gchar * group, + const gchar * param, const gchar * def) +{ + gchar *ret; + if (!mc_config || !group || !param) + return def ? g_strdup (def) : NULL; + + if (! mc_config_has_param(mc_config, group, param)) + { + mc_config_set_string (mc_config, group, param, def ? def : ""); + return def ? g_strdup (def) : NULL; + } + + ret = g_key_file_get_string (mc_config->handle, group, param, NULL); + + if (!ret) + ret = def ? g_strdup (def) : NULL; + return ret; +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean +mc_config_get_bool (mc_config_t * mc_config, const gchar * group, + const gchar * param, gboolean def) +{ + if (!mc_config || !group || !param) + return def; + + if (! mc_config_has_param(mc_config, group, param)) + { + mc_config_set_bool (mc_config, group, param, def); + return def; + } + + return g_key_file_get_boolean (mc_config->handle, group, param, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +int +mc_config_get_int (mc_config_t * mc_config, const gchar * group, + const gchar * param, int def) +{ + if (!mc_config || !group || !param) + return def; + + if (! mc_config_has_param(mc_config, group, param)) + { + mc_config_set_int (mc_config, group, param, def); + return def; + } + + return g_key_file_get_integer (mc_config->handle, group, param, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gchar ** +mc_config_get_string_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, gsize * length) +{ + if (!mc_config || !group || !param) + return NULL; + + return g_key_file_get_string_list (mc_config->handle, group, param, + length, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +gboolean * +mc_config_get_bool_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, gsize * length) +{ + if (!mc_config || !group || !param) + return NULL; + + return g_key_file_get_boolean_list (mc_config->handle, group, param, + length, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +int * +mc_config_get_int_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, gsize * length) +{ + if (!mc_config || !group || !param) + return NULL; + + return g_key_file_get_integer_list (mc_config->handle, group, param, + length, NULL); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ diff --git a/src/mcconfig/mcconfig.h b/src/mcconfig/mcconfig.h new file mode 100644 index 000000000..563c8dc7c --- /dev/null +++ b/src/mcconfig/mcconfig.h @@ -0,0 +1,97 @@ +#ifndef MC_CONFIG_H +#define MC_CONFIG_H + +/*** typedefs(not structures) and defined constants ********************/ + +#define CONFIG_APP_SECTION "Midnight-Commander" + +/*** enums *************************************************************/ + +/*** structures declarations (and typedefs of structures)***************/ + +typedef struct mc_config_struct +{ + GKeyFile *handle; + gchar *ini_path; +} mc_config_t; + +/*** global variables defined in .c file *******************************/ + +extern mc_config_t *mc_main_config; +extern mc_config_t *mc_panels_config; + +/*** declarations of public functions **********************************/ + + +/* mcconfig/common.c: */ + +mc_config_t *mc_config_init (const gchar *); +void mc_config_deinit (mc_config_t *); + +gboolean mc_config_del_param (mc_config_t *, const char *, const gchar *); +gboolean mc_config_del_group (mc_config_t *, const char *); + +gboolean mc_config_has_param (mc_config_t *, const char *, const gchar *); +gboolean mc_config_has_group (mc_config_t *, const char *); + +gboolean mc_config_read_file (mc_config_t *, const gchar *); + +gboolean mc_config_save_file (mc_config_t *); + +gboolean mc_config_save_to_file (mc_config_t *, const gchar *); + +/* mcconfig/get.c: */ + +gchar **mc_config_get_groups (mc_config_t *, gsize *); + +gchar **mc_config_get_keys (mc_config_t *, const gchar *, gsize *); + +gchar *mc_config_get_string (mc_config_t *, const gchar *, const gchar *, + const gchar *); + +gboolean mc_config_get_bool (mc_config_t *, const gchar *, const gchar *, + gboolean); + +int mc_config_get_int (mc_config_t *, const gchar *, const gchar *, int); + + +gchar **mc_config_get_string_list (mc_config_t *, const gchar *, + const gchar *, gsize *); + +gboolean *mc_config_get_bool_list (mc_config_t *, const gchar *, + const gchar *, gsize *); + +int *mc_config_get_int_list (mc_config_t *, const gchar *, + const gchar *, gsize *); + + +/* mcconfig/set.c: */ + +void +mc_config_set_string (mc_config_t *, const gchar *, + const gchar *, const gchar *); + +void +mc_config_set_bool (mc_config_t *, const gchar *, const gchar *, gboolean); + +void mc_config_set_int (mc_config_t *, const gchar *, const gchar *, int); + +void +mc_config_set_string_list (mc_config_t *, const gchar *, + const gchar *, const gchar * const[], gsize); + +void +mc_config_set_bool_list (mc_config_t *, const gchar *, + const gchar *, gboolean[], gsize); + +void +mc_config_set_int_list (mc_config_t *, const gchar *, + const gchar *, int[], gsize); + + +/* mcconfig/dialog.c: */ + +void mc_config_show_dialog (void); + + +#endif diff --git a/src/mcconfig/set.c b/src/mcconfig/set.c new file mode 100644 index 000000000..ae241f743 --- /dev/null +++ b/src/mcconfig/set.c @@ -0,0 +1,113 @@ +/* Configure module for the Midnight Commander + Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, + 2007, 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include + +#include "global.h" + +#include "mcconfig.h" +/*** global variables **************************************************/ + +/*** file scope macro definitions **************************************/ + +/*** file scope type declarations **************************************/ + +/*** file scope variables **********************************************/ + +/*** file scope functions **********************************************/ + +/*** public functions **************************************************/ + +void +mc_config_set_string (mc_config_t * mc_config, const gchar * group, + const gchar * param, const gchar * value) +{ + if (!mc_config || !group || !param || !value) + return; + + g_key_file_set_string (mc_config->handle, group, param, value); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +void +mc_config_set_bool (mc_config_t * mc_config, const gchar * group, + const gchar * param, gboolean value) +{ + if (!mc_config || !group || !param ) + return; + + g_key_file_set_boolean (mc_config->handle, group, param, value); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +void +mc_config_set_int (mc_config_t * mc_config, const gchar * group, + const gchar * param, int value) +{ + if (!mc_config || !group || !param ) + return; + + g_key_file_set_integer (mc_config->handle, group, param, value); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +void +mc_config_set_string_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, const gchar * const value[], + gsize length) +{ + if (!mc_config || !group || !param || !value || length == 0) + return; + + g_key_file_set_string_list (mc_config->handle, group, param, value, + length); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + +void +mc_config_set_bool_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, gboolean value[], gsize length) +{ + if (!mc_config || !group || !param || !value || length == 0) + return; + + g_key_file_set_boolean_list (mc_config->handle, group, param, value, + length); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +void +mc_config_set_int_list (mc_config_t * mc_config, const gchar * group, + const gchar * param, int value[], gsize length) +{ + if (!mc_config || !group || !param || !value || length == 0) + return; + + g_key_file_set_integer_list (mc_config->handle, group, param, value, + length); +} + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */