The Battle for Wesnoth  1.19.3+dev
config_filters.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include <algorithm>
16 #include <set>
17 #include <vector>
18 
19 #include "utils/config_filters.hpp"
20 
21 #include "serialization/string_utils.hpp" // for utils::split
22 #include "utils/math.hpp" // for in_ranges
23 
24 bool utils::config_filters::bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def)
25 {
26  if(!filter.has_attribute(attribute)) {
27  return true;
28  }
29 
30  return filter[attribute].to_bool() == cfg[attribute].to_bool(def);
31 }
32 
34  const config& filter, const config& cfg, const std::string& attribute, const std::string& def)
35 {
36  if(!filter.has_attribute(attribute)) {
37  return true;
38  }
39 
40  const std::vector<std::string> filter_attribute = utils::split(filter[attribute]);
41  return (
42  std::find(filter_attribute.begin(), filter_attribute.end(), cfg[attribute].str(def)) != filter_attribute.end());
43 }
44 
45 bool utils::config_filters::set_includes_if_present(const config& filter, const config& cfg, const std::string& attribute)
46 {
47  if(!filter.has_attribute(attribute)) {
48  return true;
49  }
50 
51  if(!cfg.has_attribute(attribute)) {
52  return false;
53  }
54 
55  const std::set<std::string> filter_attribute = utils::split_set(filter[attribute].str());
56  const std::set<std::string> cfg_attribute = utils::split_set(cfg[attribute].str());
57  for(const std::string& fil_at : filter_attribute) {
58  if (cfg_attribute.count(fil_at) == 0){
59  return false;
60  }
61  }
62  return true;
63 }
64 
65 bool utils::config_filters::unsigned_matches_if_present(const config& filter, const config& cfg, const std::string& attribute)
66 {
67  if(!filter.has_attribute(attribute)) {
68  return true;
69  }
70  if(!cfg.has_attribute(attribute)) {
71  return false;
72  }
73 
74  return in_ranges<int>(cfg[attribute].to_int(0), utils::parse_ranges_unsigned(filter[attribute].str()));
75 }
76 
77 bool utils::config_filters::int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<int> def)
78 {
79  if(!filter.has_attribute(attribute)) {
80  return true;
81  }
82  if(!cfg.has_attribute(attribute) && !def) {
83  return false;
84  }
85 
86  //if filter attribute is "default" check if cfg attribute equals to def.
87  if(filter[attribute] == "default" && def){
88  return (cfg[attribute].to_int(*def) == *def);
89  }
90  int value_def = def ? (*def) : 0;
91  return in_ranges<int>(cfg[attribute].to_int(value_def), utils::parse_ranges_int(filter[attribute].str()));
92 }
93 
95  const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, utils::optional<int> def)
96 {
97  if(int_matches_if_present(filter, cfg, attribute, def)) {
98  return true;
99  }
100 
101  // Check if cfg[opposite].empty() and have optional def.
102  // If def don't exist return false.
103  if(!cfg.has_attribute(attribute)) {
104  if(!cfg.has_attribute(opposite) && !def) {
105  return false;
106  }
107  //if filter attribute is "default" check if cfg attribute equals to def.
108  if(filter[attribute] == "default" && def){
109  return (cfg[attribute].to_int(*def) == *def);
110  }
111  int value_def = def ? (*def) : 0;
112  return in_ranges<int>(-cfg[opposite].to_int(value_def), utils::parse_ranges_int(filter[attribute].str()));
113  }
114 
115  return false;
116 }
117 
118 bool utils::config_filters::double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, utils::optional<double> def)
119 {
120  if(!filter.has_attribute(attribute)) {
121  return true;
122  }
123  if(!cfg.has_attribute(attribute) && !def) {
124  return false;
125  }
126 
127  //if filter attribute is "default" check if cfg attribute equals to def.
128  if(filter[attribute] == "default" && def){
129  return (cfg[attribute].to_int(*def) == *def);
130  }
131  double value_def = def ? (*def) : 1;
132  return in_ranges<double>(cfg[attribute].to_double(value_def), utils::parse_ranges_real(filter[attribute].str()));
133 }
134 
135 bool utils::config_filters::bool_or_empty(const config& filter, const config& cfg, const std::string& attribute)
136 {
137  if(!filter.has_attribute(attribute)) {
138  return true;
139  }
140 
141  std::set<std::string> filter_attribute = utils::split_set(filter[attribute].str());
142  bool is_matches = false;
143  if(cfg.has_attribute(attribute)){
144  if(filter_attribute.count("yes") != 0 || filter_attribute.count("true") != 0){
145  is_matches = cfg[attribute].to_bool();
146  }
147  if(!is_matches && (filter_attribute.count("no") != 0 || filter_attribute.count("false") != 0)){
148  is_matches = !cfg[attribute].to_bool();
149  }
150  } else {
151  if(filter_attribute.count("none") != 0){
152  is_matches = true;
153  }
154  }
155  return is_matches;
156 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
bool has_attribute(config_key_type key) const
Definition: config.cpp:155
General math utility functions.
bool int_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, utils::optional< int > def=utils::nullopt)
bool set_includes_if_present(const config &filter, const config &cfg, const std::string &attribute)
filter[attribute] and cfg[attribute] are assumed to be comma-separated lists.
bool unsigned_matches_if_present(const config &filter, const config &cfg, const std::string &attribute)
Restricts filters to only looking for values that are zero or more.
bool double_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, utils::optional< double > def=utils::nullopt)
Checks whether the filter matches the value of cfg[attribute].
bool int_matches_if_present_or_negative(const config &filter, const config &cfg, const std::string &attribute, const std::string &opposite, utils::optional< int > def=utils::nullopt)
Supports filters using "add" and "sub" attributes, for example a filter add=1 matching a cfg containi...
bool string_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, const std::string &def)
bool bool_or_empty(const config &filter, const config &cfg, const std::string &attribute)
bool bool_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, bool def)
Checks whether the filter matches the value of cfg[attribute].
std::set< std::string > split_set(std::string_view s, char sep, const int flags)
std::vector< std::pair< int, int > > parse_ranges_int(const std::string &str)
Handles a comma-separated list of inputs to parse_range.
std::vector< std::pair< int, int > > parse_ranges_unsigned(const std::string &str)
Handles a comma-separated list of inputs to parse_range, in a context that does not expect negative v...
std::vector< std::pair< double, double > > parse_ranges_real(const std::string &str)
std::vector< std::string > split(const config_attribute_value &val)