From c1363bea107b15bdc10ce80671747be891661889 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 26 Oct 2009 18:41:15 +0100 Subject: Add Set/Get Parent and a helper to create a Node from a plist_t. --- include/plist/Array.h | 4 +- include/plist/Boolean.h | 4 +- include/plist/Data.h | 4 +- include/plist/Date.h | 4 +- include/plist/Dictionary.h | 4 +- include/plist/Integer.h | 4 +- include/plist/Node.h | 9 ++-- include/plist/Real.h | 4 +- include/plist/String.h | 4 +- include/plist/Structure.h | 4 +- include/plist/Utils.h | 1 + src/Array.cpp | 107 ++++----------------------------------------- src/Boolean.cpp | 4 +- src/Data.cpp | 4 +- src/Date.cpp | 4 +- src/Dictionary.cpp | 106 +++----------------------------------------- src/Integer.cpp | 4 +- src/Node.cpp | 18 ++++++-- src/Real.cpp | 4 +- src/String.cpp | 4 +- src/Structure.cpp | 4 +- src/Utils.cpp | 53 ++++++++++++++++++---- 22 files changed, 113 insertions(+), 245 deletions(-) diff --git a/include/plist/Array.h b/include/plist/Array.h index de4860e..8fd07cd 100644 --- a/include/plist/Array.h +++ b/include/plist/Array.h @@ -31,8 +31,8 @@ namespace PList class Array : public Structure { public : - Array(); - Array(plist_t node); + Array(Node* parent = NULL); + Array(plist_t node, Node* parent = NULL); Array(Array& a); Array& operator=(Array& a); virtual ~Array(); diff --git a/include/plist/Boolean.h b/include/plist/Boolean.h index b902171..149f8da 100644 --- a/include/plist/Boolean.h +++ b/include/plist/Boolean.h @@ -30,8 +30,8 @@ namespace PList class Boolean : public Node { public : - Boolean(); - Boolean(plist_t node); + Boolean(Node* parent = NULL); + Boolean(plist_t node, Node* parent = NULL); Boolean(Boolean& b); Boolean& operator=(Boolean& b); Boolean(bool b); diff --git a/include/plist/Data.h b/include/plist/Data.h index 3db98f7..59a0096 100644 --- a/include/plist/Data.h +++ b/include/plist/Data.h @@ -31,8 +31,8 @@ namespace PList class Data : public Node { public : - Data(); - Data(plist_t node); + Data(Node* parent = NULL); + Data(plist_t node, Node* parent = NULL); Data(Data& d); Data& operator=(Data& d); Data(const std::vector& buff); diff --git a/include/plist/Date.h b/include/plist/Date.h index d3cd605..22565ef 100644 --- a/include/plist/Date.h +++ b/include/plist/Date.h @@ -31,8 +31,8 @@ namespace PList class Date : public Node { public : - Date(); - Date(plist_t node); + Date(Node* parent = NULL); + Date(plist_t node, Node* parent = NULL); Date(Date& d); Date& operator=(Date& d); Date(timeval t); diff --git a/include/plist/Dictionary.h b/include/plist/Dictionary.h index dbb27d1..38604c8 100644 --- a/include/plist/Dictionary.h +++ b/include/plist/Dictionary.h @@ -32,8 +32,8 @@ namespace PList class Dictionary : public Structure { public : - Dictionary(); - Dictionary(plist_t node); + Dictionary(Node* parent = NULL); + Dictionary(plist_t node, Node* parent = NULL); Dictionary(Dictionary& d); Dictionary& operator=(Dictionary& d); virtual ~Dictionary(); diff --git a/include/plist/Integer.h b/include/plist/Integer.h index a86d0ca..fefcea1 100644 --- a/include/plist/Integer.h +++ b/include/plist/Integer.h @@ -30,8 +30,8 @@ namespace PList class Integer : public Node { public : - Integer(); - Integer(plist_t node); + Integer(Node* parent = NULL); + Integer(plist_t node, Node* parent = NULL); Integer(Integer& i); Integer& operator=(Integer& i); Integer(uint64_t i); diff --git a/include/plist/Node.h b/include/plist/Node.h index 702d018..6e5411a 100644 --- a/include/plist/Node.h +++ b/include/plist/Node.h @@ -33,15 +33,18 @@ class Node virtual ~Node(); virtual Node* Clone() = 0; + Node * GetParent(); + void SetParent(Node* parent); plist_type GetType(); plist_t GetPlist(); protected: - Node(); - Node(plist_t node); - Node(plist_type type); + Node(Node* parent = NULL); + Node(plist_t node, Node* parent = NULL); + Node(plist_type type, Node* parent = NULL); plist_t _node; + Node* _parent; }; }; diff --git a/include/plist/Real.h b/include/plist/Real.h index b011c7a..755842e 100644 --- a/include/plist/Real.h +++ b/include/plist/Real.h @@ -30,8 +30,8 @@ namespace PList class Real : public Node { public : - Real(); - Real(plist_t node); + Real(Node* parent = NULL); + Real(plist_t node, Node* parent = NULL); Real(Real& d); Real& operator=(Real& d); Real(double d); diff --git a/include/plist/String.h b/include/plist/String.h index 64181f3..58b8619 100644 --- a/include/plist/String.h +++ b/include/plist/String.h @@ -31,8 +31,8 @@ namespace PList class String : public Node { public : - String(); - String(plist_t node); + String(Node* parent = NULL); + String(plist_t node, Node* parent = NULL); String(String& s); String& operator=(String& s); String(const std::string& s); diff --git a/include/plist/Structure.h b/include/plist/Structure.h index 4910439..6f100cc 100644 --- a/include/plist/Structure.h +++ b/include/plist/Structure.h @@ -40,8 +40,8 @@ class Structure : public Node std::vector ToBin(); protected: - Structure(); - Structure(plist_type type); + Structure(Node* parent = NULL); + Structure(plist_type type, Node* parent = NULL); private: Structure(Structure& s); diff --git a/include/plist/Utils.h b/include/plist/Utils.h index 54baf02..65bec7e 100644 --- a/include/plist/Utils.h +++ b/include/plist/Utils.h @@ -30,6 +30,7 @@ namespace PList class Utils { public: + static Node* FromPlist(plist_t node, Node* parent = NULL); static Structure* FromXml(const std::string& in); static Structure* FromBin(const std::vector& in); diff --git a/src/Array.cpp b/src/Array.cpp index b85a114..7c8272c 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -20,23 +20,17 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include namespace PList { -Array::Array() : Structure(PLIST_ARRAY) +Array::Array(Node* parent) : Structure(PLIST_ARRAY, parent) { _array.clear(); } -Array::Array(plist_t node) : Structure() +Array::Array(plist_t node, Node* parent) : Structure(parent) { _node = node; uint32_t size = plist_array_get_size(_node); @@ -44,36 +38,7 @@ Array::Array(plist_t node) : Structure() for (uint32_t i = 0; i < size; i++) { plist_t subnode = plist_array_get_item(_node, i); - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _array.push_back( new Dictionary(subnode) ); - break; - case PLIST_ARRAY: - _array.push_back( new Array(subnode) ); - break; - case PLIST_BOOLEAN: - _array.push_back( new Boolean(subnode) ); - break; - case PLIST_UINT: - _array.push_back( new Integer(subnode) ); - break; - case PLIST_REAL: - _array.push_back( new Real(subnode) ); - break; - case PLIST_STRING: - _array.push_back( new String(subnode) ); - break; - case PLIST_DATE: - _array.push_back( new Date(subnode) ); - break; - case PLIST_DATA: - _array.push_back( new Data(subnode) ); - break; - default: - break; - } + _array.push_back( Utils::FromPlist(subnode, this) ); } } @@ -86,36 +51,7 @@ Array::Array(PList::Array& a) : Structure() for (uint32_t i = 0; i < size; i++) { plist_t subnode = plist_array_get_item(_node, i); - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _array.push_back( new Dictionary(subnode) ); - break; - case PLIST_ARRAY: - _array.push_back( new Array(subnode) ); - break; - case PLIST_BOOLEAN: - _array.push_back( new Boolean(subnode) ); - break; - case PLIST_UINT: - _array.push_back( new Integer(subnode) ); - break; - case PLIST_REAL: - _array.push_back( new Real(subnode) ); - break; - case PLIST_STRING: - _array.push_back( new String(subnode) ); - break; - case PLIST_DATE: - _array.push_back( new Date(subnode) ); - break; - case PLIST_DATA: - _array.push_back( new Data(subnode) ); - break; - default: - break; - } + _array.push_back( Utils::FromPlist(subnode, this) ); } } @@ -134,36 +70,7 @@ Array& Array::operator=(PList::Array& a) for (uint32_t i = 0; i < size; i++) { plist_t subnode = plist_array_get_item(_node, i); - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _array.push_back( new Dictionary(subnode) ); - break; - case PLIST_ARRAY: - _array.push_back( new Array(subnode) ); - break; - case PLIST_BOOLEAN: - _array.push_back( new Boolean(subnode) ); - break; - case PLIST_UINT: - _array.push_back( new Integer(subnode) ); - break; - case PLIST_REAL: - _array.push_back( new Real(subnode) ); - break; - case PLIST_STRING: - _array.push_back( new String(subnode) ); - break; - case PLIST_DATE: - _array.push_back( new Date(subnode) ); - break; - case PLIST_DATA: - _array.push_back( new Data(subnode) ); - break; - default: - break; - } + _array.push_back( Utils::FromPlist(subnode, this) ); } } @@ -191,6 +98,7 @@ void Array::Append(Node* node) if (node) { Node* clone = node->Clone(); + clone->SetParent(this); plist_array_append_item(_node, clone->GetPlist()); _array.push_back(clone); } @@ -201,6 +109,7 @@ void Array::Insert(Node* node, unsigned int pos) if (node) { Node* clone = node->Clone(); + clone->SetParent(this); plist_array_insert_item(_node, clone->GetPlist(), pos); std::vector::iterator it = _array.begin(); it += pos; diff --git a/src/Boolean.cpp b/src/Boolean.cpp index 03d17c8..3b20f45 100644 --- a/src/Boolean.cpp +++ b/src/Boolean.cpp @@ -24,11 +24,11 @@ namespace PList { -Boolean::Boolean() : Node(PLIST_BOOLEAN) +Boolean::Boolean(Node* parent) : Node(PLIST_BOOLEAN, parent) { } -Boolean::Boolean(plist_t node) : Node(node) +Boolean::Boolean(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/Data.cpp b/src/Data.cpp index 87a508a..a171c03 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -24,11 +24,11 @@ namespace PList { -Data::Data() : Node(PLIST_DATA) +Data::Data(Node* parent) : Node(PLIST_DATA, parent) { } -Data::Data(plist_t node) : Node(node) +Data::Data(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/Date.cpp b/src/Date.cpp index 1aebfab..6321c84 100644 --- a/src/Date.cpp +++ b/src/Date.cpp @@ -24,11 +24,11 @@ namespace PList { -Date::Date() : Node(PLIST_DATE) +Date::Date(Node* parent) : Node(PLIST_DATE, parent) { } -Date::Date(plist_t node) : Node(node) +Date::Date(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/Dictionary.cpp b/src/Dictionary.cpp index a9f85ea..2c56c89 100644 --- a/src/Dictionary.cpp +++ b/src/Dictionary.cpp @@ -20,22 +20,16 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include namespace PList { -Dictionary::Dictionary() : Structure(PLIST_DICT) +Dictionary::Dictionary(Node* parent) : Structure(PLIST_DICT, parent) { } -Dictionary::Dictionary(plist_t node) : Structure() +Dictionary::Dictionary(plist_t node, Node* parent) : Structure(parent) { _node = node; plist_dict_iter it = NULL; @@ -46,36 +40,7 @@ Dictionary::Dictionary(plist_t node) : Structure() plist_dict_next_item(_node, it, &key, &subnode); while (subnode) { - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _map[std::string(key)] = new Dictionary(subnode); - break; - case PLIST_ARRAY: - _map[std::string(key)] = new Array(subnode); - break; - case PLIST_BOOLEAN: - _map[std::string(key)] = new Boolean(subnode); - break; - case PLIST_UINT: - _map[std::string(key)] = new Integer(subnode); - break; - case PLIST_REAL: - _map[std::string(key)] = new Real(subnode); - break; - case PLIST_STRING: - _map[std::string(key)] = new String(subnode); - break; - case PLIST_DATE: - _map[std::string(key)] = new Date(subnode); - break; - case PLIST_DATA: - _map[std::string(key)] = new Data(subnode); - break; - default: - break; - } + _map[std::string(key)] = Utils::FromPlist(subnode, this); subnode = NULL; free(key); @@ -103,36 +68,7 @@ Dictionary::Dictionary(PList::Dictionary& d) : Structure() plist_dict_next_item(_node, it, &key, &subnode); while (subnode) { - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _map[std::string(key)] = new Dictionary(subnode); - break; - case PLIST_ARRAY: - _map[std::string(key)] = new Array(subnode); - break; - case PLIST_BOOLEAN: - _map[std::string(key)] = new Boolean(subnode); - break; - case PLIST_UINT: - _map[std::string(key)] = new Integer(subnode); - break; - case PLIST_REAL: - _map[std::string(key)] = new Real(subnode); - break; - case PLIST_STRING: - _map[std::string(key)] = new String(subnode); - break; - case PLIST_DATE: - _map[std::string(key)] = new Date(subnode); - break; - case PLIST_DATA: - _map[std::string(key)] = new Data(subnode); - break; - default: - break; - } + _map[std::string(key)] = Utils::FromPlist(subnode, this); subnode = NULL; free(key); @@ -160,36 +96,7 @@ Dictionary& Dictionary::operator=(PList::Dictionary& d) plist_dict_next_item(_node, it, &key, &subnode); while (subnode) { - plist_type subtype = plist_get_node_type(subnode); - switch(subtype) - { - case PLIST_DICT: - _map[std::string(key)] = new Dictionary(subnode); - break; - case PLIST_ARRAY: - _map[std::string(key)] = new Array(subnode); - break; - case PLIST_BOOLEAN: - _map[std::string(key)] = new Boolean(subnode); - break; - case PLIST_UINT: - _map[std::string(key)] = new Integer(subnode); - break; - case PLIST_REAL: - _map[std::string(key)] = new Real(subnode); - break; - case PLIST_STRING: - _map[std::string(key)] = new String(subnode); - break; - case PLIST_DATE: - _map[std::string(key)] = new Date(subnode); - break; - case PLIST_DATA: - _map[std::string(key)] = new Data(subnode); - break; - default: - break; - } + _map[std::string(key)] = Utils::FromPlist(subnode, this); subnode = NULL; free(key); @@ -239,6 +146,7 @@ Dictionary::iterator Dictionary::Insert(const std::string& key, Node* node) if (node) { Node* clone = node->Clone(); + clone->SetParent(this); plist_dict_insert_item(_node, key.c_str(), clone->GetPlist()); delete _map[key]; _map[key] = clone; diff --git a/src/Integer.cpp b/src/Integer.cpp index cf1ad33..9cf6619 100644 --- a/src/Integer.cpp +++ b/src/Integer.cpp @@ -24,11 +24,11 @@ namespace PList { -Integer::Integer() : Node(PLIST_UINT) +Integer::Integer(Node* parent) : Node(PLIST_UINT, parent) { } -Integer::Integer(plist_t node) : Node(node) +Integer::Integer(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/Node.cpp b/src/Node.cpp index ace1990..4497bb1 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -24,15 +24,15 @@ namespace PList { -Node::Node() +Node::Node(Node* parent) : _parent(parent) { } -Node::Node(plist_t node) : _node(node) +Node::Node(plist_t node, Node* parent) : _node(node), _parent(parent) { } -Node::Node(plist_type type) +Node::Node(plist_type type, Node* parent) : _parent(parent) { _node = NULL; @@ -72,6 +72,7 @@ Node::~Node() { plist_free(_node); _node = NULL; + _parent = NULL; } plist_type Node::GetType() @@ -86,4 +87,15 @@ plist_t Node::GetPlist() { return _node; } + +Node* Node::GetParent() +{ + return _parent; +} + +void Node::SetParent(Node* parent) +{ + _parent = parent; +} + }; diff --git a/src/Real.cpp b/src/Real.cpp index e527f6d..a877b40 100644 --- a/src/Real.cpp +++ b/src/Real.cpp @@ -24,11 +24,11 @@ namespace PList { -Real::Real() : Node(PLIST_REAL) +Real::Real(Node* parent) : Node(PLIST_REAL, parent) { } -Real::Real(plist_t node) : Node(node) +Real::Real(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/String.cpp b/src/String.cpp index dbc81ac..e6cceaa 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -24,11 +24,11 @@ namespace PList { -String::String() : Node(PLIST_STRING) +String::String(Node* parent) : Node(PLIST_STRING, parent) { } -String::String(plist_t node) : Node(node) +String::String(plist_t node, Node* parent) : Node(node, parent) { } diff --git a/src/Structure.cpp b/src/Structure.cpp index 6fd9b3d..5c7dc9a 100644 --- a/src/Structure.cpp +++ b/src/Structure.cpp @@ -24,10 +24,10 @@ namespace PList { -Structure::Structure() : Node() +Structure::Structure(Node* parent) : Node(parent) { } -Structure::Structure(plist_type type) : Node(type) +Structure::Structure(plist_type type, Node* parent) : Node(type, parent) { } diff --git a/src/Utils.cpp b/src/Utils.cpp index 4e47994..a88b2ba 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -22,44 +22,79 @@ #include #include #include +#include +#include +#include +#include +#include +#include namespace PList { -static Structure* FromPlist(plist_t root) +Node* Utils::FromPlist(plist_t node, Node* parent) { - Structure* ret = NULL; - if (root) + Node* ret = NULL; + if (node) { - plist_type type = plist_get_node_type(root); + plist_type type = plist_get_node_type(node); switch(type) { case PLIST_DICT: - ret = new Dictionary(root); + ret = new Dictionary(node, parent); break; case PLIST_ARRAY: - ret = new Array(root); + ret = new Array(node, parent); break; case PLIST_BOOLEAN: + ret = new Boolean(node, parent); + break; case PLIST_UINT: + ret = new Integer(node, parent); + break; case PLIST_REAL: + ret = new Real(node, parent); + break; case PLIST_STRING: + ret = new String(node, parent); + break; case PLIST_DATE: + ret = new Date(node, parent); + break; case PLIST_DATA: + ret = new Data(node, parent); + break; default: - plist_free(root); + plist_free(node); break; } } return ret; } +static Structure* ImportStruct(plist_t root) +{ + Structure* ret = NULL; + plist_type type = plist_get_node_type(root); + + if (PLIST_ARRAY == type || PLIST_DICT == type) + { + ret = static_cast(Utils::FromPlist(root)); + } + else + { + plist_free(root); + } + + return ret; +} + Structure* Utils::FromXml(const std::string& in) { plist_t root = NULL; plist_from_xml(in.c_str(), in.size(), &root); - return FromPlist(root); + return ImportStruct(root); } Structure* Utils::FromBin(const std::vector& in) @@ -67,7 +102,7 @@ Structure* Utils::FromBin(const std::vector& in) plist_t root = NULL; plist_from_bin(&in[0], in.size(), &root); - return FromPlist(root); + return ImportStruct(root); } -- cgit v1.1-32-gdbae