diff options
author | Nikias Bassen | 2019-01-21 02:22:47 +0100 |
---|---|---|
committer | Nikias Bassen | 2019-01-21 02:22:47 +0100 |
commit | 344384574bc2c659756f0ce9d4319265f715bf1f (patch) | |
tree | 4b24a64aad1a0e46d1e8ce8f34d1612a5ce670c3 /src/Dictionary.cpp | |
parent | 84d6af8f82b30b6519bb401d467febe4ea981dad (diff) | |
download | libplist-344384574bc2c659756f0ce9d4319265f715bf1f.tar.gz libplist-344384574bc2c659756f0ce9d4319265f715bf1f.tar.bz2 |
cpp: Dictionary: Reduce code duplication with helper function
Diffstat (limited to 'src/Dictionary.cpp')
-rw-r--r-- | src/Dictionary.cpp | 63 |
1 files changed, 16 insertions, 47 deletions
diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index 98eeb93..44198cd 100644 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -28,27 +28,28 @@ Dictionary::Dictionary(Node* parent) : Structure(PLIST_DICT, parent) { } -Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) +static void dictionary_fill(Dictionary *_this, std::map<std::string,Node*> map, plist_t node) { - _node = node; plist_dict_iter it = NULL; - - char* key = NULL; + plist_dict_new_iter(node, &it); plist_t subnode = NULL; - plist_dict_new_iter(_node, &it); - plist_dict_next_item(_node, it, &key, &subnode); - while (subnode) - { - _map[std::string(key)] = Node::FromPlist(subnode, this); - + do { + char *key = NULL; subnode = NULL; + plist_dict_next_item(node, it, &key, &subnode); + if (key && subnode) + map[std::string(key)] = Node::FromPlist(subnode, _this); free(key); - key = NULL; - plist_dict_next_item(_node, it, &key, &subnode); - } + } while (subnode); free(it); } +Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) +{ + _node = node; + dictionary_fill(this, _map, _node); +} + Dictionary::Dictionary(const PList::Dictionary& d) : Structure() { for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) @@ -57,24 +58,8 @@ Dictionary::Dictionary(const PList::Dictionary& d) : Structure() delete it->second; } _map.clear(); - _node = plist_copy(d.GetPlist()); - plist_dict_iter it = NULL; - - char* key = NULL; - plist_t subnode = NULL; - plist_dict_new_iter(_node, &it); - plist_dict_next_item(_node, it, &key, &subnode); - while (subnode) - { - _map[std::string(key)] = Node::FromPlist(subnode, this); - - subnode = NULL; - free(key); - key = NULL; - plist_dict_next_item(_node, it, &key, &subnode); - } - free(it); + dictionary_fill(this, _map, _node); } Dictionary& Dictionary::operator=(PList::Dictionary& d) @@ -85,24 +70,8 @@ Dictionary& Dictionary::operator=(PList::Dictionary& d) delete it->second; } _map.clear(); - _node = plist_copy(d.GetPlist()); - plist_dict_iter it = NULL; - - char* key = NULL; - plist_t subnode = NULL; - plist_dict_new_iter(_node, &it); - plist_dict_next_item(_node, it, &key, &subnode); - while (subnode) - { - _map[std::string(key)] = Node::FromPlist(subnode, this); - - subnode = NULL; - free(key); - key = NULL; - plist_dict_next_item(_node, it, NULL, &subnode); - } - free(it); + dictionary_fill(this, _map, _node); return *this; } |