mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-01 13:27:54 +00:00
now able to safely insert to an index at array.length...
...using [set_variables] mode=insert (before it would create empty child data before inserting).
This commit is contained in:
parent
7498fd2fc5
commit
6511160a75
@ -113,6 +113,15 @@ config::const_child_itors config::child_range(const std::string& key) const
|
||||
}
|
||||
}
|
||||
|
||||
size_t config::child_count(const std::string& key) const
|
||||
{
|
||||
child_map::const_iterator i = children.find(key);
|
||||
if(i != children.end()) {
|
||||
return i->second.size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const config::child_list& config::get_children(const std::string& key) const
|
||||
{
|
||||
const child_map::const_iterator i = children.find(key);
|
||||
|
@ -75,6 +75,7 @@ public:
|
||||
|
||||
child_itors child_range(const std::string& key);
|
||||
const_child_itors child_range(const std::string& key) const;
|
||||
size_t child_count(const std::string& key) const;
|
||||
|
||||
const child_list& get_children(const std::string& key) const;
|
||||
const child_map& all_children() const;
|
||||
@ -89,9 +90,9 @@ public:
|
||||
|
||||
const t_string& get_attribute(const std::string& key) const;
|
||||
bool has_attribute(const std::string& key) const {return values.find(key) != values.end();}
|
||||
|
||||
/**
|
||||
* This should only be used if there is no mapping of the key already,
|
||||
|
||||
/**
|
||||
* This should only be used if there is no mapping of the key already,
|
||||
* e.g. when creating a new config object. It does not replace an existing value.
|
||||
* @returns true when it added the key-value pair, false if it already existed
|
||||
* (and you probably wanted to use config[key] = value)
|
||||
|
@ -1313,8 +1313,20 @@ namespace {
|
||||
std::string mode = cfg["mode"]; // replace, append, merge, or insert
|
||||
if(mode == "extend") {
|
||||
mode = "append";
|
||||
} else if(mode != "append" && mode != "merge" && mode != "insert") {
|
||||
mode = "replace";
|
||||
} else if(mode != "append" && mode != "merge") {
|
||||
if(mode == "insert") {
|
||||
size_t child_count = dest.vars->child_count(dest.key);
|
||||
if(dest.index >= child_count) {
|
||||
while(dest.index >= ++child_count) {
|
||||
//inserting past the end requires empty data
|
||||
dest.vars->append(config(dest.key));
|
||||
}
|
||||
//inserting at the end is handled by an append
|
||||
mode = "append";
|
||||
}
|
||||
} else {
|
||||
mode = "replace";
|
||||
}
|
||||
}
|
||||
|
||||
const vconfig::child_list values = cfg.get_children("value");
|
||||
|
Loading…
x
Reference in New Issue
Block a user