diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-01-28 16:22:17 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2014-01-28 16:22:17 +0000 |
commit | 149fad4a73a71ef670be9a01741a52efd76e35e6 (patch) | |
tree | d0dff47a6eec7b0193a26252135280f0426b9bcd | |
parent | 26ede9ee72ced3e2b40123ab50bd0a518a8a6288 (diff) | |
download | rspamd-149fad4a73a71ef670be9a01741a52efd76e35e6.tar.gz rspamd-149fad4a73a71ef670be9a01741a52efd76e35e6.zip |
Make gstring emitting generic.
-rw-r--r-- | src/protocol.c | 72 | ||||
-rw-r--r-- | src/util.c | 76 | ||||
-rw-r--r-- | src/util.h | 9 |
3 files changed, 87 insertions, 70 deletions
diff --git a/src/protocol.c b/src/protocol.c index 2b7f03b5b..345723fb9 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -650,68 +650,6 @@ rspamd_metric_result_ucl (struct worker_task *task, struct metric_result *mres, return obj; } -/* - * GString ucl emitting functions - */ -static int -rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) -{ - GString *buf = ud; - - if (len == 1) { - g_string_append_c (buf, c); - } - else { - if (buf->allocated_len - buf->len <= len) { - g_string_set_size (buf, buf->len + len + 1); - } - memset (&buf->str[buf->len], c, len); - buf->len += len; - buf->str[buf->len] = '\0'; - } - - return 0; -} - -static int -rspamd_gstring_append_len (const unsigned char *str, size_t len, void *ud) -{ - GString *buf = ud; - - g_string_append_len (buf, str, len); - - return 0; -} - -static int -rspamd_gstring_append_int (int64_t val, void *ud) -{ - GString *buf = ud; - - rspamd_printf_gstring (buf, "%L", (intmax_t)val); - return 0; -} - -static int -rspamd_gstring_append_double (double val, void *ud) -{ - GString *buf = ud; - const double delta = 0.0000001; - - if (val == (double)(int)val) { - rspamd_printf_gstring (buf, "%.1f", val); - } - else if (fabs (val - (double)(int)val) < delta) { - /* Write at maximum precision */ - rspamd_printf_gstring (buf, "%.*g", DBL_DIG, val); - } - else { - rspamd_printf_gstring (buf, "%f", val); - } - - return 0; -} - static void write_check_reply (struct rspamd_http_message *msg, struct worker_task *task) { @@ -720,12 +658,6 @@ write_check_reply (struct rspamd_http_message *msg, struct worker_task *task) GHashTableIter hiter; gpointer h, v; ucl_object_t *top = NULL, *obj; - struct ucl_emitter_functions func = { - .ucl_emitter_append_character = rspamd_gstring_append_character, - .ucl_emitter_append_len = rspamd_gstring_append_len, - .ucl_emitter_append_int = rspamd_gstring_append_int, - .ucl_emitter_append_double = rspamd_gstring_append_double - }; /* Output the first line - check status */ logbuf = g_string_sized_new (BUFSIZ); @@ -762,8 +694,8 @@ write_check_reply (struct rspamd_http_message *msg, struct worker_task *task) g_string_free (logbuf, TRUE); msg->body = g_string_sized_new (BUFSIZ); - func.ud = msg->body; - ucl_object_emit_full (top, UCL_EMIT_JSON_COMPACT, &func); + + rspamd_ucl_emit_gstring (top, UCL_EMIT_JSON_COMPACT, msg->body); ucl_object_unref (top); /* Increase counters */ diff --git a/src/util.c b/src/util.c index 65f3b106b..3bf98a536 100644 --- a/src/util.c +++ b/src/util.c @@ -2178,5 +2178,81 @@ rspamd_ip_is_valid (void *ptr, int af) } /* + * GString ucl emitting functions + */ +static int +rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) +{ + GString *buf = ud; + + if (len == 1) { + g_string_append_c (buf, c); + } + else { + if (buf->allocated_len - buf->len <= len) { + g_string_set_size (buf, buf->len + len + 1); + } + memset (&buf->str[buf->len], c, len); + buf->len += len; + buf->str[buf->len] = '\0'; + } + + return 0; +} + +static int +rspamd_gstring_append_len (const unsigned char *str, size_t len, void *ud) +{ + GString *buf = ud; + + g_string_append_len (buf, str, len); + + return 0; +} + +static int +rspamd_gstring_append_int (int64_t val, void *ud) +{ + GString *buf = ud; + + rspamd_printf_gstring (buf, "%L", (intmax_t)val); + return 0; +} + +static int +rspamd_gstring_append_double (double val, void *ud) +{ + GString *buf = ud; + const double delta = 0.0000001; + + if (val == (double)(int)val) { + rspamd_printf_gstring (buf, "%.1f", val); + } + else if (fabs (val - (double)(int)val) < delta) { + /* Write at maximum precision */ + rspamd_printf_gstring (buf, "%.*g", DBL_DIG, val); + } + else { + rspamd_printf_gstring (buf, "%f", val); + } + + return 0; +} + +void +rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString *target) +{ + struct ucl_emitter_functions func = { + .ucl_emitter_append_character = rspamd_gstring_append_character, + .ucl_emitter_append_len = rspamd_gstring_append_len, + .ucl_emitter_append_int = rspamd_gstring_append_int, + .ucl_emitter_append_double = rspamd_gstring_append_double + }; + + func.ud = target; + ucl_object_emit_full (obj, emit_type, &func); +} + +/* * vi:ts=4 */ diff --git a/src/util.h b/src/util.h index 9b7891c05..2f33e91ee 100644 --- a/src/util.h +++ b/src/util.h @@ -7,6 +7,7 @@ #include "statfile.h" #include "printf.h" #include "fstring.h" +#include "ucl.h" struct config_file; struct rspamd_main; @@ -457,4 +458,12 @@ void rspamd_random_bytes (gchar *buf, gsize buflen); */ gboolean rspamd_ip_is_valid (void *ptr, int af); +/** + * Emit UCL object to gstring + * @param obj object to emit + * @param emit_type emitter type + * @param target target string + */ +void rspamd_ucl_emit_gstring (ucl_object_t *obj, enum ucl_emitter emit_type, GString *target); + #endif |