lyb REFACTOR added lyb_print_node/lyb_parse_node
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index d84bb1f..a36ee6a 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -1383,6 +1383,54 @@
}
/**
+ * @brief Parse node.
+ *
+ * @param[in] out Out structure.
+ * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
+ * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
+ * @param[in] lybctx LYB context.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
+ struct ly_set *parsed)
+{
+ LY_ERR ret;
+ const struct lysc_node *snode;
+ const struct lys_module *mod;
+
+ if (!parent || !parent->schema) {
+ /* top-level or opaque, read module name */
+ ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
+ LY_CHECK_RET(ret);
+
+ /* read hash, find the schema node starting from mod */
+ ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
+ } else {
+ /* read hash, find the schema node starting from parent schema */
+ ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
+ }
+ LY_CHECK_RET(ret);
+
+ if (!snode) {
+ ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
+ } else if (snode->nodetype & LYS_LEAFLIST) {
+ ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
+ } else if (snode->nodetype == LYS_LIST) {
+ ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
+ } else if (snode->nodetype & LYD_NODE_ANY) {
+ ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
+ } else if (snode->nodetype & LYD_NODE_INNER) {
+ ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
+ } else {
+ ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
+ }
+ LY_CHECK_RET(ret);
+
+ return ret;
+}
+
+/**
* @brief Parse siblings (@ref lyb_print_siblings()).
*
* @param[in] lybctx LYB context.
@@ -1396,8 +1444,6 @@
struct ly_set *parsed)
{
LY_ERR ret;
- const struct lysc_node *snode;
- const struct lys_module *mod;
ly_bool top_level;
if (lybctx->lybctx->in->current[0] == 0) {
@@ -1412,32 +1458,7 @@
LY_CHECK_RET(ret);
while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
- if (!parent || !parent->schema) {
- /* top-level or opaque, read module name */
- ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
- LY_CHECK_RET(ret);
-
- /* read hash, find the schema node starting from mod */
- ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
- } else {
- /* read hash, find the schema node starting from parent schema */
- ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
- }
- LY_CHECK_RET(ret);
-
- if (!snode) {
- ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
- } else if (snode->nodetype & LYS_LEAFLIST) {
- ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
- } else if (snode->nodetype == LYS_LIST) {
- ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
- } else if (snode->nodetype & LYD_NODE_ANY) {
- ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
- } else if (snode->nodetype & LYD_NODE_INNER) {
- ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
- } else {
- ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
- }
+ ret = lyb_parse_node(lybctx, parent, first_p, parsed);
LY_CHECK_RET(ret);
if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
diff --git a/src/printer_lyb.c b/src/printer_lyb.c
index 71f6faa..448aa21 100644
--- a/src/printer_lyb.c
+++ b/src/printer_lyb.c
@@ -1123,6 +1123,50 @@
}
/**
+ * @brief Print node.
+ *
+ * @param[in] out Out structure.
+ * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
+ * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
+ * @param[in] lybctx LYB context.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lyb_print_node(struct ly_out *out, const struct lyd_node **printed_node, struct hash_table **sibling_ht,
+ struct lyd_lyb_ctx *lybctx)
+{
+ const struct lyd_node *node = *printed_node;
+
+ /* write model info first, for all opaque and top-level nodes */
+ if (!node->schema && (!node->parent || !node->parent->schema)) {
+ LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
+ } else if (node->schema && !lysc_data_parent(node->schema)) {
+ LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
+ }
+
+ /* write schema hash */
+ LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, sibling_ht, lybctx->lybctx));
+
+ if (!node->schema) {
+ LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
+ } else if (node->schema->nodetype & LYS_LEAFLIST) {
+ LY_CHECK_RET(lyb_print_node_leaflist(out, node, lybctx, &node));
+ } else if (node->schema->nodetype == LYS_LIST) {
+ LY_CHECK_RET(lyb_print_node_list(out, node, lybctx, &node));
+ } else if (node->schema->nodetype & LYD_NODE_ANY) {
+ LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
+ } else if (node->schema->nodetype & LYD_NODE_INNER) {
+ LY_CHECK_RET(lyb_print_node_inner(out, node, lybctx));
+ } else {
+ LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
+ }
+
+ *printed_node = node;
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Print siblings.
*
* @verbatim
@@ -1165,41 +1209,24 @@
LY_CHECK_RET(lyb_write_start_siblings(out, lybctx->lybctx));
- /* write all the siblings */
- LY_LIST_FOR(node, node) {
+ if (top_level) {
+ /* write all the siblings */
+ LY_LIST_FOR(node, node) {
+ /* do not reuse sibling hash tables from different modules */
+ if (!node->schema || (node->schema->module != prev_mod)) {
+ sibling_ht = NULL;
+ prev_mod = node->schema ? node->schema->module : NULL;
+ }
- /* do not reuse sibling hash tables from different modules */
- if (top_level && (!node->schema || (node->schema->module != prev_mod))) {
- sibling_ht = NULL;
- prev_mod = node->schema ? node->schema->module : NULL;
+ LY_CHECK_RET(lyb_print_node(out, &node, &sibling_ht, lybctx));
+
+ if (!(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
+ break;
+ }
}
-
- /* write model info first, for all opaque and top-level nodes */
- if (!node->schema && (!node->parent || !node->parent->schema)) {
- LY_CHECK_RET(lyb_print_model(out, NULL, lybctx->lybctx));
- } else if (node->schema && !lysc_data_parent(node->schema)) {
- LY_CHECK_RET(lyb_print_model(out, node->schema->module, lybctx->lybctx));
- }
-
- /* write schema hash */
- LY_CHECK_RET(lyb_print_schema_hash(out, (struct lysc_node *)node->schema, &sibling_ht, lybctx->lybctx));
-
- if (!node->schema) {
- LY_CHECK_RET(lyb_print_node_opaq(out, (struct lyd_node_opaq *)node, lybctx));
- } else if (node->schema->nodetype & LYS_LEAFLIST) {
- LY_CHECK_RET(lyb_print_node_leaflist(out, node, lybctx, &node));
- } else if (node->schema->nodetype == LYS_LIST) {
- LY_CHECK_RET(lyb_print_node_list(out, node, lybctx, &node));
- } else if (node->schema->nodetype & LYD_NODE_ANY) {
- LY_CHECK_RET(lyb_print_node_any(out, (struct lyd_node_any *)node, lybctx));
- } else if (node->schema->nodetype & LYD_NODE_INNER) {
- LY_CHECK_RET(lyb_print_node_inner(out, node, lybctx));
- } else {
- LY_CHECK_RET(lyb_print_node_leaf(out, node, lybctx));
- }
-
- if (top_level && !(lybctx->print_options & LYD_PRINT_WITHSIBLINGS)) {
- break;
+ } else {
+ LY_LIST_FOR(node, node) {
+ LY_CHECK_RET(lyb_print_node(out, &node, &sibling_ht, lybctx));
}
}