From 363b52cd56283d45abcb00b39be293d5caad2edc Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 26 Mar 2016 12:11:30 +0100 Subject: [PATCH] Added functions ZakCgiSession::set_value_full and ::get_value_full (closes #993). --- src/session.c | 67 +++++++++++++++++++++++++++++++++++++++++-------- src/session.h | 5 +++- tests/session.c | 5 ++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/session.c b/src/session.c index 67da1ac..afba2fd 100644 --- a/src/session.c +++ b/src/session.c @@ -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: diff --git a/src/session.h b/src/session.h index caa23c2..2f64548 100644 --- a/src/session.h +++ b/src/session.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2016 Andrea Zagli * * 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); diff --git a/tests/session.c b/tests/session.c index 1469250..2289b36 100644 --- a/tests/session.c +++ b/tests/session.c @@ -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.

"); g_string_append (str, "
\n" -- 2.49.0