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.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-05-21 03:13:36 +02:00
|
|
|
#ifndef LAGI_PRE
|
2011-12-22 22:12:25 +01:00
|
|
|
#include <cassert>
|
2010-05-21 03:13:36 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <memory>
|
2011-12-22 22:12:25 +01:00
|
|
|
#include <sstream>
|
2011-07-15 06:04:34 +02:00
|
|
|
#endif
|
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
|
|
|
|
2010-06-13 03:29:32 +02:00
|
|
|
#include "libaegisub/access.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"
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
put_option(
|
|
|
|
obj[path.substr(0, pos)],
|
|
|
|
path.substr(pos + 1),
|
|
|
|
value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (typename std::vector<T>::const_iterator it = value.begin(); it != value.end(); ++it) {
|
|
|
|
json::Object obj;
|
|
|
|
obj[element_key] = *it;
|
|
|
|
array.push_back(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
put_option(obj, path, array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
|
|
|
|
2011-10-11 02:06:34 +02:00
|
|
|
Options::Options(const std::string &file, const std::string& default_config, const OptionSetting setting)
|
2011-12-22 22:12:06 +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() {
|
2011-01-04 05:24:40 +01:00
|
|
|
if ((setting & FLUSH_SKIP) != FLUSH_SKIP) {
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
2010-05-23 08:58:11 +02:00
|
|
|
for (OptionValueMap::iterator i = values.begin(); i != values.end(); i++) {
|
|
|
|
delete i->second;
|
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Options::ConfigNext(std::istream& stream) {
|
|
|
|
LoadConfig(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Options::ConfigUser() {
|
2010-06-24 03:24:09 +02:00
|
|
|
std::auto_ptr<std::istream> stream;
|
2010-06-13 03:29:32 +02:00
|
|
|
|
|
|
|
try {
|
2010-06-24 03:24:09 +02:00
|
|
|
stream.reset(agi::io::Open(config_file));
|
2010-06-13 03:29:32 +02:00
|
|
|
} catch (const acs::AcsNotFound&) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @todo Handle other errors such as parsing and notifying the user.
|
2011-12-22 22:12:15 +01:00
|
|
|
LoadConfig(*stream, true);
|
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
|
|
|
/// @todo Store all previously loaded configs in an array for bug report purposes,
|
|
|
|
/// this is just a temp stub.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-12-22 22:12:15 +01:00
|
|
|
ConfigVisitor config_visitor(values, "", ignore_errors);
|
2010-05-21 03:13:36 +02:00
|
|
|
config_root.Accept(config_visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionValue* Options::Get(const std::string &name) {
|
|
|
|
OptionValueMap::iterator index;
|
|
|
|
|
2010-05-23 08:58:11 +02:00
|
|
|
if ((index = values.find(name)) != 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
|
|
|
}
|
|
|
|
|
|
|
|
void Options::Flush() {
|
|
|
|
json::Object obj_out;
|
|
|
|
|
|
|
|
for (OptionValueMap::const_iterator i = values.begin(); i != values.end(); ++i) {
|
2011-07-15 06:04:42 +02:00
|
|
|
switch (i->second->GetType()) {
|
|
|
|
case OptionValue::Type_String:
|
2011-12-22 22:12:25 +01:00
|
|
|
put_option(obj_out, i->first, i->second->GetString());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Int:
|
2011-12-22 22:12:25 +01:00
|
|
|
put_option(obj_out, i->first, i->second->GetInt());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Double:
|
2011-12-22 22:12:25 +01:00
|
|
|
put_option(obj_out, i->first, i->second->GetDouble());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-07-15 06:04:42 +02:00
|
|
|
case OptionValue::Type_Colour:
|
2011-12-22 22:12:25 +01:00
|
|
|
put_option(obj_out, i->first, i->second->GetColour());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_Bool:
|
2011-12-22 22:12:25 +01:00
|
|
|
put_option(obj_out, i->first, i->second->GetBool());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_String:
|
|
|
|
put_array(obj_out, i->first, "string", i->second->GetListString());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Int:
|
|
|
|
put_array(obj_out, i->first, "int", i->second->GetListInt());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Double:
|
|
|
|
put_array(obj_out, i->first, "double", i->second->GetListDouble());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Colour:
|
|
|
|
put_array(obj_out, i->first, "colour", i->second->GetListColour());
|
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:
|
|
|
|
put_array(obj_out, i->first, "bool", i->second->GetListBool());
|
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io::Save file(config_file);
|
|
|
|
json::Writer::Write(obj_out, file.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace agi
|