Browse Source

Move some aux functions from pline.c to kly.c

Serj Kalichev 1 year ago
parent
commit
1d3c36a0b3
3 changed files with 90 additions and 108 deletions
  1. 73 0
      src/kly.c
  2. 5 108
      src/pline.c
  3. 12 0
      src/private.h

+ 73 - 0
src/kly.c

@@ -19,6 +19,24 @@
 #include "pline.h"
 
 
+int klysc_key_compare(const void *first, const void *second)
+{
+	const klysc_key_t *f = (const klysc_key_t *)first;
+	const klysc_key_t *s = (const klysc_key_t *)second;
+
+	return strcmp(f->node->name, s->node->name);
+}
+
+
+int klysc_key_kcompare(const void *key, const void *list_item)
+{
+	const char *f = (const char *)key;
+	const klysc_key_t *s = (const klysc_key_t *)list_item;
+
+	return strcmp(f, s->node->name);
+}
+
+
 // Get extension by name from schema node
 static bool_t klysc_ext(const struct lysc_ext_instance *exts,
 	const char *module, const char *name, const char **argument)
@@ -126,3 +144,58 @@ char *klyd_node_value(const struct lyd_node *node)
 
 	return result;
 }
+
+
+// Don't use standard lys_find_child() because it checks given module to be
+// equal to found node's module. So augmented nodes will not be found.
+const struct lysc_node *klysc_find_child(const struct lysc_node *node,
+	const char *name)
+{
+	const struct lysc_node *iter = NULL;
+
+	if (!node)
+		return NULL;
+
+	LY_LIST_FOR(node, iter) {
+		if (!(iter->nodetype & SRP_NODETYPE_CONF))
+			continue;
+		if (!(iter->flags & LYS_CONFIG_W))
+			continue;
+		// Special case. LYS_CHOICE and LYS_CASE must search for
+		// specified name inside themselfs.
+		if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
+			const struct lysc_node *node_in = NULL;
+			node_in = klysc_find_child(lysc_node_child(iter), name);
+			if (node_in)
+				return node_in;
+			continue;
+		}
+		if (!faux_str_cmp(iter->name, name))
+			return iter;
+	}
+
+	return NULL;
+}
+
+
+struct lysc_ident *klysc_find_ident(struct lysc_ident *ident, const char *name)
+{
+	LY_ARRAY_COUNT_TYPE u = 0;
+
+	if (!ident)
+		return NULL;
+
+	if (!ident->derived) {
+		if (!faux_str_cmp(name, ident->name))
+			return ident;
+		return NULL;
+	}
+
+	LY_ARRAY_FOR(ident->derived, u) {
+		struct lysc_ident *identity = klysc_find_ident(ident->derived[u], name);
+		if (identity)
+			return identity;
+	}
+
+	return NULL;
+}

+ 5 - 108
src/pline.c

@@ -266,61 +266,6 @@ void pline_debug(pline_t *pline)
 }
 
 
-// Don't use standard lys_find_child() because it checks given module to be
-// equal to found node's module. So augmented nodes will not be found.
-static const struct lysc_node *find_child(const struct lysc_node *node,
-	const char *name)
-{
-	const struct lysc_node *iter = NULL;
-
-	if (!node)
-		return NULL;
-
-	LY_LIST_FOR(node, iter) {
-		if (!(iter->nodetype & SRP_NODETYPE_CONF))
-			continue;
-		if (!(iter->flags & LYS_CONFIG_W))
-			continue;
-		// Special case. LYS_CHOICE and LYS_CASE must search for
-		// specified name inside themselfs.
-		if (iter->nodetype & (LYS_CHOICE | LYS_CASE)) {
-			const struct lysc_node *node_in = NULL;
-			node_in = find_child(lysc_node_child(iter), name);
-			if (node_in)
-				return node_in;
-			continue;
-		}
-		if (!faux_str_cmp(iter->name, name))
-			return iter;
-	}
-
-	return NULL;
-}
-
-
-static struct lysc_ident *find_ident(struct lysc_ident *ident, const char *name)
-{
-	LY_ARRAY_COUNT_TYPE u = 0;
-
-	if (!ident)
-		return NULL;
-
-	if (!ident->derived) {
-		if (!faux_str_cmp(name, ident->name))
-			return ident;
-		return NULL;
-	}
-
-	LY_ARRAY_FOR(ident->derived, u) {
-		struct lysc_ident *identity = find_ident(ident->derived[u], name);
-		if (identity)
-			return identity;
-	}
-
-	return NULL;
-}
-
-
 static const char *identityref_prefix(struct lysc_type_identityref *type,
 	const char *name)
 {
@@ -329,7 +274,7 @@ static const char *identityref_prefix(struct lysc_type_identityref *type,
 	assert(type);
 
 	LY_ARRAY_FOR(type->bases, u) {
-		struct lysc_ident *identity = find_ident(type->bases[u], name);
+		struct lysc_ident *identity = klysc_find_ident(type->bases[u], name);
 		if (identity)
 			return identity->module->name;
 	}
@@ -338,29 +283,6 @@ static const char *identityref_prefix(struct lysc_type_identityref *type,
 }
 
 
-size_t list_num_of_keys(const struct lysc_node *node)
-{
-	const struct lysc_node *iter = NULL;
-	size_t num = 0;
-
-	assert(node);
-	if (!node)
-		return 0;
-	if (!(node->nodetype & LYS_LIST))
-		return 0;
-
-	LY_LIST_FOR(lysc_node_child(node), iter) {
-		if (!(iter->nodetype & LYS_LEAF))
-			continue;
-		if (!(iter->flags & LYS_KEY))
-			continue;
-		num++;
-	}
-
-	return num;
-}
-
-
 // Get module name by internal prefix. Sysrepo requests use module names but not
 // prefixes.
 static const char *module_by_prefix(const struct lysp_module *parsed, const char *prefix)
@@ -536,31 +458,6 @@ static char *leafref_xpath(const struct lysc_node *node, const char *node_path)
 }
 
 
-typedef struct {
-	const struct lysc_node *node;
-	const char *value;
-	const char *dflt;
-} klysc_key_t;
-
-
-static int klysc_key_compare(const void *first, const void *second)
-{
-	const klysc_key_t *f = (const klysc_key_t *)first;
-	const klysc_key_t *s = (const klysc_key_t *)second;
-
-	return strcmp(f->node->name, s->node->name);
-}
-
-
-static int klysc_key_kcompare(const void *key, const void *list_item)
-{
-	const char *f = (const char *)key;
-	const klysc_key_t *s = (const klysc_key_t *)list_item;
-
-	return strcmp(f, s->node->name);
-}
-
-
 static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
 	pline_t *pline, pline_opts_t *opts)
 {
@@ -623,7 +520,7 @@ static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *a
 			}
 
 			// Next element
-			node = find_child(module->compiled->data, str);
+			node = klysc_find_child(module->compiled->data, str);
 			if (!node)
 				break;
 
@@ -639,7 +536,7 @@ static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *a
 			}
 
 			// Next element
-			node = find_child(lysc_node_child(node), str);
+			node = klysc_find_child(lysc_node_child(node), str);
 
 		// List
 		} else if (node->nodetype & LYS_LIST) {
@@ -845,7 +742,7 @@ static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *a
 			}
 
 			// Next element
-			node = find_child(lysc_node_child(node), str);
+			node = klysc_find_child(lysc_node_child(node), str);
 
 		// Leaf
 		} else if (node->nodetype & LYS_LEAF) {
@@ -962,7 +859,7 @@ static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *a
 			}
 
 			// Next element
-			node = find_child(lysc_node_child(node), str);
+			node = klysc_find_child(lysc_node_child(node), str);
 
 		} else {
 			break;

+ 12 - 0
src/private.h

@@ -80,12 +80,24 @@ void show_subtree(const struct lyd_node *nodes_list, size_t level,
 int sr_module_is_internal(const struct lys_module *ly_mod);
 
 // kly helper library
+typedef struct {
+	const struct lysc_node *node;
+	const char *value;
+	const char *dflt;
+} klysc_key_t;
+int klysc_key_compare(const void *first, const void *second);
+int klysc_key_kcompare(const void *key, const void *list_item);
+
 bool_t klysc_node_ext(const struct lysc_node *node,
 	const char *module, const char *name, const char **argument);
 bool_t klysc_node_ext_is_password(const struct lysc_node *node);
 const char *klysc_node_ext_completion(const struct lysc_node *node);
 const char *klysc_node_ext_default(const struct lysc_node *node);
 char *klyd_node_value(const struct lyd_node *node);
+const struct lysc_node *klysc_find_child(const struct lysc_node *node,
+	const char *name);
+struct lysc_ident *klysc_find_ident(struct lysc_ident *ident, const char *name);
+
 
 C_DECL_END