]> source.dussan.org Git - rspamd.git/commitdiff
Add integer converting function.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Aug 2013 14:15:26 +0000 (15:15 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Aug 2013 14:15:26 +0000 (15:15 +0100)
src/cfg_rcl.c
src/cfg_rcl.h

index 638b554455ffe7537c492e1fc40d264d5e43b47f..b59524cfbd2f22eb857f710cb1ca6cec1295e492 100644 (file)
@@ -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;
+}
index 8b2a3fdc1983af58c2ea22d944f1edc9f25b163f..7ee2a4fbbac04961b9a8442b34ea37d0d7e45577 100644 (file)
@@ -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_ */