From 2a2575d73e37909c74480fc4b106f44c6ae7fca2 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 9 Oct 2016 16:28:52 +0200 Subject: [PATCH] Added function ZakConfi::add_config_from_confi (closes #1066). Added tests add_config_from_confi and get_tree. Some adjustments. --- .gitignore | 2 + plugins/db/plgdb.c | 31 +++---------- src/confi.c | 65 +++++++++++++++++++++++++- src/libzakconfi.h | 5 ++ tests/Makefile.am | 5 +- tests/test_add_config_from_confi.c | 48 ++++++++++++++++++++ tests/test_get_tree.c | 73 ++++++++++++++++++++++++++++++ 7 files changed, 202 insertions(+), 27 deletions(-) create mode 100644 tests/test_add_config_from_confi.c create mode 100644 tests/test_get_tree.c diff --git a/.gitignore b/.gitignore index 99325c2..b04bb01 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,9 @@ Rules-quot *.exe tests/test tests/test_get_configs_list +tests/test_get_tree tests/test_add_config +tests/test_add_config_from_confi tests/test_set_config *.csv gtk-doc.m4 diff --git a/plugins/db/plgdb.c b/plugins/db/plgdb.c index 25b0ef8..677c4af 100644 --- a/plugins/db/plgdb.c +++ b/plugins/db/plgdb.c @@ -289,9 +289,11 @@ static GdaDataModel } id_parent = gdaex_data_model_get_field_value_integer_at (dm, 0, "id"); } + g_free (token); i++; } + g_strfreev (tokens); return dm; } @@ -631,37 +633,16 @@ static ZakConfiKey sql = g_strdup_printf ("SELECT id" " FROM %cvalues%c" " WHERE id_configs = %d" + " AND id_parent = %d" " AND key = '%s'", priv->chrquot, priv->chrquot, priv->id_config, + id_parent, gdaex_strescape (key_, NULL)); dm = gdaex_query (priv->gdaex, sql); g_free (sql); - if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) - { - id = gdaex_data_model_get_value_integer_at (dm, 0, 0); - g_object_unref (dm); - - sql = g_strdup_printf ("UPDATE %cvalues%c" - " SET %ckey%c = '%s'," - " value = '%s'," - " WHERE id_configs = %d" - " AND id = %d", - priv->chrquot, priv->chrquot, - priv->chrquot, priv->chrquot, - gdaex_strescape (key_, NULL), - gdaex_strescape (ck->value, NULL), - priv->id_config, - id); - if (gdaex_execute (priv->gdaex, sql) == -1) - { - /* TO DO */ - g_free (sql); - return NULL; - } - g_free (sql); - } - else + if (dm == NULL + || gda_data_model_get_n_rows (dm) < 1) { id = 0; diff --git a/src/confi.c b/src/confi.c index da051b9..102dedf 100644 --- a/src/confi.c +++ b/src/confi.c @@ -300,7 +300,7 @@ ZakConfiConfi * @name: * @description: * - * Returns: a #ZakConfiConfi struct. + * Returns: a #ZakConfiConfi struct just rceated. */ ZakConfiConfi *zak_confi_add_config (const gchar *cnc_string, @@ -325,6 +325,69 @@ ZakConfiConfi return cc; } +static gboolean +add_traverse_func (GNode *node, + gpointer data) +{ + ZakConfiKey *ck = (ZakConfiKey *)node->data; + + if (g_strcmp0 (ck->key, "") != 0 + && g_strcmp0 (ck->key, "/") != 0) + { + zak_confi_add_key ((ZakConfi *)data, ck->path, ck->key, ck->value); + } + + return FALSE; +} + +/** + * zak_confi_add_config_from_confi: + * @confi_source: + * @cnc_string: + * @name_new: + * @description_new: + * + * Returns: a #ZakConfiConfi struct just created. + */ +ZakConfiConfi +*zak_confi_add_config_from_confi (ZakConfi *confi_source, + const gchar *cnc_string, + const gchar *name_new, + const gchar *description_new) +{ + ZakConfi *confi; + ZakConfiConfi *cc; + + GNode *tree; + + gchar *confi_name; + + cc = zak_confi_add_config (cnc_string, name_new, description_new); + if (cc != NULL) + { + tree = zak_confi_get_tree (confi_source); + if (tree != NULL) + { + confi_name = g_strdup_printf ("%s;CONFI_NAME=%s", cnc_string, cc->name); + + confi = zak_confi_new (confi_name); + + if (confi != NULL) + { + g_node_traverse (tree, G_PRE_ORDER, G_TRAVERSE_ALL, -1, add_traverse_func, (gpointer)confi); + } + + g_free (confi_name); + } + else + { + g_warning ("ZakConfi source is empty."); + } + } + + return cc; +} + /** * zak_confi_get_tree: * @confi: a #ZakConfi object. diff --git a/src/libzakconfi.h b/src/libzakconfi.h index 7a2b943..dbc93e6 100644 --- a/src/libzakconfi.h +++ b/src/libzakconfi.h @@ -66,6 +66,11 @@ ZakConfiConfi *zak_confi_add_config (const gchar *cnc_string, const gchar *name, const gchar *description); +ZakConfiConfi *zak_confi_add_config_from_confi (ZakConfi *confi_source, + const gchar *cnc_string, + const gchar *name_new, + const gchar *description_new); + GNode *zak_confi_get_tree (ZakConfi *confi); gchar *zak_confi_normalize_root (const gchar *root); diff --git a/tests/Makefile.am b/tests/Makefile.am index d07a501..bf61e14 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,7 +1,8 @@ AM_CPPFLAGS = $(WARN_CFLAGS) \ $(DISABLE_DEPRECATED_CFLAGS) \ $(LIBCONFI_CFLAGS) \ - -I$(top_srcdir)/src + -I$(top_srcdir)/src \ + -DPLUGINSDIR=\""$(libdir)/$(PACKAGE)/plugins"\" LIBS = $(LIBCONFI_LIBS) \ -L../src -lzakconfi \ @@ -11,7 +12,9 @@ LDADD = $(top_builddir)/src/libzakconfi.la noinst_PROGRAMS = test \ test_add_config \ + test_add_config_from_confi \ test_get_configs_list \ + test_get_tree \ test_set_config EXTRA_DIST = gir.py diff --git a/tests/test_add_config_from_confi.c b/tests/test_add_config_from_confi.c new file mode 100644 index 0000000..4b24098 --- /dev/null +++ b/tests/test_add_config_from_confi.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2016 Andrea Zagli + * + * 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 "libzakconfi.h" + +int +main (int argc, char **argv) +{ + PeasEngine *engine; + + ZakConfi *confi; + + if (argc < 5) + { + g_error ("Usage: test_add_config "); + return 0; + } + + engine = peas_engine_get_default (); + peas_engine_add_search_path (engine, PLUGINSDIR, NULL); + + confi = zak_confi_new (argv[1]); + + if (zak_confi_add_config_from_confi (confi, argv[2], argv[3], argv[4]) == NULL) + { + g_warning ("Config %s not created.", argv[2]); + } + + return 0; +} diff --git a/tests/test_get_tree.c b/tests/test_get_tree.c new file mode 100644 index 0000000..01e7d8f --- /dev/null +++ b/tests/test_get_tree.c @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2016 Andrea Zagli + * + * 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 "libzakconfi.h" + +gboolean +traverse_func (GNode *node, + gpointer data) +{ + ZakConfiKey *ck = (ZakConfiKey *)node->data; + g_printf ("%s%s%s => %s\n", ck->path, g_strcmp0 (ck->path, "") == 0 ? "" : "/", ck->key, ck->value); + + return FALSE; +} + +int +main (int argc, char **argv) +{ + PeasEngine *engine; + ZakConfi *confi; + PeasPluginInfo *ppinfo; + GNode *tree; + + if (argc < 2) + { + g_error ("Usage: test "); + return 0; + } + + engine = peas_engine_get_default (); + peas_engine_add_search_path (engine, PLUGINSDIR, NULL); + + confi = zak_confi_new (argv[1]); + if (confi == NULL) + { + g_error ("Error on configuration initialization."); + return 0; + } + + ppinfo = zak_confi_get_plugin_info (confi); + g_printf ("Plugin info\n"); + g_printf ("Name: %s\n", peas_plugin_info_get_name (ppinfo)); + g_printf ("Module dir: %s\n", peas_plugin_info_get_module_dir (ppinfo)); + g_printf ("Module name: %s\n", peas_plugin_info_get_module_name (ppinfo)); + g_printf ("\n"); + + g_printf ("Traversing the entire tree\n"); + tree = zak_confi_get_tree (confi); + g_node_traverse (tree, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_func, NULL); + g_printf ("\n"); + + zak_confi_destroy (confi); + + return 0; +} -- 2.49.0