fsTinyXml.h

Engine/source/persistence/taml/fsTinyXml.h

More...

Classes:

Public Functions

bool
AttemptPrintTiNode(class fsTiXmlDocument * node, FileStream & stream, int depth)

Detailed Description

Public Functions

AttemptPrintTiNode(class fsTiXmlDocument * node, FileStream & stream, int depth)

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _FSTINYXML_H_
 25#define _FSTINYXML_H_
 26
 27
 28#ifndef TINYXML_INCLUDED
 29#include "tinyxml/tinyxml.h"
 30#endif
 31
 32#include "platform/platform.h"
 33
 34#ifndef _FILESTREAM_H_
 35#include "core/stream/fileStream.h"
 36#endif
 37
 38class fsTiXmlNode
 39{
 40public:
 41   virtual void Print( FileStream& stream, int depth ) const = 0;
 42};
 43
 44class fsTiXmlDocument : public TiXmlDocument, public fsTiXmlNode
 45{
 46public:
 47   bool LoadFile( FileStream &stream, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 48   bool SaveFile( FileStream &stream ) const;
 49   /// Load a file using the given filename. Returns true if successful.
 50   bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
 51   /// Save a file using the given filename. Returns true if successful.
 52   bool SaveFile( const char * filename ) const;
 53   virtual void Print( FileStream& stream, int depth ) const;
 54
 55   virtual TiXmlNode* Clone() const
 56   {
 57      TiXmlDocument* clone = new fsTiXmlDocument();
 58      if ( !clone )
 59         return 0;
 60
 61      CopyTo( clone );
 62      return clone;
 63   }
 64   TiXmlNode* Identify( const char* p, TiXmlEncoding encoding );
 65};
 66
 67class fsTiXmlAttribute : public TiXmlAttribute, public fsTiXmlNode
 68{
 69public:
 70   virtual void Print( FileStream& stream, int depth, TIXML_STRING* str ) const;
 71   virtual void Print( FileStream& stream, int depth) const
 72   {
 73      Print(stream, depth, 0);
 74   }
 75};
 76
 77class fsTiXmlDeclaration : public TiXmlDeclaration, public fsTiXmlNode
 78{
 79public:
 80   fsTiXmlDeclaration(){};
 81   fsTiXmlDeclaration(  const char* _version,
 82                  const char* _encoding,
 83                  const char* _standalone ) : TiXmlDeclaration(_version, _encoding, _standalone) { }
 84
 85   virtual void Print( FileStream& stream, int depth, TIXML_STRING* str ) const;
 86   virtual void Print( FileStream& stream, int depth) const
 87   {
 88      Print(stream, depth, 0);
 89   }
 90
 91   virtual TiXmlNode* Clone() const
 92   {
 93      fsTiXmlDeclaration* clone = new fsTiXmlDeclaration();
 94
 95      if ( !clone )
 96         return 0;
 97
 98      CopyTo( clone );
 99      return clone;
100   }
101};
102
103class fsTiXmlElement : public TiXmlElement, public fsTiXmlNode
104{
105public:
106   fsTiXmlElement(const char* in_value) : TiXmlElement(in_value) { }
107
108   virtual void Print( FileStream& stream, int depth ) const;
109
110   virtual TiXmlNode* Clone() const
111   {
112      TiXmlElement* clone = new fsTiXmlElement( Value() );
113      if ( !clone )
114         return 0;
115
116      CopyTo( clone );
117      return clone;
118   }
119
120   void SetAttribute( const char* name, const char * _value )
121   {
122      TiXmlAttribute* attrib = attributeSet.Find( name );
123      if(!attrib)
124      {
125         attrib = new fsTiXmlAttribute();
126         attributeSet.Add( attrib );
127         attrib->SetName( name );
128      }
129      if ( attrib ) {
130         attrib->SetValue( _value );
131      }
132   }
133   void SetAttribute( const char * name, int _value)
134   {
135      TiXmlAttribute* attrib = attributeSet.Find( name );
136      if(!attrib)
137      {
138         attrib = new fsTiXmlAttribute();
139         attributeSet.Add( attrib );
140         attrib->SetName( name );
141      }
142      if ( attrib ) {
143         attrib->SetIntValue(_value);
144      }
145   }
146   TiXmlNode* Identify( const char* p, TiXmlEncoding encoding );
147   virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
148};
149
150class fsTiXmlComment : public TiXmlComment, public fsTiXmlNode
151{
152public:
153   virtual void Print( FileStream& stream, int depth ) const;
154
155   virtual TiXmlNode* Clone() const
156   {
157      TiXmlComment* clone = new fsTiXmlComment();
158
159      if ( !clone )
160         return 0;
161
162      CopyTo( clone );
163      return clone;
164   }
165};
166
167class fsTiXmlText : public TiXmlText, public fsTiXmlNode
168{
169public:
170   fsTiXmlText (const char * initValue ) : TiXmlText(initValue) { }
171   virtual void Print( FileStream& stream, int depth ) const;
172
173   virtual TiXmlNode* Clone() const
174   {
175      TiXmlText* clone = 0;
176      clone = new fsTiXmlText( "" );
177
178      if ( !clone )
179         return 0;
180
181      CopyTo( clone );
182      return clone;
183   }
184};
185
186class fsTiXmlUnknown : public TiXmlUnknown, public fsTiXmlNode
187{
188public:
189   virtual void Print( FileStream& stream, int depth ) const;
190
191   virtual TiXmlNode* Clone() const
192   {
193      TiXmlUnknown* clone = new fsTiXmlUnknown();
194
195      if ( !clone )
196         return 0;
197
198      CopyTo( clone );
199      return clone;
200   }
201};
202
203static bool AttemptPrintTiNode(class fsTiXmlDocument* node, FileStream& stream, int depth)
204{
205   fsTiXmlDocument* fsDoc = dynamic_cast<fsTiXmlDocument*>(node);
206   if(fsDoc != NULL)
207   {
208      fsDoc->Print(stream, depth);
209      return true;
210   }
211   fsTiXmlUnknown* fsUnk = dynamic_cast<fsTiXmlUnknown*>(node);
212   if(fsUnk != NULL)
213   {
214      fsUnk->Print(stream, depth);
215      return true;
216   }
217   fsTiXmlText* fsTxt = dynamic_cast<fsTiXmlText*>(node);
218   if(fsTxt != NULL)
219   {
220      fsTxt->Print(stream, depth);
221      return true;
222   }
223   fsTiXmlComment* fsCom = dynamic_cast<fsTiXmlComment*>(node);
224   if(fsCom != NULL)
225   {
226      fsCom->Print(stream, depth);
227      return true;
228   }
229   fsTiXmlElement* fsElm = dynamic_cast<fsTiXmlElement*>(node);
230   if(fsElm != NULL)
231   {
232      fsElm->Print(stream, depth);
233      return true;
234   }
235   fsTiXmlDeclaration* fsDec = dynamic_cast<fsTiXmlDeclaration*>(node);
236   if(fsDec != NULL)
237   {
238      fsDec->Print(stream, depth);
239      return true;
240   }
241   fsTiXmlAttribute* fsAtt = dynamic_cast<fsTiXmlAttribute*>(node);
242   if(fsAtt != NULL)
243   {
244      fsAtt->Print(stream, depth);
245      return true;
246   }
247   return false;
248}
249#endif //_FSTINYXML_H_
250