Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/XML/tinystr.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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