<commit_before>

#include "PropertyHandler.h"

#include "Error.h"
#include "standard_properties.h"
#include "utils.h"
#include "logging.h"

#include <algorithm>

using namespace tcam;


PropertyHandler::PropertyHandler ()
{}


PropertyHandler::~PropertyHandler ()
{}


bool PropertyHandler::set_properties (std::vector<std::shared_ptr<Property>> device,
                                      std::vector<std::shared_ptr<Property>> emulated)
{
    clear();

    device_properties.insert(device_properties.end(), device.begin(), device.end());
    emulated_properties.insert(emulated_properties.end(), emulated.begin(), emulated.end());

    generate_properties();

    return true;
}


std::vector<std::shared_ptr<Property>> PropertyHandler::get_properties ()
{
    return external_properties;
}


void PropertyHandler::sync ()
{}


void PropertyHandler::clear ()
{
    properties.clear();
    external_properties.clear();

    device_properties.clear();
    emulated_properties.clear();
}


bool PropertyHandler::setProperty (const Property& p)
{
    for (auto& prop : properties)
    {
        if (prop.external_property->getID() == p.getID())
        {
            if (prop.external_property->isReadOnly())
            {
                setError(Error("Property is read only.", EACCES));
                return false;
            }

            prop.internal_property->setProperty(p);
            prop.external_property->setStruct(p.getStruct());
        }
    }

    if (p.getID() == TCAM_PROPERTY_EXPOSURE_AUTO)
    {
        auto pea = find_property(emulated_properties, TCAM_PROPERTY_EXPOSURE_AUTO);
        bool vla = static_cast<PropertyBoolean&>(*pea).getValue();
        if (vla)
        {
            auto pe = find_property(external_properties, TCAM_PROPERTY_EXPOSURE);
            auto s = pe->getStruct();
            s.flags = set_bit(s.flags, TCAM_PROPERTY_FLAG_READ_ONLY);
            pe->setStruct(s);
        }
        else
        {
            auto pe = find_property(external_properties, TCAM_PROPERTY_EXPOSURE);
            auto s = pe->getStruct();
            s.flags = unset_bit(s.flags, TCAM_PROPERTY_FLAG_READ_ONLY);
            pe->setStruct(s);
        }
    }
    return false;
}


bool PropertyHandler::getProperty (Property& p)
{
    auto prop = find_mapping_external(p.getID());

    if (prop.internal_property != nullptr)
    {
        auto i = prop.internal_property;
        prop.external_property->setStruct(i->getStruct());
        p.setStruct(i->getStruct());
        return true;<commit_msg>Assured external flags are not overwritten<commit_after>

#include "PropertyHandler.h"

#include "Error.h"
#include "standard_properties.h"
#include "utils.h"
#include "logging.h"

#include <algorithm>

using namespace tcam;


PropertyHandler::PropertyHandler ()
{}


PropertyHandler::~PropertyHandler ()
{}


bool PropertyHandler::set_properties (std::vector<std::shared_ptr<Property>> device,
                                      std::vector<std::shared_ptr<Property>> emulated)
{
    clear();

    device_properties.insert(device_properties.end(), device.begin(), device.end());
    emulated_properties.insert(emulated_properties.end(), emulated.begin(), emulated.end());

    generate_properties();

    return true;
}


std::vector<std::shared_ptr<Property>> PropertyHandler::get_properties ()
{
    return external_properties;
}


void PropertyHandler::sync ()
{}


void PropertyHandler::clear ()
{
    properties.clear();
    external_properties.clear();

    device_properties.clear();
    emulated_properties.clear();
}


bool PropertyHandler::setProperty (const Property& p)
{
    for (auto& prop : properties)
    {
        if (prop.external_property->getID() == p.getID())
        {
            if (prop.external_property->isReadOnly())
            {
                setError(Error("Property is read only.", EACCES));
                return false;
            }

            prop.internal_property->setProperty(p);
            prop.external_property->setStruct(p.getStruct());
        }
    }

    if (p.getID() == TCAM_PROPERTY_EXPOSURE_AUTO)
    {
        auto pea = find_property(emulated_properties, TCAM_PROPERTY_EXPOSURE_AUTO);
        bool vla = static_cast<PropertyBoolean&>(*pea).getValue();
        if (vla)
        {
            auto pe = find_property(external_properties, TCAM_PROPERTY_EXPOSURE);
            auto s = pe->getStruct();
            s.flags = set_bit(s.flags, TCAM_PROPERTY_FLAG_READ_ONLY);
            pe->setStruct(s);
        }
        else
        {
            auto pe = find_property(external_properties, TCAM_PROPERTY_EXPOSURE);
            auto s = pe->getStruct();
            s.flags = unset_bit(s.flags, TCAM_PROPERTY_FLAG_READ_ONLY);
            pe->setStruct(s);
        }
    }
    return false;
}


bool PropertyHandler::getProperty (Property& p)
{
    auto prop = find_mapping_external(p.getID());

    if (prop.internal_property != nullptr)
    {
        auto i = prop.internal_property;

        auto ext_struct = prop.external_property->getStruct();

        ext_struct.value = i->getStruct().value;

        prop.external_property->setStruct(ext_struct);
        p.setStruct(ext_struct);
        return true;
{'fraction_non_alphanumeric': 0.12557692307692309, 'fraction_numerical': 0.0, 'mean_word_length': 2.81025641025641, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6055302', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1519, 'n_words': 255}