]> saetta.ns0.it Git - zakconfi/libzakconfi/commitdiff
Simulating multiple folder levels using group name. libpeas
authorAndrea Zagli <azagli@libero.it>
Mon, 25 Apr 2016 15:25:08 +0000 (17:25 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 25 Apr 2016 15:25:08 +0000 (17:25 +0200)
plugins/file/plgfile.c
tests/conf.conf
tests/test.c

index 91f4849325ddd06b96e34f28f8b30ef8abc86433..f8e886acc7f52ba36432c3771a5d259f9199b252 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * plgfile.c
- * This file is part of confi
+ * This file is part of lizakconfi
  *
- * Copyright (C) 2014 Andrea Zagli
+ * Copyright (C) 2014-2016 Andrea Zagli
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU Library General Public License as published by
@@ -38,7 +38,9 @@
 
 static void confi_pluggable_iface_init (ConfiPluggableInterface *iface);
 
-static gboolean confi_file_plugin_path_get_group_and_key (ConfiPluggable *pluggable, const gchar *path, gchar **group, gchar **key);
+gboolean confi_file_plugin_initialize (ConfiPluggable *pluggable, const gchar *cnc_string);
+
+static gboolean confi_file_plugin_path_get_group_and_key (const gchar *path, gchar **group, gchar **key);
 static gchar *confi_file_plugin_path_get_value_from_file (ConfiPluggable *pluggable, const gchar *path);
 static gchar *confi_file_plugin_path_get_value (ConfiPluggable *pluggable, const gchar *path);
 static gboolean confi_file_plugin_path_set_value (ConfiPluggable *pluggable, const gchar *path, const gchar *value);
@@ -196,47 +198,24 @@ confi_file_plugin_initialize (ConfiPluggable *pluggable, const gchar *cnc_string
 }
 
 static gboolean
-confi_file_plugin_path_get_group_and_key (ConfiPluggable *pluggable, const gchar *path, gchar **group, gchar **key)
+confi_file_plugin_path_get_group_and_key (const gchar *path, gchar **group, gchar **key)
 {
-       gchar *path_;
-       gchar **tokens;
-
-       guint l;
-       guint i;
-       guint c;
+       gchar *last;
 
        if (path == NULL) return FALSE;
+       if (path[strlen (path) - 1] == '/') return FALSE;
 
-       path_ = g_strdup_printf ("%s/", path);
-       tokens = g_strsplit (path_, "/", -1);
-       if (tokens == NULL) return FALSE;
-
-       l = g_strv_length (tokens);
-       c = 1;
-       for (i = 0; i < l; i++)
+       last = g_strrstr (path, "/");
+       if (last == NULL)
                {
-                       if (g_strcmp0 (tokens[i], "") != 0)
-                               {
-                                       if (c == 1)
-                                               {
-                                                       *group = g_strdup (tokens[i]);
-                                                       g_strstrip (*group);
-                                                       c = 2;
-                                               }
-                                       else if (c == 2)
-                                               {
-                                                       *key = g_strdup (tokens[i]);
-                                                       g_strstrip (*key);
-                                                       c = 3;
-                                               }
-                                       if (c > 2)
-                                               {
-                                                       break;
-                                               }
-                               }
+                       return FALSE;
                }
-       g_strfreev (tokens);
-       g_free (path_);
+
+       *group = g_strndup (path + (path[0] == '/' ? 1 : 0), strlen (path + (path[0] == '/' ? 1 : 0)) - strlen (last));
+       g_strstrip (*group);
+
+       *key = g_strdup (last + 1);
+       g_strstrip (*key);
 
        return TRUE;
 }
@@ -257,7 +236,7 @@ static gchar
 
        group = NULL;
        key = NULL;
-       if (!confi_file_plugin_path_get_group_and_key (pluggable, path, &group, &key))
+       if (!confi_file_plugin_path_get_group_and_key (path, &group, &key))
                {
                        return NULL;
                }
@@ -384,6 +363,7 @@ confi_file_plugin_path_set_value (ConfiPluggable *pluggable, const gchar *path,
 {
        gboolean ret;
 
+       gchar *path_;
        gchar *group;
        gchar *key;
 
@@ -393,9 +373,15 @@ confi_file_plugin_path_set_value (ConfiPluggable *pluggable, const gchar *path,
 
        g_return_val_if_fail (value != NULL, FALSE);
 
+       path_ = confi_path_normalize (pluggable, path);
+       if (path_ == NULL)
+               {
+                       return FALSE;
+               }
+
        group = NULL;
        key = NULL;
-       if (!confi_file_plugin_path_get_group_and_key (pluggable, path, &group, &key))
+       if (!confi_file_plugin_path_get_group_and_key (path_, &group, &key))
                {
                        return FALSE;
                }
@@ -452,7 +438,7 @@ static ConfiKey
                {
                        group = NULL;
                        key_ = NULL;
-                       if (confi_file_plugin_path_get_group_and_key (pluggable, path, &group, &key_))
+                       if (confi_file_plugin_path_get_group_and_key (path, &group, &key_))
                                {
                                        ck = g_new0 (ConfiKey, 1);
                                        ck->key = g_strdup (key);
@@ -505,7 +491,7 @@ static ConfiKey
 
        group = NULL;
        key = NULL;
-       if (confi_file_plugin_path_get_group_and_key (pluggable, path_, &group, &key))
+       if (confi_file_plugin_path_get_group_and_key (path_, &group, &key))
                {
                        ck = g_new0 (ConfiKey, 1);
                        ck->key = g_strdup (key);
@@ -539,7 +525,7 @@ confi_file_plugin_remove_path (ConfiPluggable *pluggable, const gchar *path)
 
        group = NULL;
        key = NULL;
-       if (confi_file_plugin_path_get_group_and_key (pluggable, path, &group, &key))
+       if (confi_file_plugin_path_get_group_and_key (path, &group, &key))
                {
                        error = NULL;
                        ret = g_key_file_remove_key (priv->kfile, group, key, &error);
index 7f8af97a4903e754a8c29251603a96f46f57c1a3..7085ace74e64efeb3b4ba89558cea552600efb75 100644 (file)
@@ -2,13 +2,13 @@
 name=Default
 description=Default configuration from file
 
-[FOLDER1]
+[folder]
 key1=value key 1
-# comment to key2 of folder 1
 key2=value key 2
 
-[FOLDER2]
-# comment to key1 of folder 2
-key1=value key 1 folder 2
-key2=value key 2 folder 2
-key999=value for key999, programmatically setted
+[folder/key1]
+key1_1=value key 1 1
+key1_2=value key 1 2
+
+[folder/key2]
+key2-1=value key 2 1
index 850b4073a12af602a5665acbbee96b4c2408b052..f5ade1ff8ad3686c7ce7dcf340df3a3eb4594de8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2014 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2005-2016 Andrea Zagli <azagli@libero.it>
  *
  *  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
@@ -81,56 +81,29 @@ main (int argc, char **argv)
        g_node_traverse (tree, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_func, NULL);
        g_printf ("\n");
 
-       if (g_strcmp0 (peas_plugin_info_get_module_name (ppinfo), "file") == 0)
-               {
-                       gchar *val = confi_path_get_value (confi, "FOLDER1/key1");
-                       g_printf ("Value from key \"FOLDER1/key1\"\n%s\n\n", val);
-                       confi_path_set_value (confi, "FOLDER1/key1", "new value programmatically setted");
-                       g_printf ("Value from key \"FOLDER1/key1\"\n%s\n\n", confi_path_get_value (confi, "FOLDER1/key1"));
-                       confi_path_set_value (confi, "FOLDER1/key1", val);
-
-                       confi_add_key (confi, "FOLDER2", "key999", NULL);
-                       confi_path_set_value (confi, "FOLDER2/key999", "value for key999, programmatically setted");
-
-                       ConfiKey *ck;
-                       ck = confi_path_get_confi_key (confi, "FOLDER1/key2");
-                       g_printf ("ConfiKey for FOLDER1/key2\n");
-                       g_printf ("Path: %s\n", ck->path);
-                       g_printf ("Key: %s\n", ck->key);
-                       g_printf ("Description: %s\n", ck->description);
-                       g_printf ("Value: %s\n", ck->value);
-                       g_printf ("\n");
-
-                       g_printf ("Setting root \"FOLDER2\"\n");
-                       confi_set_root (confi, "FOLDER2");
-                       g_printf ("Value from key \"key2\" %s\n", confi_path_get_value (confi, "key2"));
-                       g_printf ("Value from key \"FOLDER2/key2\" (expected null) %s\n", confi_path_get_value (confi, "FOLDER2/key2"));
-               }
-       else
-               {
-                       gchar *val = confi_path_get_value (confi, "folder/key1/key1_2");
-                       g_printf ("Value from key \"folder/key1/key1_2\"\n%s\n\n", val);
-                       confi_path_set_value (confi, "folder/key1/key1_2", "new value programmatically setted");
-                       g_printf ("Value from key \"folder/key1/key1_2\"\n%s\n\n", confi_path_get_value (confi, "folder/key1/key1_2"));
-                       confi_path_set_value (confi, "folder/key1/key1_2", val);
-
-                       confi_add_key (confi, "folder/key2", "key2-2", NULL);
-                       confi_path_set_value (confi, "folder/key2/key2-2", "value for key2-2, programmatically setted");
-
-                       ConfiKey *ck;
-                       ck = confi_path_get_confi_key (confi, "folder/key2/key2-2");
-                       g_printf ("ConfiKey for folder/key2/key2-2\n");
-                       g_printf ("Path: %s\n", ck->path);
-                       g_printf ("Key: %s\n", ck->key);
-                       g_printf ("Description: %s\n", ck->description);
-                       g_printf ("Value: %s\n", ck->value);
-                       g_printf ("\n");
-
-                       g_printf ("Setting root \"folder/key2\"\n");
-                       confi_set_root (confi, "folder/key2");
-                       g_printf ("Value from key \"key2-1\" %s\n", confi_path_get_value (confi, "key2-1"));
-                       g_printf ("Value from key \"folder/key1/key1_2\" (expected null) %s\n", confi_path_get_value (confi, "folder/key1/key1_2"));
-               }
+       gchar *val = confi_path_get_value (confi, "folder/key1/key1_2");
+       g_printf ("Value from key \"folder/key1/key1_2\"\n%s\n\n", val);
+       confi_path_set_value (confi, "folder/key1/key1_2", "new value programmatically setted");
+       g_printf ("Value from key \"folder/key1/key1_2\"\n%s\n\n", confi_path_get_value (confi, "folder/key1/key1_2"));
+       confi_path_set_value (confi, "folder/key1/key1_2", val);
+       g_printf ("Value from key \"folder/key1/key1_2\"\n%s\n\n", confi_path_get_value (confi, "folder/key1/key1_2"));
+
+       confi_add_key (confi, "folder/key2", "key2-2", NULL);
+       confi_path_set_value (confi, "folder/key2/key2-2", "value for key2-2, programmatically setted");
+
+       ConfiKey *ck;
+       ck = confi_path_get_confi_key (confi, "folder/key2/key2-2");
+       g_printf ("ConfiKey for folder/key2/key2-2\n");
+       g_printf ("Path: %s\n", ck->path);
+       g_printf ("Key: %s\n", ck->key);
+       g_printf ("Description: %s\n", ck->description);
+       g_printf ("Value: %s\n", ck->value);
+       g_printf ("\n");
+
+       g_printf ("Setting root \"folder/key2\"\n");
+       confi_set_root (confi, "folder/key2");
+       g_printf ("Value from key \"key2-1\" %s\n", confi_path_get_value (confi, "key2-1"));
+       g_printf ("Value from key \"folder/key1/key1_2\" (expected null) %s\n", confi_path_get_value (confi, "folder/key1/key1_2"));
 
        confi_destroy (confi);