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.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @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-23 08:58:11 +02:00
|
|
|
#include <memory>
|
2010-05-21 03:13:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <libaegisub/colour.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>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name, bool ignore_errors)
|
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)
|
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) {
|
2011-12-22 22:12:15 +01:00
|
|
|
ConfigVisitor config_visitor(values, name + index->first, ignore_errors);
|
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>
|
2011-12-22 22:12:15 +01:00
|
|
|
OptionValue *ConfigVisitor::ReadArray(json::Array const& src, std::string const& array_type, void (OptionValueType::*set_list)(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
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
arr.push_back(obj.begin()->second);
|
2011-11-04 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OptionValueType *ret = new OptionValueType(name);
|
|
|
|
(ret->*set_list)(arr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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));
|
2011-11-04 20:42:40 +01:00
|
|
|
else if (array_type == "colour")
|
2011-12-22 22:12:15 +01:00
|
|
|
AddOptionValue(ReadArray(array, array_type, &OptionValueListColour::SetListColour));
|
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) {
|
2011-10-17 23:59:59 +02:00
|
|
|
if (string.find("rgb(") == 0) {
|
2011-10-18 00:00:28 +02:00
|
|
|
AddOptionValue(new OptionValueColour(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;
|
|
|
|
}
|
|
|
|
|
2011-11-04 20:42:50 +01:00
|
|
|
if (!values.count(name)) {
|
|
|
|
values[name] = opt;
|
2010-05-21 03:13:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-23 08:58:11 +02:00
|
|
|
// Ensure than opt is deleted at the end of this function even if the Set
|
|
|
|
// method throws
|
|
|
|
std::auto_ptr<OptionValue> auto_opt(opt);
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
try {
|
|
|
|
switch (opt->GetType()) {
|
|
|
|
case OptionValue::Type_String:
|
|
|
|
values[name]->SetString(opt->GetString());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_Int:
|
|
|
|
values[name]->SetInt(opt->GetInt());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_Double:
|
|
|
|
values[name]->SetDouble(opt->GetDouble());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_Colour:
|
|
|
|
values[name]->SetColour(opt->GetColour());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_Bool:
|
|
|
|
values[name]->SetBool(opt->GetBool());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_List_String:
|
|
|
|
values[name]->SetListString(opt->GetListString());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_List_Int:
|
|
|
|
values[name]->SetListInt(opt->GetListInt());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_List_Double:
|
|
|
|
values[name]->SetListDouble(opt->GetListDouble());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_List_Colour:
|
|
|
|
values[name]->SetListColour(opt->GetListColour());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
case OptionValue::Type_List_Bool:
|
|
|
|
values[name]->SetListBool(opt->GetListBool());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (agi::OptionValueError const& e) {
|
|
|
|
if (ignore_errors)
|
|
|
|
LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << e.GetChainedMessage();
|
|
|
|
else
|
|
|
|
throw;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace agi
|