diff options
Diffstat (limited to 'src/tafgroup.cpp')
-rw-r--r-- | src/tafgroup.cpp | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/tafgroup.cpp b/src/tafgroup.cpp new file mode 100644 index 0000000..1837bd8 --- /dev/null +++ b/src/tafgroup.cpp | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2008 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 | |||
12 | Bu::TafGroup::TafGroup( const Bu::FString &sName ) : | ||
13 | TafNode( typeGroup ), | ||
14 | sName( sName ) | ||
15 | { | ||
16 | } | ||
17 | |||
18 | Bu::TafGroup::~TafGroup() | ||
19 | { | ||
20 | for( NodeList::iterator i = lChildren.begin(); i != lChildren.end(); i++ ) | ||
21 | { | ||
22 | delete (*i); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | const Bu::FString &Bu::TafGroup::getName() const | ||
27 | { | ||
28 | return sName; | ||
29 | } | ||
30 | |||
31 | void Bu::TafGroup::setName( const Bu::FString &sName ) | ||
32 | { | ||
33 | this->sName = sName; | ||
34 | } | ||
35 | |||
36 | Bu::TafNode *Bu::TafGroup::addChild( Bu::TafNode *pNode ) | ||
37 | { | ||
38 | switch( pNode->getType() ) | ||
39 | { | ||
40 | case typeGroup: | ||
41 | addChild( (Bu::TafGroup *)pNode ); | ||
42 | break; | ||
43 | |||
44 | case typeProperty: | ||
45 | addChild( (Bu::TafProperty *)pNode ); | ||
46 | break; | ||
47 | |||
48 | case typeComment: | ||
49 | addChild( (Bu::TafComment *)pNode ); | ||
50 | break; | ||
51 | } | ||
52 | |||
53 | return pNode; | ||
54 | } | ||
55 | |||
56 | Bu::TafGroup *Bu::TafGroup::addChild( TafGroup *pNode ) | ||
57 | { | ||
58 | TafGroup *pGroup = (TafGroup *)pNode; | ||
59 | if( !hChildren.has( pGroup->getName() ) ) | ||
60 | hChildren.insert( pGroup->getName(), GroupList() ); | ||
61 | hChildren.get( pGroup->getName() ).append( pGroup ); | ||
62 | lChildren.append( pNode ); | ||
63 | return pNode; | ||
64 | } | ||
65 | |||
66 | Bu::TafProperty *Bu::TafGroup::addChild( TafProperty *pNode ) | ||
67 | { | ||
68 | TafProperty *pProperty = (TafProperty *)pNode; | ||
69 | if( !hProp.has( pProperty->getName() ) ) | ||
70 | hProp.insert( pProperty->getName(), PropList() ); | ||
71 | hProp.get( pProperty->getName() ).append( pProperty->getValue() ); | ||
72 | lChildren.append( pNode ); | ||
73 | return pNode; | ||
74 | } | ||
75 | |||
76 | Bu::TafComment *Bu::TafGroup::addChild( TafComment *pNode ) | ||
77 | { | ||
78 | lChildren.append( pNode ); | ||
79 | return pNode; | ||
80 | } | ||
81 | |||
82 | Bu::TafGroup *Bu::TafGroup::addGroup( const Bu::FString &sName ) | ||
83 | { | ||
84 | return addChild( new TafGroup( sName ) ); | ||
85 | } | ||
86 | |||
87 | Bu::TafProperty *Bu::TafGroup::addProperty( | ||
88 | const Bu::FString &sName, const Bu::FString &sValue ) | ||
89 | { | ||
90 | return addChild( new TafProperty( sName, sValue ) ); | ||
91 | } | ||
92 | |||
93 | bool Bu::TafGroup::hasChild( const Bu::FString &sName ) const | ||
94 | { | ||
95 | return hChildren.has( sName ); | ||
96 | } | ||
97 | |||
98 | const Bu::TafGroup::GroupList &Bu::TafGroup::getChildren( const Bu::FString &sName ) const | ||
99 | { | ||
100 | try { | ||
101 | return hChildren.get( sName ); | ||
102 | } catch( Bu::HashException &e ) | ||
103 | { | ||
104 | throw Bu::TafException("No children of group \"%s\" match \"%s\".", | ||
105 | this->sName.getStr(), sName.getStr() ); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | const Bu::TafGroup::NodeList &Bu::TafGroup::getChildren() const | ||
110 | { | ||
111 | return lChildren; | ||
112 | } | ||
113 | |||
114 | const Bu::TafGroup *Bu::TafGroup::getChild( const Bu::FString &sName ) const | ||
115 | { | ||
116 | try { | ||
117 | return hChildren.get( sName ).first(); | ||
118 | } catch( Bu::HashException &e ) | ||
119 | { | ||
120 | throw Bu::TafException("No children of group \"%s\" match \"%s\".", | ||
121 | this->sName.getStr(), sName.getStr() ); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | bool Bu::TafGroup::hasProperty( const Bu::FString &sName ) const | ||
126 | { | ||
127 | return hProp.has( sName ); | ||
128 | } | ||
129 | |||
130 | const Bu::TafGroup::PropList &Bu::TafGroup::getProperties( const Bu::FString &sName ) const | ||
131 | { | ||
132 | try { | ||
133 | return hProp.get( sName ); | ||
134 | } catch( Bu::HashException &e ) | ||
135 | { | ||
136 | throw Bu::TafException("No properties of group \"%s\" match \"%s\".", | ||
137 | this->sName.getStr(), sName.getStr() ); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | const Bu::FString &Bu::TafGroup::getProperty( const Bu::FString &sName ) const | ||
142 | { | ||
143 | try { | ||
144 | return hProp.get( sName ).first(); | ||
145 | } catch( Bu::HashException &e ) | ||
146 | { | ||
147 | throw Bu::TafException("No properties of group \"%s\" match \"%s\".", | ||
148 | this->sName.getStr(), sName.getStr() ); | ||
149 | } | ||
150 | } | ||
151 | |||
152 | const Bu::FString &Bu::TafGroup::getProperty( const Bu::FString &sName, | ||
153 | const Bu::FString &sDef ) const | ||
154 | { | ||
155 | try | ||
156 | { | ||
157 | return hProp.get( sName ).first(); | ||
158 | } | ||
159 | catch( Bu::HashException &e ) | ||
160 | { | ||
161 | return sDef; | ||
162 | } | ||
163 | } | ||
164 | |||