2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file option_visit.cpp
|
|
|
|
/// @brief Cajun JSON visitor to load config values.
|
|
|
|
/// @see option_visit.h
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-12-22 22:20:44 +01:00
|
|
|
#include "../config.h"
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
#include "option_visit.h"
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#ifndef LAGI_PRE
|
2011-12-22 22:12:25 +01:00
|
|
|
#include <cassert>
|
2011-07-27 00:25:21 +02:00
|
|
|
#include <cmath>
|
2010-05-21 03:13:36 +02:00
|
|
|
#endif
|
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
#include <libaegisub/color.h>
|
2011-12-22 22:12:15 +01:00
|
|
|
#include <libaegisub/log.h>
|
2011-07-15 06:04:34 +02:00
|
|
|
#include <libaegisub/option_value.h>
|
2012-04-11 06:10:56 +02:00
|
|
|
#include <libaegisub/scoped_ptr.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
2012-04-11 06:10:56 +02:00
|
|
|
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors, bool replace)
|
2011-07-15 06:04:34 +02:00
|
|
|
: values(val)
|
2011-10-11 02:06:34 +02:00
|
|
|
, name(member_name)
|
2011-12-22 22:12:15 +01:00
|
|
|
, ignore_errors(ignore_errors)
|
2012-04-11 06:10:56 +02:00
|
|
|
, replace(replace)
|
2011-07-15 06:04:34 +02:00
|
|
|
{
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
template<class ErrorType>
|
|
|
|
void ConfigVisitor::Error(const char *message) {
|
|
|
|
if (ignore_errors)
|
|
|
|
LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << message;
|
|
|
|
else
|
|
|
|
throw ErrorType(message);
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
void ConfigVisitor::Visit(const json::Object& object) {
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Object::const_iterator index(object.begin()), index_end(object.end());
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-11 02:06:34 +02:00
|
|
|
if (!name.empty())
|
|
|
|
name += "/";
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
for (; index != index_end; ++index) {
|
2012-04-11 06:10:56 +02:00
|
|
|
ConfigVisitor config_visitor(values, name + index->first, ignore_errors, replace);
|
2011-10-18 00:00:28 +02:00
|
|
|
index->second.Accept(config_visitor);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:42:40 +01:00
|
|
|
template<class OptionValueType, class ValueType>
|
2012-04-11 06:10:56 +02:00
|
|
|
OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const& array_type, void (OptionValueType::*)(const std::vector<ValueType>&)) {
|
2011-11-04 20:42:40 +01:00
|
|
|
std::vector<ValueType> arr;
|
|
|
|
arr.reserve(src.size());
|
|
|
|
|
|
|
|
for (json::Array::const_iterator it = src.begin(); it != src.end(); ++it) {
|
|
|
|
json::Object const& obj = *it;
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
if (obj.size() != 1) {
|
|
|
|
Error<OptionJsonValueArray>("Invalid array member");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (obj.begin()->first != array_type) {
|
|
|
|
Error<OptionJsonValueArray>("Attempt to insert value into array of wrong type");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-04 20:42:40 +01:00
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
arr.push_back(ValueType(obj.begin()->second));
|
2011-11-04 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
2012-04-11 06:10:56 +02:00
|
|
|
return new OptionValueType(name, arr);
|
2011-11-04 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
void ConfigVisitor::Visit(const json::Array& array) {
|
2011-12-22 22:12:15 +01:00
|
|
|
if (array.empty()) {
|
|
|
|
Error<OptionJsonValueArray>("Cannot infer the type of an empty array");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-04 20:42:40 +01:00
|
|
|
|
|
|
|
json::Object const& front = array.front();
|
2011-12-22 22:12:15 +01:00
|
|
|
if (front.size() != 1) {
|
|
|
|
Error<OptionJsonValueArray>("Invalid array member");
|
|
|
|
return;
|
|
|
|
}
|
2011-11-04 20:42:40 +01:00
|
|
|
|
|
|
|
const std::string& array_type = front.begin()->first;
|
|
|
|
|
|
|
|
if (array_type == "string")
|
2011-12-22 22:12:15 +01:00
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListString::SetListString));
|
2011-11-04 20:42:40 +01:00
|
|
|
else if (array_type == "int")
|
2011-12-22 22:12:15 +01:00
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListInt::SetListInt));
|
2011-11-04 20:42:40 +01:00
|
|
|
else if (array_type == "double")
|
2011-12-22 22:12:15 +01:00
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListDouble::SetListDouble));
|
2011-11-04 20:42:40 +01:00
|
|
|
else if (array_type == "bool")
|
2011-12-22 22:12:15 +01:00
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListBool::SetListBool));
|
2012-10-26 16:09:14 +02:00
|
|
|
else if (array_type == "color")
|
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListColor::SetListColor));
|
2011-11-04 20:42:40 +01:00
|
|
|
else
|
2011-12-22 22:12:15 +01:00
|
|
|
Error<OptionJsonValueArray>("Array type not handled");
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
void ConfigVisitor::Visit(const json::Integer& number) {
|
|
|
|
AddOptionValue(new OptionValueInt(name, number));
|
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
void ConfigVisitor::Visit(const json::Double& number) {
|
|
|
|
AddOptionValue(new OptionValueDouble(name, number));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::String& string) {
|
2012-10-26 16:09:14 +02:00
|
|
|
if (string.size() && (string.find("rgb(") == 0 || string[0] == '#' || string[0] == '&')) {
|
|
|
|
AddOptionValue(new OptionValueColor(name, string));
|
2010-05-21 03:13:36 +02:00
|
|
|
} else {
|
2011-10-18 00:00:28 +02:00
|
|
|
AddOptionValue(new OptionValueString(name, string));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Boolean& boolean) {
|
2011-10-18 00:00:28 +02:00
|
|
|
AddOptionValue(new OptionValueBool(name, boolean));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Null& null) {
|
2011-12-22 22:12:15 +01:00
|
|
|
Error<OptionJsonValueNull>("Attempt to read null value");
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::AddOptionValue(OptionValue* opt) {
|
2011-12-22 22:12:15 +01:00
|
|
|
if (!opt) {
|
|
|
|
assert(ignore_errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-16 18:47:06 +02:00
|
|
|
OptionValueMap::iterator it = values.find(name);
|
|
|
|
if (it == values.end())
|
2012-04-11 06:10:56 +02:00
|
|
|
values[name] = opt;
|
|
|
|
else if (replace) {
|
2012-10-16 18:47:06 +02:00
|
|
|
delete it->second;
|
|
|
|
it->second = opt;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2012-04-11 06:10:56 +02:00
|
|
|
else {
|
|
|
|
try {
|
|
|
|
// Ensure than opt is deleted at the end of this function even if the Set
|
|
|
|
// method throws
|
|
|
|
agi::scoped_ptr<OptionValue> auto_opt(opt);
|
|
|
|
values[name]->Set(opt);
|
|
|
|
}
|
|
|
|
catch (agi::OptionValueError const& e) {
|
|
|
|
if (ignore_errors)
|
|
|
|
LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << e.GetChainedMessage();
|
|
|
|
else
|
|
|
|
throw;
|
2011-12-22 22:12:15 +01:00
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace agi
|