From 43f45800779ff60d9aeec775afb09fe405c04242 Mon Sep 17 00:00:00 2001 From: Sergey Kitov Date: Mon, 26 Feb 2018 16:33:19 +0200 Subject: [PATCH] lib: Add some event handling functions. event_has_all_categories() - checks if given event contains all the categories of an another event. event_has_all_fields() - checks if given event contains all the fields of an another event. event_dup() - duplicates an event. event_copy_categories_fields() - copies all categories and fields from source to dest. --- src/lib/lib-event.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ src/lib/lib-event.h | 13 +++++++++ 2 files changed, 82 insertions(+) diff --git a/src/lib/lib-event.c b/src/lib/lib-event.c index 416e6b56dd..4c66fdf515 100644 --- a/src/lib/lib-event.c +++ b/src/lib/lib-event.c @@ -46,6 +46,75 @@ static void event_copy_parent_defaults(struct event *event, event->forced_debug = parent->forced_debug; } +static bool +event_find_category(struct event *event, const struct event_category *category); + +static struct event_field * +event_find_field_int(struct event *event, const char *key); + +void event_copy_categories_fields(struct event *to, struct event *from) +{ + unsigned int cat_count; + struct event_category *const *categories = event_get_categories(from, &cat_count); + while (cat_count-- > 0) + event_add_category(to, categories[cat_count]); + const struct event_field *fld; + if (!array_is_created(&from->fields)) + return; + array_foreach(&from->fields, fld) { + switch (fld->value_type) { + case EVENT_FIELD_VALUE_TYPE_STR: + event_add_str(to, fld->key, fld->value.str); + break; + case EVENT_FIELD_VALUE_TYPE_INTMAX: + event_add_int(to, fld->key, fld->value.intmax); + break; + case EVENT_FIELD_VALUE_TYPE_TIMEVAL: + event_add_timeval(to, fld->key, &fld->value.timeval); + break; + default: + break; + } + } +} + +bool event_has_all_categories(struct event *event, const struct event *other) +{ + struct event_category **cat; + if (!array_is_created(&other->categories)) + return TRUE; + if (!array_is_created(&event->categories)) + return FALSE; + array_foreach_modifiable(&other->categories, cat) { + if (!event_find_category(event, *cat)) + return FALSE; + } + return TRUE; +} + +bool event_has_all_fields(struct event *event, const struct event *other) +{ + struct event_field *fld; + if (!array_is_created(&other->fields)) + return TRUE; + array_foreach_modifiable(&other->fields, fld) { + if (event_find_field_int(event, fld->key) == NULL) + return FALSE; + } + return TRUE; +} + +struct event *event_dup(const struct event *source) +{ + struct event *ret = event_create(source->parent); + string_t *str = t_str_new(256); + const char *err; + event_export(source, str); + if (!event_import(ret, str_c(str), &err)) + i_panic("event_import(%s) failed: %s", str_c(str), err); + return ret; +} + #undef event_create struct event *event_create(struct event *parent, const char *source_filename, unsigned int source_linenum) diff --git a/src/lib/lib-event.h b/src/lib/lib-event.h index cd4a84abdb..d283846975 100644 --- a/src/lib/lib-event.h +++ b/src/lib/lib-event.h @@ -73,6 +73,19 @@ struct event_passthrough { struct event *(*event)(void); }; +/* Returns TRUE if the event has all the categories that the "other" event has (and maybe more). */ +bool event_has_all_categories(struct event *event, const struct event *other); +/* Returns TRUE if the event has all the fields that the "other" event has (and maybe more). + Only the fields in the events themselves are checked. Parent events' fields are not checked. */ +bool event_has_all_fields(struct event *event, const struct event *other); + +/* Returns the source event duplicated into a new event. */ +struct event *event_dup(const struct event *source); +/* Copy all categories and fields from source to dest. + Only the fields and categories in source event itself are copied. + Parent events' fields and categories aren't copied. */ +void event_copy_categories_fields(struct event *dest, struct event *source); + /* Create a new empty event under the parent event, or NULL for root event. */ struct event *event_create(struct event *parent, const char *source_filename, unsigned int source_linenum);