diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-08-29 15:00:50 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-08-29 15:00:50 +0100 |
commit | 627869c66dd4619908d116ca6d207b27dd6a9b34 (patch) | |
tree | d4d242963287cb841c8966c85b4c02e39bb21fa7 /src | |
parent | dc1196f1064a4bc19a8880c04e4c79a9cef54cc5 (diff) | |
download | rspamd-627869c66dd4619908d116ca6d207b27dd6a9b34.tar.gz rspamd-627869c66dd4619908d116ca6d207b27dd6a9b34.zip |
Add the parser of string fileds in structs.
Diffstat (limited to 'src')
-rw-r--r-- | src/cfg_rcl.c | 34 | ||||
-rw-r--r-- | src/cfg_rcl.h | 22 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/cfg_rcl.c b/src/cfg_rcl.c index 864a0c935..638b55445 100644 --- a/src/cfg_rcl.c +++ b/src/cfg_rcl.c @@ -255,3 +255,37 @@ rspamd_read_rcl_config (struct rspamd_rcl_section *top, return TRUE; } + +gboolean +rspamd_rcl_parse_struct_string (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; + gchar **target; + const gsize num_str_len = 32; + + target = (gchar **)(((gchar *)pd->user_struct) + pd->offset); + switch (obj->type) { + case RSPAMD_CL_STRING: + /* Direct assigning is safe, as object is likely linked to the cfg mem_pool */ + *target = obj->value.sv; + break; + case RSPAMD_CL_INT: + *target = memory_pool_alloc (cfg->cfg_pool, num_str_len); + rspamd_snprintf (*target, num_str_len, "%L", obj->value.iv); + break; + case RSPAMD_CL_FLOAT: + *target = memory_pool_alloc (cfg->cfg_pool, num_str_len); + rspamd_snprintf (*target, num_str_len, "%f", obj->value.dv); + break; + case RSPAMD_CL_BOOLEAN: + *target = memory_pool_alloc (cfg->cfg_pool, num_str_len); + rspamd_snprintf (*target, num_str_len, "%b", (gboolean)obj->value.iv); + break; + default: + g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert object or array to string"); + return FALSE; + } + + return TRUE; +} diff --git a/src/cfg_rcl.h b/src/cfg_rcl.h index d02d196b1..8b2a3fdc1 100644 --- a/src/cfg_rcl.h +++ b/src/cfg_rcl.h @@ -83,4 +83,26 @@ struct rspamd_rcl_section *rspamd_rcl_config_get_section (struct rspamd_rcl_sect gboolean rspamd_read_rcl_config (struct rspamd_rcl_section *top, struct config_file *cfg, rspamd_cl_object_t *obj, GError **err); +/** + * Here is a section of common handlers that accepts rcl_struct_parser + * which itself contains a struct pointer and the offset of a member in a + * specific structure + */ +struct rspamd_rcl_struct_parser { + gpointer user_struct; + goffset offset; +}; + +/** + * Parse a string 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 string value has been successfully parsed + */ +gboolean rspamd_rcl_parse_struct_string (struct config_file *cfg, rspamd_cl_object_t *obj, + gpointer ud, struct rspamd_rcl_section *section, GError **err); + #endif /* CFG_RCL_H_ */ |