]> saetta.ns0.it Git - libzakcgi/commitdiff
Added functions ZakCgiSession::set_value_full and ::get_value_full (closes #993).
authorAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 11:11:30 +0000 (12:11 +0100)
committerAndrea Zagli <azagli@libero.it>
Sat, 26 Mar 2016 11:11:30 +0000 (12:11 +0100)
src/session.c
src/session.h
tests/session.c

index 67da1acf62711d71c78689eff6066f69bd222dc6..afba2fdb3cecc4cea9bcca56a4e9e05e2e394acc 100644 (file)
@@ -286,14 +286,15 @@ gchar
 }
 
 /**
- * zak_cgi_session_set_value:
+ * zak_cgi_session_set_value_full:
  * @session:
+ * @group:
  * @name:
  * @value:
  *
  */
 void
-zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value)
+zak_cgi_session_set_value_full (ZakCgiSession *session, const gchar *group, const gchar *name, const gchar *value)
 {
        ZakCgiSessionPrivate *priv = ZAK_CGI_SESSION_GET_PRIVATE (session);
 
@@ -301,20 +302,36 @@ zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gcha
 
        if (priv->kfile != NULL)
                {
-                       if (value == NULL)
+                       if (name == NULL)
                                {
                                        error = NULL;
-                                       if (!g_key_file_remove_key (priv->kfile, "SESSION", name, &error)
+                                       if (!g_key_file_remove_group (priv->kfile, group, &error)
                                                || error != NULL)
                                                {
-                                                       g_warning ("Unable to unset key «%s»: %s.",
+                                                       g_warning ("Unable to unset key «%s» in group «%s»: %s.",
                                                                           name,
+                                                                          group,
                                                                           error != NULL && error->message != NULL ? error->message : "no details");
                                                }
                                }
                        else
                                {
-                                       g_key_file_set_value (priv->kfile, "SESSION", name, value);
+                                       if (value == NULL)
+                                               {
+                                                       error = NULL;
+                                                       if (!g_key_file_remove_key (priv->kfile, group, name, &error)
+                                                               || error != NULL)
+                                                               {
+                                                                       g_warning ("Unable to unset key «%s» in group «%s»: %s.",
+                                                                                          name,
+                                                                                          group,
+                                                                                          error != NULL && error->message != NULL ? error->message : "no details");
+                                                               }
+                                               }
+                                       else
+                                               {
+                                                       g_key_file_set_value (priv->kfile, group, name, value);
+                                               }
                                }
 
                        error = NULL;
@@ -328,14 +345,15 @@ zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gcha
 }
 
 /**
- * zak_cgi_session_get_value:
+ * zak_cgi_session_get_value_full:
  * @session:
+ * @group:
  * @name:
  *
  * Returns: a value from session.
  */
 gchar
-*zak_cgi_session_get_value (ZakCgiSession *session, const gchar *name)
+*zak_cgi_session_get_value_full (ZakCgiSession *session, const gchar *group, const gchar *name)
 {
        gchar *ret;
 
@@ -347,18 +365,45 @@ gchar
        if (priv->kfile != NULL)
                {
                        error = NULL;
-                       ret = g_key_file_get_value (priv->kfile, "SESSION", name, &error);
+                       ret = g_key_file_get_value (priv->kfile, group, name, &error);
                        if (error != NULL)
                                {
-                                       g_warning ("Unable to get session value «%s»: %s",
+                                       g_warning ("Unable to get session value «%s» in group «%s»: %s",
                                                           name,
-                                                          error->message != NULL ? error->message : "noo details.");
+                                                          group,
+                                                          error->message != NULL ? error->message : "no details.");
                                }
                }
 
        return ret;
 }
 
+/**
+ * zak_cgi_session_set_value:
+ * @session:
+ * @name:
+ * @value:
+ *
+ */
+void
+zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value)
+{
+       zak_cgi_session_set_value_full (session, "SESSION", name, value);
+}
+
+/**
+ * zak_cgi_session_get_value:
+ * @session:
+ * @name:
+ *
+ * Returns: a value from session.
+ */
+gchar
+*zak_cgi_session_get_value (ZakCgiSession *session, const gchar *name)
+{
+       return zak_cgi_session_get_value_full (session, "SESSION", name);
+}
+
 /**
  * zak_cgi_session_close:
  * @session:
index caa23c2b64fc206f1ebfe864381d3ad68e8ae4c3..2f645484a40b463118347195ed3a89c7981bd284 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -57,6 +57,9 @@ ZakCgiSession *zak_cgi_session_new (ZakCgiMain *zakcgimain,
 
 gchar *zak_cgi_session_get_header (ZakCgiSession *session);
 
+void zak_cgi_session_set_value_full (ZakCgiSession *session, const gchar *group, const gchar *name, const gchar *value);
+gchar *zak_cgi_session_get_value_full (ZakCgiSession *session, const gchar *group, const gchar *name);
+
 void zak_cgi_session_set_value (ZakCgiSession *session, const gchar *name, const gchar *value);
 gchar *zak_cgi_session_get_value (ZakCgiSession *session, const gchar *name);
 
index 146925044d3100f1f0e196a92cd176756db1ddbc..2289b3693419e3584e7b8345ba389060bbacb785 100644 (file)
@@ -75,7 +75,10 @@ main (int argc, char *argv[])
                                                        ht_stdin = zak_cgi_main_parse_stdin (env, boundary[1]);
                                                        if (g_hash_table_lookup (ht_stdin, "reset") != NULL)
                                                                {
+                                                                       g_warning ("Session: %s", zak_cgi_session_get_value_full (session, "NEW GROUP", "new key"));
+
                                                                        zak_cgi_session_set_value (session, "user_name", NULL);
+                                                                       zak_cgi_session_set_value_full (session, "NEW GROUP", NULL, NULL);
                                                                }
                                                        else
                                                                {
@@ -114,6 +117,8 @@ main (int argc, char *argv[])
                                }
                        else
                                {
+                                       zak_cgi_session_set_value_full (session, "NEW GROUP", "new key", "new value");
+
                                        g_string_append (str, ", on the second page.<br/><br/>");
                                        g_string_append (str,
                                                         "<form action=\"/cgi-bin/session\" method=\"post\" enctype=\"multipart/form-data\">\n"