From 37df6f46e3e119e1f00e4bc64b5acf5c0919752e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 26 Mar 2016 15:06:40 +0100 Subject: [PATCH] Added functions ZakCgiSession::[gs]et_value_full_* (closes #992). --- src/session.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ src/session.h | 8 ++++ tests/session.c | 8 +++- 3 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/session.c b/src/session.c index afba2fd..61d0462 100644 --- a/src/session.c +++ b/src/session.c @@ -20,6 +20,8 @@ #include #endif +#include + #include #include @@ -404,6 +406,112 @@ gchar return zak_cgi_session_get_value_full (session, "SESSION", name); } +/** + * zak_cgi_session_set_value_full_int: + * @session: + * @group: + * @name: + * @value: + * + */ +void +zak_cgi_session_set_value_full_int (ZakCgiSession *session, const gchar *group, const gchar *name, gint value) +{ + gchar *str; + + str = g_strdup_printf ("%d", value); + zak_cgi_session_set_value_full (session, group, name, str); + g_free (str); +} + +/** + * zak_cgi_session_set_value_full_double: + * @session: + * @group: + * @name: + * @value: + * + */ +void +zak_cgi_session_set_value_full_double (ZakCgiSession *session, const gchar *group, const gchar *name, gdouble value) +{ + gchar *str; + + str = g_strdup_printf ("%f", value); + zak_cgi_session_set_value_full (session, group, name, str); + g_free (str); +} + +/** + * zak_cgi_session_set_value_full_boolean: + * @session: + * @group: + * @name: + * @value: + * + */ +void +zak_cgi_session_set_value_full_boolean (ZakCgiSession *session, const gchar *group, const gchar *name, gboolean value) +{ + zak_cgi_session_set_value_full_int (session, group, name, (gint)value); +} + +/** + * zak_cgi_session_get_value_full_int: + * @session: + * @group: + * @name: + * + * Returns: + */ +gint +zak_cgi_session_get_value_full_int (ZakCgiSession *session, const gchar *group, const gchar *name) +{ + gchar *str; + gint ret; + + str = zak_cgi_session_get_value_full (session, group, name); + ret = strtol (str, NULL, 10); + g_free (str); + + return ret; +} + +/** + * zak_cgi_session_get_value_full_double: + * @session: + * @group: + * @name: + * + * Returns: + */ +gdouble +zak_cgi_session_get_value_full_double (ZakCgiSession *session, const gchar *group, const gchar *name) +{ + gchar *str; + gdouble ret; + + str = zak_cgi_session_get_value_full (session, group, name); + ret = g_strtod (str, NULL); + g_free (str); + + return ret; +} + +/** + * zak_cgi_session_get_value_full_boolean: + * @session: + * @group: + * @name: + * + * Returns: + */ +gboolean +zak_cgi_session_get_value_full_boolean (ZakCgiSession *session, const gchar *group, const gchar *name) +{ + return (gboolean)zak_cgi_session_get_value_full_int (session, group, name); +} + /** * zak_cgi_session_close: * @session: diff --git a/src/session.h b/src/session.h index 2f64548..ce238a6 100644 --- a/src/session.h +++ b/src/session.h @@ -63,6 +63,14 @@ gchar *zak_cgi_session_get_value_full (ZakCgiSession *session, const gchar *grou 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); +void zak_cgi_session_set_value_full_int (ZakCgiSession *session, const gchar *group, const gchar *name, gint value); +void zak_cgi_session_set_value_full_double (ZakCgiSession *session, const gchar *group, const gchar *name, gdouble value); +void zak_cgi_session_set_value_full_boolean (ZakCgiSession *session, const gchar *group, const gchar *name, gboolean value); + +gint zak_cgi_session_get_value_full_int (ZakCgiSession *session, const gchar *group, const gchar *name); +gdouble zak_cgi_session_get_value_full_double (ZakCgiSession *session, const gchar *group, const gchar *name); +gboolean zak_cgi_session_get_value_full_boolean (ZakCgiSession *session, const gchar *group, const gchar *name); + void zak_cgi_session_close (ZakCgiSession *session); diff --git a/tests/session.c b/tests/session.c index 2289b36..b26bf60 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")); + g_warning ("new key: %s", zak_cgi_session_get_value_full (session, "NEW GROUP", "new key")); + g_warning ("new int: %d", zak_cgi_session_get_value_full_int (session, "NEW GROUP", "new int")); + g_warning ("new double: %f", zak_cgi_session_get_value_full_double (session, "NEW GROUP", "new double")); + g_warning ("new boolean: %d", zak_cgi_session_get_value_full_boolean (session, "NEW GROUP", "new boolean")); zak_cgi_session_set_value (session, "user_name", NULL); zak_cgi_session_set_value_full (session, "NEW GROUP", NULL, NULL); @@ -118,6 +121,9 @@ main (int argc, char *argv[]) else { zak_cgi_session_set_value_full (session, "NEW GROUP", "new key", "new value"); + zak_cgi_session_set_value_full_int (session, "NEW GROUP", "new int", 55); + zak_cgi_session_set_value_full_double (session, "NEW GROUP", "new double", 123.66); + zak_cgi_session_set_value_full_boolean (session, "NEW GROUP", "new boolean", TRUE); g_string_append (str, ", on the second page.

"); g_string_append (str, -- 2.49.0