aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-17 15:22:19 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-17 15:22:19 +0000
commit45c172ad0af49d2c73ddc3fcd2111718c6a8b19d (patch)
tree8dd75d91013de0f1f8ea80394056af04d85ce62c /src/libutil
parentc77ddf6def7a88a3842f2c78e27fe82bf78ce2fc (diff)
downloadrspamd-45c172ad0af49d2c73ddc3fcd2111718c6a8b19d.tar.gz
rspamd-45c172ad0af49d2c73ddc3fcd2111718c6a8b19d.zip
Add conversion from expr to string.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/expression.c60
-rw-r--r--src/libutil/expression.h7
2 files changed, 67 insertions, 0 deletions
diff --git a/src/libutil/expression.c b/src/libutil/expression.c
index 67db9e7de..c24f139b8 100644
--- a/src/libutil/expression.c
+++ b/src/libutil/expression.c
@@ -24,6 +24,7 @@
#include "config.h"
#include "expression.h"
+#include "printf.h"
#include "regexp.h"
enum rspamd_expression_op {
@@ -516,3 +517,62 @@ rspamd_process_expression (struct rspamd_expression *expr, gpointer data)
return 0;
}
+
+GString *
+rspamd_expression_tostring (struct rspamd_expression *expr)
+{
+ GString *res;
+ struct rspamd_expression_elt *elt;
+ const char *op_str = NULL;
+ guint i;
+
+ g_assert (expr != NULL);
+
+ res = g_string_new (NULL);
+
+ for (i = 0; i < expr->expressions->len; i ++) {
+ elt = &g_array_index (expr->expressions, struct rspamd_expression_elt, i);
+
+ if (elt->type == ELT_ATOM) {
+ g_string_append_len (res, elt->p.atom->str, elt->p.atom->len);
+ }
+ else if (elt->type == ELT_LIMIT) {
+ rspamd_printf_gstring (res, "%d", elt->p.lim.val);
+ }
+ else {
+ switch (elt->p.op) {
+ case OP_MULT:
+ op_str = "*";
+ break;
+ case OP_PLUS:
+ op_str = "+";
+ break;
+ case OP_NOT:
+ op_str = "!";
+ break;
+ case OP_GE:
+ op_str = ">=";
+ break;
+ case OP_GT:
+ op_str = ">";
+ break;
+ case OP_LE:
+ op_str = "<=";
+ break;
+ case OP_LT:
+ op_str = ">=";
+ break;
+ default:
+ op_str = "???";
+ break;
+ }
+ g_string_append (res, op_str);
+ }
+
+ if (i != expr->expressions->len - 1) {
+ g_string_append_c (res, ' ');
+ }
+ }
+
+ return res;
+}
diff --git a/src/libutil/expression.h b/src/libutil/expression.h
index 4179424c6..7d771963d 100644
--- a/src/libutil/expression.h
+++ b/src/libutil/expression.h
@@ -77,4 +77,11 @@ gboolean rspamd_parse_expression (const gchar *line, gsize len,
*/
gint rspamd_process_expression (struct rspamd_expression *expr, gpointer data);
+/**
+ * Shows string representation of an expression
+ * @param expr expression to show
+ * @return freshly allocated string with expression
+ */
+GString *rspamd_expression_tostring (struct rspamd_expression *expr);
+
#endif /* SRC_LIBUTIL_EXPRESSION_H_ */