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-12-22 22:20:44 +01:00
|
|
|
#include "../config.h"
|
|
|
|
|
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"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
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"
|
2010-06-03 20:09:00 +02:00
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "option_visit.h"
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
#include <boost/range/adaptor/map.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
2012-11-13 15:07:39 +01:00
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
namespace {
|
|
|
|
/// @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.
|
|
|
|
void put_option(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
|
|
|
|
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());
|
|
|
|
obj[path] = value;
|
|
|
|
}
|
2012-11-13 15:07:39 +01:00
|
|
|
else
|
|
|
|
put_option(obj[path.substr(0, pos)], path.substr(pos + 1), value);
|
2011-12-22 22:12:25 +01: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;
|
2012-09-25 03:15:20 +02:00
|
|
|
for (T const& elem : value) {
|
2011-12-26 23:20:49 +01:00
|
|
|
array.push_back(json::Object());
|
2012-09-25 03:15:20 +02:00
|
|
|
static_cast<json::Object&>(array.back())[element_key] = (json::UnknownElement)elem;
|
2011-12-22 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
put_option(obj, path, array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
Options::Options(agi::fs::path const& file, const std::string& 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";
|
2010-05-21 03:13:36 +02:00
|
|
|
std::istringstream stream(default_config);
|
|
|
|
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();
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
for (auto option_value : values | boost::adaptors::map_values)
|
|
|
|
delete option_value;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Options::ConfigNext(std::istream& stream) {
|
|
|
|
LoadConfig(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Options::ConfigUser() {
|
2010-06-13 03:29:32 +02:00
|
|
|
try {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::unique_ptr<std::istream> stream(io::Open(config_file));
|
|
|
|
LoadConfig(*stream, true);
|
|
|
|
}
|
|
|
|
catch (fs::FileNotFound const&) {
|
2010-06-13 03:29:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/// @todo Handle other errors such as parsing and notifying the user.
|
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;
|
2010-05-23 08:58:11 +02:00
|
|
|
} catch (json::Exception& e) {
|
|
|
|
/// @todo Do something better here, maybe print the exact error
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_E("option/load") << "json::Exception: " << e.what();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 06:10:56 +02:00
|
|
|
ConfigVisitor config_visitor(values, "", ignore_errors, !ignore_errors);
|
2010-05-21 03:13:36 +02:00
|
|
|
config_root.Accept(config_visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionValue* Options::Get(const std::string &name) {
|
2012-11-13 15:07:39 +01:00
|
|
|
auto index = values.find(name);
|
|
|
|
if (index != values.end())
|
2010-05-21 03:13:36 +02:00
|
|
|
return index->second;
|
|
|
|
|
2011-07-26 21:51:38 +02:00
|
|
|
LOG_E("option/get") << "agi::Options::Get Option not found: (" << name << ")";
|
2011-07-26 21:51:47 +02:00
|
|
|
throw OptionErrorNotFound("Option value not found: " + 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) {
|
|
|
|
switch (ov.second->GetType()) {
|
2011-07-15 06:04:42 +02:00
|
|
|
case OptionValue::Type_String:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_option(obj_out, ov.first, ov.second->GetString());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Int:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_option(obj_out, ov.first, ov.second->GetInt());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Double:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_option(obj_out, ov.first, ov.second->GetDouble());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
case OptionValue::Type_Color:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_option(obj_out, ov.first, ov.second->GetColor().GetRgbFormatted());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Bool:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_option(obj_out, ov.first, ov.second->GetBool());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_String:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_array(obj_out, ov.first, "string", ov.second->GetListString());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Int:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_array(obj_out, ov.first, "int", ov.second->GetListInt());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Double:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_array(obj_out, ov.first, "double", ov.second->GetListDouble());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
|
|
|
|
2012-10-26 16:09:14 +02:00
|
|
|
case OptionValue::Type_List_Color:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_array(obj_out, ov.first, "color", ov.second->GetListColor());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:12:25 +01:00
|
|
|
case OptionValue::Type_List_Bool:
|
2012-11-13 15:07:39 +01:00
|
|
|
put_array(obj_out, ov.first, "bool", ov.second->GetListBool());
|
2011-12-22 22:12:25 +01:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 02:21:03 +01:00
|
|
|
json::Writer::Write(obj_out, io::Save(config_file).Get());
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
} // namespace agi
|