Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:31:27

0001 /*
0002 www.sourceforge.net/projects/tinyxml
0003 
0004 This software is provided 'as-is', without any express or implied
0005 warranty. In no event will the authors be held liable for any
0006 damages arising from the use of this software.
0007 
0008 Permission is granted to anyone to use this software for any
0009 purpose, including commercial applications, and to alter it and
0010 redistribute it freely, subject to the following restrictions:
0011 
0012 1. The origin of this software must not be misrepresented; you must
0013 not claim that you wrote the original software. If you use this
0014 software in a product, an acknowledgment in the product documentation
0015 would be appreciated but is not required.
0016 
0017 2. Altered source versions must be plainly marked as such, and
0018 must not be misrepresented as being the original software.
0019 
0020 3. This notice may not be removed or altered from any source
0021 distribution.
0022 */
0023 
0024 
0025 #ifndef TIXML_USE_STL
0026 
0027 #ifndef PANDORA_TIXML_STRING_INCLUDED
0028 #define PANDORA_TIXML_STRING_INCLUDED
0029 
0030 #include <assert.h>
0031 #include <string.h>
0032 
0033 /*  The support for explicit isn't that universal, and it isn't really
0034     required - it is used to check that the TiXmlString class isn't incorrectly
0035     used. Be nice to old compilers and macro it here:
0036 */
0037 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
0038     // Microsoft visual studio, version 6 and higher.
0039     #define TIXML_EXPLICIT explicit
0040 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
0041     // GCC version 3 and higher.s
0042     #define TIXML_EXPLICIT explicit
0043 #else
0044     #define TIXML_EXPLICIT
0045 #endif
0046 
0047 namespace pandora // Added namespace
0048 {
0049 
0050 /*
0051    TiXmlString is an emulation of a subset of the std::string template.
0052    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
0053    Only the member functions relevant to the TinyXML project have been implemented.
0054    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
0055    a string and there's no more room, we allocate a buffer twice as big as we need.
0056 */
0057 class TiXmlString
0058 {
0059   public :
0060     // The size type used
0061     typedef size_t size_type;
0062 
0063     // Error value for find primitive
0064     static const size_type npos; // = -1;
0065 
0066 
0067     // TiXmlString empty constructor
0068     TiXmlString () : rep_(&nullrep_)
0069     {
0070     }
0071 
0072     // TiXmlString copy constructor
0073     TiXmlString ( const TiXmlString & copy) : rep_(0)
0074     {
0075         init(copy.length());
0076         memcpy(start(), copy.data(), length());
0077     }
0078 
0079     // TiXmlString constructor, based on a string
0080     TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
0081     {
0082         init( static_cast<size_type>( strlen(copy) ));
0083         memcpy(start(), copy, length());
0084     }
0085 
0086     // TiXmlString constructor, based on a string
0087     TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
0088     {
0089         init(len);
0090         memcpy(start(), str, len);
0091     }
0092 
0093     // TiXmlString destructor
0094     ~TiXmlString ()
0095     {
0096         quit();
0097     }
0098 
0099     TiXmlString& operator = (const char * copy)
0100     {
0101         return assign( copy, (size_type)strlen(copy));
0102     }
0103 
0104     TiXmlString& operator = (const TiXmlString & copy)
0105     {
0106         return assign(copy.start(), copy.length());
0107     }
0108 
0109 
0110     // += operator. Maps to append
0111     TiXmlString& operator += (const char * suffix)
0112     {
0113         return append(suffix, static_cast<size_type>( strlen(suffix) ));
0114     }
0115 
0116     // += operator. Maps to append
0117     TiXmlString& operator += (char single)
0118     {
0119         return append(&single, 1);
0120     }
0121 
0122     // += operator. Maps to append
0123     TiXmlString& operator += (const TiXmlString & suffix)
0124     {
0125         return append(suffix.data(), suffix.length());
0126     }
0127 
0128 
0129     // Convert a TiXmlString into a null-terminated char *
0130     const char * c_str () const { return rep_->str; }
0131 
0132     // Convert a TiXmlString into a char * (need not be null terminated).
0133     const char * data () const { return rep_->str; }
0134 
0135     // Return the length of a TiXmlString
0136     size_type length () const { return rep_->size; }
0137 
0138     // Alias for length()
0139     size_type size () const { return rep_->size; }
0140 
0141     // Checks if a TiXmlString is empty
0142     bool empty () const { return rep_->size == 0; }
0143 
0144     // Return capacity of string
0145     size_type capacity () const { return rep_->capacity; }
0146 
0147 
0148     // single char extraction
0149     const char& at (size_type index) const
0150     {
0151         assert( index < length() );
0152         return rep_->str[ index ];
0153     }
0154 
0155     // [] operator
0156     char& operator [] (size_type index) const
0157     {
0158         assert( index < length() );
0159         return rep_->str[ index ];
0160     }
0161 
0162     // find a char in a string. Return TiXmlString::npos if not found
0163     size_type find (char lookup) const
0164     {
0165         return find(lookup, 0);
0166     }
0167 
0168     // find a char in a string from an offset. Return TiXmlString::npos if not found
0169     size_type find (char tofind, size_type offset) const
0170     {
0171         if (offset >= length()) return npos;
0172 
0173         for (const char* p = c_str() + offset; *p != '\0'; ++p)
0174         {
0175            if (*p == tofind) return static_cast< size_type >( p - c_str() );
0176         }
0177         return npos;
0178     }
0179 
0180     void clear ()
0181     {
0182         //Lee:
0183         //The original was just too strange, though correct:
0184         //  TiXmlString().swap(*this);
0185         //Instead use the quit & re-init:
0186         quit();
0187         init(0,0);
0188     }
0189 
0190     /*  Function to reserve a big amount of data when we know we'll need it. Be aware that this
0191         function DOES NOT clear the content of the TiXmlString if any exists.
0192     */
0193     void reserve (size_type cap);
0194 
0195     TiXmlString& assign (const char* str, size_type len);
0196 
0197     TiXmlString& append (const char* str, size_type len);
0198 
0199     void swap (TiXmlString& other)
0200     {
0201         Rep* r = rep_;
0202         rep_ = other.rep_;
0203         other.rep_ = r;
0204     }
0205 
0206   private:
0207 
0208     void init(size_type sz) { init(sz, sz); }
0209     void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
0210     char* start() const { return rep_->str; }
0211     char* finish() const { return rep_->str + rep_->size; }
0212 
0213     struct Rep
0214     {
0215         size_type size, capacity;
0216         char str[1];
0217     };
0218 
0219     void init(size_type sz, size_type cap)
0220     {
0221         if (cap)
0222         {
0223             // Lee: the original form:
0224             //  rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
0225             // doesn't work in some cases of new being overloaded. Switching
0226             // to the normal allocation, although use an 'int' for systems
0227             // that are overly picky about structure alignment.
0228             const size_type bytesNeeded = sizeof(Rep) + cap;
0229             const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); 
0230             rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
0231 
0232             rep_->str[ rep_->size = sz ] = '\0';
0233             rep_->capacity = cap;
0234         }
0235         else
0236         {
0237             rep_ = &nullrep_;
0238         }
0239     }
0240 
0241     void quit()
0242     {
0243         if (rep_ != &nullrep_)
0244         {
0245             // The rep_ is really an array of ints. (see the allocator, above).
0246             // Cast it back before delete, so the compiler won't incorrectly call destructors.
0247             delete [] ( reinterpret_cast<int*>( rep_ ) );
0248         }
0249     }
0250 
0251     Rep * rep_;
0252     static Rep nullrep_;
0253 
0254 } ;
0255 
0256 
0257 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
0258 {
0259     return    ( a.length() == b.length() )              // optimization on some platforms
0260            && ( strcmp(a.c_str(), b.c_str()) == 0 );    // actual compare
0261 }
0262 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
0263 {
0264     return strcmp(a.c_str(), b.c_str()) < 0;
0265 }
0266 
0267 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
0268 inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
0269 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
0270 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
0271 
0272 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
0273 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
0274 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
0275 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
0276 
0277 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
0278 TiXmlString operator + (const TiXmlString & a, const char* b);
0279 TiXmlString operator + (const char* a, const TiXmlString & b);
0280 
0281 
0282 /*
0283    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
0284    Only the operators that we need for TinyXML have been developped.
0285 */
0286 class TiXmlOutStream : public TiXmlString
0287 {
0288 public :
0289 
0290     // TiXmlOutStream << operator.
0291     TiXmlOutStream & operator << (const TiXmlString & in)
0292     {
0293         *this += in;
0294         return *this;
0295     }
0296 
0297     // TiXmlOutStream << operator.
0298     TiXmlOutStream & operator << (const char * in)
0299     {
0300         *this += in;
0301         return *this;
0302     }
0303 
0304 } ;
0305 
0306 } // Added namespace pandora
0307 
0308 #endif  // PANDORA_TIXML_STRING_INCLUDED
0309 #endif  // TIXML_USE_STL