aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2013-08-29 16:06:40 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2013-08-29 16:06:40 +0100
commit82776e7dc0eed455fc7fdf144e96286abb3ef3bd (patch)
treeb78ffda390dd584c58fe92ea0c3c79941b8531d7
parentddea9ac6acfa6bf3fb7aefa89c93ccf42cbe4c06 (diff)
downloadrspamd-82776e7dc0eed455fc7fdf144e96286abb3ef3bd.tar.gz
rspamd-82776e7dc0eed455fc7fdf144e96286abb3ef3bd.zip
Add time fields parser.
-rw-r--r--src/cfg_rcl.c45
-rw-r--r--src/cfg_rcl.h23
2 files changed, 63 insertions, 5 deletions
diff --git a/src/cfg_rcl.c b/src/cfg_rcl.c
index 942c944a6..f2f48f912 100644
--- a/src/cfg_rcl.c
+++ b/src/cfg_rcl.c
@@ -354,6 +354,51 @@ rspamd_rcl_parse_struct_double (struct config_file *cfg, rspamd_cl_object_t *obj
if (!rspamd_cl_obj_todouble_safe (obj, target)) {
g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to double");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+rspamd_rcl_parse_struct_time (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 *psec;
+ gdouble *pdv;
+ struct timeval *ptv;
+ struct timespec *pts;
+ } target;
+ gdouble val;
+
+ if (!rspamd_cl_obj_todouble_safe (obj, &val)) {
+ g_set_error (err, CFG_RCL_ERROR, EINVAL, "cannot convert param to double");
+ return FALSE;
+ }
+
+ if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMEVAL) {
+ target.ptv = (struct timeval *)(((gchar *)pd->user_struct) + pd->offset);
+ target.ptv->tv_sec = (glong)val;
+ target.ptv->tv_usec = (val - (glong)val) * 1000000;
+ }
+ else if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMESPEC) {
+ target.pts = (struct timespec *)(((gchar *)pd->user_struct) + pd->offset);
+ target.pts->tv_sec = (glong)val;
+ target.pts->tv_nsec = (val - (glong)val) * 1000000000000LL;
+ }
+ else if (pd->flags == RSPAMD_CL_FLAG_TIME_FLOAT) {
+ target.pdv = (double *)(((gchar *)pd->user_struct) + pd->offset);
+ *target.pdv = val;
+ }
+ else if (pd->flags == RSPAMD_CL_FLAG_TIME_INTEGER) {
+ target.psec = (gint *)(((gchar *)pd->user_struct) + pd->offset);
+ *target.psec = val;
+ }
+ else {
+ g_set_error (err, CFG_RCL_ERROR, EINVAL, "invalid flags to parse time value");
+ return FALSE;
}
return TRUE;
diff --git a/src/cfg_rcl.h b/src/cfg_rcl.h
index 93b9bba7d..8c77009a2 100644
--- a/src/cfg_rcl.h
+++ b/src/cfg_rcl.h
@@ -92,11 +92,12 @@ struct rspamd_rcl_struct_parser {
gpointer user_struct;
goffset offset;
gsize size;
-#define RSPAMD_CL_FLAG_TIME_FLOAT 0x1 << 0
-#define RSPAMD_CL_FLAG_TIME_TIMEVAL 0x1 << 1
-#define RSPAMD_CL_FLAG_TIME_TIMESPEC 0x1 << 2
-#define RSPAMD_CL_FLAG_TIME_INTEGER 0x1 << 3
- gint flags;
+ enum {
+ RSPAMD_CL_FLAG_TIME_FLOAT = 0x1 << 0,
+ RSPAMD_CL_FLAG_TIME_TIMEVAL = 0x1 << 1,
+ RSPAMD_CL_FLAG_TIME_TIMESPEC = 0x1 << 2,
+ RSPAMD_CL_FLAG_TIME_INTEGER = 0x1 << 3
+ } flags;
};
/**
@@ -136,4 +137,16 @@ gboolean rspamd_rcl_parse_struct_integer (struct config_file *cfg, rspamd_cl_obj
gboolean rspamd_rcl_parse_struct_double (struct config_file *cfg, rspamd_cl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err);
+/**
+ * Parse a time field of a structure
+ * @param cfg config pointer
+ * @param obj object to parse
+ * @param ud struct_parser structure (flags mean the exact structure used)
+ * @param section the current section
+ * @param err error pointer
+ * @return TRUE if a value has been successfully parsed
+ */
+gboolean rspamd_rcl_parse_struct_time (struct config_file *cfg, rspamd_cl_object_t *obj,
+ gpointer ud, struct rspamd_rcl_section *section, GError **err);
+
#endif /* CFG_RCL_H_ */