diff options
Diffstat (limited to 'swig')
| -rw-r--r-- | swig/plist.i | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/swig/plist.i b/swig/plist.i index 2347a6d..41ff7cc 100644 --- a/swig/plist.i +++ b/swig/plist.i | |||
| @@ -6,7 +6,17 @@ | |||
| 6 | #include <plist/plist.h> | 6 | #include <plist/plist.h> |
| 7 | typedef struct { | 7 | typedef struct { |
| 8 | plist_t node; | 8 | plist_t node; |
| 9 | char should_keep_plist; | ||
| 9 | } PListNode; | 10 | } PListNode; |
| 11 | |||
| 12 | PListNode *allocate_wrapper() { | ||
| 13 | PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode)); | ||
| 14 | if (wrapper) { | ||
| 15 | memset(wrapper, 0, sizeof(PListNode)); | ||
| 16 | return wrapper; | ||
| 17 | } | ||
| 18 | return NULL; | ||
| 19 | } | ||
| 10 | %} | 20 | %} |
| 11 | 21 | ||
| 12 | %include "stdint.i" | 22 | %include "stdint.i" |
| @@ -27,7 +37,6 @@ typedef enum { | |||
| 27 | } plist_type; | 37 | } plist_type; |
| 28 | 38 | ||
| 29 | typedef struct { | 39 | typedef struct { |
| 30 | plist_t node; | ||
| 31 | } PListNode; | 40 | } PListNode; |
| 32 | 41 | ||
| 33 | %extend PListNode { // Attach these functions to struct Vector | 42 | %extend PListNode { // Attach these functions to struct Vector |
| @@ -35,12 +44,16 @@ typedef struct { | |||
| 35 | PListNode* node = NULL; | 44 | PListNode* node = NULL; |
| 36 | switch (t) { | 45 | switch (t) { |
| 37 | case PLIST_ARRAY : | 46 | case PLIST_ARRAY : |
| 38 | node = (PListNode*) malloc(sizeof(PListNode)); | 47 | node = allocate_wrapper(); |
| 39 | node->node = plist_new_array(); | 48 | if (node) { |
| 49 | node->node = plist_new_array(); | ||
| 50 | } | ||
| 40 | break; | 51 | break; |
| 41 | case PLIST_DICT : | 52 | case PLIST_DICT : |
| 42 | node = (PListNode*) malloc(sizeof(PListNode)); | 53 | node = allocate_wrapper(); |
| 43 | node->node = plist_new_dict(); | 54 | if (node) { |
| 55 | node->node = plist_new_dict(); | ||
| 56 | } | ||
| 44 | break; | 57 | break; |
| 45 | default : | 58 | default : |
| 46 | node = NULL; | 59 | node = NULL; |
| @@ -50,19 +63,21 @@ typedef struct { | |||
| 50 | } | 63 | } |
| 51 | 64 | ||
| 52 | PListNode(char* xml) { | 65 | PListNode(char* xml) { |
| 53 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 66 | PListNode* plist = allocate_wrapper(); |
| 54 | plist_from_xml(xml, strlen(xml), &plist->node); | 67 | plist_from_xml(xml, strlen(xml), &plist->node); |
| 55 | return plist; | 68 | return plist; |
| 56 | } | 69 | } |
| 57 | 70 | ||
| 58 | PListNode(char* bin, uint64_t len) { | 71 | PListNode(char* bin, uint64_t len) { |
| 59 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 72 | PListNode* plist = allocate_wrapper(); |
| 60 | plist_from_bin(bin, len, &plist->node); | 73 | plist_from_bin(bin, len, &plist->node); |
| 61 | return plist; | 74 | return plist; |
| 62 | } | 75 | } |
| 63 | 76 | ||
| 64 | ~PListNode() { | 77 | ~PListNode() { |
| 65 | plist_free($self->node); | 78 | if (!$self->should_keep_plist) { |
| 79 | plist_free($self->node); | ||
| 80 | } | ||
| 66 | free($self); | 81 | free($self); |
| 67 | } | 82 | } |
| 68 | 83 | ||
| @@ -97,20 +112,29 @@ typedef struct { | |||
| 97 | } | 112 | } |
| 98 | 113 | ||
| 99 | PListNode* get_first_child() { | 114 | PListNode* get_first_child() { |
| 100 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 115 | PListNode* plist = allocate_wrapper(); |
| 101 | plist_get_first_child(&$self->node); | 116 | if (plist) { |
| 117 | plist->node = plist_get_first_child(&$self->node); | ||
| 118 | plist->should_keep_plist = 1; | ||
| 119 | } | ||
| 102 | return plist; | 120 | return plist; |
| 103 | } | 121 | } |
| 104 | 122 | ||
| 105 | PListNode* get_next_sibling() { | 123 | PListNode* get_next_sibling() { |
| 106 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 124 | PListNode* plist = allocate_wrapper(); |
| 107 | plist_get_next_sibling(&$self->node); | 125 | if (plist) { |
| 126 | plist->node = plist_get_next_sibling(&$self->node); | ||
| 127 | plist->should_keep_plist = 1; | ||
| 128 | } | ||
| 108 | return plist; | 129 | return plist; |
| 109 | } | 130 | } |
| 110 | 131 | ||
| 111 | PListNode* get_prev_sibling() { | 132 | PListNode* get_prev_sibling() { |
| 112 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 133 | PListNode* plist = allocate_wrapper(); |
| 113 | plist_get_prev_sibling(&$self->node); | 134 | if (plist) { |
| 135 | plist->node = plist_get_prev_sibling(&$self->node); | ||
| 136 | plist->should_keep_plist = 1; | ||
| 137 | } | ||
| 114 | return plist; | 138 | return plist; |
| 115 | } | 139 | } |
| 116 | 140 | ||
| @@ -156,14 +180,20 @@ typedef struct { | |||
| 156 | } | 180 | } |
| 157 | 181 | ||
| 158 | PListNode* find_node_by_key(char *s) { | 182 | PListNode* find_node_by_key(char *s) { |
| 159 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 183 | PListNode* plist = allocate_wrapper(); |
| 160 | plist = plist_find_node_by_key($self->node, s); | 184 | if (plist) { |
| 185 | plist->node = plist_find_node_by_key($self->node, s); | ||
| 186 | plist->should_keep_plist = 1; | ||
| 187 | } | ||
| 161 | return plist; | 188 | return plist; |
| 162 | } | 189 | } |
| 163 | 190 | ||
| 164 | PListNode* find_node_by_string(char* s) { | 191 | PListNode* find_node_by_string(char* s) { |
| 165 | PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); | 192 | PListNode* plist = allocate_wrapper(); |
| 166 | plist = plist_find_node_by_string($self->node, s); | 193 | if (plist) { |
| 194 | plist->node = plist_find_node_by_string($self->node, s); | ||
| 195 | plist->should_keep_plist = 1; | ||
| 196 | } | ||
| 167 | return plist; | 197 | return plist; |
| 168 | } | 198 | } |
| 169 | 199 | ||
