35 #define ERR_CF LOG_STREAM(err, log_config)
36 #define DBG_CF LOG_STREAM(debug, log_config)
39 #define ERR_WML LOG_STREAM(err, log_wml)
44 template<
typename Map,
typename Key>
45 typename Map::mapped_type& map_get(Map& map, Key&& key)
47 auto res = map.lower_bound(key);
49 if(res == map.end() || key != res->first) {
50 res = map.emplace_hint(res, std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>());
57 template<
typename Map,
typename Key>
58 int map_erase_key(Map& map, Key&& key)
60 auto i = map.find(key);
82 : values_(cfg.values_)
114 : values_(std::move(cfg.values_))
115 , children_(std::move(cfg.children_))
116 , ordered_children(std::move(cfg.ordered_children))
132 }
else if(name ==
"_") {
136 return std::all_of(name.begin(), name.end(), [](
const char&
c)
144 return (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') ||
145 (c >=
'0' && c <=
'9') || (c ==
'_');
180 cfg.clear_all_children();
184 for(
const any_child value : cfg.all_children_range()) {
185 add_child(value.key, std::move(value.cfg));
187 cfg.clear_all_children();
218 values_ = std::move(cfg.values_);
226 cfg.clear_attributes();
248 merged_children.
append(std::move(cfg));
252 add_child(key, std::move(merged_children));
261 typedef std::map<std::string, config> config_map;
262 config_map merged_children_map;
264 merged_children_map[cfg[
attribute]].append(std::move(cfg));
268 for(
const config_map::value_type&
i : merged_children_map) {
287 child_map::const_iterator
i =
children_.find(key);
299 child_map::const_iterator
i =
children_.find(key);
301 return i->second.size();
319 child_map::const_iterator
i =
children_.find(key);
324 template<
class Tchildren>
328 auto i = children.find(key);
329 if(
i == children.end()) {
330 DBG_CF <<
"The config object has no child named ‘" << key <<
"’.";
331 return utils::nullopt;
335 n =
static_cast<int>(
i->second.size()) +
n;
339 return *
i->second.at(
n);
340 }
catch(
const std::out_of_range&) {
341 DBG_CF <<
"The config object has only ‘" <<
i->second.size() <<
"’ children named ‘" << key
342 <<
"’; request for the index ‘" <<
n <<
"’ cannot be honored.";
343 return utils::nullopt;
351 if(
auto res = get_child_impl(
children_, key, 0)) {
354 throw error(
"Mandatory WML child ‘[" + std::string(key) +
"]’ missing in ‘" + parent +
"’. Please report this bug.");
360 if(
auto res = get_child_impl(
children_, key, 0)) {
363 throw error(
"Mandatory WML child ‘[" + std::string(key) +
"]’ missing in ‘" + parent +
"’. Please report this bug.");
369 if(
auto res = get_child_impl(
children_, key,
n)) {
372 throw error(
"Child [" + std::string(key) +
"] at index " + std::to_string(
n) +
" not found");
378 if(
auto res = get_child_impl(
children_, key,
n)) {
381 throw error(
"Child [" + std::string(key) +
"] at index " + std::to_string(
n) +
" not found");
397 static const config empty_cfg;
398 child_map::const_iterator
i =
children_.find(key);
400 return *
i->second.front();
408 child_map::const_iterator
i =
children_.find(key);
410 return *
i->second.front();
419 const std::string what =
formatter() <<
"[" << in_tag <<
"][" << old_key <<
"]";
424 return utils::nullopt;
433 const std::string what =
formatter() <<
"[" << in_tag <<
"][" << old_key <<
"]";
444 v.emplace_back(
new config());
452 v.emplace_back(
new config(val));
461 v.emplace_back(
new config(std::move(val)));
470 if(
index > v.size()) {
471 throw error(
"illegal index to add child at");
476 bool inserted =
false;
482 if(ord->pos != value.
pos)
484 if(!inserted && ord->index ==
index) {
487 }
else if(ord->index >=
index) {
502 const size_t npos =
static_cast<size_t>(-1);
523 auto next = std::find_if(pos_it, end,[&](
const child_pos&
p){
return p.pos->first == key; });
534 const auto index = next->index;
537 for(
auto ord = next; ord != end; ++ord) {
551 struct remove_ordered
560 return pos.
pos == iter_;
584 if(i_src ==
src.children_.end()) {
588 src.ordered_children.erase(
589 std::remove_if(
src.ordered_children.begin(),
src.ordered_children.end(), remove_ordered(i_src)),
590 src.ordered_children.end());
595 const auto before =
dst.
size();
596 dst.insert(
dst.end(), std::make_move_iterator(i_src->second.begin()), std::make_move_iterator(i_src->second.end()));
597 src.children_.erase(i_src);
600 for(std::size_t j = before; j <
dst.
size(); ++j) {
609 for(std::pair<const std::string, child_list>&
p :
children_) {
610 for(
auto& cfg :
p.second) {
611 cfg->recursive_clear_value(key);
620 std::size_t found = 0;
628 }
else if(
p.index >
index) {
634 pos->second.erase(pos->second.begin() +
index);
649 ERR_CF <<
"Error: attempting to delete non-existing child: " << key <<
"[" <<
index <<
"]";
663 const auto predicate = [
p](
const std::unique_ptr<config>& child)
668 auto child_it = std::find_if(pos->second.begin(), pos->second.end(), predicate);
669 while(child_it != pos->second.end()) {
670 const auto index = std::distance(pos->second.begin(), child_it);
672 child_it = std::find_if(pos->second.begin() +
index, pos->second.end(), predicate);
678 const attribute_map::const_iterator
i =
values_.find(key);
684 return empty_attribute;
689 attribute_map::const_iterator
i =
values_.find(key);
690 return i !=
values_.end() ? &
i->second :
nullptr;
701 auto res =
values_.lower_bound(key);
703 if(res ==
values_.end() || key != res->first) {
704 res =
values_.emplace_hint(res, std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>());
713 const std::string what =
formatter() <<
"[" << in_tag <<
"]" << old_key <<
"=";
714 const std::string
msg =
formatter() <<
"Use " << key <<
"= instead. " << message;
718 attribute_map::const_iterator
i =
values_.find(key);
729 return empty_attribute;
735 const std::string what =
formatter() <<
"[" << in_tag <<
"]" << old_key <<
"=";
741 return empty_attribute;
746 assert(
this != &cfg);
748 std::string key = v.first;
749 if(key.substr(0, 7) ==
"add_to_") {
750 std::string add_to = key.substr(7);
751 values_[add_to] =
values_[add_to].to_double() + v.second.to_double();
752 }
else if(key.substr(0, 10) ==
"concat_to_") {
753 std::string concat_to = key.substr(10);
756 values_[concat_to] =
values_[concat_to].t_str() + v.second.t_str();
768 while(range.begin() != range.end() && range.begin()->second.blank()) {
780 while(range.begin() != range.end() && range.begin()->second.blank()) {
791 DBG_CF <<
"Key ‘" << name <<
"’ value ‘" << value <<
"’ pair not found as child of key ‘" << key <<
"’.";
794 return utils::nullopt;
798 [&](
const std::unique_ptr<config>& pcfg) {
799 const config& cfg = *pcfg;
800 return cfg[name] == value;
804 if(j !=
i->second.end()) {
808 DBG_CF <<
"Key ‘" << name <<
"’ value ‘" << value <<
"’ pair not found as child of key ‘" << key <<
"’.";
810 return utils::nullopt;
819 throw error(
"Cannot find child [" + std::string(key) +
"] with " + name +
"=" + value);
828 throw error(
"Cannot find child [" + std::string(key) +
"] with " + name +
"=" + value);
864 return any_child(&i_->pos->first, i_->pos->second[i_->index].get());
922 config* inserts =
nullptr;
925 if(v.second.blank()) {
929 const attribute_map::const_iterator j =
c.values_.find(v.first);
930 if(j ==
c.values_.end() || (v.second != j->second && !v.second.blank())) {
931 if(inserts ==
nullptr) {
935 (*inserts)[v.first] = v.second;
939 config* deletes =
nullptr;
941 for(
const auto& v :
c.values_) {
942 if(v.second.blank()) {
946 const attribute_map::const_iterator itor =
values_.find(v.first);
947 if(itor ==
values_.end() || itor->second.blank()) {
948 if(deletes ==
nullptr) {
952 (*deletes)[v.first] =
"x";
956 std::vector<std::string> entities;
959 entities.push_back(child.first);
962 for(
const auto& child :
c.children_) {
964 entities.push_back(child.first);
968 for(
const std::string& entity : entities) {
969 const child_map::const_iterator itor_a =
children_.find(entity);
970 const child_map::const_iterator itor_b =
c.children_.find(entity);
976 const child_list&
b = itor_b !=
c.children_.end() ? itor_b->second : dummy;
978 std::size_t ndeletes = 0;
979 std::size_t
ai = 0, bi = 0;
980 while(
ai != a.size() || bi !=
b.size()) {
982 if(
ai < a.size() && bi <
b.size() && *a[
ai] == *
b[bi]) {
988 std::stringstream buf;
992 if(
b.size() - bi > a.size() -
ai) {
994 buf << bi - ndeletes;
995 new_delete.
values_[
"index"] = buf.str();
1004 else if(
b.size() - bi < a.size() -
ai) {
1007 new_insert.
values_[
"index"] = buf.str();
1018 new_change.
values_[
"index"] = buf.str();
1036 for(
const attribute& v : inserts->attribute_range()) {
1042 for(
const attribute& v : deletes->attribute_range()) {
1048 const std::size_t
index = lexical_cast<std::size_t>(
i[
"index"].str());
1050 if(
item.key.empty()) {
1056 throw error(
"error in diff: could not find element '" +
item.key +
"'");
1059 itor->second[
index]->apply_diff(
item.cfg, track);
1064 const auto index = lexical_cast<std::size_t>(
i[
"index"].str());
1074 const auto index = lexical_cast<std::size_t>(
i[
"index"].str());
1081 throw error(
"error in diff: could not find element '" +
item.key +
"'");
1094 const auto index = lexical_cast<std::size_t>(
i[
"index"].str());
1101 const std::size_t
index = lexical_cast<std::size_t>(
i[
"index"].str());
1103 if(
item.key.empty()) {
1109 throw error(
"error in diff: could not find element '" +
item.key +
"'");
1112 itor->second[
index]->clear_diff_track(
item.cfg);
1116 for(std::pair<const std::string, child_list>&
p :
children_) {
1117 for(
auto& cfg :
p.second) {
1128 std::vector<child_pos> to_remove;
1129 std::map<std::string, unsigned> visitations;
1137 const std::string& tag =
i->pos->first;
1138 const child_map::const_iterator j =
c.children_.find(tag);
1140 if(j !=
c.children_.end()) {
1141 unsigned& visits = visitations[tag];
1143 if(visits < j->second.size()) {
1145 const config& merge_child = *j->second[visits++];
1147 if(merge_child[
"__remove"].to_bool()) {
1148 to_remove.push_back(*
i);
1157 for(
const auto& pair :
c.children_) {
1158 const std::string& tag = pair.first;
1159 unsigned& visits = visitations[tag];
1160 while(visits < pair.second.size()) {
1166 std::map<std::string, std::size_t> removals;
1168 const std::string& tag = pos.pos->first;
1169 auto& removes = removals[tag];
1203 if(
i.first.compare(0, 8,
"glob_on_") == 0) {
1211 if(!v || *v !=
i.second) {
1219 if(
i.key ==
"not") {
1220 result = result && !
matches(
i.cfg);
1222 }
else if(
i.key ==
"and") {
1225 }
else if(
i.key ==
"or") {
1232 if(j.matches(
i.cfg)) {
1238 result = result && found;
1246 std::ostringstream outstream;
1248 return outstream.str();
1257 if(val.second.blank()) {
1261 for(
int j = 0; j <
i - 1; j++) {
1265 outstream << val.first <<
" = " << val.second <<
'\n';
1269 for(
int j = 0; j <
i - 1; ++j) {
1273 outstream <<
"[" << child.key <<
"]\n";
1274 outstream << child.cfg;
1276 for(
int j = 0; j <
i - 1; ++j) {
1280 outstream <<
"[/" << child.key <<
"]\n";
1289 static const unsigned int hash_length = 128;
1290 static const char hash_string[] =
"+-,.<>0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1294 for(
i = 0;
i != hash_length; ++
i) {
1302 if(val.second.blank()) {
1306 for(
char c : val.first) {
1308 if(++
i == hash_length) {
1313 std::string base_str = val.second.t_str().base_str();
1314 for(
const char c : base_str) {
1316 if(++
i == hash_length) {
1323 std::string child_hash = ch.cfg.hash();
1324 for(
char c : child_hash) {
1327 if(
i == hash_length) {
1333 for(
i = 0;
i != hash_length; ++
i) {
1356 return valid_tag(pair.first) &&
1357 std::all_of(pair.second.begin(), pair.second.end(),
1358 [](const auto& c) { return c->validate_wml(); });
1373 for(; !x.empty() && !y.empty(); x.pop_front(), y.pop_front()) {
1374 if(x.front().key != y.front().key || x.front().cfg != y.front().cfg) {
1379 return x.empty() && y.empty();
static unsigned int hash_str(const std::string &str)
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
bool blank() const
Tests for an attribute that was never set.
A config object defines a single node in a WML file, with access to child nodes.
const attribute_value & get_old_attribute(config_key_type key, const std::string &old_key, const std::string &in_tag, const std::string &message="") const
Function to handle backward compatibility Get the value of key and if missing try old_key and log a d...
void append(const config &cfg)
Append data from another config object to this one.
const attribute_value & get_or(const config_key_type key, const config_key_type default_key) const
Chooses a value.
all_children_iterator erase(const all_children_iterator &i)
const_all_children_iterator ordered_begin() const
boost::iterator_range< const_all_children_iterator > const_all_children_itors
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
std::size_t attribute_count() const
Count the number of non-blank attributes.
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
attribute_value & operator[](config_key_type key)
Returns a reference to the attribute with the given key.
void recursive_clear_value(config_key_type key)
bool matches(const config &filter) const
void remove_child(config_key_type key, std::size_t index)
const_attr_itors attribute_range() const
std::size_t child_count(config_key_type key) const
attribute_map values_
All the attributes of this node.
bool validate_wml() const
Returns true if this object represents valid WML, i.e.
optional_config_impl< const config > get_deprecated_child(config_key_type old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated child and log a deprecation message.
void merge_attributes(const config &)
const_all_children_iterator ordered_end() const
void merge_children(const std::string &key)
All children with the given key will be merged into the first element with that key.
boost::iterator_range< child_iterator > child_itors
config & add_child_at(config_key_type key, const config &val, std::size_t index)
config & find_mandatory_child(config_key_type key, const std::string &name, const std::string &value)
void append_children_by_move(config &cfg, const std::string &key)
Moves children with the given name from the given config to this one.
const attribute_value & get_deprecated_attribute(config_key_type old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated attribute without a direct substitute, and log a deprecation message.
void merge_children_by_attribute(const std::string &key, const std::string &attribute)
All children with the given key and with equal values of the specified attribute will be merged into ...
std::vector< std::unique_ptr< config > > child_list
optional_config_impl< config > find_child(config_key_type key, const std::string &name, const std::string &value)
Returns the first child of tag key with a name attribute containing value.
static bool valid_tag(config_key_type name)
boost::iterator_range< attribute_iterator > attr_itors
const_all_children_iterator ordered_cbegin() const
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
bool has_attribute(config_key_type key) const
void merge_with(const config &c)
Merge config 'c' into this config, overwriting this config's values.
void clear_children_impl(config_key_type key)
const_child_itors get_deprecated_child_range(config_key_type old_key, const std::string &in_tag, DEP_LEVEL level, const std::string &message) const
Get a deprecated child range and log a deprecation message.
void clear_all_children()
void inherit_from(const config &c)
Merge config 'c' into this config, preserving this config's values.
const_all_children_itors all_children_range() const
In-order iteration over all children.
static const char * diff_track_attribute
The name of the attribute used for tracking diff changes.
boost::iterator_range< all_children_iterator > all_children_itors
child_itors child_range(config_key_type key)
void append_attributes(const config &cfg)
Adds attributes from cfg.
config & child_or_add(config_key_type key)
Returns a reference to the first child with the given key.
boost::iterator_range< const_attribute_iterator > const_attr_itors
void apply_diff(const config &diff, bool track=false)
A function to apply a diff config onto this config object.
std::size_t all_children_count() const
child_map children_
A list of all children of this node.
const_all_children_iterator ordered_cend() const
config & add_child_at_total(config_key_type key, const config &val, std::size_t pos)
void remove_attribute(config_key_type key)
static bool valid_attribute(config_key_type name)
config get_diff(const config &c) const
A function to get the differences between this object, and 'c', as another config object.
std::string debug() const
attribute_map::value_type attribute
void inherit_attributes(const config &c)
Merge the attributes of config 'c' into this config, preserving this config's values.
std::size_t find_total_first_of(config_key_type key, std::size_t start=0)
std::vector< child_pos > ordered_children
void remove_children(config_key_type key, std::function< bool(const config &)> p=[](config){return true;})
Removes all children with tag key for which p returns true.
void clear_diff_track(const config &diff)
Clear any tracking info from a previous apply_diff call with tracking.
boost::iterator_range< const_child_iterator > const_child_itors
void append_children(const config &cfg)
Adds children from cfg.
void splice_children(config &src, const std::string &key)
Moves all the children with tag key from src to this.
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
friend bool operator==(const config &a, const config &b)
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
config & operator=(const config &)
config & add_child(config_key_type key)
std::ostream & operator<<(std::ostream &outstream, const config &cfg)
static lg::log_domain log_wml("wml")
static lg::log_domain log_config("config")
Definitions for the interface to Wesnoth Markup Language (WML).
std::string_view config_key_type
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
DEP_LEVEL
See https://wiki.wesnoth.org/CompatibilityStandards for more info.
Interfaces for manipulating version numbers of engine, add-ons, etc.
New lexcical_cast header.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
std::pair< std::string, unsigned > item
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), '+' as one or more characters,...
std::string::const_iterator iterator
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
std::vector< child_pos >::iterator Itor
reference operator*() const
const child_map::key_type & key
reference operator*() const
constexpr point size() const
static map_location::DIRECTION n