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
|
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
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-07-15 06:04:34 +02:00
|
|
|
#include <libaegisub/option_value.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "option_visit.h"
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
2011-07-15 06:04:34 +02:00
|
|
|
ConfigVisitor::ConfigVisitor(OptionValueMap &val, const std::string &member_name)
|
|
|
|
: values(val)
|
2011-10-11 02:06:34 +02:00
|
|
|
, name(member_name)
|
2011-07-15 06:04:34 +02:00
|
|
|
{
|
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) {
|
|
|
|
const json::Object::Member& member = *index;
|
|
|
|
const std::string &member_name = member.name;
|
|
|
|
const json::UnknownElement& element = member.element;
|
|
|
|
|
|
|
|
ConfigVisitor config_visitor(values, name + member_name);
|
|
|
|
|
|
|
|
element.Accept(config_visitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Array& array) {
|
2010-05-23 08:58:11 +02:00
|
|
|
OptionValueList *array_list = NULL;
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Array::const_iterator index(array.begin()), indexEnd(array.end());
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
for (; index != indexEnd; ++index) {
|
|
|
|
const json::Object& index_array = *index;
|
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Object::const_iterator index_object(index_array.begin()), index_objectEnd(index_array.end());
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
for (; index_object != index_objectEnd; ++index_object) {
|
|
|
|
const json::Object::Member& member = *index_object;
|
|
|
|
const std::string& member_name = member.name;
|
|
|
|
|
|
|
|
// This can only happen once since a list must always be of the same
|
|
|
|
// type, if we try inserting another type into it we want it to fail.
|
2011-10-11 02:06:34 +02:00
|
|
|
if (!array_list) {
|
2010-05-21 03:13:36 +02:00
|
|
|
if (member_name == "string") {
|
|
|
|
array_list = new OptionValueListString(name);
|
|
|
|
} else if (member_name == "int") {
|
|
|
|
array_list = new OptionValueListInt(name);
|
|
|
|
} else if (member_name == "double") {
|
|
|
|
array_list = new OptionValueListDouble(name);
|
|
|
|
} else if (member_name == "bool") {
|
|
|
|
array_list = new OptionValueListBool(name);
|
|
|
|
} else if (member_name == "colour") {
|
|
|
|
array_list = new OptionValueListColour(name);
|
|
|
|
} else {
|
|
|
|
throw OptionJsonValueArray("Array type not handled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (member_name == "string") {
|
|
|
|
std::string val = (json::String)member.element;
|
|
|
|
array_list->InsertString(val);
|
|
|
|
|
|
|
|
} else if (member_name == "int") {
|
|
|
|
int64_t val = (int64_t)(json::Number)member.element;
|
|
|
|
array_list->InsertInt(val);
|
|
|
|
|
|
|
|
} else if (member_name == "double") {
|
|
|
|
double val = (json::Number)member.element;
|
|
|
|
array_list->InsertDouble(val);
|
|
|
|
|
|
|
|
} else if (member_name == "bool") {
|
|
|
|
bool val = (json::Boolean)member.element;
|
|
|
|
array_list->InsertBool(val);
|
|
|
|
|
|
|
|
} else if (member_name == "colour") {
|
|
|
|
std::string val = (json::String)member.element;
|
|
|
|
Colour col(val);
|
|
|
|
array_list->InsertColour(col);
|
|
|
|
}
|
|
|
|
} catch (agi::Exception&) {
|
2010-05-23 08:58:11 +02:00
|
|
|
delete array_list;
|
2010-05-21 03:13:36 +02:00
|
|
|
throw OptionJsonValueArray("Attempt to insert value into array of wrong type");
|
|
|
|
}
|
|
|
|
} // for index_object
|
|
|
|
} // for index
|
2010-05-23 08:58:11 +02:00
|
|
|
|
|
|
|
if (array_list) AddOptionValue(array_list);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Number& number) {
|
|
|
|
double val = number.Value();
|
|
|
|
|
|
|
|
if (int64_t(val) == ceil(val)) {
|
|
|
|
OptionValue *opt = new OptionValueInt(name, int64_t(val));
|
|
|
|
AddOptionValue(opt);
|
|
|
|
} else {
|
|
|
|
OptionValue *opt = new OptionValueDouble(name, val);
|
|
|
|
AddOptionValue(opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::String& string) {
|
|
|
|
OptionValue *opt;
|
|
|
|
if (string.Value().find("rgb(") == 0) {
|
|
|
|
opt = new OptionValueColour(name, string.Value());
|
|
|
|
} else {
|
|
|
|
opt = new OptionValueString(name, string.Value());
|
|
|
|
}
|
|
|
|
AddOptionValue(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Boolean& boolean) {
|
|
|
|
OptionValue *opt = new OptionValueBool(name, boolean.Value());
|
|
|
|
AddOptionValue(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::Visit(const json::Null& null) {
|
|
|
|
throw OptionJsonValueNull("Attempt to read null value");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigVisitor::AddOptionValue(OptionValue* opt) {
|
|
|
|
OptionValue *opt_cur;
|
|
|
|
|
|
|
|
OptionValueMap::iterator index;
|
|
|
|
|
2011-10-11 02:06:34 +02:00
|
|
|
if ((index = values.find(name)) != values.end()) {
|
2010-05-21 03:13:36 +02:00
|
|
|
opt_cur = index->second;
|
|
|
|
} else {
|
2011-10-11 02:06:34 +02:00
|
|
|
values.insert(OptionValuePair(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);
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
int type = opt_cur->GetType();
|
|
|
|
switch (type) {
|
|
|
|
case OptionValue::Type_String:
|
|
|
|
opt_cur->SetString(opt->GetString());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_Int:
|
|
|
|
opt_cur->SetInt(opt->GetInt());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_Double:
|
|
|
|
opt_cur->SetDouble(opt->GetDouble());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_Colour:
|
|
|
|
opt_cur->SetColour(opt->GetColour());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_Bool:
|
|
|
|
opt_cur->SetBool(opt->GetBool());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OptionValue::Type_List_String: {
|
|
|
|
std::vector<std::string> array;
|
|
|
|
opt->GetListString(array);
|
|
|
|
opt_cur->SetListString(array);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Int: {
|
|
|
|
std::vector<int64_t> array;
|
|
|
|
opt->GetListInt(array);
|
|
|
|
opt_cur->SetListInt(array);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Double: {
|
|
|
|
std::vector<double> array;
|
|
|
|
opt->GetListDouble(array);
|
|
|
|
opt_cur->SetListDouble(array);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Colour: {
|
|
|
|
std::vector<Colour> array;
|
|
|
|
opt->GetListColour(array);
|
|
|
|
opt_cur->SetListColour(array);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OptionValue::Type_List_Bool: {
|
|
|
|
std::vector<bool> array;
|
|
|
|
opt->GetListBool(array);
|
|
|
|
opt_cur->SetListBool(array);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace agi
|