diff options
Diffstat (limited to 'src/Array.cpp')
| -rw-r--r-- | src/Array.cpp | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/Array.cpp b/src/Array.cpp new file mode 100644 index 0000000..6f1d3f9 --- /dev/null +++ b/src/Array.cpp | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | /* | ||
| 2 | * Array.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/Array.h> | ||
| 23 | #include <plist/Dictionary.h> | ||
| 24 | |||
| 25 | namespace PList | ||
| 26 | { | ||
| 27 | |||
| 28 | Array::Array() : Structure(PLIST_ARRAY) | ||
| 29 | { | ||
| 30 | _array.clear(); | ||
| 31 | } | ||
| 32 | |||
| 33 | Array::Array(plist_t node) : Structure() | ||
| 34 | { | ||
| 35 | _node = node; | ||
| 36 | uint32_t size = plist_array_get_size(_node); | ||
| 37 | |||
| 38 | for (uint32_t i = 0; i < size; i++) | ||
| 39 | { | ||
| 40 | plist_t subnode = plist_array_get_item(_node, i); | ||
| 41 | plist_type subtype = plist_get_node_type(subnode); | ||
| 42 | switch(subtype) | ||
| 43 | { | ||
| 44 | case PLIST_DICT: | ||
| 45 | _array.push_back( new Dictionary(subnode) ); | ||
| 46 | break; | ||
| 47 | case PLIST_ARRAY: | ||
| 48 | _array.push_back( new Array(subnode) ); | ||
| 49 | break; | ||
| 50 | case PLIST_BOOLEAN: | ||
| 51 | case PLIST_UINT: | ||
| 52 | case PLIST_REAL: | ||
| 53 | case PLIST_STRING: | ||
| 54 | case PLIST_DATE: | ||
| 55 | case PLIST_DATA: | ||
| 56 | default: | ||
| 57 | _array.push_back( new Node(subnode) ); | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | Array::Array(Array& a) | ||
| 64 | { | ||
| 65 | plist_free(_node); | ||
| 66 | for (int it = 0; it < _array.size(); it++) | ||
| 67 | { | ||
| 68 | delete _array.at(it); | ||
| 69 | } | ||
| 70 | _array.clear(); | ||
| 71 | |||
| 72 | _node = plist_copy(a.GetPlist()); | ||
| 73 | uint32_t size = plist_array_get_size(_node); | ||
| 74 | |||
| 75 | for (uint32_t i = 0; i < size; i++) | ||
| 76 | { | ||
| 77 | plist_t subnode = plist_array_get_item(_node, i); | ||
| 78 | plist_type subtype = plist_get_node_type(subnode); | ||
| 79 | switch(subtype) | ||
| 80 | { | ||
| 81 | case PLIST_DICT: | ||
| 82 | _array.push_back( new Dictionary(subnode) ); | ||
| 83 | break; | ||
| 84 | case PLIST_ARRAY: | ||
| 85 | _array.push_back( new Array(subnode) ); | ||
| 86 | break; | ||
| 87 | case PLIST_BOOLEAN: | ||
| 88 | case PLIST_UINT: | ||
| 89 | case PLIST_REAL: | ||
| 90 | case PLIST_STRING: | ||
| 91 | case PLIST_DATE: | ||
| 92 | case PLIST_DATA: | ||
| 93 | default: | ||
| 94 | _array.push_back( new Node(subnode) ); | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | Array& Array::operator=(const Array& a) | ||
| 101 | { | ||
| 102 | plist_free(_node); | ||
| 103 | for (int it = 0; it < _array.size(); it++) | ||
| 104 | { | ||
| 105 | delete _array.at(it); | ||
| 106 | } | ||
| 107 | _array.clear(); | ||
| 108 | |||
| 109 | _node = plist_copy(a.GetPlist()); | ||
| 110 | uint32_t size = plist_array_get_size(_node); | ||
| 111 | |||
| 112 | for (uint32_t i = 0; i < size; i++) | ||
| 113 | { | ||
| 114 | plist_t subnode = plist_array_get_item(_node, i); | ||
| 115 | plist_type subtype = plist_get_node_type(subnode); | ||
| 116 | switch(subtype) | ||
| 117 | { | ||
| 118 | case PLIST_DICT: | ||
| 119 | _array.push_back( new Dictionary(subnode) ); | ||
| 120 | break; | ||
| 121 | case PLIST_ARRAY: | ||
| 122 | _array.push_back( new Array(subnode) ); | ||
| 123 | break; | ||
| 124 | case PLIST_BOOLEAN: | ||
| 125 | case PLIST_UINT: | ||
| 126 | case PLIST_REAL: | ||
| 127 | case PLIST_STRING: | ||
| 128 | case PLIST_DATE: | ||
| 129 | case PLIST_DATA: | ||
| 130 | default: | ||
| 131 | _array.push_back( new Node(subnode) ); | ||
| 132 | break; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | Array::~Array() | ||
| 138 | { | ||
| 139 | plist_free(_node); | ||
| 140 | for (int it = 0; it < _array.size(); it++) | ||
| 141 | { | ||
| 142 | delete _array.at(it); | ||
| 143 | } | ||
| 144 | _array.clear(); | ||
| 145 | } | ||
| 146 | |||
| 147 | Node* Array::operator[](unsigned int index) | ||
| 148 | { | ||
| 149 | return _array.at(index); | ||
| 150 | } | ||
| 151 | |||
| 152 | void Array::Append(Node* node) | ||
| 153 | { | ||
| 154 | if (node) | ||
| 155 | { | ||
| 156 | plist_array_append_item(_node, node->GetPlist()); | ||
| 157 | _array.push_back(node); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void Array::Insert(Node* node, unsigned int pos) | ||
| 162 | { | ||
| 163 | if (node) | ||
| 164 | { | ||
| 165 | plist_array_insert_item(_node, node->GetPlist(), pos); | ||
| 166 | std::vector<Node*>::iterator it = _array.begin(); | ||
| 167 | it += pos; | ||
| 168 | _array.insert(it, node); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | void Array::Remove(Node* node) | ||
| 173 | { | ||
| 174 | if (node) | ||
| 175 | { | ||
| 176 | uint32_t pos = plist_array_get_item_index(node->GetPlist()); | ||
| 177 | plist_array_remove_item(_node, pos); | ||
| 178 | std::vector<Node*>::iterator it = _array.begin(); | ||
| 179 | it += pos; | ||
| 180 | _array.erase(it); | ||
| 181 | delete node; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | void Array::Remove(unsigned int pos) | ||
| 186 | { | ||
| 187 | plist_array_remove_item(_node, pos); | ||
| 188 | std::vector<Node*>::iterator it = _array.begin(); | ||
| 189 | it += pos; | ||
| 190 | delete _array.at(pos); | ||
| 191 | _array.erase(it); | ||
| 192 | } | ||
| 193 | |||
| 194 | }; | ||
