Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/tinyxml/ticpp.cc @ 1505

Last change on this file since 1505 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 24.1 KB
Line 
1#define TIXML_USE_TICPP
2
3/*
4http://code.google.com/p/ticpp/
5Copyright (c) 2006 Ryan Pusztai, Ryan Mulder
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#ifdef TIXML_USE_TICPP
26
27#include "ticpp.h"
28#include "ticpprc.h"
29#include "tinyxml.h"
30#include <sstream>
31
32using namespace ticpp;
33
34// In the following Visitor functions, casting away const should be safe, as the object can only be referred to by a const &
35bool Visitor::VisitEnter( const TiXmlDocument& doc )
36{
37        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
38}
39
40bool Visitor::VisitExit( const TiXmlDocument& doc )
41{
42        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
43}
44
45bool Visitor::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
46{
47        if ( 0 != firstAttribute )
48        {
49                Attribute attribute( const_cast< TiXmlAttribute* >( firstAttribute ) );
50                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), &attribute );
51        }
52        else
53        {
54                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), 0 );
55        }
56}
57
58bool Visitor::VisitExit( const TiXmlElement& element )
59{
60        return VisitExit( Element( const_cast< TiXmlElement* >( &element ) ) );
61}
62
63bool Visitor::Visit( const TiXmlDeclaration& declaration )
64{
65        return Visit( Declaration( const_cast< TiXmlDeclaration* >( &declaration ) ) );
66}
67
68bool Visitor::Visit( const TiXmlStylesheetReference& stylesheet )
69{
70        return Visit( StylesheetReference( const_cast< TiXmlStylesheetReference* >( &stylesheet ) ) );
71}
72
73bool Visitor::Visit( const TiXmlText& text )
74{
75        return Visit( Text( const_cast< TiXmlText* >( &text ) ) );
76}
77
78bool Visitor::Visit( const TiXmlComment& comment )
79{
80        return Visit( Comment( const_cast< TiXmlComment* >( &comment ) ) );
81}
82
83Attribute::Attribute()
84{
85        SetTiXmlPointer( new TiXmlAttribute() );
86        m_impRC->InitRef();
87}
88
89Attribute::Attribute( TiXmlAttribute* attribute )
90{
91        SetTiXmlPointer( attribute );
92        m_impRC->IncRef();
93}
94
95Attribute::Attribute( const std::string& name, const std::string& value )
96{
97        SetTiXmlPointer( new TiXmlAttribute( name, value ) );
98        m_impRC->InitRef();
99}
100
101void Attribute::operator=( const Attribute& copy )
102{
103        // Dropping the reference to the old object
104        this->m_impRC->DecRef();
105
106        // Pointing to the new Object
107        SetTiXmlPointer( copy.m_tiXmlPointer );
108
109        // The internal tixml pointer changed in the above line
110        this->m_impRC->IncRef();
111}
112
113Attribute::Attribute( const Attribute& copy ) : Base()
114{
115        // Dropping the reference to the old object
116        this->m_impRC->DecRef();
117
118        // Pointing to the new Object
119        SetTiXmlPointer( copy.m_tiXmlPointer );
120
121        // The internal tixml pointer changed in the above line
122        this->m_impRC->IncRef();
123}
124
125Attribute::~Attribute()
126{
127        m_impRC->DecRef();
128}
129
130std::string Attribute::Value() const
131{
132        ValidatePointer();
133        return m_tiXmlPointer->ValueStr();
134}
135
136std::string Attribute::Name() const
137{
138        ValidatePointer();
139        return m_tiXmlPointer->Name();
140}
141
142Attribute* Attribute::Next( bool throwIfNoAttribute ) const
143{
144        ValidatePointer();
145        TiXmlAttribute* attribute = m_tiXmlPointer->Next();
146        if ( 0 == attribute )
147        {
148                if ( throwIfNoAttribute )
149                {
150                        TICPPTHROW( "No more attributes found" )
151                }
152                else
153                {
154                        return 0;
155                }
156        }
157
158        Attribute* temp = new Attribute( attribute );
159        m_spawnedWrappers.push_back( temp );
160
161        return temp;
162}
163
164Attribute* Attribute::Previous( bool throwIfNoAttribute ) const
165{
166        ValidatePointer();
167        TiXmlAttribute* attribute = m_tiXmlPointer->Previous();
168        if ( 0 == attribute )
169        {
170                if ( throwIfNoAttribute )
171                {
172                        TICPPTHROW( "No more attributes found" )
173                }
174                else
175                {
176                        return 0;
177                }
178        }
179
180        Attribute* temp = new Attribute( attribute );
181        m_spawnedWrappers.push_back( temp );
182
183        return temp;
184}
185
186void Attribute::IterateNext( const std::string&, Attribute** next ) const
187{
188        *next = Next( false );
189}
190
191void Attribute::IteratePrevious( const std::string&, Attribute** previous ) const
192{
193        *previous = Previous( false );
194}
195
196void Attribute::Print( FILE* file, int depth ) const
197{
198        ValidatePointer();
199        m_tiXmlPointer->Print( file, depth );
200}
201
202void Attribute::SetTiXmlPointer( TiXmlAttribute* newPointer )
203{
204        m_tiXmlPointer = newPointer;
205        SetImpRC( newPointer );
206}
207
208//*****************************************************************************
209
210Node* Node::NodeFactory( TiXmlNode* tiXmlNode, bool throwIfNull, bool rememberSpawnedWrapper ) const
211{
212        if ( 0 == tiXmlNode )
213        {
214                if ( throwIfNull )
215                {
216                        TICPPTHROW( "tiXmlNode is NULL" )
217                }
218                else
219                {
220                        return 0;
221                }
222        }
223
224        Node* temp;
225        switch ( tiXmlNode->Type() )
226        {
227                case TiXmlNode::DOCUMENT:
228                        temp = new Document( tiXmlNode->ToDocument() );
229                        break;
230
231                case TiXmlNode::ELEMENT:
232                        temp = new Element( tiXmlNode->ToElement() );
233                        break;
234
235                case TiXmlNode::COMMENT:
236                        temp = new Comment( tiXmlNode->ToComment() );
237                        break;
238
239                case TiXmlNode::TEXT:
240                        temp = new Text( tiXmlNode->ToText() );
241                        break;
242
243                case TiXmlNode::DECLARATION:
244                        temp = new Declaration( tiXmlNode->ToDeclaration() );
245                        break;
246
247                case TiXmlNode::STYLESHEETREFERENCE:
248                        temp = new StylesheetReference( tiXmlNode->ToStylesheetReference() );
249                        break;
250
251                default:
252                        TICPPTHROW( "Type is unsupported" )
253        }
254
255        if ( rememberSpawnedWrapper )
256        {
257                m_spawnedWrappers.push_back( temp );
258        }
259        return temp;
260}
261
262
263std::string Node::Value() const
264{
265        return GetTiXmlPointer()->ValueStr();
266}
267
268void Node::Clear()
269{
270        GetTiXmlPointer()->Clear();
271}
272
273Node* Node::Parent( bool throwIfNoParent ) const
274{
275        TiXmlNode* parent = GetTiXmlPointer()->Parent();
276        if ( ( 0 == parent ) && throwIfNoParent )
277        {
278                TICPPTHROW( "No parent exists" );
279        }
280
281        return NodeFactory( parent, false );
282}
283
284Node* Node::FirstChild( bool throwIfNoChildren ) const
285{
286        return FirstChild( "", throwIfNoChildren );
287}
288
289Node* Node::FirstChild( const std::string& value, bool throwIfNoChildren ) const
290{
291        return FirstChild( value.c_str(), throwIfNoChildren );
292}
293
294Node* Node::FirstChild( const char* value, bool throwIfNoChildren ) const
295{
296        TiXmlNode* childNode;
297        if ( 0 == strlen( value ) )
298        {
299                childNode = GetTiXmlPointer()->FirstChild();
300        }
301        else
302        {
303                childNode = GetTiXmlPointer()->FirstChild( value );
304        }
305
306        if ( ( 0 == childNode ) && throwIfNoChildren )
307        {
308                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
309        }
310
311        return NodeFactory( childNode, false );
312}
313
314Node* Node::LastChild( bool throwIfNoChildren ) const
315{
316        return LastChild( "", throwIfNoChildren );
317}
318
319Node* Node::LastChild( const std::string& value, bool throwIfNoChildren ) const
320{
321        return LastChild( value.c_str(), throwIfNoChildren );
322}
323
324Node* Node::LastChild( const char* value, bool throwIfNoChildren ) const
325{
326        TiXmlNode* childNode;
327        if ( 0 == strlen( value ) )
328        {
329                childNode = GetTiXmlPointer()->LastChild();
330        }
331        else
332        {
333                childNode = GetTiXmlPointer()->LastChild( value );
334        }
335
336        if ( ( 0 == childNode ) && throwIfNoChildren )
337        {
338                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
339        }
340
341        return NodeFactory( childNode, false );
342}
343
344Node* Node::IterateChildren ( Node* previous ) const
345{
346        TiXmlNode* pointer;
347        if ( 0 == previous )
348        {
349                pointer = GetTiXmlPointer()->IterateChildren( 0 );
350        }
351        else
352        {
353                pointer = GetTiXmlPointer()->IterateChildren( previous->GetTiXmlPointer() );
354        }
355
356        return NodeFactory( pointer, false );
357}
358
359Node* Node::IterateChildren( const std::string& value, Node* previous ) const
360{
361        TiXmlNode* pointer;
362        if ( 0 == previous )
363        {
364                pointer = GetTiXmlPointer()->IterateChildren( value, 0 );
365        }
366        else
367        {
368                pointer = GetTiXmlPointer()->IterateChildren( value, previous->GetTiXmlPointer() );
369        }
370
371        return NodeFactory( pointer, false );
372}
373
374Node* Node::InsertEndChild( Node& addThis )
375{
376        if ( addThis.Type() == TiXmlNode::DOCUMENT )
377        {
378                TICPPTHROW( "Node is a Document and can't be inserted" );
379        }
380
381        // Increment reference count when adding to the tree
382        addThis.m_impRC->IncRef();
383
384        TiXmlNode* pointer = GetTiXmlPointer()->InsertEndChild( *addThis.GetTiXmlPointer() );
385        if ( 0 == pointer )
386        {
387                TICPPTHROW( "Node can't be inserted" );
388        }
389
390        return NodeFactory( pointer );
391}
392
393Node* Node::LinkEndChild( Node* childNode )
394{
395        if ( childNode->Type() == TiXmlNode::DOCUMENT )
396        {
397                TICPPTHROW( "Node is a Document and can't be linked" );
398        }
399
400        // Increment reference count when adding to the tree
401        childNode->m_impRC->IncRef();
402
403        if ( 0 == GetTiXmlPointer()->LinkEndChild( childNode->GetTiXmlPointer() ) )
404        {
405                TICPPTHROW( "Node can't be linked" );
406        }
407
408        return childNode;
409}
410
411Node* Node::InsertBeforeChild( Node* beforeThis, Node& addThis )
412{
413        if ( addThis.Type() == TiXmlNode::DOCUMENT )
414        {
415                TICPPTHROW( "Node is a Document and can't be inserted" );
416        }
417
418        // Increment reference count when adding to the tree
419        addThis.m_impRC->IncRef();
420
421        TiXmlNode* pointer = GetTiXmlPointer()->InsertBeforeChild( beforeThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
422        if ( 0 == pointer )
423        {
424                TICPPTHROW( "Node can't be inserted" );
425        }
426
427        return NodeFactory( pointer );
428}
429
430Node* Node::InsertAfterChild( Node* afterThis, Node& addThis )
431{
432        if ( addThis.Type() == TiXmlNode::DOCUMENT )
433        {
434                TICPPTHROW( "Node is a Document and can't be inserted" );
435        }
436
437        // Increment reference count when adding to the tree
438        addThis.m_impRC->IncRef();
439
440        TiXmlNode* pointer = GetTiXmlPointer()->InsertAfterChild( afterThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
441        if ( 0 == pointer )
442        {
443                TICPPTHROW( "Node can't be inserted" );
444        }
445
446        return NodeFactory( pointer );
447}
448
449Node* Node::ReplaceChild( Node* replaceThis, Node& withThis )
450{
451        if ( withThis.Type() == TiXmlNode::DOCUMENT )
452        {
453                TICPPTHROW( "Node is a Document and can't be inserted" );
454        }
455
456        // Increment reference count when adding to the tree
457        withThis.m_impRC->IncRef();
458
459        TiXmlNode* pointer = GetTiXmlPointer()->ReplaceChild( replaceThis->GetTiXmlPointer(), *withThis.GetTiXmlPointer() );
460        if ( 0 == pointer )
461        {
462                TICPPTHROW( "Node can't be inserted" );
463        }
464
465        return NodeFactory( pointer );
466}
467
468void Node::RemoveChild( Node* removeThis )
469{
470        if  ( !GetTiXmlPointer()->RemoveChild( removeThis->GetTiXmlPointer() ) )
471        {
472                TICPPTHROW( "Node to remove (" << removeThis->Value() << ") is not a child of this Node (" << Value() << ")" )
473        }
474}
475
476Node* Node::PreviousSibling( bool throwIfNoSiblings ) const
477{
478        return PreviousSibling( "", throwIfNoSiblings );
479}
480
481Node* Node::PreviousSibling( const std::string& value, bool throwIfNoSiblings ) const
482{
483        return PreviousSibling( value.c_str(), throwIfNoSiblings );
484}
485
486Node* Node::PreviousSibling( const char* value, bool throwIfNoSiblings ) const
487{
488        TiXmlNode* sibling;
489        if ( 0 == strlen( value ) )
490        {
491                sibling = GetTiXmlPointer()->PreviousSibling();
492        }
493        else
494        {
495                sibling = GetTiXmlPointer()->PreviousSibling( value );
496        }
497
498        if ( ( 0 == sibling ) && throwIfNoSiblings )
499        {
500                TICPPTHROW( "No Siblings found with value, '" << value << "', Prior to this Node (" << Value() << ")" )
501        }
502
503        return NodeFactory( sibling, false );
504}
505
506Node* Node::NextSibling( bool throwIfNoSiblings ) const
507{
508        return NextSibling( "", throwIfNoSiblings );
509}
510
511Node* Node::NextSibling( const std::string& value, bool throwIfNoSiblings ) const
512{
513        return NextSibling( value.c_str(), throwIfNoSiblings );
514}
515
516Node* Node::NextSibling( const char* value, bool throwIfNoSiblings ) const
517{
518        TiXmlNode* sibling;
519        if ( 0 == strlen( value ) )
520        {
521                sibling = GetTiXmlPointer()->NextSibling();
522        }
523        else
524        {
525                sibling = GetTiXmlPointer()->NextSibling( value );
526        }
527
528        if ( ( 0 == sibling ) && throwIfNoSiblings )
529        {
530                TICPPTHROW( "No Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
531        }
532
533        return NodeFactory( sibling, false );
534}
535
536Element* Node::NextSiblingElement( bool throwIfNoSiblings ) const
537{
538        return NextSiblingElement( "", throwIfNoSiblings );
539}
540
541Element* Node::NextSiblingElement( const std::string& value, bool throwIfNoSiblings ) const
542{
543        return NextSiblingElement( value.c_str(), throwIfNoSiblings );
544}
545
546Element* Node::NextSiblingElement( const char* value, bool throwIfNoSiblings ) const
547{
548        TiXmlElement* sibling;
549        if ( 0 == strlen( value ) )
550        {
551                sibling = GetTiXmlPointer()->NextSiblingElement();
552        }
553        else
554        {
555                sibling = GetTiXmlPointer()->NextSiblingElement( value );
556        }
557
558        if ( 0 == sibling )
559        {
560                if ( throwIfNoSiblings )
561                {
562                        TICPPTHROW( "No Element Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
563                }
564                else
565                {
566                        return 0;
567                }
568        }
569
570        Element* temp = new Element( sibling );
571        m_spawnedWrappers.push_back( temp );
572
573        return temp;
574}
575
576Element* Node::FirstChildElement( bool throwIfNoChildren ) const
577{
578        return FirstChildElement( "", throwIfNoChildren );
579}
580
581Element* Node::FirstChildElement( const std::string& value, bool throwIfNoChildren ) const
582{
583        return FirstChildElement( value.c_str(), throwIfNoChildren );
584}
585
586Element* Node::FirstChildElement( const char* value, bool throwIfNoChildren ) const
587{
588        TiXmlElement* element;
589        if ( 0 == strlen( value ) )
590        {
591                element = GetTiXmlPointer()->FirstChildElement();
592        }
593        else
594        {
595                element = GetTiXmlPointer()->FirstChildElement( value );
596        }
597
598        if ( 0 == element )
599        {
600                if( throwIfNoChildren )
601                {
602                        TICPPTHROW( "Element (" << Value() << ") does NOT contain a child with the value of '" << value << "'" )
603                }
604                else
605                {
606                        return 0;
607                }
608        }
609
610        Element* temp = new Element( element );
611        m_spawnedWrappers.push_back( temp );
612
613        return temp;
614}
615
616int Node::Type() const
617{
618        return GetTiXmlPointer()->Type();
619}
620
621Document* Node::GetDocument( bool throwIfNoDocument ) const
622{
623        TiXmlDocument* doc = GetTiXmlPointer()->GetDocument();
624        if ( 0 == doc )
625        {
626                if( throwIfNoDocument )
627                {
628                        TICPPTHROW( "This node (" << Value() << ") is not linked under a document" )
629                }
630                else
631                {
632                        return 0;
633                }
634        }
635        Document* temp = new Document( doc );
636        m_spawnedWrappers.push_back( temp );
637
638        return temp;
639}
640
641bool Node::NoChildren() const
642{
643        return GetTiXmlPointer()->NoChildren();
644}
645
646Document* Node::ToDocument() const
647{
648        TiXmlDocument* doc = GetTiXmlPointer()->ToDocument();
649        if ( 0 == doc )
650        {
651                TICPPTHROW( "This node (" << Value() << ") is not a Document" )
652        }
653        Document* temp = new Document( doc );
654        m_spawnedWrappers.push_back( temp );
655
656        return temp;
657}
658
659Element* Node::ToElement() const
660{
661        TiXmlElement* doc = GetTiXmlPointer()->ToElement();
662        if ( 0 == doc )
663        {
664                TICPPTHROW( "This node (" << Value() << ") is not a Element" )
665        }
666        Element* temp = new Element( doc );
667        m_spawnedWrappers.push_back( temp );
668
669        return temp;
670}
671
672Comment* Node::ToComment() const
673{
674        TiXmlComment* doc = GetTiXmlPointer()->ToComment();
675        if ( 0 == doc )
676        {
677                TICPPTHROW( "This node (" << Value() << ") is not a Comment" )
678        }
679        Comment* temp = new Comment( doc );
680        m_spawnedWrappers.push_back( temp );
681
682        return temp;
683}
684
685Text* Node::ToText() const
686{
687        TiXmlText* doc = GetTiXmlPointer()->ToText();
688        if ( 0 == doc )
689        {
690                TICPPTHROW( "This node (" << Value() << ") is not a Text" )
691        }
692        Text* temp = new Text( doc );
693        m_spawnedWrappers.push_back( temp );
694
695        return temp;
696}
697
698Declaration* Node::ToDeclaration() const
699{
700        TiXmlDeclaration* doc = GetTiXmlPointer()->ToDeclaration();
701        if ( 0 == doc )
702        {
703                TICPPTHROW( "This node (" << Value() << ") is not a Declaration" )
704        }
705        Declaration* temp = new Declaration( doc );
706        m_spawnedWrappers.push_back( temp );
707
708        return temp;
709}
710
711StylesheetReference* Node::ToStylesheetReference() const
712{
713        TiXmlStylesheetReference* doc = GetTiXmlPointer()->ToStylesheetReference();
714        if ( 0 == doc )
715        {
716                TICPPTHROW( "This node (" << Value() << ") is not a StylesheetReference" )
717        }
718        StylesheetReference* temp = new StylesheetReference( doc );
719        m_spawnedWrappers.push_back( temp );
720
721        return temp;
722}
723
724std::auto_ptr< Node > Node::Clone() const
725{
726        TiXmlNode* node = GetTiXmlPointer()->Clone();
727        if ( 0 == node )
728        {
729                TICPPTHROW( "Node could not be cloned" );
730        }
731        std::auto_ptr< Node > temp( NodeFactory( node, false, false ) );
732
733        // Take ownership of the memory from TiXml
734        temp->m_impRC->InitRef();
735
736        return temp;
737}
738
739bool Node::Accept( TiXmlVisitor* visitor ) const
740{
741        return GetTiXmlPointer()->Accept( visitor );
742}
743
744//*****************************************************************************
745
746Comment::Comment()
747: NodeImp< TiXmlComment >( new TiXmlComment() )
748{
749        m_impRC->InitRef();
750}
751
752Comment::Comment( TiXmlComment* comment )
753: NodeImp< TiXmlComment >( comment )
754{
755}
756
757Comment::Comment( const std::string& comment )
758: NodeImp< TiXmlComment >( new TiXmlComment() )
759{
760        m_impRC->InitRef();
761        m_tiXmlPointer->SetValue( comment );
762}
763
764//*****************************************************************************
765
766Text::Text()
767: NodeImp< TiXmlText >( new TiXmlText("") )
768{
769        m_impRC->InitRef();
770}
771
772
773Text::Text( const std::string& value )
774: NodeImp< TiXmlText >( new TiXmlText( value ) )
775{
776        m_impRC->InitRef();
777}
778
779Text::Text( TiXmlText* text )
780: NodeImp< TiXmlText >( text )
781{
782}
783
784
785//*****************************************************************************
786
787Document::Document()
788: NodeImp< TiXmlDocument >( new TiXmlDocument() )
789{
790        m_impRC->InitRef();
791}
792
793Document::Document( TiXmlDocument* document )
794: NodeImp< TiXmlDocument >( document )
795{
796}
797
798Document::Document( const char* documentName )
799: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
800{
801        m_impRC->InitRef();
802}
803
804Document::Document( const std::string& documentName )
805: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
806{
807        m_impRC->InitRef();
808}
809
810void Document::LoadFile( TiXmlEncoding encoding )
811{
812        if ( !m_tiXmlPointer->LoadFile( encoding ) )
813        {
814                TICPPTHROW( "Couldn't load " << m_tiXmlPointer->Value() );
815        }
816}
817
818void Document::SaveFile( void ) const
819{
820        if ( !m_tiXmlPointer->SaveFile() )
821        {
822                TICPPTHROW( "Couldn't save " << m_tiXmlPointer->Value() );
823        }
824}
825
826void Document::LoadFile( const std::string& filename, TiXmlEncoding encoding )
827{
828        if ( !m_tiXmlPointer->LoadFile( filename.c_str(), encoding ) )
829        {
830                TICPPTHROW( "Couldn't load " << filename );
831        }
832}
833
834void Document::LoadFile( const char* filename, TiXmlEncoding encoding )
835{
836        if ( !m_tiXmlPointer->LoadFile( filename, encoding ) )
837        {
838                TICPPTHROW( "Couldn't load " << filename );
839        }
840}
841
842void Document::SaveFile( const std::string& filename ) const
843{
844        if ( !m_tiXmlPointer->SaveFile( filename.c_str() ) )
845        {
846                TICPPTHROW( "Couldn't save " << filename );
847        }
848}
849
850void Document::Parse( const std::string& xml, bool throwIfParseError, TiXmlEncoding encoding )
851{
852        m_tiXmlPointer->Parse( xml.c_str(), 0, encoding );
853        if( throwIfParseError && m_tiXmlPointer->Error() )
854        {
855                TICPPTHROW( "Error parsing xml: " << m_tiXmlPointer->ErrorDesc() );
856        }
857}
858
859//*****************************************************************************
860
861Element::Element()
862: NodeImp< TiXmlElement >( new TiXmlElement( "DefaultValueCausedByCreatingAnElementWithNoParameters" ) )
863{
864        m_impRC->InitRef();
865}
866
867Element::Element( const std::string& value )
868: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
869{
870        m_impRC->InitRef();
871}
872
873Element::Element( const char* value )
874: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
875{
876        m_impRC->InitRef();
877}
878
879Element::Element( TiXmlElement* element )
880: NodeImp< TiXmlElement >( element )
881{
882}
883
884Attribute* Element::FirstAttribute( bool throwIfNoAttributes ) const
885{
886        ValidatePointer();
887        TiXmlAttribute* attribute = m_tiXmlPointer->FirstAttribute();
888        if ( ( 0 == attribute ) && throwIfNoAttributes )
889        {
890                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
891        }
892
893        if ( 0 == attribute )
894        {
895                if( throwIfNoAttributes )
896                {
897                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
898                }
899                else
900                {
901                        return 0;
902                }
903        }
904
905        Attribute* temp = new Attribute( attribute );
906        m_spawnedWrappers.push_back( temp );
907
908        return temp;
909}
910
911Attribute* Element::LastAttribute( bool throwIfNoAttributes ) const
912{
913        ValidatePointer();
914        TiXmlAttribute* attribute = m_tiXmlPointer->LastAttribute();
915        if ( ( 0 == attribute ) && throwIfNoAttributes )
916        {
917                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
918        }
919
920        if ( 0 == attribute )
921        {
922                if( throwIfNoAttributes )
923                {
924                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
925                }
926                else
927                {
928                        return 0;
929                }
930        }
931
932        Attribute* temp = new Attribute( attribute );
933        m_spawnedWrappers.push_back( temp );
934
935        return temp;
936}
937
938std::string Element::GetAttributeOrDefault( const std::string& name, const std::string& defaultValue ) const
939{
940        std::string value;
941        if ( !GetAttributeImp( name, &value ) )
942        {
943                return defaultValue;
944        }
945        return value;
946}
947
948std::string Element::GetAttribute( const std::string& name ) const
949{
950        return GetAttributeOrDefault( name, std::string() );
951}
952
953bool Element::GetAttributeImp( const std::string& name, std::string* value ) const
954{
955        ValidatePointer();
956
957        // Get value from TinyXML, if the attribute exists
958        const char* retVal = m_tiXmlPointer->Attribute( name.c_str() );
959
960        // TinyXML returns NULL if the attribute doesn't exist
961        if ( 0 == retVal )
962        {
963                return false;
964        }
965        else
966        {
967                *value = retVal;
968                return true;
969        }
970}
971
972bool Element::GetTextImp( std::string* value ) const
973{
974        ValidatePointer();
975
976        // Get value from TinyXML, if the attribute exists
977        const char* retVal = m_tiXmlPointer->GetText();
978
979        // TinyXML returns NULL if the attribute doesn't exist
980        if ( 0 == retVal )
981        {
982                return false;
983        }
984        else
985        {
986                *value = retVal;
987                return true;
988        }
989}
990
991//*****************************************************************************
992
993Declaration::Declaration()
994: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration() )
995{
996        m_impRC->InitRef();
997}
998
999Declaration::Declaration( TiXmlDeclaration* declaration )
1000: NodeImp< TiXmlDeclaration >( declaration )
1001{
1002}
1003
1004Declaration::Declaration( const std::string& version, const std::string& encoding, const std::string& standalone )
1005: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration( version, encoding, standalone ) )
1006{
1007        m_impRC->InitRef();
1008}
1009
1010std::string Declaration::Version() const
1011{
1012        return m_tiXmlPointer->Version();
1013}
1014
1015std::string Declaration::Encoding() const
1016{
1017        return m_tiXmlPointer->Encoding();
1018}
1019
1020std::string Declaration::Standalone() const
1021{
1022        return m_tiXmlPointer->Standalone();
1023}
1024
1025//*****************************************************************************
1026
1027StylesheetReference::StylesheetReference()
1028: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference() )
1029{
1030        m_impRC->InitRef();
1031}
1032
1033StylesheetReference::StylesheetReference( TiXmlStylesheetReference* stylesheetReference )
1034: NodeImp< TiXmlStylesheetReference >( stylesheetReference )
1035{
1036}
1037
1038StylesheetReference::StylesheetReference( const std::string& type, const std::string& href )
1039: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference( type, href ) )
1040{
1041        m_impRC->InitRef();
1042}
1043
1044std::string StylesheetReference::Type() const
1045{
1046        return m_tiXmlPointer->Type();
1047}
1048
1049std::string StylesheetReference::Href() const
1050{
1051        return m_tiXmlPointer->Href();
1052}
1053
1054//*****************************************************************************
1055
1056Exception::Exception(const std::string &details)
1057:
1058m_details( details )
1059{
1060
1061}
1062
1063Exception::~Exception() throw()
1064{
1065}
1066
1067const char* Exception::what() const throw()
1068{
1069        return m_details.c_str();
1070}
1071
1072//*****************************************************************************
1073
1074TiCppRC::TiCppRC()
1075{
1076        // Spawn reference counter for this object
1077        m_tiRC = new TiCppRCImp( this );
1078}
1079
1080TiCppRC::~TiCppRC()
1081{
1082        // Set pointer held by reference counter to NULL
1083        this->m_tiRC->Nullify();
1084
1085        // Decrement reference - so reference counter will delete itself if necessary
1086        this->m_tiRC->DecRef();
1087}
1088
1089//*****************************************************************************
1090
1091TiCppRCImp::TiCppRCImp( TiCppRC* tiCppRC )
1092        : m_count( 1 ), m_tiCppRC ( tiCppRC )
1093{
1094}
1095
1096void TiCppRCImp::IncRef()
1097{
1098        m_count++;
1099}
1100
1101void TiCppRCImp::DecRef()
1102{
1103        m_count--;
1104        if ( 0 == m_count )
1105        {
1106                delete m_tiCppRC;
1107                delete this;
1108        }
1109}
1110
1111void TiCppRCImp::InitRef()
1112{
1113        m_count = 1;
1114}
1115
1116void TiCppRCImp::Nullify()
1117{
1118        m_tiCppRC = 0;
1119}
1120
1121TiCppRC* TiCppRCImp::Get()
1122{
1123        return m_tiCppRC;
1124}
1125
1126bool TiCppRCImp::IsNull()
1127{
1128        return 0 == m_tiCppRC;
1129}
1130
1131#endif // TIXML_USE_TICPP
Note: See TracBrowser for help on using the repository browser.