diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-08-29 15:15:26 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-08-29 15:15:26 +0100 |
commit | 7ade35b5e8abb9fc5f7d0d92421ef20bfd0186bc (patch) | |
tree | 2b8d74296d42cff47fabcd2abd28c723617ee74b /src | |
parent | 627869c66dd4619908d116ca6d207b27dd6a9b34 (diff) | |
download | rspamd-7ade35b5e8abb9fc5f7d0d92421ef20bfd0186bc.tar.gz rspamd-7ade35b5e8abb9fc5f7d0d92421ef20bfd0186bc.zip |
Add integer converting function.
Diffstat (limited to 'src')
-rw-r--r-- | src/cfg_rcl.c | 53 | ||||
-rw-r--r-- | src/cfg_rcl.h | 14 |
2 files changed, 67 insertions, 0 deletions
diff --git a/src/cfg_rcl.c b/src/cfg_rcl.c index 638b55445..b59524cfb 100644 --- a/src/cfg_rcl.c +++ b/src/cfg_rcl.c @@ -289,3 +289,56 @@ rspamd_rcl_parse_struct_string (struct config_file *cfg, rspamd_cl_object_t *obj return TRUE; } + +gboolean +rspamd_rcl_parse_struct_integer (struct config_file *cfg, rspamd_cl_object_t *obj, + gpointer ud, struct rspamd_rcl_section *section, GError **err) +{ + struct rspamd_rcl_struct_parser *pd = ud; + union { + gint *ip; + gint32 *i32p; + gint16 *i16p; + gint64 *i64p; + } target; + gint64 val; + + if (pd->size == sizeof (gint)) { + target.ip = (gint *)(((gchar *)pd->user_struct) + pd->offset); + if (!rspamd_cl_obj_toint_safe (obj, &val)) { + g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to integer"); + return FALSE; + } + *target.ip = val; + } + else if (pd->size == sizeof (gint32)) { + target.i32p = (gint32 *)(((gchar *)pd->user_struct) + pd->offset); + if (!rspamd_cl_obj_toint_safe (obj, &val)) { + g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to integer"); + return FALSE; + } + *target.i32p = val; + } + else if (pd->size == sizeof (gint16)) { + target.i16p = (gint16 *)(((gchar *)pd->user_struct) + pd->offset); + if (!rspamd_cl_obj_toint_safe (obj, &val)) { + g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to integer"); + return FALSE; + } + *target.i16p = val; + } + else if (pd->size == sizeof (gint64)) { + target.i64p = (gint64 *)(((gchar *)pd->user_struct) + pd->offset); + if (!rspamd_cl_obj_toint_safe (obj, &val)) { + g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to integer"); + return FALSE; + } + *target.i64p = val; + } + else { + g_set_error (err, CFG_RCL_ERROR, E2BIG, "unknown integer size"); + return FALSE; + } + + return TRUE; +} diff --git a/src/cfg_rcl.h b/src/cfg_rcl.h index 8b2a3fdc1..7ee2a4fbb 100644 --- a/src/cfg_rcl.h +++ b/src/cfg_rcl.h @@ -91,6 +91,7 @@ gboolean rspamd_read_rcl_config (struct rspamd_rcl_section *top, struct rspamd_rcl_struct_parser { gpointer user_struct; goffset offset; + gsize size; }; /** @@ -105,4 +106,17 @@ struct rspamd_rcl_struct_parser { gboolean rspamd_rcl_parse_struct_string (struct config_file *cfg, rspamd_cl_object_t *obj, gpointer ud, struct rspamd_rcl_section *section, GError **err); +/** + * Parse an integer field of a structure + * @param cfg config pointer + * @param obj object to parse + * @param ud struct_parser structure + * @param section the current section + * @param err error pointer + * @return TRUE if a value has been successfully parsed + */ +gboolean rspamd_rcl_parse_struct_integer (struct config_file *cfg, rspamd_cl_object_t *obj, + gpointer ud, struct rspamd_rcl_section *section, GError **err); + + #endif /* CFG_RCL_H_ */ |