summaryrefslogtreecommitdiffstats
path: root/swig
diff options
context:
space:
mode:
Diffstat (limited to 'swig')
-rw-r--r--swig/plist.i104
1 files changed, 49 insertions, 55 deletions
diff --git a/swig/plist.i b/swig/plist.i
index a56592e..5987b77 100644
--- a/swig/plist.i
+++ b/swig/plist.i
@@ -9,10 +9,12 @@ typedef struct {
9 char should_keep_plist; 9 char should_keep_plist;
10} PListNode; 10} PListNode;
11 11
12PListNode *allocate_wrapper() { 12PListNode *allocate_wrapper(plist_t plist, char should_keep_plist) {
13 PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode)); 13 PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode));
14 if (wrapper) { 14 if (wrapper) {
15 memset(wrapper, 0, sizeof(PListNode)); 15 memset(wrapper, 0, sizeof(PListNode));
16 wrapper->node = plist;
17 wrapper->should_keep_plist = should_keep_plist;
16 return wrapper; 18 return wrapper;
17 } 19 }
18 return NULL; 20 return NULL;
@@ -37,6 +39,8 @@ typedef enum {
37} plist_type; 39} plist_type;
38 40
39typedef struct { 41typedef struct {
42 plist_t node;
43 char should_keep_plist;
40} PListNode; 44} PListNode;
41 45
42%extend PListNode { // Attach these functions to struct Vector 46%extend PListNode { // Attach these functions to struct Vector
@@ -44,16 +48,10 @@ typedef struct {
44 PListNode* node = NULL; 48 PListNode* node = NULL;
45 switch (t) { 49 switch (t) {
46 case PLIST_ARRAY : 50 case PLIST_ARRAY :
47 node = allocate_wrapper(); 51 node = allocate_wrapper( plist_new_array(), 0 );
48 if (node) {
49 node->node = plist_new_array();
50 }
51 break; 52 break;
52 case PLIST_DICT : 53 case PLIST_DICT :
53 node = allocate_wrapper(); 54 node = allocate_wrapper( plist_new_dict(), 0 );
54 if (node) {
55 node->node = plist_new_dict();
56 }
57 break; 55 break;
58 default : 56 default :
59 node = NULL; 57 node = NULL;
@@ -63,27 +61,36 @@ typedef struct {
63 } 61 }
64 62
65 PListNode(char* xml) { 63 PListNode(char* xml) {
66 PListNode* plist = allocate_wrapper(); 64 plist_t plist = NULL;
67 plist_from_xml(xml, strlen(xml), &plist->node); 65 plist_from_xml(xml, strlen(xml), &plist);
68 return plist; 66 if (plist)
67 return allocate_wrapper( plist, 0 );
68 return NULL;
69 } 69 }
70 70
71 PListNode(char* bin, uint64_t len) { 71 PListNode(char* bin, uint64_t len) {
72 PListNode* plist = allocate_wrapper(); 72 plist_t plist = NULL;
73 plist_from_bin(bin, len, &plist->node); 73 plist_from_bin(bin, len, &plist);
74 return plist; 74 if (plist)
75 return allocate_wrapper( plist, 0 );
76 return NULL;
75 } 77 }
76 78
77 ~PListNode() { 79 ~PListNode() {
78 if (!$self->should_keep_plist) { 80 if (!$self->should_keep_plist) {
79 plist_free($self->node); 81 plist_free($self->node);
80 } 82 }
83 $self->node = NULL;
84 $self->should_keep_plist = 0;
81 free($self); 85 free($self);
86 $self = NULL;
82 } 87 }
83 88
84 void add_sub_node(PListNode* subnode) { 89 void add_sub_node(PListNode* subnode) {
85 if (subnode) { 90 if (subnode) {
86 plist_add_sub_node($self->node, subnode->node); 91 plist_add_sub_node($self->node, subnode->node);
92 //subnode is not root anymore. Do not delete tree
93 subnode->should_keep_plist = 1;
87 } 94 }
88 } 95 }
89 96
@@ -112,30 +119,27 @@ typedef struct {
112 } 119 }
113 120
114 PListNode* get_first_child() { 121 PListNode* get_first_child() {
115 PListNode* plist = allocate_wrapper(); 122 plist_t node = plist_get_first_child( $self->node );
116 if (plist) { 123 if (node) {
117 plist->node = plist_get_first_child(&$self->node); 124 return allocate_wrapper(node, 1);
118 plist->should_keep_plist = 1;
119 } 125 }
120 return plist; 126 return NULL;
121 } 127 }
122 128
123 PListNode* get_next_sibling() { 129 PListNode* get_next_sibling() {
124 PListNode* plist = allocate_wrapper(); 130 plist_t node = plist_get_next_sibling( $self->node );
125 if (plist) { 131 if (node) {
126 plist->node = plist_get_next_sibling(&$self->node); 132 return allocate_wrapper(node, 1);
127 plist->should_keep_plist = 1;
128 } 133 }
129 return plist; 134 return NULL;
130 } 135 }
131 136
132 PListNode* get_prev_sibling() { 137 PListNode* get_prev_sibling() {
133 PListNode* plist = allocate_wrapper(); 138 plist_t node = plist_get_prev_sibling( $self->node );
134 if (plist) { 139 if (node) {
135 plist->node = plist_get_prev_sibling(&$self->node); 140 return allocate_wrapper(node, 1);
136 plist->should_keep_plist = 1;
137 } 141 }
138 return plist; 142 return NULL;
139 } 143 }
140 144
141 char* as_key() { 145 char* as_key() {
@@ -182,12 +186,7 @@ typedef struct {
182 PListNode* find_node_by_key(char *s) { 186 PListNode* find_node_by_key(char *s) {
183 plist_t node = plist_find_node_by_key($self->node, s); 187 plist_t node = plist_find_node_by_key($self->node, s);
184 if (node) { 188 if (node) {
185 PListNode* plist = allocate_wrapper(); 189 return allocate_wrapper(node, 1);
186 if (plist) {
187 plist->node = node;
188 plist->should_keep_plist = 1;
189 }
190 return plist;
191 } 190 }
192 return NULL; 191 return NULL;
193 } 192 }
@@ -195,25 +194,15 @@ typedef struct {
195 PListNode* find_node_by_string(char* s) { 194 PListNode* find_node_by_string(char* s) {
196 plist_t node = plist_find_node_by_string($self->node, s); 195 plist_t node = plist_find_node_by_string($self->node, s);
197 if (node) { 196 if (node) {
198 PListNode* plist = allocate_wrapper(); 197 return allocate_wrapper(node, 1);
199 if (plist) { 198 }
200 plist->node = node;
201 plist->should_keep_plist = 1;
202 }
203 return plist;
204 }
205 return NULL; 199 return NULL;
206 } 200 }
207 201
208 PListNode* get_array_nth_el(unsigned int n) { 202 PListNode* get_array_nth_el(unsigned int n) {
209 plist_t node = plist_get_array_nth_el($self->node, n); 203 plist_t node = plist_get_array_nth_el($self->node, n);
210 if (node) { 204 if (node) {
211 PListNode* plist = allocate_wrapper(); 205 return allocate_wrapper(node, 1);
212 if (plist) {
213 plist->node = node;
214 plist->should_keep_plist = 1;
215 }
216 return plist;
217 } 206 }
218 return NULL; 207 return NULL;
219 } 208 }
@@ -221,12 +210,7 @@ typedef struct {
221 PListNode* get_dict_el_from_key(char *key) { 210 PListNode* get_dict_el_from_key(char *key) {
222 plist_t node = plist_get_dict_el_from_key($self->node, key); 211 plist_t node = plist_get_dict_el_from_key($self->node, key);
223 if (node) { 212 if (node) {
224 PListNode* plist = allocate_wrapper(); 213 return allocate_wrapper(node, 1);
225 if (plist) {
226 plist->node = node;
227 plist->should_keep_plist = 1;
228 }
229 return plist;
230 } 214 }
231 return NULL; 215 return NULL;
232 } 216 }
@@ -246,10 +230,20 @@ typedef struct {
246 } 230 }
247 231
248 void from_xml (char* xml) { 232 void from_xml (char* xml) {
233 if (!$self->should_keep_plist) {
234 plist_free($self->node);
235 }
236 $self->node = NULL;
237 $self->should_keep_plist = 0;
249 plist_from_xml(xml, strlen(xml), &$self->node); 238 plist_from_xml(xml, strlen(xml), &$self->node);
250 } 239 }
251 240
252 void from_bin (char* data, uint64_t len) { 241 void from_bin (char* data, uint64_t len) {
242 if (!$self->should_keep_plist) {
243 plist_free($self->node);
244 }
245 $self->node = NULL;
246 $self->should_keep_plist = 0;
253 plist_from_bin(data, len, &$self->node); 247 plist_from_bin(data, len, &$self->node);
254 } 248 }
255}; 249};