From 6511160a751ce7a5fe26fc2409e8380f95edc2c9 Mon Sep 17 00:00:00 2001 From: Patrick Parker Date: Sun, 31 Aug 2008 19:56:15 +0000 Subject: [PATCH] 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). --- src/config.cpp | 9 +++++++++ src/config.hpp | 7 ++++--- src/game_events.cpp | 16 ++++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index f03d6398d0c..e5acc24915e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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); diff --git a/src/config.hpp b/src/config.hpp index 6b2e39dcb02..72955a21693 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -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) diff --git a/src/game_events.cpp b/src/game_events.cpp index 10c18c95415..b8868eece96 100644 --- a/src/game_events.cpp +++ b/src/game_events.cpp @@ -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");