Commit a922b714 authored by Jonathan Beck's avatar Jonathan Beck

Add C++ binding.

parent e492ef67
/*
* Array.h
* Array node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRING_H
#define STRING_H
#include <plist/Structure.h>
#include <vector>
namespace PList
{
class Array : public Structure
{
public :
Array();
Array(plist_t node);
Array(Array& a);
Array& operator=(const Array& a);
virtual ~Array();
Node* operator[](unsigned int index);
void Append(Node* node);
void Insert(Node* node, unsigned int pos);
void Remove(Node* node);
void Remove(unsigned int pos);
private :
std::vector<Node*> _array;
};
};
#endif // STRING_H
/*
* Boolean.h
* Boolean node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef BOOLEAN_H
#define BOOLEAN_H
#include <plist/Node.h>
namespace PList
{
class Boolean : public Node
{
public :
Boolean();
Boolean(bool b);
virtual ~Boolean();
void SetValue(bool b);
bool GetValue();
};
};
#endif // BOOLEAN_H
/*
* Data.h
* Data node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DATA_H
#define DATA_H
#include <plist/Node.h>
#include <vector>
namespace PList
{
class Data : public Node
{
public :
Data();
Data(std::vector<char>& buff);
virtual ~Data();
void SetValue(std::vector<char>& buff);
std::vector<char> GetValue();
};
};
#endif // DATA_H
/*
* Date.h
* Date node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DATE_H
#define DATE_H
#include <plist/Node.h>
namespace PList
{
class Date : public Node
{
public :
Date();
Date(uint64_t i);
virtual ~Date();
void SetValue(uint64_t i);
uint64_t GetValue();
};
};
#endif // DATE_H
/*
* Dictionary.h
* Dictionary node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <plist/Structure.h>
#include <map>
#include <string>
namespace PList
{
class Dictionary : public Structure
{
public :
Dictionary();
Dictionary(plist_t node);
Dictionary(Dictionary& d);
Dictionary& operator=(const Dictionary& d);
virtual ~Dictionary();
typedef std::map<std::string,Node*>::iterator iterator;
Node* operator[](std::string& key);
iterator Begin();
iterator End();
void Insert(std::string& key, Node* node);
void Remove(Node* node);
void Remove(std::string& key);
private :
std::map<std::string,Node*> _map;
};
};
#endif // DICTIONARY_H
/*
* Integer.h
* Integer node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef INTEGER_H
#define INTEGER_H
#include <plist/Node.h>
namespace PList
{
class Integer : public Node
{
public :
Integer();
Integer(uint64_t i);
virtual ~Integer();
void SetValue(uint64_t i);
uint64_t GetValue();
};
};
#endif // INTEGER_H
/*
* Node.h
* Abstract node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef NODE_H
#define NODE_H
#include <plist/plist.h>
namespace PList
{
class Node
{
public :
virtual ~Node();
Node(plist_t node);
Node(Node& node);
Node& operator=(const Node& node);
plist_type GetType();
plist_t GetPlist() const;
protected:
Node();
Node(plist_type type);
plist_t _node;
};
};
#endif // NODE_H
/*
* Real.h
* Real node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef REAL_H
#define REAL_H
#include <plist/Node.h>
namespace PList
{
class Real : public Node
{
public :
Real();
Real(double d);
virtual ~Real();
void SetValue(double d);
double GetValue();
};
};
#endif // REAL_H
/*
* String.h
* String node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRING_H
#define STRING_H
#include <plist/Node.h>
#include <string>
namespace PList
{
class String : public Node
{
public :
String();
String(std::string& s);
virtual ~String();
void SetValue(std::string& s);
std::string GetValue();
};
};
#endif // STRING_H
/*
* Structure.h
* Structure node type for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRUCTURE_H
#define STRUCTURE_H
#include <plist/Node.h>
#include <string>
#include <vector>
namespace PList
{
class Structure : public Node
{
public :
virtual ~Structure();
uint32_t GetSize();
std::string ToXml();
std::vector<char> ToBin();
protected:
Structure();
Structure(plist_type type);
private:
Structure(Structure& s);
Structure& operator=(const Structure& s);
};
};
#endif // STRUCTURE_H
/*
* Utils.h
* Import functions for C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef UTILS_H
#define UTILS_H
#include <plist/Structure.h>
#include <string>
namespace PList
{
class Utils
{
public:
static Structure* FromXml(std::string& in);
static Structure* FromBin(std::vector<char>& in);
private:
Utils();
~Utils();
};
};
#endif // UTILS_H
/*
* plist++.h
* Main include of libplist C++ binding
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBPLIST++_H
#define LIBPLIST++_H
#include "plist.h"
#include "Array.h"
#include "Boolean.h"
#include "Data.h"
#include "Date.h"
#include "Dictionary.h"
#include "Integer.h"
#include "Node.h"
#include "Real.h"
#include "String.h"
#include "Structure.h"
#include "Utils.h"
#endif
/*
* Array.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Array.h>
#include <plist/Dictionary.h>
namespace PList
{
Array::Array() : Structure(PLIST_ARRAY)
{
_array.clear();
}
Array::Array(plist_t node) : Structure()
{
_node = node;
uint32_t size = plist_array_get_size(_node);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_array.push_back( new Node(subnode) );
break;
}
}
}
Array::Array(Array& a)
{
plist_free(_node);
for (int it = 0; it < _array.size(); it++)
{
delete _array.at(it);
}
_array.clear();
_node = plist_copy(a.GetPlist());
uint32_t size = plist_array_get_size(_node);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_array.push_back( new Node(subnode) );
break;
}
}
}
Array& Array::operator=(const Array& a)
{
plist_free(_node);
for (int it = 0; it < _array.size(); it++)
{
delete _array.at(it);
}
_array.clear();
_node = plist_copy(a.GetPlist());
uint32_t size = plist_array_get_size(_node);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_array.push_back( new Node(subnode) );
break;
}
}
}
Array::~Array()
{
plist_free(_node);
for (int it = 0; it < _array.size(); it++)
{
delete _array.at(it);
}
_array.clear();
}
Node* Array::operator[](unsigned int index)
{
return _array.at(index);
}
void Array::Append(Node* node)
{
if (node)
{
plist_array_append_item(_node, node->GetPlist());
_array.push_back(node);
}
}
void Array::Insert(Node* node, unsigned int pos)
{
if (node)
{
plist_array_insert_item(_node, node->GetPlist(), pos);
std::vector<Node*>::iterator it = _array.begin();
it += pos;
_array.insert(it, node);
}
}
void Array::Remove(Node* node)
{
if (node)
{
uint32_t pos = plist_array_get_item_index(node->GetPlist());
plist_array_remove_item(_node, pos);
std::vector<Node*>::iterator it = _array.begin();
it += pos;
_array.erase(it);
delete node;
}
}
void Array::Remove(unsigned int pos)
{
plist_array_remove_item(_node, pos);
std::vector<Node*>::iterator it = _array.begin();
it += pos;
delete _array.at(pos);
_array.erase(it);
}
};
/*
* Boolean.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Boolean.h>
namespace PList
{
Boolean::Boolean() : Node(PLIST_BOOLEAN)
{
}
Boolean::Boolean(bool b) : Node(PLIST_BOOLEAN)
{
plist_set_bool_val(_node, b);
}
Boolean::~Boolean()
{
}
void Boolean::SetValue(bool b)
{
plist_set_bool_val(_node, b);
}
bool Boolean::GetValue()
{
uint8_t b = 0;
plist_get_bool_val(_node, &b);
return b;
}
};
......@@ -7,11 +7,28 @@ SET(libplist_SRC
bplist.c
xplist.c )
SET(libplist++_SRC
Node.cpp
Boolean.cpp
Integer.cpp
Real.cpp
String.cpp
Date.cpp
Data.cpp
Structure.cpp
Array.cpp
Dictionary.cpp
Utils.cpp
)
ADD_LIBRARY( plist SHARED ${libplist_SRC} )
TARGET_LINK_LIBRARIES( plist ${LIBXML2_LIBRARIES} ${GLIB2_LIBRARIES} )
SET_TARGET_PROPERTIES( plist PROPERTIES VERSION ${LIBPLIST_LIBVERSION} )
SET_TARGET_PROPERTIES( plist PROPERTIES SOVERSION ${LIBPLIST_SOVERSION} )
ADD_LIBRARY( plist++ SHARED ${libplist++_SRC} )
TARGET_LINK_LIBRARIES( plist++ plist )
INSTALL(TARGETS plist
RUNTIME DESTINATION bin COMPONENT lib
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT dev
......
/*
* Data.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Data.h>
namespace PList
{
Data::Data() : Node(PLIST_DATA)
{
}
Data::Data(std::vector<char>& buff) : Node(PLIST_DATA)
{
plist_set_data_val(_node, &buff[0], buff.size());
}
Data::~Data()
{
}
void Data::SetValue(std::vector<char>& buff)
{
plist_set_data_val(_node, &buff[0], buff.size());
}
std::vector<char> Data::GetValue()
{
char* buff = NULL;
uint64_t length = 0;
plist_get_data_val(_node, &buff, &length);
std::vector<char> ret(buff, buff + length);
free(buff);
return ret;
}
};
/*
* Date.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Date.h>
namespace PList
{
Date::Date() : Node(PLIST_DATE)
{
}
Date::Date(uint64_t i) : Node(PLIST_DATE)
{
plist_set_date_val(_node, i, 0);
}
Date::~Date()
{
}
void Date::SetValue(uint64_t i)
{
plist_set_date_val(_node, i, 0);
}
uint64_t Date::GetValue()
{
int32_t i = 0;
plist_get_date_val(_node, &i, &i);
return i;
}
};
/*
* Dictionary.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Dictionary.h>
#include <plist/Array.h>
namespace PList
{
Dictionary::Dictionary() : Structure(PLIST_DICT)
{
}
Dictionary::Dictionary(plist_t node) : Structure()
{
_node = node;
plist_dict_iter it = NULL;
char* key = NULL;
plist_t subnode = NULL;
plist_dict_new_iter(_node, &it);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_map[std::string(key)] = new Node(subnode);
break;
}
subnode = NULL;
free(key);
key = NULL;
plist_dict_next_item(_node, it, NULL, &subnode);
}
free(it);
}
Dictionary::Dictionary(Dictionary& d)
{
for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
{
plist_free(it->second->GetPlist());
delete it->second;
}
_map.clear();
_node = plist_copy(d.GetPlist());
plist_dict_iter it = NULL;
char* key = NULL;
plist_t subnode = NULL;
plist_dict_new_iter(_node, &it);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_map[std::string(key)] = new Node(subnode);
break;
}
subnode = NULL;
free(key);
key = NULL;
plist_dict_next_item(_node, it, NULL, &subnode);
}
free(it);
}
Dictionary& Dictionary::operator=(const Dictionary& d)
{
for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
{
plist_free(it->second->GetPlist());
delete it->second;
}
_map.clear();
_node = plist_copy(d.GetPlist());
plist_dict_iter it = NULL;
char* key = NULL;
plist_t subnode = NULL;
plist_dict_new_iter(_node, &it);
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:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
_map[std::string(key)] = new Node(subnode);
break;
}
subnode = NULL;
free(key);
key = NULL;
plist_dict_next_item(_node, it, NULL, &subnode);
}
free(it);
}
Dictionary::~Dictionary()
{
for (Dictionary::iterator it = _map.begin(); it != _map.end(); it++)
{
plist_free(it->second->GetPlist());
delete it->second;
}
_map.clear();
}
Node* Dictionary::operator[](std::string& key)
{
return _map[key];
}
Dictionary::iterator Dictionary::Begin()
{
return _map.begin();
}
Dictionary::iterator Dictionary::End()
{
return _map.end();
}
void Dictionary::Insert(std::string& key, Node* node)
{
if (node)
{
plist_dict_insert_item(_node, key.c_str(), node->GetPlist());
delete _map[key];
_map[key] = node;
}
}
void Dictionary::Remove(Node* node)
{
if (node)
{
char* key = NULL;
plist_dict_get_item_key(node->GetPlist(), &key);
plist_dict_remove_item(_node, key);
std::string skey = key;
free(key);
delete node;
}
}
void Dictionary::Remove(std::string& key)
{
plist_dict_remove_item(_node, key.c_str());
delete _map[key];
}
};
/*
* Integer.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Integer.h>
namespace PList
{
Integer::Integer() : Node(PLIST_UINT)
{
}
Integer::Integer(uint64_t i) : Node(PLIST_UINT)
{
plist_set_uint_val(_node, i);
}
Integer::~Integer()
{
}
void Integer::SetValue(uint64_t i)
{
plist_set_uint_val(_node, i);
}
uint64_t Integer::GetValue()
{
uint64_t i = 0;
plist_get_uint_val(_node, &i);
return i;
}
};
/*
* Node.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Node.h>
namespace PList
{
Node::Node()
{
}
Node::Node(plist_t node) : _node(node)
{
}
Node::Node(plist_type type)
{
_node = NULL;
switch(type) {
case PLIST_BOOLEAN:
_node = plist_new_bool(0);
break;
case PLIST_UINT:
_node = plist_new_uint(0);
break;
case PLIST_REAL:
_node = plist_new_real(0.);
break;
case PLIST_STRING:
_node = plist_new_string("");
break;
case PLIST_DATA:
_node = plist_new_data(NULL,0);
break;
case PLIST_DATE:
_node = plist_new_date(0,0);
break;
case PLIST_ARRAY:
_node = plist_new_array();
break;
case PLIST_DICT:
_node = plist_new_dict();
break;
case PLIST_KEY:
case PLIST_NONE:
default:
break;
}
}
Node::~Node()
{
plist_free(_node);
_node = NULL;
}
Node::Node(Node& node)
{
plist_free(_node);
_node = NULL;
_node = plist_copy(_node);
}
Node& Node::operator=(const Node& node)
{
plist_free(_node);
_node = NULL;
_node = plist_copy(_node);
}
plist_type Node::GetType()
{
if (_node)
{
return plist_get_node_type(_node);
}
}
plist_t Node::GetPlist() const
{
return _node;
}
};
/*
* Real.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Real.h>
namespace PList
{
Real::Real() : Node(PLIST_REAL)
{
}
Real::Real(double d) : Node(PLIST_REAL)
{
plist_set_real_val(_node, d);
}
Real::~Real()
{
}
void Real::SetValue(double d)
{
plist_set_real_val(_node, d);
}
double Real::GetValue()
{
double d = 0.;
plist_get_real_val(_node, &d);
return d;
}
};
/*
* String.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/String.h>
namespace PList
{
String::String() : Node(PLIST_STRING)
{
}
String::String(std::string& s) : Node(PLIST_STRING)
{
plist_set_string_val(_node, s.c_str());
}
String::~String()
{
}
void String::SetValue(std::string& s)
{
plist_set_string_val(_node, s.c_str());
}
std::string String::GetValue()
{
char* s = NULL;
plist_get_string_val(_node, &s);
std::string ret = s;
free(s);
return ret;
}
};
/*
* Structure.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Structure.h>
namespace PList
{
Structure::Structure() : Node()
{
}
Structure::Structure(plist_type type) : Node(type)
{
}
Structure::~Structure()
{
}
uint32_t Structure::GetSize()
{
uint32_t size = 0;
plist_type type = plist_get_node_type(_node);
if (type == PLIST_ARRAY)
{
size = plist_array_get_size(_node);
}
else if (type == PLIST_DICT)
{
size = plist_dict_get_size(_node);
}
return size;
}
std::string Structure::ToXml()
{
char* xml = NULL;
uint32_t length = 0;
plist_to_xml(_node, &xml, &length);
std::string ret(xml, xml+length);
free(xml);
return ret;
}
std::vector<char> Structure::ToBin()
{
char* bin = NULL;
uint32_t length = 0;
plist_to_bin(_node, &bin, &length);
std::vector<char> ret(bin, bin+length);
free(bin);
return ret;
}
};
/*
* Utils.cpp
*
* Copyright (c) 2009 Jonathan Beck All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <plist/Utils.h>
#include <plist/Dictionary.h>
#include <plist/Array.h>
namespace PList
{
static Structure* FromPlist(plist_t root)
{
Structure* ret = NULL;
if (root)
{
plist_type type = plist_get_node_type(root);
switch(type)
{
case PLIST_DICT:
ret = new Dictionary(root);
break;
case PLIST_ARRAY:
ret = new Array(root);
break;
case PLIST_BOOLEAN:
case PLIST_UINT:
case PLIST_REAL:
case PLIST_STRING:
case PLIST_DATE:
case PLIST_DATA:
default:
plist_free(root);
break;
}
}
return ret;
}
Structure* Utils::FromXml(std::string& in)
{
plist_t root = NULL;
plist_from_xml(in.c_str(), in.size(), &root);
return FromPlist(root);
}
Structure* Utils::FromBin(std::vector<char>& in)
{
plist_t root = NULL;
plist_from_bin(&in[0], in.size(), &root);
return FromPlist(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