<commit_before>/**
 * @file rp_dt_lookup.c
 * @author <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
 * @brief
 *
 * @copyright
 * Copyright 2016 Cisco Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdbool.h>

#include "rp_dt_lookup.h"

static int
rp_dt_push_child_nodes_to_stack(rp_node_stack_t **stack, struct lyd_node *node)
{
    CHECK_NULL_ARG2(stack, node);
    int rc = SR_ERR_OK;
    struct lyd_node *n = node->child;
    while (NULL != n) {
        rc = rp_ns_push(stack, n);
        if (SR_ERR_OK != rc) {
            return SR_ERR_INTERNAL;
        }
        n = n->next;
    }
    return rc;
}

static int
rp_dt_push_nodes_with_same_name_to_stack(rp_node_stack_t **stack, struct lyd_node *node)
{
    CHECK_NULL_ARG2(stack, node);
    int rc = SR_ERR_OK;
    struct lyd_node *n = node;
    while (NULL != n) {
        if (NULL == n->schema || NULL == n->schema->name) {
            SR_LOG_ERR_MSG("Missing schema information");
            return SR_ERR_INTERNAL;
        }
        if (0 == strcmp(node->schema->name, n->schema->name)) {
            rc = rp_ns_push(stack, n);
            if (SR_ERR_OK != rc) {
                return SR_ERR_INTERNAL;
            }
        }
        n = n->next;
    }
    return rc;
}

int
rp_dt_get_all_children_node(struct lyd_node *node, struct lyd_node ***nodes, size_t *count)
{
    CHECK_NULL_ARG3(node, nodes, count);
    /* get node count */
    size_t cnt = 0;
    struct lyd_node *n = node->child;
    while (NULL != n) {
        n = n->next;
        cnt++;
    }

    *count = cnt;
    *nodes = calloc(cnt, sizeof(**nodes));
    if (NULL == *nodes) {
        SR_LOG_ERR_MSG("Memory allocation failed");
        return SR_ERR_NOMEM;
    }

    n = node->child;
    cnt = 0;<commit_msg>Fix order of nodes in sr_get_items_iter<commit_after>/**
 * @file rp_dt_lookup.c
 * @author <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
 * @brief
 *
 * @copyright
 * Copyright 2016 Cisco Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdbool.h>

#include "rp_dt_lookup.h"

static int
rp_dt_push_child_nodes_to_stack(rp_node_stack_t **stack, struct lyd_node *node)
{
    CHECK_NULL_ARG2(stack, node);
    int rc = SR_ERR_OK;
    struct lyd_node *n = node->child;
    if (NULL == n) {
        return rc;
    }
    /* find last child, push the child on stack in reverse order*/
    while (NULL != n->next) {
        n = n->next;
    }

    while (1) {
        rc = rp_ns_push(stack, n);
        if (SR_ERR_OK != rc) {
            return SR_ERR_INTERNAL;
        }
        if (node->child == n) {
            break;
        }
        n = n->prev;
    }
    return rc;
}

static int
rp_dt_push_nodes_with_same_name_to_stack(rp_node_stack_t **stack, struct lyd_node *node)
{
    CHECK_NULL_ARG2(stack, node);
    int rc = SR_ERR_OK;
    struct lyd_node *n = node;
    /* find last sibling, push them on stack in reverse order*/
    while (NULL != n->next) {
        n = n->next;
    }

    while (1) {
        if (NULL == n->schema || NULL == n->schema->name) {
            SR_LOG_ERR_MSG("Missing schema information");
            return SR_ERR_INTERNAL;
        }
        if (0 == strcmp(node->schema->name, n->schema->name)) {
            rc = rp_ns_push(stack, n);
            if (SR_ERR_OK != rc) {
                return SR_ERR_INTERNAL;
            }
        }
        if (node == n) {
            break;
        }
        n = n->prev;
    }
    return rc;
}

int
rp_dt_get_all_children_node(struct lyd_node *node, struct lyd_node ***nodes, size_t *count)
{
    CHECK_NULL_ARG3(node, nodes, count);
    /* get node count */
    size_t cnt = 0;
    struct lyd_node *n = node->child;
    while (NULL != n) {
        n = n->next;
        cnt++;
    }

    *count = cnt;
    *nodes = calloc(cnt, sizeof(**nodes));
    if (NULL == *nodes) {
        SR_LOG_ERR_MSG("Memory allocation failed");
        return SR_ERR_NOMEM;
    }

    n = node->child;
    cnt = 0;
{'fraction_non_alphanumeric': 0.11960467930617184, 'fraction_numerical': 0.0060508269463493344, 'mean_word_length': 2.4059065934065935, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514082', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1656, 'n_words': 482}