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
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();
......
......@@ -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);
......
......@@ -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<char>& buff);
......
......@@ -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);
......
......@@ -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();
......
......@@ -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);
......
......@@ -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;
};
};
......
......@@ -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);
......
......@@ -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);
......
......@@ -40,8 +40,8 @@ class Structure : public Node
std::vector<char> ToBin();
protected:
Structure();
Structure(plist_type type);
Structure(Node* parent = NULL);
Structure(plist_type type, Node* parent = NULL);
private:
Structure(Structure& s);
......
......@@ -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<char>& in);
......
......@@ -20,23 +20,17 @@
#include <stdlib.h>
#include <plist/Array.h>
#include <plist/Dictionary.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>
#include <plist/Utils.h>
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<Node*>::iterator it = _array.begin();
it += pos;
......
......@@ -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)
{
}
......
......@@ -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)
{
}
......
......@@ -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)
{
}
......
......@@ -20,22 +20,16 @@
#include <stdlib.h>
#include <plist/Dictionary.h>
#include <plist/Array.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>
#include <plist/Utils.h>
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;
......
......@@ -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)
{
}
......
......@@ -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;
}
};
......@@ -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)
{
}
......
......@@ -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)
{
}
......
......@@ -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)
{
}
......
......@@ -22,44 +22,79 @@
#include <plist/Utils.h>
#include <plist/Dictionary.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
{
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<Structure*>(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<char>& in)
......@@ -67,7 +102,7 @@ Structure* Utils::FromBin(const std::vector<char>& in)
plist_t root = NULL;
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