Commit 33ca5426 authored by Jonathan Beck's avatar Jonathan Beck

Fix subnode lifecycle (do not free).

Always return NULL if node not found.
parent 21e389bc
...@@ -9,10 +9,12 @@ typedef struct { ...@@ -9,10 +9,12 @@ typedef struct {
char should_keep_plist; char should_keep_plist;
} PListNode; } PListNode;
PListNode *allocate_wrapper() { PListNode *allocate_wrapper(plist_t plist, char should_keep_plist) {
PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode)); PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode));
if (wrapper) { if (wrapper) {
memset(wrapper, 0, sizeof(PListNode)); memset(wrapper, 0, sizeof(PListNode));
wrapper->node = plist;
wrapper->should_keep_plist = should_keep_plist;
return wrapper; return wrapper;
} }
return NULL; return NULL;
...@@ -37,6 +39,8 @@ typedef enum { ...@@ -37,6 +39,8 @@ typedef enum {
} plist_type; } plist_type;
typedef struct { typedef struct {
plist_t node;
char should_keep_plist;
} PListNode; } PListNode;
%extend PListNode { // Attach these functions to struct Vector %extend PListNode { // Attach these functions to struct Vector
...@@ -44,16 +48,10 @@ typedef struct { ...@@ -44,16 +48,10 @@ typedef struct {
PListNode* node = NULL; PListNode* node = NULL;
switch (t) { switch (t) {
case PLIST_ARRAY : case PLIST_ARRAY :
node = allocate_wrapper(); node = allocate_wrapper( plist_new_array(), 0 );
if (node) {
node->node = plist_new_array();
}
break; break;
case PLIST_DICT : case PLIST_DICT :
node = allocate_wrapper(); node = allocate_wrapper( plist_new_dict(), 0 );
if (node) {
node->node = plist_new_dict();
}
break; break;
default : default :
node = NULL; node = NULL;
...@@ -63,27 +61,36 @@ typedef struct { ...@@ -63,27 +61,36 @@ typedef struct {
} }
PListNode(char* xml) { PListNode(char* xml) {
PListNode* plist = allocate_wrapper(); plist_t plist = NULL;
plist_from_xml(xml, strlen(xml), &plist->node); plist_from_xml(xml, strlen(xml), &plist);
return plist; if (plist)
return allocate_wrapper( plist, 0 );
return NULL;
} }
PListNode(char* bin, uint64_t len) { PListNode(char* bin, uint64_t len) {
PListNode* plist = allocate_wrapper(); plist_t plist = NULL;
plist_from_bin(bin, len, &plist->node); plist_from_bin(bin, len, &plist);
return plist; if (plist)
return allocate_wrapper( plist, 0 );
return NULL;
} }
~PListNode() { ~PListNode() {
if (!$self->should_keep_plist) { if (!$self->should_keep_plist) {
plist_free($self->node); plist_free($self->node);
} }
$self->node = NULL;
$self->should_keep_plist = 0;
free($self); free($self);
$self = NULL;
} }
void add_sub_node(PListNode* subnode) { void add_sub_node(PListNode* subnode) {
if (subnode) { if (subnode) {
plist_add_sub_node($self->node, subnode->node); plist_add_sub_node($self->node, subnode->node);
//subnode is not root anymore. Do not delete tree
subnode->should_keep_plist = 1;
} }
} }
...@@ -112,30 +119,27 @@ typedef struct { ...@@ -112,30 +119,27 @@ typedef struct {
} }
PListNode* get_first_child() { PListNode* get_first_child() {
PListNode* plist = allocate_wrapper(); plist_t node = plist_get_first_child( $self->node );
if (plist) { if (node) {
plist->node = plist_get_first_child(&$self->node); return allocate_wrapper(node, 1);
plist->should_keep_plist = 1;
} }
return plist; return NULL;
} }
PListNode* get_next_sibling() { PListNode* get_next_sibling() {
PListNode* plist = allocate_wrapper(); plist_t node = plist_get_next_sibling( $self->node );
if (plist) { if (node) {
plist->node = plist_get_next_sibling(&$self->node); return allocate_wrapper(node, 1);
plist->should_keep_plist = 1;
} }
return plist; return NULL;
} }
PListNode* get_prev_sibling() { PListNode* get_prev_sibling() {
PListNode* plist = allocate_wrapper(); plist_t node = plist_get_prev_sibling( $self->node );
if (plist) { if (node) {
plist->node = plist_get_prev_sibling(&$self->node); return allocate_wrapper(node, 1);
plist->should_keep_plist = 1;
} }
return plist; return NULL;
} }
char* as_key() { char* as_key() {
...@@ -182,12 +186,7 @@ typedef struct { ...@@ -182,12 +186,7 @@ typedef struct {
PListNode* find_node_by_key(char *s) { PListNode* find_node_by_key(char *s) {
plist_t node = plist_find_node_by_key($self->node, s); plist_t node = plist_find_node_by_key($self->node, s);
if (node) { if (node) {
PListNode* plist = allocate_wrapper(); return allocate_wrapper(node, 1);
if (plist) {
plist->node = node;
plist->should_keep_plist = 1;
}
return plist;
} }
return NULL; return NULL;
} }
...@@ -195,12 +194,7 @@ typedef struct { ...@@ -195,12 +194,7 @@ typedef struct {
PListNode* find_node_by_string(char* s) { PListNode* find_node_by_string(char* s) {
plist_t node = plist_find_node_by_string($self->node, s); plist_t node = plist_find_node_by_string($self->node, s);
if (node) { if (node) {
PListNode* plist = allocate_wrapper(); return allocate_wrapper(node, 1);
if (plist) {
plist->node = node;
plist->should_keep_plist = 1;
}
return plist;
} }
return NULL; return NULL;
} }
...@@ -208,12 +202,7 @@ typedef struct { ...@@ -208,12 +202,7 @@ typedef struct {
PListNode* get_array_nth_el(unsigned int n) { PListNode* get_array_nth_el(unsigned int n) {
plist_t node = plist_get_array_nth_el($self->node, n); plist_t node = plist_get_array_nth_el($self->node, n);
if (node) { if (node) {
PListNode* plist = allocate_wrapper(); return allocate_wrapper(node, 1);
if (plist) {
plist->node = node;
plist->should_keep_plist = 1;
}
return plist;
} }
return NULL; return NULL;
} }
...@@ -221,12 +210,7 @@ typedef struct { ...@@ -221,12 +210,7 @@ typedef struct {
PListNode* get_dict_el_from_key(char *key) { PListNode* get_dict_el_from_key(char *key) {
plist_t node = plist_get_dict_el_from_key($self->node, key); plist_t node = plist_get_dict_el_from_key($self->node, key);
if (node) { if (node) {
PListNode* plist = allocate_wrapper(); return allocate_wrapper(node, 1);
if (plist) {
plist->node = node;
plist->should_keep_plist = 1;
}
return plist;
} }
return NULL; return NULL;
} }
...@@ -246,10 +230,20 @@ typedef struct { ...@@ -246,10 +230,20 @@ typedef struct {
} }
void from_xml (char* xml) { void from_xml (char* xml) {
if (!$self->should_keep_plist) {
plist_free($self->node);
}
$self->node = NULL;
$self->should_keep_plist = 0;
plist_from_xml(xml, strlen(xml), &$self->node); plist_from_xml(xml, strlen(xml), &$self->node);
} }
void from_bin (char* data, uint64_t len) { void from_bin (char* data, uint64_t len) {
if (!$self->should_keep_plist) {
plist_free($self->node);
}
$self->node = NULL;
$self->should_keep_plist = 0;
plist_from_bin(data, len, &$self->node); plist_from_bin(data, len, &$self->node);
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment