From 6b4e9bbcbd76b65ef8d79d83fa128173c30d2f2e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 10 Sep 2020 13:22:33 +0200 Subject: [PATCH] mapping: fix intialization --- include/villas/mapping.h | 15 +++++++++----- lib/mapping.cpp | 45 +++++++++++++++++++++++++++++++++------- lib/signal.cpp | 3 +++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/include/villas/mapping.h b/include/villas/mapping.h index 2f7650b1a..8ef2d46f9 100644 --- a/include/villas/mapping.h +++ b/include/villas/mapping.h @@ -45,6 +45,7 @@ struct signal; struct vlist; enum class MappingType { + UNKNOWN, DATA, STATS, HEADER, @@ -62,7 +63,7 @@ enum class MappingTimestampType { }; struct mapping_entry { - const char *node_name; + char *node_name; struct vnode *node; /**< The node to which this mapping refers. */ enum MappingType type; /**< The mapping type. Selects one of the union fields below. */ @@ -79,8 +80,8 @@ struct mapping_entry { int offset; struct signal *signal; - const char *first; - const char *last; + char *first; + char *last; } data; struct { @@ -100,9 +101,13 @@ struct mapping_entry { int mapping_entry_prepare(struct mapping_entry *me, struct vlist *nodes); -int mapping_entry_update(const struct mapping_entry *e, struct sample *remapped, const struct sample *original); +int mapping_entry_update(const struct mapping_entry *me, struct sample *remapped, const struct sample *original); -int mapping_entry_parse(struct mapping_entry *e, json_t *cfg); +int mapping_entry_init(struct mapping_entry *me); + +int mapping_entry_destroy(struct mapping_entry *me); + +int mapping_entry_parse(struct mapping_entry *me, json_t *cfg); int mapping_entry_parse_str(struct mapping_entry *e, const std::string &str); diff --git a/lib/mapping.cpp b/lib/mapping.cpp index 15da70dcc..756756500 100644 --- a/lib/mapping.cpp +++ b/lib/mapping.cpp @@ -44,28 +44,29 @@ int mapping_entry_parse_str(struct mapping_entry *me, const std::string &str) if (mr[1].matched) me->node_name = strdup(mr.str(1).c_str()); - + if (mr[9].matched) me->node_name = strdup(mr.str(9).c_str()); if (mr[6].matched) { me->data.first = strdup(mr.str(6).c_str()); - - if (mr[7].matched) - me->data.last = strdup(mr.str(7).c_str()); + me->data.last = mr[7].matched + ? strdup(mr.str(7).c_str()) + : nullptr; me->type = MappingType::DATA; } else if (mr[10].matched) { me->data.first = strdup(mr.str(10).c_str()); - - if (mr[11].matched) - me->data.last = strdup(mr.str(11).c_str()); + me->data.last = mr[11].matched + ? strdup(mr.str(11).c_str()) + : nullptr; me->type = MappingType::DATA; } else if (mr[8].matched) { me->data.first = strdup(mr.str(8).c_str()); + me->data.last = nullptr; me->type = MappingType::DATA; } @@ -106,10 +107,28 @@ int mapping_entry_parse_str(struct mapping_entry *me, const std::string &str) return 0; invalid_format: - + throw RuntimeError("Failed to parse mapping expression: {}", str); } +int mapping_entry_init(struct mapping_entry *me) +{ + me->type = MappingType::UNKNOWN; + + me->node = nullptr; + me->node_name = nullptr; + + return 0; +} + +int mapping_entry_destroy(struct mapping_entry *me) +{ + if (me->node_name) + free(me->node_name); + + return 0; +} + int mapping_entry_parse(struct mapping_entry *me, json_t *cfg) { const char *str; @@ -143,6 +162,10 @@ int mapping_list_parse(struct vlist *ml, json_t *cfg) if (!me) throw MemoryAllocationError(); + ret = mapping_entry_init(me); + if (ret) + goto out; + ret = mapping_entry_parse(me, json_entry); if (ret) goto out; @@ -218,6 +241,9 @@ int mapping_update(const struct mapping_entry *me, struct sample *remapped, cons len = MIN((unsigned) me->length, original->length - me->data.offset); break; + + case MappingType::UNKNOWN: + return -1; } if (me->offset + len > remapped->length) @@ -379,6 +405,9 @@ int mapping_entry_to_str(const struct mapping_entry *me, unsigned index, char ** else strcatf(str, "data[%u]", index); break; + + case MappingType::UNKNOWN: + return -1; } return 0; diff --git a/lib/signal.cpp b/lib/signal.cpp index 0d5f262bf..199a4fdbb 100644 --- a/lib/signal.cpp +++ b/lib/signal.cpp @@ -86,6 +86,9 @@ int signal_init_from_mapping(struct signal *s, const struct mapping_entry *me, u if (me->data.signal->unit) s->name = strdup(me->data.signal->unit); break; + + case MappingType::UNKNOWN: + return -1; } return 0;