<commit_before>/**
 * @file Tree_Data.cpp
 * @author <NAME> <<EMAIL>>
 * @brief Implementation of header Tree_Data.hpp.
 *
 * Copyright (c) 2017 Deutsche Telekom AG.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>

#include "Xml.hpp"
#include "Libyang.hpp"
#include "Tree_Data.hpp"
#include "Tree_Schema.hpp"

extern "C" {
#include "libyang.h"
#include "tree_data.h"
#include "tree_schema.h"
}

Value::Value(lyd_val value, uint16_t value_type, S_Deleter deleter):
    value(value),
    type(value_type),
    deleter(deleter)
{};
Value::~Value() {};
S_Data_Node Value::instance() {
    if (LY_TYPE_INST != type) {
        return nullptr;
    }
    return value.instance ? std::make_shared<Data_Node>(value.instance, deleter) : nullptr;
}
S_Data_Node Value::leafref() {
    if (LY_TYPE_LEAFREF != type) {
        return nullptr;
    }
    return value.leafref ? std::make_shared<Data_Node>(value.leafref, deleter) : nullptr;
}

Data_Node::Data_Node(struct lyd_node *node, S_Deleter deleter):
    node(node),
    deleter(deleter)
{};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new(parent ? parent->node : NULL, module->module, name);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *val_str) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_leaf(parent ? parent->node : NULL, module->module, name, val_str);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *value, LYD_ANYDATA_VALUETYPE value_type) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value, value_type);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Data_Node value) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value->node, LYD_ANYDATA_DATATREE);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Xml_Elem value) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value->elem, LYD_ANYDATA_XML);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = nullptr;
}
Data_Node::Data_Node(S_Context context, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options) {
    lyd_node *new_node = nullptr;

    if (!context) {
        throw std::invalid_argument("Context can not be empty");
    }
    if (!path) {
        throw std::invalid_argument("Path can not be empty");
    }

    new_node = lyd_new_path(NULL, context->ctx, path, (void *) value, value_type, options);
    if (!new_node) {
        check_libyang_error(context->ctx);
    }

    node = new_node;
    if (new_node) {
        deleter = std::make_shared<Deleter>(node, nullptr);
    } else {
        deleter = nullptr;
    }
}
Data_Node::Data_Node(S_Context context, const char *path, S_Data_Node value, int options) {
    lyd_node *new_node = nullptr;

    if (!context) {
        throw std::invalid_argument("Context can not be empty");
    }
    if (!path) {
        throw std::invalid_argument("Path can not be empty");<commit_msg>C++: fix memory leak in Data_Node constructor<commit_after>/**
 * @file Tree_Data.cpp
 * @author <NAME> <<EMAIL>>
 * @brief Implementation of header Tree_Data.hpp.
 *
 * Copyright (c) 2017 Deutsche Telekom AG.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>

#include "Xml.hpp"
#include "Libyang.hpp"
#include "Tree_Data.hpp"
#include "Tree_Schema.hpp"

extern "C" {
#include "libyang.h"
#include "tree_data.h"
#include "tree_schema.h"
}

Value::Value(lyd_val value, uint16_t value_type, S_Deleter deleter):
    value(value),
    type(value_type),
    deleter(deleter)
{};
Value::~Value() {};
S_Data_Node Value::instance() {
    if (LY_TYPE_INST != type) {
        return nullptr;
    }
    return value.instance ? std::make_shared<Data_Node>(value.instance, deleter) : nullptr;
}
S_Data_Node Value::leafref() {
    if (LY_TYPE_LEAFREF != type) {
        return nullptr;
    }
    return value.leafref ? std::make_shared<Data_Node>(value.leafref, deleter) : nullptr;
}

Data_Node::Data_Node(struct lyd_node *node, S_Deleter deleter):
    node(node),
    deleter(deleter)
{};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new(parent ? parent->node : nullptr, module->module, name);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = !parent ? std::make_shared<Deleter>(node, nullptr) : nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *val_str) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_leaf(parent ? parent->node : nullptr, module->module, name, val_str);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = !parent ? std::make_shared<Deleter>(node, nullptr) : nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, const char *value, LYD_ANYDATA_VALUETYPE value_type) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value, value_type);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = !parent ? std::make_shared<Deleter>(node, nullptr) : nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Data_Node value) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value->node, LYD_ANYDATA_DATATREE);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = !parent ? std::make_shared<Deleter>(node, nullptr) : nullptr;
};
Data_Node::Data_Node(S_Data_Node parent, S_Module module, const char *name, S_Xml_Elem value) {
    lyd_node *new_node = nullptr;

    if (!module) {
        throw std::invalid_argument("Module can not be empty");
    }

    new_node = lyd_new_anydata(parent ? parent->node : NULL, module->module, name, (void *) value->elem, LYD_ANYDATA_XML);
    if (!new_node) {
        check_libyang_error(module->module->ctx);
    }

    node = new_node;
    deleter = !parent ? std::make_shared<Deleter>(node, nullptr) : nullptr;
}
Data_Node::Data_Node(S_Context context, const char *path, const char *value, LYD_ANYDATA_VALUETYPE value_type, int options) {
    lyd_node *new_node = nullptr;

    if (!context) {
        throw std::invalid_argument("Context can not be empty");
    }
    if (!path) {
        throw std::invalid_argument("Path can not be empty");
    }

    new_node = lyd_new_path(NULL, context->ctx, path, (void *) value, value_type, options);
    if (!new_node) {
        check_libyang_error(context->ctx);
    }

    node = new_node;
    if (new_node) {
        deleter = std::make_shared<Deleter>(node, nullptr);
    } else {
        deleter = nullptr;
    }
}
Data_Node::Data_Node(S_Context context, const char *path, S_Data_Node value, int options) {
    lyd_node *new_node = nullptr;

    if (!context) {
        throw std::invalid_argument("Context can not be empty");
    }
    if (!path) {
        throw std::invalid_argument("Path can not be empty");
{'fraction_non_alphanumeric': 0.13163775673087155, 'fraction_numerical': 0.0017026710652335852, 'mean_word_length': 3.6295566502463052, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 76, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4197450', 'n_tokens_mistral': 3256, 'n_tokens_neox': 3178, 'n_words': 764}