diff options
Diffstat (limited to 'src/squelch/automatizer.cc')
-rw-r--r-- | src/squelch/automatizer.cc | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/squelch/automatizer.cc b/src/squelch/automatizer.cc index c3c52b1b..b06367a8 100644 --- a/src/squelch/automatizer.cc +++ b/src/squelch/automatizer.cc @@ -12,20 +12,21 @@ namespace squelch { /** * Parse rules from JSON array */ -static std::vector< std::shared_ptr < Rule > > parse_rules(json_t *rules_j) +static std::vector< std::shared_ptr < Rule > > parse_rules(jsoncons::json const &rules_json) { std::vector< std::shared_ptr < Rule > > rules; - if (!json_is_array(rules_j)) + if (!rules_json.is_array()) { msg_format("Error 'rules' is not an array"); return rules; } - for (size_t i = 0; i < json_array_size(rules_j); i++) + auto rules_array = rules_json.array_value(); + + for (auto const &rule_value : rules_array) { - json_t *rule_j = json_array_get(rules_j, i); - auto rule = Rule::parse_rule(rule_j); + auto rule = Rule::parse_rule(rule_value); if (rule) { rules.push_back(rule); @@ -63,25 +64,25 @@ bool Automatizer::apply_rules(object_type *o_ptr, int item_idx) const return false; } -std::shared_ptr<json_t> Automatizer::to_json() const +jsoncons::json Automatizer::to_json() const { - auto rules_json = std::shared_ptr<json_t>(json_array(), &json_decref); + auto document = jsoncons::json::array(); for (auto rule : m_rules) { - json_array_append_new(rules_json.get(), rule->to_json()); + document.push_back(rule->to_json()); } - return rules_json; + return document; } -void Automatizer::load_json(json_t *json) +void Automatizer::load_json(jsoncons::json const &document) { // Go through all the found rules - auto rules = parse_rules(json); + auto rules = parse_rules(document); // Load rule - for (auto rule : rules) + for (auto rule: rules) { append_rule(rule); } @@ -204,13 +205,15 @@ void Automatizer::add_new_condition(std::function<std::shared_ptr<Condition> ()> factory); } -void Automatizer::get_rule_names(std::vector<const char *> *names) const +std::vector<std::string> Automatizer::get_rule_names() const { - names->resize(m_rules.size()); - for (size_t i = 0; i < m_rules.size(); i++) + std::vector<std::string> names; + names.reserve(m_rules.size()); + for (auto const &rule: m_rules) { - (*names)[i] = m_rules.at(i)->get_name(); + names.push_back(rule->get_name()); } + return names; } int Automatizer::rules_count() const |