From 7e5b4ac5ce82ebb5d03f2a6b186dcf1dbcc6fb3c Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 24 Feb 2019 11:10:44 +0100 Subject: [PATCH] refactor: add {node, path}_is_{enabled, reversed}() --- include/villas/node.h | 3 ++- include/villas/path.h | 6 +++++- lib/node.c | 8 +++++--- lib/path.c | 28 +++++++++++++++++++--------- lib/super_node.cpp | 4 ++-- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/include/villas/node.h b/include/villas/node.h index e7d34eac3..2b2f7fb08 100644 --- a/include/villas/node.h +++ b/include/villas/node.h @@ -198,7 +198,8 @@ struct node_type * node_type(struct node *n) struct memory_type * node_memory_type(struct node *n, struct memory_type *parent); -int node_is_valid_name(const char *name); +bool node_is_valid_name(const char *name); + bool node_is_enabled(const struct node *n); diff --git a/include/villas/path.h b/include/villas/path.h index 1abe9ab0c..0eaaf4b99 100644 --- a/include/villas/path.h +++ b/include/villas/path.h @@ -157,7 +157,11 @@ int path_uses_node(struct path *p, struct node *n); */ int path_parse(struct path *p, json_t *cfg, struct vlist *nodes); -int path_is_simple(struct path *p); +bool path_is_simple(const struct path *p); + +bool path_is_enabled(const struct path *p); + +bool path_is_reversed(const struct path *p); /** @} */ diff --git a/lib/node.c b/lib/node.c index f9752f903..c08043198 100644 --- a/lib/node.c +++ b/lib/node.c @@ -595,16 +595,18 @@ invalid2: return 0; } -int node_is_valid_name(const char *name) +bool node_is_valid_name(const char *name) { for (const char *p = name; *p; p++) { if (isalnum(*p) || (*p == '_') || (*p == '-')) continue; - return -1; + return false; } - return 0; + return true; +} + bool node_is_enabled(const struct node *n) { return n->enabled; diff --git a/lib/path.c b/lib/path.c index 4a0795dcd..63d2e05b4 100644 --- a/lib/path.c +++ b/lib/path.c @@ -545,8 +545,8 @@ int path_start(struct path *p) p->poll ? "yes" : "no", mask, p->rate, - p->enabled ? "yes" : "no", - p->reverse ? "yes" : "no", + path_is_enabled(p) ? "yes" : "no", + path_is_reversed(p) ? "yes" : "no", p->queuelen, vlist_length(&p->hooks), vlist_length(&p->sources), @@ -723,22 +723,32 @@ int path_uses_node(struct path *p, struct node *n) return -1; } -int path_is_simple(struct path *p) +bool path_is_simple(const struct path *p) { int ret; const char *in = NULL, *out = NULL; ret = json_unpack(p->cfg, "{ s: s, s: s }", "in", &in, "out", &out); if (ret) - return ret; + return false; ret = node_is_valid_name(in); - if (ret) - return ret; + if (!ret) + return false; ret = node_is_valid_name(out); - if (ret) - return ret; + if (!ret) + return false; - return 0; + return true; +} + +bool path_is_enabled(const struct path *p) +{ + return p->enabled; +} + +bool path_is_reversed(const struct path *p) +{ + return p->reverse; } diff --git a/lib/super_node.cpp b/lib/super_node.cpp index de842388e..0eba7eb04 100644 --- a/lib/super_node.cpp +++ b/lib/super_node.cpp @@ -218,7 +218,7 @@ int SuperNode::parseJson(json_t *j) const char *type; ret = node_is_valid_name(name); - if (ret) + if (!ret) throw RuntimeError("Invalid name for node: {}", name); ret = json_unpack_ex(json_node, &err, 0, "{ s: s }", "type", &type); @@ -266,7 +266,7 @@ parse: path *p = (path *) alloc(sizeof(path)); if (p->reverse) { /* Only simple paths can be reversed */ ret = path_is_simple(p); - if (ret) + if (!ret) throw RuntimeError("Complex paths can not be reversed!"); /* Parse a second time with in/out reversed */