summaryrefslogtreecommitdiffstats
path: root/src/Dictionary.cpp
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2009-10-13 20:04:06 +0200
committerGravatar Jonathan Beck2009-10-13 20:04:06 +0200
commita922b714c9b75fdc67735d674758d4eaedfd32f9 (patch)
tree509ec53c18c0be36a9e650eb0f760854cd8d4957 /src/Dictionary.cpp
parente492ef675c404cc6c0d1cfa26e47a1c16c850d5f (diff)
downloadlibplist-a922b714c9b75fdc67735d674758d4eaedfd32f9.tar.gz
libplist-a922b714c9b75fdc67735d674758d4eaedfd32f9.tar.bz2
Add C++ binding.
Diffstat (limited to 'src/Dictionary.cpp')
-rw-r--r--src/Dictionary.cpp217
1 files changed, 217 insertions, 0 deletions
diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp
new file mode 100644
index 0000000..5bace76
--- /dev/null
+++ b/src/Dictionary.cpp
@@ -0,0 +1,217 @@
1/*
2 * Dictionary.cpp
3 *
4 * Copyright (c) 2009 Jonathan Beck All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
22#include <plist/Dictionary.h>
23#include <plist/Array.h>
24
25namespace PList
26{
27
28Dictionary::Dictionary() : Structure(PLIST_DICT)
29{
30}
31
32Dictionary::Dictionary(plist_t node) : Structure()
33{
34 _node = node;
35 plist_dict_iter it = NULL;
36
37 char* key = NULL;
38 plist_t subnode = NULL;
39 plist_dict_new_iter(_node, &it);
40 plist_dict_next_item(_node, it, &key, &subnode);
41 while (subnode)
42 {
43 plist_type subtype = plist_get_node_type(subnode);
44 switch(subtype)
45 {
46 case PLIST_DICT:
47 _map[std::string(key)] = new Dictionary(subnode);
48 break;
49 case PLIST_ARRAY:
50 _map[std::string(key)] = new Array(subnode);
51 break;
52 case PLIST_BOOLEAN:
53 case PLIST_UINT:
54 case PLIST_REAL:
55 case PLIST_STRING:
56 case PLIST_DATE:
57 case PLIST_DATA:
58 default:
59 _map[std::string(key)] = new Node(subnode);
60 break;
61 }
62
63 subnode = NULL;
64 free(key);
65 key = NULL;
66 plist_dict_next_item(_node, it, NULL, &subnode);
67 }
68 free(it);
69}
70
71Dictionary::Dictionary(Dictionary& d)
72{
73 for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
74 {
75 plist_free(it->second->GetPlist());
76 delete it->second;
77 }
78 _map.clear();
79
80 _node = plist_copy(d.GetPlist());
81 plist_dict_iter it = NULL;
82
83 char* key = NULL;
84 plist_t subnode = NULL;
85 plist_dict_new_iter(_node, &it);
86 plist_dict_next_item(_node, it, &key, &subnode);
87 while (subnode)
88 {
89 plist_type subtype = plist_get_node_type(subnode);
90 switch(subtype)
91 {
92 case PLIST_DICT:
93 _map[std::string(key)] = new Dictionary(subnode);
94 break;
95 case PLIST_ARRAY:
96 _map[std::string(key)] = new Array(subnode);
97 break;
98 case PLIST_BOOLEAN:
99 case PLIST_UINT:
100 case PLIST_REAL:
101 case PLIST_STRING:
102 case PLIST_DATE:
103 case PLIST_DATA:
104 default:
105 _map[std::string(key)] = new Node(subnode);
106 break;
107 }
108
109 subnode = NULL;
110 free(key);
111 key = NULL;
112 plist_dict_next_item(_node, it, NULL, &subnode);
113 }
114 free(it);
115}
116
117Dictionary& Dictionary::operator=(const Dictionary& d)
118{
119 for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
120 {
121 plist_free(it->second->GetPlist());
122 delete it->second;
123 }
124 _map.clear();
125
126 _node = plist_copy(d.GetPlist());
127 plist_dict_iter it = NULL;
128
129 char* key = NULL;
130 plist_t subnode = NULL;
131 plist_dict_new_iter(_node, &it);
132 plist_dict_next_item(_node, it, &key, &subnode);
133 while (subnode)
134 {
135 plist_type subtype = plist_get_node_type(subnode);
136 switch(subtype)
137 {
138 case PLIST_DICT:
139 _map[std::string(key)] = new Dictionary(subnode);
140 break;
141 case PLIST_ARRAY:
142 _map[std::string(key)] = new Array(subnode);
143 break;
144 case PLIST_BOOLEAN:
145 case PLIST_UINT:
146 case PLIST_REAL:
147 case PLIST_STRING:
148 case PLIST_DATE:
149 case PLIST_DATA:
150 default:
151 _map[std::string(key)] = new Node(subnode);
152 break;
153 }
154
155 subnode = NULL;
156 free(key);
157 key = NULL;
158 plist_dict_next_item(_node, it, NULL, &subnode);
159 }
160 free(it);
161}
162
163Dictionary::~Dictionary()
164{
165 for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
166 {
167 plist_free(it->second->GetPlist());
168 delete it->second;
169 }
170 _map.clear();
171}
172
173Node* Dictionary::operator[](std::string& key)
174{
175 return _map[key];
176}
177
178Dictionary::iterator Dictionary::Begin()
179{
180 return _map.begin();
181}
182
183Dictionary::iterator Dictionary::End()
184{
185 return _map.end();
186}
187
188void Dictionary::Insert(std::string& key, Node* node)
189{
190 if (node)
191 {
192 plist_dict_insert_item(_node, key.c_str(), node->GetPlist());
193 delete _map[key];
194 _map[key] = node;
195 }
196}
197
198void Dictionary::Remove(Node* node)
199{
200 if (node)
201 {
202 char* key = NULL;
203 plist_dict_get_item_key(node->GetPlist(), &key);
204 plist_dict_remove_item(_node, key);
205 std::string skey = key;
206 free(key);
207 delete node;
208 }
209}
210
211void Dictionary::Remove(std::string& key)
212{
213 plist_dict_remove_item(_node, key.c_str());
214 delete _map[key];
215}
216
217};