<commit_before>/* This file is part of Ingen.
 * Copyright (C) 2007 <NAME> <http://drobilla.net>
 * 
 * Ingen is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) 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 General Public License for details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility> // for pair, make_pair
#include <cassert>
#include <cstring>
#include <string>
#include <cstdlib>  // for atof
#include <cmath>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <raul/Path.hpp>
#include "interface/EngineInterface.hpp"
#include "PatchModel.hpp"
#include "NodeModel.hpp"
#include "ConnectionModel.hpp"
#include "PortModel.hpp"
#include "PresetModel.hpp"
#include "PluginModel.hpp"
#include "DeprecatedLoader.hpp"

using namespace std;

namespace Ingen {
namespace Client {

string
DeprecatedLoader::nameify_if_invalid(const string& name)
{
	if (Path::is_valid_name(name)) {
		return name;
	} else {
		const string new_name = Path::nameify(name);
		assert(Path::is_valid_name(new_name));
		if (new_name != name)
			cerr << "WARNING: Illegal name '" << name << "' converted to '"
				<< new_name << "'" << endl;
		return new_name;
	}
}


string
DeprecatedLoader::translate_load_path(const string& path)
{
	std::map<string,string>::iterator t = _load_path_translations.find(path);
	
	if (t != _load_path_translations.end()) {
		assert(Path::is_valid((*t).second));
		return (*t).second;
	} else {
		assert(Path::is_valid(path));
		return path;
	}
}


/** Add a piece of data to a Variables, translating from deprecated unqualified keys
 *
 * Adds a namespace prefix for known keys, and ignores the rest.
 */
void
DeprecatedLoader::add_variable(GraphObject::Variables& data, string old_key, string value)
{
	string key = "";
	if (old_key == "module-x")
		key = "ingenuity:canvas-x";
	else if (old_key == "module-y")
		key = "ingenuity:canvas-y";

	if (key != "") {
		// FIXME: should this overwrite existing values?
		if (data.find(key) == data.end()) {
			// Hack to make module-x and module-y set as floats
			char* c_val = strdup(value.c_str());
			char* endptr = NULL;
			float fval = strtof(c_val, &endptr);

			if (endptr != c_val && *endptr == '\0')
				data[key] = Atom(fval);
			else
				data[key] = Atom(value);
			
			free(c_val);<commit_msg>Fix locale problems in ingen, flowcanvas, and redlandmm (',' decimal point).<commit_after>/* This file is part of Ingen.
 * Copyright (C) 2007 <NAME> <http://drobilla.net>
 * 
 * Ingen is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) 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 General Public License for details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <utility> // for pair, make_pair
#include <cassert>
#include <cstring>
#include <string>
#include <cstdlib>  // for atof
#include <cmath>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <raul/Path.hpp>
#include "interface/EngineInterface.hpp"
#include "PatchModel.hpp"
#include "NodeModel.hpp"
#include "ConnectionModel.hpp"
#include "PortModel.hpp"
#include "PresetModel.hpp"
#include "PluginModel.hpp"
#include "DeprecatedLoader.hpp"

using namespace std;

namespace Ingen {
namespace Client {

string
DeprecatedLoader::nameify_if_invalid(const string& name)
{
	if (Path::is_valid_name(name)) {
		return name;
	} else {
		const string new_name = Path::nameify(name);
		assert(Path::is_valid_name(new_name));
		if (new_name != name)
			cerr << "WARNING: Illegal name '" << name << "' converted to '"
				<< new_name << "'" << endl;
		return new_name;
	}
}


string
DeprecatedLoader::translate_load_path(const string& path)
{
	std::map<string,string>::iterator t = _load_path_translations.find(path);
	
	if (t != _load_path_translations.end()) {
		assert(Path::is_valid((*t).second));
		return (*t).second;
	} else {
		assert(Path::is_valid(path));
		return path;
	}
}


/** Add a piece of data to a Variables, translating from deprecated unqualified keys
 *
 * Adds a namespace prefix for known keys, and ignores the rest.
 */
void
DeprecatedLoader::add_variable(GraphObject::Variables& data, string old_key, string value)
{
	string key = "";
	if (old_key == "module-x")
		key = "ingenuity:canvas-x";
	else if (old_key == "module-y")
		key = "ingenuity:canvas-y";

	if (key != "") {
		// FIXME: should this overwrite existing values?
		if (data.find(key) == data.end()) {
			// Hack to make module-x and module-y set as floats
			char* c_val = strdup(value.c_str());
			char* endptr = NULL;
	
			// FIXME: locale kludges
			char* locale = strdup(setlocale(LC_NUMERIC, NULL));
			
			float fval = strtof(c_val, &endptr);
			
			setlocale(LC_NUMERIC, locale);
			free(locale);

			if (endptr != c_val && *endptr == '\0')
				data[key] = Atom(fval);
			else
				data[key] = Atom(value);
			
			free(c_val);
{'fraction_non_alphanumeric': 0.1299783369438427, 'fraction_numerical': 0.0056657223796034, 'mean_word_length': 4.274165202108963, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1993016', 'n_tokens_mistral': 2100, 'n_tokens_neox': 1931, 'n_words': 634}