1 /*
2  * Node.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 <cstdlib>
22 #include <plist/Node.h>
23 #include <plist/Structure.h>
24 #include <plist/Dictionary.h>
25 #include <plist/Array.h>
26 #include <plist/Boolean.h>
27 #include <plist/Integer.h>
28 #include <plist/Real.h>
29 #include <plist/String.h>
30 #include <plist/Key.h>
31 #include <plist/Uid.h>
32 #include <plist/Data.h>
33 #include <plist/Date.h>
34 
35 namespace PList
36 {
37 
Node(Node * parent)38 Node::Node(Node* parent) : _parent(parent)
39 {
40 }
41 
Node(plist_t node,Node * parent)42 Node::Node(plist_t node, Node* parent) : _node(node), _parent(parent)
43 {
44 }
45 
Node(plist_type type,Node * parent)46 Node::Node(plist_type type, Node* parent) : _parent(parent)
47 {
48     _node = NULL;
49 
50     switch (type)
51     {
52     case PLIST_BOOLEAN:
53         _node = plist_new_bool(0);
54         break;
55     case PLIST_UINT:
56         _node = plist_new_uint(0);
57         break;
58     case PLIST_REAL:
59         _node = plist_new_real(0.);
60         break;
61     case PLIST_STRING:
62         _node = plist_new_string("");
63         break;
64     case PLIST_KEY:
65         _node = plist_new_string("");
66         plist_set_key_val(_node, "");
67         break;
68     case PLIST_UID:
69 	_node = plist_new_uid(0);
70 	break;
71     case PLIST_DATA:
72         _node = plist_new_data(NULL,0);
73         break;
74     case PLIST_DATE:
75         _node = plist_new_date(0,0);
76         break;
77     case PLIST_ARRAY:
78         _node = plist_new_array();
79         break;
80     case PLIST_DICT:
81         _node = plist_new_dict();
82         break;
83     case PLIST_NONE:
84     default:
85         break;
86     }
87 }
88 
~Node()89 Node::~Node()
90 {
91 	/* If the Node is in a container, let _node be cleaned up by
92 	 * operations on the parent plist_t. Otherwise, duplicate frees
93 	 * occur when a Node is removed from or replaced in a Dictionary.
94 	 */
95 	if (_parent == NULL)
96 		plist_free(_node);
97     _node = NULL;
98     _parent = NULL;
99 }
100 
GetType() const101 plist_type Node::GetType() const
102 {
103     if (_node)
104     {
105         return plist_get_node_type(_node);
106     }
107     return PLIST_NONE;
108 }
109 
GetPlist() const110 plist_t Node::GetPlist() const
111 {
112     return _node;
113 }
114 
GetParent() const115 Node* Node::GetParent() const
116 {
117     return _parent;
118 }
119 
FromPlist(plist_t node,Node * parent)120 Node* Node::FromPlist(plist_t node, Node* parent)
121 {
122     Node* ret = NULL;
123     if (node)
124     {
125         plist_type type = plist_get_node_type(node);
126         switch (type)
127         {
128         case PLIST_DICT:
129             ret = new Dictionary(node, parent);
130             break;
131         case PLIST_ARRAY:
132             ret = new Array(node, parent);
133             break;
134         case PLIST_BOOLEAN:
135             ret = new Boolean(node, parent);
136             break;
137         case PLIST_UINT:
138             ret = new Integer(node, parent);
139             break;
140         case PLIST_REAL:
141             ret = new Real(node, parent);
142             break;
143         case PLIST_STRING:
144             ret = new String(node, parent);
145             break;
146         case PLIST_KEY:
147             ret = new Key(node, parent);
148             break;
149         case PLIST_UID:
150             ret = new Uid(node, parent);
151             break;
152         case PLIST_DATE:
153             ret = new Date(node, parent);
154             break;
155         case PLIST_DATA:
156             ret = new Data(node, parent);
157             break;
158         default:
159             plist_free(node);
160             break;
161         }
162     }
163     return ret;
164 }
165 
166 }  // namespace PList
167