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.cpp
|
|
|
|
/// @brief Option interface.
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-07-15 06:04:34 +02:00
|
|
|
#include "libaegisub/option.h"
|
|
|
|
|
2010-06-11 04:25:07 +02:00
|
|
|
#include "libaegisub/cajun/reader.h"
|
|
|
|
#include "libaegisub/cajun/writer.h"
|
|
|
|
#include "libaegisub/cajun/elements.h"
|
2014-06-28 03:22:58 +02:00
|
|
|
#include "libaegisub/cajun/visitor.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-06-28 00:55:05 +02:00
|
|
|
#include "libaegisub/exception.h"
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "libaegisub/fs.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "libaegisub/io.h"
|
2010-06-03 20:09:00 +02:00
|
|
|
#include "libaegisub/log.h"
|
2011-07-15 06:04:34 +02:00
|
|
|
#include "libaegisub/option_value.h"
|
2014-06-28 03:22:58 +02:00
|
|
|
#include "libaegisub/make_unique.h"
|
2010-06-03 20:09:00 +02:00
|
|
|
|
2014-06-28 03:22:58 +02:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2014-03-21 21:41:33 +01:00
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
2012-11-13 15:07:39 +01:00
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
namespace {
|
2014-06-28 03:22:58 +02:00
|
|
|
using namespace agi;
|
|
|
|
|
|
|
|
DEFINE_EXCEPTION(OptionJsonValueError, Exception);
|
|
|
|
|
|
|
|
class ConfigVisitor final : public json::ConstVisitor {
|
|
|
|
std::vector<std::unique_ptr<OptionValue>> values;
|
|
|
|
|
|
|
|
/// Option name prefix to add to read names
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
/// Log errors rather than throwing them, for when loading user config files
|
|
|
|
/// (as a bad user config file shouldn't make the program fail to start)
|
|
|
|
bool ignore_errors;
|
|
|
|
|
|
|
|
void Error(const char *message) {
|
|
|
|
if (ignore_errors)
|
|
|
|
LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << message;
|
|
|
|
else
|
|
|
|
throw OptionJsonValueError(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class OptionValueType>
|
|
|
|
void ReadArray(json::Array const& src, std::string const& array_type) {
|
|
|
|
typename OptionValueType::value_type arr;
|
|
|
|
arr.reserve(src.size());
|
2014-12-28 21:45:55 +01:00
|
|
|
for (json::Object const& obj : src)
|
2014-06-28 03:22:58 +02:00
|
|
|
arr.push_back((typename OptionValueType::value_type::value_type)(obj.begin()->second));
|
|
|
|
|
|
|
|
values.push_back(agi::make_unique<OptionValueType>(name, std::move(arr)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const json::Object& object) {
|
|
|
|
auto old_name = name;
|
|
|
|
for (auto const& obj : object) {
|
2014-06-28 05:44:47 +02:00
|
|
|
name = old_name + (old_name.empty() ? "" : "/") + obj.first;
|
2014-06-28 03:22:58 +02:00
|
|
|
obj.second.Accept(*this);
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
2014-06-28 03:22:58 +02:00
|
|
|
name = old_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const json::Array& array) {
|
|
|
|
if (array.empty())
|
|
|
|
return Error("Cannot infer the type of an empty array");
|
|
|
|
|
|
|
|
json::Object const& front = array.front();
|
|
|
|
if (front.size() != 1)
|
|
|
|
return Error("Invalid array member");
|
|
|
|
|
|
|
|
auto const& array_type = front.begin()->first;
|
2014-12-28 21:45:55 +01:00
|
|
|
for (json::Object const& obj : array) {
|
|
|
|
if (obj.size() != 1)
|
|
|
|
return Error("Invalid array member");
|
|
|
|
if (obj.begin()->first != array_type)
|
|
|
|
return Error("Attempt to insert value into array of wrong type");
|
|
|
|
}
|
|
|
|
|
2014-06-28 03:22:58 +02:00
|
|
|
if (array_type == "string")
|
|
|
|
ReadArray<OptionValueListString>(array, array_type);
|
|
|
|
else if (array_type == "int")
|
|
|
|
ReadArray<OptionValueListInt>(array, array_type);
|
|
|
|
else if (array_type == "double")
|
|
|
|
ReadArray<OptionValueListDouble>(array, array_type);
|
|
|
|
else if (array_type == "bool")
|
|
|
|
ReadArray<OptionValueListBool>(array, array_type);
|
|
|
|
else if (array_type == "color")
|
|
|
|
ReadArray<OptionValueListColor>(array, array_type);
|
2012-11-13 15:07:39 +01:00
|
|
|
else
|
2014-06-28 03:22:58 +02:00
|
|
|
Error("Array type not handled");
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
2014-06-28 04:38:53 +02:00
|
|
|
void Visit(int64_t number) {
|
2014-06-28 03:22:58 +02:00
|
|
|
values.push_back(agi::make_unique<OptionValueInt>(name, number));
|
|
|
|
}
|
|
|
|
|
2014-06-28 04:38:53 +02:00
|
|
|
void Visit(double number) {
|
2014-06-28 03:22:58 +02:00
|
|
|
values.push_back(agi::make_unique<OptionValueDouble>(name, number));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const json::String& string) {
|
|
|
|
size_t size = string.size();
|
|
|
|
if ((size == 4 && string[0] == '#') ||
|
|
|
|
(size == 7 && string[0] == '#') ||
|
|
|
|
(size >= 10 && boost::starts_with(string, "rgb(")) ||
|
|
|
|
((size == 9 || size == 10) && boost::starts_with(string, "&H")))
|
|
|
|
{
|
|
|
|
values.push_back(agi::make_unique<OptionValueColor>(name, string));
|
|
|
|
} else {
|
|
|
|
values.push_back(agi::make_unique<OptionValueString>(name, string));
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
2014-06-28 03:22:58 +02:00
|
|
|
}
|
|
|
|
|
2014-06-28 04:38:53 +02:00
|
|
|
void Visit(bool boolean) {
|
2014-06-28 03:22:58 +02:00
|
|
|
values.push_back(agi::make_unique<OptionValueBool>(name, boolean));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const json::Null& null) {
|
|
|
|
Error("Attempt to read null value");
|
|
|
|
}
|
2011-12-22 22:12:25 +01:00
|
|
|
|
2014-06-28 03:22:58 +02:00
|
|
|
public:
|
|
|
|
ConfigVisitor(bool ignore_errors) : ignore_errors(ignore_errors) { }
|
|
|
|
std::vector<std::unique_ptr<OptionValue>> Values() { return std::move(values); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief Write an option to a json object
|
|
|
|
/// @param[out] obj Parent object
|
|
|
|
/// @param[in] path Path option should be stored in.
|
|
|
|
/// @param[in] value Value to write.
|
2014-06-28 05:44:47 +02:00
|
|
|
void put_option(json::Object &obj, const std::string &path, json::UnknownElement value) {
|
2014-06-28 03:22:58 +02:00
|
|
|
std::string::size_type pos = path.find('/');
|
|
|
|
// Not having a '/' denotes it is a leaf.
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
assert(obj.find(path) == obj.end());
|
2014-06-28 05:44:47 +02:00
|
|
|
obj[path] = std::move(value);
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
2014-06-28 03:22:58 +02:00
|
|
|
else
|
2014-06-28 05:44:47 +02:00
|
|
|
put_option(obj[path.substr(0, pos)], path.substr(pos + 1), std::move(value));
|
2014-06-28 03:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void put_array(json::Object &obj, const std::string &path, const char *element_key, std::vector<T> const& value) {
|
|
|
|
json::Array array;
|
2014-12-28 21:45:55 +01:00
|
|
|
array.resize(value.size());
|
|
|
|
for (size_t i = 0, size = value.size(); i < size; ++i)
|
|
|
|
static_cast<json::Object&>(array[i])[element_key] = (json::UnknownElement)value[i];
|
2014-06-28 05:44:47 +02:00
|
|
|
put_option(obj, path, std::move(array));
|
2014-06-28 03:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct option_name_cmp {
|
|
|
|
bool operator()(std::unique_ptr<OptionValue> const& a, std::unique_ptr<OptionValue> const& b) const {
|
|
|
|
return a->GetName() < b->GetName();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(std::unique_ptr<OptionValue> const& a, std::string const& b) const {
|
|
|
|
return a->GetName() < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(std::unique_ptr<OptionValue> const& a, const char *b) const {
|
|
|
|
return a->GetName() < b;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
|
|
|
|
2014-03-21 22:02:49 +01:00
|
|
|
Options::Options(agi::fs::path const& file, std::pair<const char *, size_t> default_config, const OptionSetting setting)
|
2012-12-16 22:18:18 +01:00
|
|
|
: config_file(file)
|
|
|
|
, setting(setting)
|
|
|
|
{
|
2010-06-03 20:09:00 +02:00
|
|
|
LOG_D("agi/options") << "New Options object";
|
2014-03-21 22:02:49 +01:00
|
|
|
boost::interprocess::ibufferstream stream(default_config.first, default_config.second);
|
2010-05-21 03:13:36 +02:00
|
|
|
LoadConfig(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
Options::~Options() {
|
2012-11-13 15:07:39 +01:00
|
|
|
if ((setting & FLUSH_SKIP) != FLUSH_SKIP)
|
2011-01-04 05:24:40 +01:00
|
|
|
Flush();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Options::ConfigUser() {
|
2010-06-13 03:29:32 +02:00
|
|
|
try {
|
2014-06-28 03:22:58 +02:00
|
|
|
LoadConfig(*io::Open(config_file), true);
|
2013-01-04 16:01:50 +01:00
|
|
|
}
|
|
|
|
catch (fs::FileNotFound const&) {
|
2010-06-13 03:29:32 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
void Options::LoadConfig(std::istream& stream, bool ignore_errors) {
|
2010-05-21 03:13:36 +02:00
|
|
|
json::UnknownElement config_root;
|
|
|
|
|
|
|
|
try {
|
|
|
|
json::Reader::Read(config_root, stream);
|
|
|
|
} catch (json::Reader::ParseException& e) {
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_E("option/load") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1;
|
2014-06-28 03:22:58 +02:00
|
|
|
return;
|
2010-05-23 08:58:11 +02:00
|
|
|
} catch (json::Exception& e) {
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_E("option/load") << "json::Exception: " << e.what();
|
2014-06-28 03:22:58 +02:00
|
|
|
return;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-28 03:22:58 +02:00
|
|
|
ConfigVisitor config_visitor(ignore_errors);
|
2010-05-21 03:13:36 +02:00
|
|
|
config_root.Accept(config_visitor);
|
2014-06-28 03:22:58 +02:00
|
|
|
|
|
|
|
auto new_values = config_visitor.Values();
|
|
|
|
if (new_values.empty()) return;
|
|
|
|
|
|
|
|
sort(begin(new_values), end(new_values), option_name_cmp());
|
|
|
|
|
|
|
|
if (values.empty()) {
|
|
|
|
values = std::move(new_values);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto src_it = begin(new_values), src_end = end(new_values);
|
|
|
|
auto dst_it = begin(values), dst_end = end(values);
|
|
|
|
|
|
|
|
while (src_it != src_end && dst_it != dst_end) {
|
|
|
|
int cmp = (*src_it)->GetName().compare((*dst_it)->GetName());
|
|
|
|
if (cmp < 0) // Option doesn't exist in defaults so skip
|
|
|
|
++src_it;
|
|
|
|
else if (cmp > 0)
|
|
|
|
++dst_it;
|
|
|
|
else {
|
|
|
|
if (ignore_errors) {
|
|
|
|
try {
|
|
|
|
(*dst_it)->Set((*src_it).get());
|
|
|
|
}
|
|
|
|
catch (agi::InternalError const& e) {
|
|
|
|
LOG_E("option/load/config_visitor") << "Error loading option from user configuration: " << e.GetMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*dst_it = std::move(*src_it);
|
|
|
|
}
|
|
|
|
++src_it;
|
|
|
|
++dst_it;
|
|
|
|
}
|
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-28 03:22:58 +02:00
|
|
|
OptionValue *Options::Get(const char *name) {
|
|
|
|
auto index = lower_bound(begin(values), end(values), name, option_name_cmp());
|
|
|
|
if (index != end(values) && (*index)->GetName() == name)
|
|
|
|
return index->get();
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_E("option/get") << "agi::Options::Get Option not found: (" << name << ")";
|
2014-06-28 03:22:58 +02:00
|
|
|
throw agi::InternalError("Option value not found: " + std::string(name));
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2012-12-16 22:18:18 +01:00
|
|
|
void Options::Flush() const {
|
2010-05-21 03:13:36 +02:00
|
|
|
json::Object obj_out;
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
for (auto const& ov : values) {
|
2014-06-28 03:22:58 +02:00
|
|
|
switch (ov->GetType()) {
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::String:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_option(obj_out, ov->GetName(), ov->GetString());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::Int:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_option(obj_out, ov->GetName(), ov->GetInt());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::Double:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_option(obj_out, ov->GetName(), ov->GetDouble());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::Color:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_option(obj_out, ov->GetName(), ov->GetColor().GetRgbFormatted());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::Bool:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_option(obj_out, ov->GetName(), ov->GetBool());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::ListString:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_array(obj_out, ov->GetName(), "string", ov->GetListString());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::ListInt:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_array(obj_out, ov->GetName(), "int", ov->GetListInt());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::ListDouble:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_array(obj_out, ov->GetName(), "double", ov->GetListDouble());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::ListColor:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_array(obj_out, ov->GetName(), "color", ov->GetListColor());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-03-31 18:24:56 +02:00
|
|
|
case OptionType::ListBool:
|
2014-06-28 03:22:58 +02:00
|
|
|
put_array(obj_out, ov->GetName(), "bool", ov->GetListBool());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 04:38:53 +02:00
|
|
|
agi::JsonWriter::Write(obj_out, io::Save(config_file).Get());
|
2011-12-30 02:21:03 +01:00
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
} // namespace agi
|