aboutsummaryrefslogtreecommitdiff
path: root/src/stable/tafgroup.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/tafgroup.cpp')
-rw-r--r--src/stable/tafgroup.cpp224
1 files changed, 224 insertions, 0 deletions
diff --git a/src/stable/tafgroup.cpp b/src/stable/tafgroup.cpp
new file mode 100644
index 0000000..ee180c3
--- /dev/null
+++ b/src/stable/tafgroup.cpp
@@ -0,0 +1,224 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/tafgroup.h"
9#include "bu/tafproperty.h"
10#include "bu/tafcomment.h"
11
12Bu::TafGroup::TafGroup( const TafGroup &rSrc ) :
13 TafNode( typeGroup ),
14 sName( rSrc.sName )
15{
16 for( NodeList::const_iterator i = rSrc.lChildren.begin(); i; i++ )
17 {
18 switch( (*i)->getType() )
19 {
20 case typeGroup:
21 addChild( new TafGroup( *dynamic_cast<const TafGroup *>(*i) ) );
22 break;
23
24 case typeProperty:
25 addChild( new TafProperty( *dynamic_cast<const TafProperty *>(*i) ) );
26 break;
27
28 case typeComment:
29 addChild( new TafComment( *dynamic_cast<const TafComment *>(*i) ) );
30 break;
31 }
32 }
33}
34
35Bu::TafGroup::TafGroup( const Bu::String &sName ) :
36 TafNode( typeGroup ),
37 sName( sName )
38{
39}
40
41Bu::TafGroup::~TafGroup()
42{
43 for( NodeList::iterator i = lChildren.begin(); i != lChildren.end(); i++ )
44 {
45 delete (*i);
46 }
47}
48
49const Bu::String &Bu::TafGroup::getName() const
50{
51 return sName;
52}
53
54void Bu::TafGroup::setName( const Bu::String &sName )
55{
56 this->sName = sName;
57}
58
59Bu::TafNode *Bu::TafGroup::addChild( Bu::TafNode *pNode )
60{
61 switch( pNode->getType() )
62 {
63 case typeGroup:
64 addChild( (Bu::TafGroup *)pNode );
65 break;
66
67 case typeProperty:
68 addChild( (Bu::TafProperty *)pNode );
69 break;
70
71 case typeComment:
72 addChild( (Bu::TafComment *)pNode );
73 break;
74 }
75
76 return pNode;
77}
78
79Bu::TafGroup *Bu::TafGroup::addChild( TafGroup *pNode )
80{
81 TafGroup *pGroup = (TafGroup *)pNode;
82 if( !hChildren.has( pGroup->getName() ) )
83 hChildren.insert( pGroup->getName(), GroupList() );
84 hChildren.get( pGroup->getName() ).append( pGroup );
85 lChildren.append( pNode );
86 return pNode;
87}
88
89Bu::TafProperty *Bu::TafGroup::addChild( TafProperty *pNode )
90{
91 TafProperty *pProperty = (TafProperty *)pNode;
92 if( !hProp.has( pProperty->getName() ) )
93 hProp.insert( pProperty->getName(), PropList() );
94 hProp.get( pProperty->getName() ).append( pProperty->getValue() );
95 lChildren.append( pNode );
96 return pNode;
97}
98
99Bu::TafComment *Bu::TafGroup::addChild( TafComment *pNode )
100{
101 lChildren.append( pNode );
102 return pNode;
103}
104
105Bu::TafGroup *Bu::TafGroup::addGroup( const Bu::String &sName )
106{
107 return addChild( new TafGroup( sName ) );
108}
109
110Bu::TafProperty *Bu::TafGroup::addProperty(
111 const Bu::String &sName, const Bu::String &sValue )
112{
113 return addChild( new TafProperty( sName, sValue ) );
114}
115
116bool Bu::TafGroup::hasChild( const Bu::String &sName ) const
117{
118 return hChildren.has( sName );
119}
120
121const Bu::TafGroup::GroupList &Bu::TafGroup::getChildren( const Bu::String &sName ) const
122{
123 try {
124 return hChildren.get( sName );
125 } catch( Bu::HashException &e )
126 {
127 throw Bu::TafException("No children of group \"%s\" match \"%s\".",
128 this->sName.getStr(), sName.getStr() );
129 }
130}
131
132const Bu::TafGroup::NodeList &Bu::TafGroup::getChildren() const
133{
134 return lChildren;
135}
136
137const Bu::TafGroup *Bu::TafGroup::getChild( const Bu::String &sName ) const
138{
139 try {
140 return hChildren.get( sName ).first();
141 } catch( Bu::HashException &e )
142 {
143 throw Bu::TafException("No children of group \"%s\" match \"%s\".",
144 this->sName.getStr(), sName.getStr() );
145 }
146}
147
148bool Bu::TafGroup::hasProperty( const Bu::String &sName ) const
149{
150 return hProp.has( sName );
151}
152
153const Bu::TafGroup::PropList &Bu::TafGroup::getProperties( const Bu::String &sName ) const
154{
155 try {
156 return hProp.get( sName );
157 } catch( Bu::HashException &e )
158 {
159 throw Bu::TafException("No properties of group \"%s\" match \"%s\".",
160 this->sName.getStr(), sName.getStr() );
161 }
162}
163
164const Bu::String &Bu::TafGroup::getProperty( const Bu::String &sName ) const
165{
166 try {
167 return hProp.get( sName ).first();
168 } catch( Bu::HashException &e )
169 {
170 throw Bu::TafException("No properties of group \"%s\" match \"%s\".",
171 this->sName.getStr(), sName.getStr() );
172 }
173}
174
175const Bu::String &Bu::TafGroup::getProperty( const Bu::String &sName,
176 const Bu::String &sDef ) const
177{
178 try
179 {
180 return hProp.get( sName ).first();
181 }
182 catch( Bu::HashException &e )
183 {
184 return sDef;
185 }
186}
187
188const Bu::TafGroup *Bu::TafGroup::getChildByPath(
189 const Bu::String &sPath ) const
190{
191 return getChildByPath( sPath.split('/') );
192}
193
194const Bu::TafGroup *Bu::TafGroup::getChildByPath( Bu::StrList lPath ) const
195{
196 const Bu::TafGroup *cur = this;
197
198 for( Bu::StrList::const_iterator i = lPath.begin(); i; i++ )
199 {
200 cur = cur->getChild( *i );
201 }
202
203 return cur;
204}
205
206const Bu::String &Bu::TafGroup::getByPath( const Bu::String &sPath ) const
207{
208 return getByPath( sPath.split('/') );
209}
210
211const Bu::String &Bu::TafGroup::getByPath( Bu::StrList lPath ) const
212{
213 const Bu::TafGroup *cur = this;
214
215 for( Bu::StrList::const_iterator i = lPath.begin(); i; i++ )
216 {
217 if( !(i+1) )
218 break;
219 cur = cur->getChild( *i );
220 }
221
222 return cur->getProperty( lPath.last() );
223}
224