Commit c1363bea authored by Jonathan Beck's avatar Jonathan Beck

Add Set/Get Parent and a helper to create a Node from a plist_t.

parent bef50c08
...@@ -31,8 +31,8 @@ namespace PList ...@@ -31,8 +31,8 @@ namespace PList
class Array : public Structure class Array : public Structure
{ {
public : public :
Array(); Array(Node* parent = NULL);
Array(plist_t node); Array(plist_t node, Node* parent = NULL);
Array(Array& a); Array(Array& a);
Array& operator=(Array& a); Array& operator=(Array& a);
virtual ~Array(); virtual ~Array();
......
...@@ -30,8 +30,8 @@ namespace PList ...@@ -30,8 +30,8 @@ namespace PList
class Boolean : public Node class Boolean : public Node
{ {
public : public :
Boolean(); Boolean(Node* parent = NULL);
Boolean(plist_t node); Boolean(plist_t node, Node* parent = NULL);
Boolean(Boolean& b); Boolean(Boolean& b);
Boolean& operator=(Boolean& b); Boolean& operator=(Boolean& b);
Boolean(bool b); Boolean(bool b);
......
...@@ -31,8 +31,8 @@ namespace PList ...@@ -31,8 +31,8 @@ namespace PList
class Data : public Node class Data : public Node
{ {
public : public :
Data(); Data(Node* parent = NULL);
Data(plist_t node); Data(plist_t node, Node* parent = NULL);
Data(Data& d); Data(Data& d);
Data& operator=(Data& d); Data& operator=(Data& d);
Data(const std::vector<char>& buff); Data(const std::vector<char>& buff);
......
...@@ -31,8 +31,8 @@ namespace PList ...@@ -31,8 +31,8 @@ namespace PList
class Date : public Node class Date : public Node
{ {
public : public :
Date(); Date(Node* parent = NULL);
Date(plist_t node); Date(plist_t node, Node* parent = NULL);
Date(Date& d); Date(Date& d);
Date& operator=(Date& d); Date& operator=(Date& d);
Date(timeval t); Date(timeval t);
......
...@@ -32,8 +32,8 @@ namespace PList ...@@ -32,8 +32,8 @@ namespace PList
class Dictionary : public Structure class Dictionary : public Structure
{ {
public : public :
Dictionary(); Dictionary(Node* parent = NULL);
Dictionary(plist_t node); Dictionary(plist_t node, Node* parent = NULL);
Dictionary(Dictionary& d); Dictionary(Dictionary& d);
Dictionary& operator=(Dictionary& d); Dictionary& operator=(Dictionary& d);
virtual ~Dictionary(); virtual ~Dictionary();
......
...@@ -30,8 +30,8 @@ namespace PList ...@@ -30,8 +30,8 @@ namespace PList
class Integer : public Node class Integer : public Node
{ {
public : public :
Integer(); Integer(Node* parent = NULL);
Integer(plist_t node); Integer(plist_t node, Node* parent = NULL);
Integer(Integer& i); Integer(Integer& i);
Integer& operator=(Integer& i); Integer& operator=(Integer& i);
Integer(uint64_t i); Integer(uint64_t i);
......
...@@ -33,15 +33,18 @@ class Node ...@@ -33,15 +33,18 @@ class Node
virtual ~Node(); virtual ~Node();
virtual Node* Clone() = 0; virtual Node* Clone() = 0;
Node * GetParent();
void SetParent(Node* parent);
plist_type GetType(); plist_type GetType();
plist_t GetPlist(); plist_t GetPlist();
protected: protected:
Node(); Node(Node* parent = NULL);
Node(plist_t node); Node(plist_t node, Node* parent = NULL);
Node(plist_type type); Node(plist_type type, Node* parent = NULL);
plist_t _node; plist_t _node;
Node* _parent;
}; };
}; };
......
...@@ -30,8 +30,8 @@ namespace PList ...@@ -30,8 +30,8 @@ namespace PList
class Real : public Node class Real : public Node
{ {
public : public :
Real(); Real(Node* parent = NULL);
Real(plist_t node); Real(plist_t node, Node* parent = NULL);
Real(Real& d); Real(Real& d);
Real& operator=(Real& d); Real& operator=(Real& d);
Real(double d); Real(double d);
......
...@@ -31,8 +31,8 @@ namespace PList ...@@ -31,8 +31,8 @@ namespace PList
class String : public Node class String : public Node
{ {
public : public :
String(); String(Node* parent = NULL);
String(plist_t node); String(plist_t node, Node* parent = NULL);
String(String& s); String(String& s);
String& operator=(String& s); String& operator=(String& s);
String(const std::string& s); String(const std::string& s);
......
...@@ -40,8 +40,8 @@ class Structure : public Node ...@@ -40,8 +40,8 @@ class Structure : public Node
std::vector<char> ToBin(); std::vector<char> ToBin();
protected: protected:
Structure(); Structure(Node* parent = NULL);
Structure(plist_type type); Structure(plist_type type, Node* parent = NULL);
private: private:
Structure(Structure& s); Structure(Structure& s);
......
...@@ -30,6 +30,7 @@ namespace PList ...@@ -30,6 +30,7 @@ namespace PList
class Utils class Utils
{ {
public: public:
static Node* FromPlist(plist_t node, Node* parent = NULL);
static Structure* FromXml(const std::string& in); static Structure* FromXml(const std::string& in);
static Structure* FromBin(const std::vector<char>& in); static Structure* FromBin(const std::vector<char>& in);
......
...@@ -20,23 +20,17 @@ ...@@ -20,23 +20,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <plist/Array.h> #include <plist/Array.h>
#include <plist/Dictionary.h> #include <plist/Utils.h>
#include <plist/Boolean.h>
#include <plist/Integer.h>
#include <plist/Real.h>
#include <plist/String.h>
#include <plist/Date.h>
#include <plist/Data.h>
namespace PList namespace PList
{ {
Array::Array() : Structure(PLIST_ARRAY) Array::Array(Node* parent) : Structure(PLIST_ARRAY, parent)
{ {
_array.clear(); _array.clear();
} }
Array::Array(plist_t node) : Structure() Array::Array(plist_t node, Node* parent) : Structure(parent)
{ {
_node = node; _node = node;
uint32_t size = plist_array_get_size(_node); uint32_t size = plist_array_get_size(_node);
...@@ -44,36 +38,7 @@ Array::Array(plist_t node) : Structure() ...@@ -44,36 +38,7 @@ Array::Array(plist_t node) : Structure()
for (uint32_t i = 0; i < size; i++) for (uint32_t i = 0; i < size; i++)
{ {
plist_t subnode = plist_array_get_item(_node, i); plist_t subnode = plist_array_get_item(_node, i);
plist_type subtype = plist_get_node_type(subnode); _array.push_back( Utils::FromPlist(subnode, this) );
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;
}
} }
} }
...@@ -86,36 +51,7 @@ Array::Array(PList::Array& a) : Structure() ...@@ -86,36 +51,7 @@ Array::Array(PList::Array& a) : Structure()
for (uint32_t i = 0; i < size; i++) for (uint32_t i = 0; i < size; i++)
{ {
plist_t subnode = plist_array_get_item(_node, i); plist_t subnode = plist_array_get_item(_node, i);
plist_type subtype = plist_get_node_type(subnode); _array.push_back( Utils::FromPlist(subnode, this) );
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;
}
} }
} }
...@@ -134,36 +70,7 @@ Array& Array::operator=(PList::Array& a) ...@@ -134,36 +70,7 @@ Array& Array::operator=(PList::Array& a)
for (uint32_t i = 0; i < size; i++) for (uint32_t i = 0; i < size; i++)
{ {
plist_t subnode = plist_array_get_item(_node, i); plist_t subnode = plist_array_get_item(_node, i);
plist_type subtype = plist_get_node_type(subnode); _array.push_back( Utils::FromPlist(subnode, this) );
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;
}
} }
} }
...@@ -191,6 +98,7 @@ void Array::Append(Node* node) ...@@ -191,6 +98,7 @@ void Array::Append(Node* node)
if (node) if (node)
{ {
Node* clone = node->Clone(); Node* clone = node->Clone();
clone->SetParent(this);
plist_array_append_item(_node, clone->GetPlist()); plist_array_append_item(_node, clone->GetPlist());
_array.push_back(clone); _array.push_back(clone);
} }
...@@ -201,6 +109,7 @@ void Array::Insert(Node* node, unsigned int pos) ...@@ -201,6 +109,7 @@ void Array::Insert(Node* node, unsigned int pos)
if (node) if (node)
{ {
Node* clone = node->Clone(); Node* clone = node->Clone();
clone->SetParent(this);
plist_array_insert_item(_node, clone->GetPlist(), pos); plist_array_insert_item(_node, clone->GetPlist(), pos);
std::vector<Node*>::iterator it = _array.begin(); std::vector<Node*>::iterator it = _array.begin();
it += pos; it += pos;
......
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -20,22 +20,16 @@ ...@@ -20,22 +20,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <plist/Dictionary.h> #include <plist/Dictionary.h>
#include <plist/Array.h> #include <plist/Utils.h>
#include <plist/Boolean.h>
#include <plist/Integer.h>
#include <plist/Real.h>
#include <plist/String.h>
#include <plist/Date.h>
#include <plist/Data.h>
namespace PList 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; _node = node;
plist_dict_iter it = NULL; plist_dict_iter it = NULL;
...@@ -46,36 +40,7 @@ Dictionary::Dictionary(plist_t node) : Structure() ...@@ -46,36 +40,7 @@ Dictionary::Dictionary(plist_t node) : Structure()
plist_dict_next_item(_node, it, &key, &subnode); plist_dict_next_item(_node, it, &key, &subnode);
while (subnode) while (subnode)
{ {
plist_type subtype = plist_get_node_type(subnode); _map[std::string(key)] = Utils::FromPlist(subnode, this);
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;
}
subnode = NULL; subnode = NULL;
free(key); free(key);
...@@ -103,36 +68,7 @@ Dictionary::Dictionary(PList::Dictionary& d) : Structure() ...@@ -103,36 +68,7 @@ Dictionary::Dictionary(PList::Dictionary& d) : Structure()
plist_dict_next_item(_node, it, &key, &subnode); plist_dict_next_item(_node, it, &key, &subnode);
while (subnode) while (subnode)
{ {
plist_type subtype = plist_get_node_type(subnode); _map[std::string(key)] = Utils::FromPlist(subnode, this);
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;
}
subnode = NULL; subnode = NULL;
free(key); free(key);
...@@ -160,36 +96,7 @@ Dictionary& Dictionary::operator=(PList::Dictionary& d) ...@@ -160,36 +96,7 @@ Dictionary& Dictionary::operator=(PList::Dictionary& d)
plist_dict_next_item(_node, it, &key, &subnode); plist_dict_next_item(_node, it, &key, &subnode);
while (subnode) while (subnode)
{ {
plist_type subtype = plist_get_node_type(subnode); _map[std::string(key)] = Utils::FromPlist(subnode, this);
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;
}
subnode = NULL; subnode = NULL;
free(key); free(key);
...@@ -239,6 +146,7 @@ Dictionary::iterator Dictionary::Insert(const std::string& key, Node* node) ...@@ -239,6 +146,7 @@ Dictionary::iterator Dictionary::Insert(const std::string& key, Node* node)
if (node) if (node)
{ {
Node* clone = node->Clone(); Node* clone = node->Clone();
clone->SetParent(this);
plist_dict_insert_item(_node, key.c_str(), clone->GetPlist()); plist_dict_insert_item(_node, key.c_str(), clone->GetPlist());
delete _map[key]; delete _map[key];
_map[key] = clone; _map[key] = clone;
......
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -24,15 +24,15 @@ ...@@ -24,15 +24,15 @@
namespace PList 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; _node = NULL;
...@@ -72,6 +72,7 @@ Node::~Node() ...@@ -72,6 +72,7 @@ Node::~Node()
{ {
plist_free(_node); plist_free(_node);
_node = NULL; _node = NULL;
_parent = NULL;
} }
plist_type Node::GetType() plist_type Node::GetType()
...@@ -86,4 +87,15 @@ plist_t Node::GetPlist() ...@@ -86,4 +87,15 @@ plist_t Node::GetPlist()
{ {
return _node; return _node;
} }
Node* Node::GetParent()
{
return _parent;
}
void Node::SetParent(Node* parent)
{
_parent = parent;
}
}; };
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
namespace PList 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)
{ {
} }
......
...@@ -24,10 +24,10 @@ ...@@ -24,10 +24,10 @@
namespace PList 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)
{ {
} }
......
...@@ -22,44 +22,79 @@ ...@@ -22,44 +22,79 @@
#include <plist/Utils.h> #include <plist/Utils.h>
#include <plist/Dictionary.h> #include <plist/Dictionary.h>
#include <plist/Array.h> #include <plist/Array.h>
#include <plist/Boolean.h>
#include <plist/Integer.h>
#include <plist/Real.h>
#include <plist/String.h>
#include <plist/Data.h>
#include <plist/Date.h>
namespace PList namespace PList
{ {
static Structure* FromPlist(plist_t root) Node* Utils::FromPlist(plist_t node, Node* parent)
{ {
Structure* ret = NULL; Node* ret = NULL;
if (root) if (node)
{ {
plist_type type = plist_get_node_type(root); plist_type type = plist_get_node_type(node);
switch(type) switch(type)
{ {
case PLIST_DICT: case PLIST_DICT:
ret = new Dictionary(root); ret = new Dictionary(node, parent);
break; break;
case PLIST_ARRAY: case PLIST_ARRAY:
ret = new Array(root); ret = new Array(node, parent);
break; break;
case PLIST_BOOLEAN: case PLIST_BOOLEAN:
ret = new Boolean(node, parent);
break;
case PLIST_UINT: case PLIST_UINT:
ret = new Integer(node, parent);
break;
case PLIST_REAL: case PLIST_REAL:
ret = new Real(node, parent);
break;
case PLIST_STRING: case PLIST_STRING:
ret = new String(node, parent);
break;
case PLIST_DATE: case PLIST_DATE:
ret = new Date(node, parent);
break;
case PLIST_DATA: case PLIST_DATA:
ret = new Data(node, parent);
break;
default: default:
plist_free(root); plist_free(node);
break; break;
} }
} }
return ret; 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<Structure*>(Utils::FromPlist(root));
}
else
{
plist_free(root);
}
return ret;
}
Structure* Utils::FromXml(const std::string& in) Structure* Utils::FromXml(const std::string& in)
{ {
plist_t root = NULL; plist_t root = NULL;
plist_from_xml(in.c_str(), in.size(), &root); plist_from_xml(in.c_str(), in.size(), &root);
return FromPlist(root); return ImportStruct(root);
} }
Structure* Utils::FromBin(const std::vector<char>& in) Structure* Utils::FromBin(const std::vector<char>& in)
...@@ -67,7 +102,7 @@ Structure* Utils::FromBin(const std::vector<char>& in) ...@@ -67,7 +102,7 @@ Structure* Utils::FromBin(const std::vector<char>& in)
plist_t root = NULL; plist_t root = NULL;
plist_from_bin(&in[0], in.size(), &root); plist_from_bin(&in[0], in.size(), &root);
return FromPlist(root); return ImportStruct(root);
} }
......
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