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-07-15 06:04:34 +02:00
|
|
|
#include "libaegisub/option.h"
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#ifndef LAGI_PRE
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <map>
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <memory>
|
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"
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
2011-10-11 02:06:34 +02:00
|
|
|
Options::Options(const std::string &file, const std::string& default_config, const OptionSetting setting)
|
|
|
|
: config_file(file), config_default(default_config), config_loaded(false), 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.
|
2010-05-21 03:13:36 +02:00
|
|
|
LoadConfig(*stream);
|
|
|
|
config_loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Options::LoadConfig(std::istream& stream) {
|
|
|
|
/// @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-09-28 21:44:34 +02:00
|
|
|
ConfigVisitor config_visitor(values, "");
|
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-10-18 00:00:28 +02:00
|
|
|
PutOption(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-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, (double)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-10-18 00:00:28 +02:00
|
|
|
PutOption(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-10-18 00:00:28 +02:00
|
|
|
PutOption(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-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, i->second->GetBool());
|
2011-07-15 06:04:42 +02:00
|
|
|
break;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
case OptionValue::Type_List_String: {
|
|
|
|
std::vector<std::string> array_string;
|
|
|
|
i->second->GetListString(array_string);
|
|
|
|
|
|
|
|
json::Array array;
|
|
|
|
|
|
|
|
for (std::vector<std::string>::const_iterator i_str = array_string.begin(); i_str != array_string.end(); ++i_str) {
|
|
|
|
json::Object obj;
|
2011-10-18 00:00:28 +02:00
|
|
|
obj["string"] = *i_str;
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(obj);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, array);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Int: {
|
|
|
|
std::vector<int64_t> array_int;
|
|
|
|
i->second->GetListInt(array_int);
|
|
|
|
|
|
|
|
json::Array array;
|
|
|
|
|
|
|
|
for (std::vector<int64_t>::const_iterator i_int = array_int.begin(); i_int != array_int.end(); ++i_int) {
|
|
|
|
json::Object obj;
|
2011-10-18 00:00:28 +02:00
|
|
|
obj["int"] = (double)*i_int;
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(obj);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2011-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, array);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Double: {
|
|
|
|
std::vector<double> array_double;
|
|
|
|
i->second->GetListDouble(array_double);
|
|
|
|
|
|
|
|
json::Array array;
|
|
|
|
|
|
|
|
for (std::vector<double>::const_iterator i_double = array_double.begin(); i_double != array_double.end(); ++i_double) {
|
|
|
|
json::Object obj;
|
2011-10-18 00:00:28 +02:00
|
|
|
obj["double"] = *i_double;
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(obj);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2011-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, array);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Colour: {
|
|
|
|
std::vector<Colour> array_colour;
|
|
|
|
i->second->GetListColour(array_colour);
|
|
|
|
|
|
|
|
json::Array array;
|
|
|
|
for (std::vector<Colour>::const_iterator i_colour = array_colour.begin(); i_colour != array_colour.end(); ++i_colour) {
|
|
|
|
json::Object obj;
|
2011-10-18 00:00:28 +02:00
|
|
|
obj["colour"] = *i_colour;
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(obj);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2011-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, array);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Bool: {
|
|
|
|
std::vector<bool> array_bool;
|
|
|
|
|
|
|
|
json::Array array;
|
|
|
|
|
|
|
|
i->second->GetListBool(array_bool);
|
|
|
|
for (std::vector<bool>::const_iterator i_bool = array_bool.begin(); i_bool != array_bool.end(); ++i_bool) {
|
|
|
|
json::Object obj;
|
2011-10-18 00:00:28 +02:00
|
|
|
obj["bool"] = *i_bool;
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(obj);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
2011-10-18 00:00:28 +02:00
|
|
|
PutOption(obj_out, i->first, array);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
io::Save file(config_file);
|
|
|
|
json::Writer::Write(obj_out, file.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Options::PutOption(json::Object &obj, const std::string &path, const json::UnknownElement &value) {
|
|
|
|
// Having a '/' denotes it is a leaf.
|
|
|
|
if (path.find('/') == std::string::npos) {
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Object::iterator pos = obj.find(path);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
// Fail if a key of the same name already exists.
|
2011-10-17 23:59:35 +02:00
|
|
|
if (pos != obj.end())
|
2010-05-21 03:13:36 +02:00
|
|
|
throw OptionErrorDuplicateKey("Key already exists");
|
|
|
|
|
2011-07-15 06:04:42 +02:00
|
|
|
obj[path] = value;
|
2010-05-21 03:13:36 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
std::string thispart = path.substr(0, path.find("/"));
|
|
|
|
std::string restpart = path.substr(path.find("/")+1, path.size());
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Object::iterator pos = obj.find(thispart);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
// New key, make object.
|
2011-10-17 23:59:35 +02:00
|
|
|
if (pos == obj.end())
|
2011-10-18 00:00:38 +02:00
|
|
|
pos = obj.insert(make_pair(thispart, json::Object())).first;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
PutOptionVisitor visitor(restpart, value);
|
2011-10-17 23:59:47 +02:00
|
|
|
pos->second.Accept(visitor);
|
2010-05-21 03:13:36 +02:00
|
|
|
return visitor.result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace agi
|