<commit_before>/*
  This file is part of Ingen.
  Copyright 2007-2012 <NAME> <http://drobilla.net/>

  Ingen is free software: you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free
  Software Foundation, either version 3 of the License, or any later version.

  Ingen is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for details.

  You should have received a copy of the GNU Affero General Public License
  along with Ingen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <utility>

#include "ingen/AtomReader.hpp"
#include "ingen/Log.hpp"
#include "ingen/Node.hpp"
#include "ingen/URIMap.hpp"
#include "lv2/lv2plug.in/ns/ext/atom/util.h"
#include "raul/Path.hpp"

namespace Ingen {

AtomReader::AtomReader(URIMap&    map,
                       URIs&      uris,
                       Log&       log,
                       Forge&     forge,
                       Interface& iface)
	: _map(map)
	, _uris(uris)
	, _log(log)
	, _forge(forge)
	, _iface(iface)
{
}

void
AtomReader::get_atom(const LV2_Atom* in, Raul::Atom& out)
{
	if (in) {
		if (in->type == _uris.atom_URID) {
			const LV2_Atom_URID* urid = (const LV2_Atom_URID*)in;
			const char* uri           = _map.unmap_uri(urid->body);
			if (uri) {
				out = _forge.alloc_uri(_map.unmap_uri(urid->body));
			} else {
				_log.error(Raul::fmt("Unable to unmap URID %1%\n")
				           % urid->body);
			}
		} else {
			out = _forge.alloc(in->size, in->type, LV2_ATOM_BODY_CONST(in));
		}
	}
}

void
AtomReader::get_props(const LV2_Atom_Object*       obj,
                      Ingen::Resource::Properties& props)
{
	if (obj->body.otype) {
		props.insert(
			std::make_pair(_uris.rdf_type,
			               _forge.alloc_uri(_map.unmap_uri(obj->body.otype))));
	}
	LV2_ATOM_OBJECT_FOREACH(obj, p) {
		Raul::Atom val;
		get_atom(&p->value, val);
		props.insert(std::make_pair(_map.unmap_uri(p->key), val));
	}
}

const char*
AtomReader::atom_to_uri(const LV2_Atom* atom)
{
	if (atom && atom->type == _uris.atom_URI) {
		return (const char*)LV2_ATOM_BODY_CONST(atom);
	} else if (atom && atom->type == _uris.atom_URID) {
		return _map.unmap_uri(((const LV2_Atom_URID*)atom)->body);
	} else {
		return NULL;
	}
}

boost::optional<Raul::Path>
AtomReader::atom_to_path(const LV2_Atom* atom)
{
	const char* uri_str = atom_to_uri(atom);
	if (uri_str && Raul::URI::is_valid(uri_str) &&
	    Node::uri_is_path(Raul::URI(uri_str))) {
		return Node::uri_to_path(Raul::URI(uri_str));
	}
	return boost::optional<Raul::Path>();
}

bool
AtomReader::is_message(URIs& uris, const LV2_Atom* msg)
{
	if (msg->type != uris.atom_Blank && msg->type != uris.atom_Resource) {
		return false;
	}

	const LV2_Atom_Object* obj = (const LV2_Atom_Object*)msg;
	return (obj->body.otype == uris.patch_Get ||
	        obj->body.otype == uris.patch_Delete ||
	        obj->body.otype == uris.patch_Put ||
	        obj->body.otype == uris.patch_Patch ||
	        obj->body.otype == uris.patch_Move ||<commit_msg>Fix set messages via atom interface (e.g. port controls in LV2 patcher UI)<commit_after>/*
  This file is part of Ingen.
  Copyright 2007-2012 <NAME> <http://drobilla.net/>

  Ingen is free software: you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free
  Software Foundation, either version 3 of the License, or any later version.

  Ingen is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for details.

  You should have received a copy of the GNU Affero General Public License
  along with Ingen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <utility>

#include "ingen/AtomReader.hpp"
#include "ingen/Log.hpp"
#include "ingen/Node.hpp"
#include "ingen/URIMap.hpp"
#include "lv2/lv2plug.in/ns/ext/atom/util.h"
#include "raul/Path.hpp"

namespace Ingen {

AtomReader::AtomReader(URIMap&    map,
                       URIs&      uris,
                       Log&       log,
                       Forge&     forge,
                       Interface& iface)
	: _map(map)
	, _uris(uris)
	, _log(log)
	, _forge(forge)
	, _iface(iface)
{
}

void
AtomReader::get_atom(const LV2_Atom* in, Raul::Atom& out)
{
	if (in) {
		if (in->type == _uris.atom_URID) {
			const LV2_Atom_URID* urid = (const LV2_Atom_URID*)in;
			const char* uri           = _map.unmap_uri(urid->body);
			if (uri) {
				out = _forge.alloc_uri(_map.unmap_uri(urid->body));
			} else {
				_log.error(Raul::fmt("Unable to unmap URID %1%\n")
				           % urid->body);
			}
		} else {
			out = _forge.alloc(in->size, in->type, LV2_ATOM_BODY_CONST(in));
		}
	}
}

void
AtomReader::get_props(const LV2_Atom_Object*       obj,
                      Ingen::Resource::Properties& props)
{
	if (obj->body.otype) {
		props.insert(
			std::make_pair(_uris.rdf_type,
			               _forge.alloc_uri(_map.unmap_uri(obj->body.otype))));
	}
	LV2_ATOM_OBJECT_FOREACH(obj, p) {
		Raul::Atom val;
		get_atom(&p->value, val);
		props.insert(std::make_pair(_map.unmap_uri(p->key), val));
	}
}

const char*
AtomReader::atom_to_uri(const LV2_Atom* atom)
{
	if (atom && atom->type == _uris.atom_URI) {
		return (const char*)LV2_ATOM_BODY_CONST(atom);
	} else if (atom && atom->type == _uris.atom_URID) {
		return _map.unmap_uri(((const LV2_Atom_URID*)atom)->body);
	} else {
		return NULL;
	}
}

boost::optional<Raul::Path>
AtomReader::atom_to_path(const LV2_Atom* atom)
{
	const char* uri_str = atom_to_uri(atom);
	if (uri_str && Raul::URI::is_valid(uri_str) &&
	    Node::uri_is_path(Raul::URI(uri_str))) {
		return Node::uri_to_path(Raul::URI(uri_str));
	}
	return boost::optional<Raul::Path>();
}

bool
AtomReader::is_message(URIs& uris, const LV2_Atom* msg)
{
	if (msg->type != uris.atom_Blank && msg->type != uris.atom_Resource) {
		return false;
	}

	const LV2_Atom_Object* obj = (const LV2_Atom_Object*)msg;
	return (obj->body.otype == uris.patch_Get ||
	        obj->body.otype == uris.patch_Delete ||
	        obj->body.otype == uris.patch_Put ||
	        obj->body.otype == uris.patch_Set ||
	        obj->body.otype == uris.patch_Patch ||
	        obj->body.otype == uris.patch_Move ||
{'fraction_non_alphanumeric': 0.1380268642890227, 'fraction_numerical': 0.007874015748031496, 'mean_word_length': 3.2423051735428947, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3261499', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2387, 'n_words': 534}