diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-07 14:24:15 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-10-07 14:24:15 +0100 |
commit | 4af2ef67e91974144de793be97b8866d2a63c658 (patch) | |
tree | df3027d94959e08623b64190f25dafb5456ad4c1 /src | |
parent | ee9def66ca6120fdaa9acc623b8c8e49b1b2ea93 (diff) | |
download | rspamd-4af2ef67e91974144de793be97b8866d2a63c658.tar.gz rspamd-4af2ef67e91974144de793be97b8866d2a63c658.zip |
Add emitting of ucl object to rspamd_fstring_t.
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/fstring.c | 28 | ||||
-rw-r--r-- | src/libutil/fstring.h | 10 | ||||
-rw-r--r-- | src/libutil/str_util.c | 148 | ||||
-rw-r--r-- | src/libutil/str_util.h | 20 | ||||
-rw-r--r-- | src/libutil/util.c | 80 | ||||
-rw-r--r-- | src/libutil/util.h | 10 |
6 files changed, 206 insertions, 90 deletions
diff --git a/src/libutil/fstring.c b/src/libutil/fstring.c index e63912c9a..32fabcea4 100644 --- a/src/libutil/fstring.c +++ b/src/libutil/fstring.c @@ -144,6 +144,22 @@ rspamd_fstring_append (rspamd_fstring_t *str, const char *in, gsize len) return str; } +rspamd_fstring_t * +rspamd_fstring_append_chars (rspamd_fstring_t *str, + char c, gsize len) +{ + gsize avail = fstravail (str); + + if (avail < len) { + str = rspamd_fstring_grow (str, len); + } + + memset (str->str + str->len, c, len); + str->len += len; + + return str; +} + void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len) { @@ -329,4 +345,16 @@ rspamd_fstring_mapped_ftok_free (gpointer p) storage = (rspamd_fstring_t *) (tok->begin - 2 * sizeof (gsize)); rspamd_fstring_free (storage); g_slice_free1 (sizeof (*tok), tok); +} + +rspamd_ftok_t * +rspamd_ftok_map (const rspamd_fstring_t *s) +{ + rspamd_ftok_t *tok; + + g_assert (s != NULL); + + tok = g_slice_alloc (sizeof (*tok)); + tok->begin = s->str; + tok->len = s->len; }
\ No newline at end of file diff --git a/src/libutil/fstring.h b/src/libutil/fstring.h index 5ca74f457..d3b51821f 100644 --- a/src/libutil/fstring.h +++ b/src/libutil/fstring.h @@ -80,6 +80,11 @@ void rspamd_fstring_free (rspamd_fstring_t *str); rspamd_fstring_t* rspamd_fstring_append (rspamd_fstring_t *str, const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT; +/** + * Append `len` repeated chars `c` to string `str` + */ +rspamd_fstring_t *rspamd_fstring_append_chars (rspamd_fstring_t *str, + char c, gsize len) G_GNUC_WARN_UNUSED_RESULT; /** * Erase `len` characters at postion `pos` @@ -138,4 +143,9 @@ gint rspamd_ftok_cmp (const rspamd_ftok_t *s1, */ void rspamd_fstring_mapped_ftok_free (gpointer p); +/** + * Map token to a specified string. Token must be freed using g_slice_free1 + */ +rspamd_ftok_t *rspamd_ftok_map (const rspamd_fstring_t *s); + #endif diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index ffed9893d..82d4acfa3 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -1226,3 +1226,151 @@ rspamd_string_find_eoh (GString *input) return -1; } + +/* + * GString ucl emitting functions + */ +static int +rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) +{ + GString *buf = ud; + gsize old_len; + + if (len == 1) { + g_string_append_c (buf, c); + } + else { + if (buf->allocated_len - buf->len <= len) { + old_len = buf->len; + g_string_set_size (buf, buf->len + len + 1); + buf->len = old_len; + } + memset (&buf->str[buf->len], c, len); + buf->len += len; + } + + 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); +} + +/* + * FString ucl emitting functions + */ +static int +rspamd_fstring_emit_append_character (unsigned char c, size_t len, void *ud) +{ + rspamd_fstring_t **buf = ud; + + *buf = rspamd_fstring_append_chars (*buf, c, len); + + return 0; +} + +static int +rspamd_fstring_emit_append_len (const unsigned char *str, size_t len, void *ud) +{ + rspamd_fstring_t **buf = ud; + + *buf = rspamd_fstring_append (*buf, str, len); + + return 0; +} + +static int +rspamd_fstring_emit_append_int (int64_t val, void *ud) +{ + rspamd_fstring_t **buf = ud; + + rspamd_printf_fstring (buf, "%L", (intmax_t) val); + return 0; +} + +static int +rspamd_fstring_emit_append_double (double val, void *ud) +{ + rspamd_fstring_t **buf = ud; + const double delta = 0.0000001; + + if (val == (double)((gint) val)) { + rspamd_printf_fstring (buf, "%.1f", val); + } + else if (fabs (val - (double) (int) val) < delta) { + /* Write at maximum precision */ + rspamd_printf_fstring (buf, "%.*g", DBL_DIG, val); + } + else { + rspamd_printf_fstring (buf, "%f", val); + } + + return 0; +} + +void +rspamd_ucl_emit_fstring (ucl_object_t *obj, + enum ucl_emitter emit_type, + rspamd_fstring_t **buf) +{ + struct ucl_emitter_functions func = { + .ucl_emitter_append_character = rspamd_fstring_emit_append_character, + .ucl_emitter_append_len = rspamd_fstring_emit_append_len, + .ucl_emitter_append_int = rspamd_fstring_emit_append_int, + .ucl_emitter_append_double = rspamd_fstring_emit_append_double + }; + + func.ud = buf; + ucl_object_emit_full (obj, emit_type, &func); +} diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 801bcc109..2f822d97c 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -197,4 +197,24 @@ goffset rspamd_substring_search (const gchar *in, gsize inlen, */ goffset rspamd_string_find_eoh (GString *input); +/** + * 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); + +/** + * Emit UCL object to fstring + * @param obj object to emit + * @param emit_type emitter type + * @param target target string + */ +void rspamd_ucl_emit_fstring (ucl_object_t *obj, + enum ucl_emitter emit_type, + rspamd_fstring_t **target); + #endif /* SRC_LIBUTIL_STR_UTIL_H_ */ diff --git a/src/libutil/util.c b/src/libutil/util.c index fd66cc321..5706a2f49 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -1743,86 +1743,6 @@ restart: #endif } -/* - * GString ucl emitting functions - */ -static int -rspamd_gstring_append_character (unsigned char c, size_t len, void *ud) -{ - GString *buf = ud; - gsize old_len; - - if (len == 1) { - g_string_append_c (buf, c); - } - else { - if (buf->allocated_len - buf->len <= len) { - old_len = buf->len; - g_string_set_size (buf, buf->len + len + 1); - buf->len = old_len; - } - memset (&buf->str[buf->len], c, len); - buf->len += len; - } - - 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); -} - gdouble rspamd_get_ticks (void) { diff --git a/src/libutil/util.h b/src/libutil/util.h index df1c30e7d..7efb728b1 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -336,16 +336,6 @@ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, gint rspamd_read_passphrase (gchar *buf, gint size, gint rwflag, gpointer key); /** - * 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); - -/** * Portably return the current clock ticks as seconds * @return */ |