diff options
| author | 2019-01-21 02:22:47 +0100 | |
|---|---|---|
| committer | 2019-01-21 02:22:47 +0100 | |
| commit | 344384574bc2c659756f0ce9d4319265f715bf1f (patch) | |
| tree | 4b24a64aad1a0e46d1e8ce8f34d1612a5ce670c3 | |
| parent | 84d6af8f82b30b6519bb401d467febe4ea981dad (diff) | |
| download | libplist-344384574bc2c659756f0ce9d4319265f715bf1f.tar.gz libplist-344384574bc2c659756f0ce9d4319265f715bf1f.tar.bz2 | |
cpp: Dictionary: Reduce code duplication with helper function
| -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) | |||
| 28 | { | 28 | { |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) | 31 | static void dictionary_fill(Dictionary *_this, std::map<std::string,Node*> map, plist_t node) |
| 32 | { | 32 | { |
| 33 | _node = node; | ||
| 34 | plist_dict_iter it = NULL; | 33 | plist_dict_iter it = NULL; |
| 35 | 34 | plist_dict_new_iter(node, &it); | |
| 36 | char* key = NULL; | ||
| 37 | plist_t subnode = NULL; | 35 | plist_t subnode = NULL; |
| 38 | plist_dict_new_iter(_node, &it); | 36 | do { |
| 39 | plist_dict_next_item(_node, it, &key, &subnode); | 37 | char *key = NULL; |
| 40 | while (subnode) | ||
| 41 | { | ||
| 42 | _map[std::string(key)] = Node::FromPlist(subnode, this); | ||
| 43 | |||
| 44 | subnode = NULL; | 38 | subnode = NULL; |
| 39 | plist_dict_next_item(node, it, &key, &subnode); | ||
| 40 | if (key && subnode) | ||
| 41 | map[std::string(key)] = Node::FromPlist(subnode, _this); | ||
| 45 | free(key); | 42 | free(key); |
| 46 | key = NULL; | 43 | } while (subnode); |
| 47 | plist_dict_next_item(_node, it, &key, &subnode); | ||
| 48 | } | ||
| 49 | free(it); | 44 | free(it); |
| 50 | } | 45 | } |
| 51 | 46 | ||
| 47 | Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) | ||
| 48 | { | ||
| 49 | _node = node; | ||
| 50 | dictionary_fill(this, _map, _node); | ||
| 51 | } | ||
| 52 | |||
| 52 | Dictionary::Dictionary(const PList::Dictionary& d) : Structure() | 53 | Dictionary::Dictionary(const PList::Dictionary& d) : Structure() |
| 53 | { | 54 | { |
| 54 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) | 55 | for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++) |
| @@ -57,24 +58,8 @@ Dictionary::Dictionary(const PList::Dictionary& d) : Structure() | |||
| 57 | delete it->second; | 58 | delete it->second; |
| 58 | } | 59 | } |
| 59 | _map.clear(); | 60 | _map.clear(); |
| 60 | |||
| 61 | _node = plist_copy(d.GetPlist()); | 61 | _node = plist_copy(d.GetPlist()); |
| 62 | plist_dict_iter it = NULL; | 62 | dictionary_fill(this, _map, _node); |
| 63 | |||
| 64 | char* key = NULL; | ||
| 65 | plist_t subnode = NULL; | ||
| 66 | plist_dict_new_iter(_node, &it); | ||
| 67 | plist_dict_next_item(_node, it, &key, &subnode); | ||
| 68 | while (subnode) | ||
| 69 | { | ||
| 70 | _map[std::string(key)] = Node::FromPlist(subnode, this); | ||
| 71 | |||
| 72 | subnode = NULL; | ||
| 73 | free(key); | ||
| 74 | key = NULL; | ||
| 75 | plist_dict_next_item(_node, it, &key, &subnode); | ||
| 76 | } | ||
| 77 | free(it); | ||
| 78 | } | 63 | } |
| 79 | 64 | ||
| 80 | Dictionary& Dictionary::operator=(PList::Dictionary& d) | 65 | Dictionary& Dictionary::operator=(PList::Dictionary& d) |
| @@ -85,24 +70,8 @@ Dictionary& Dictionary::operator=(PList::Dictionary& d) | |||
| 85 | delete it->second; | 70 | delete it->second; |
| 86 | } | 71 | } |
| 87 | _map.clear(); | 72 | _map.clear(); |
| 88 | |||
| 89 | _node = plist_copy(d.GetPlist()); | 73 | _node = plist_copy(d.GetPlist()); |
| 90 | plist_dict_iter it = NULL; | 74 | dictionary_fill(this, _map, _node); |
| 91 | |||
| 92 | char* key = NULL; | ||
| 93 | plist_t subnode = NULL; | ||
| 94 | plist_dict_new_iter(_node, &it); | ||
| 95 | plist_dict_next_item(_node, it, &key, &subnode); | ||
| 96 | while (subnode) | ||
| 97 | { | ||
| 98 | _map[std::string(key)] = Node::FromPlist(subnode, this); | ||
| 99 | |||
| 100 | subnode = NULL; | ||
| 101 | free(key); | ||
| 102 | key = NULL; | ||
| 103 | plist_dict_next_item(_node, it, NULL, &subnode); | ||
| 104 | } | ||
| 105 | free(it); | ||
| 106 | return *this; | 75 | return *this; |
| 107 | } | 76 | } |
| 108 | 77 | ||
