tinystr.h
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  do nov 2 13:06:00 CET 2006  tinystr.h
00003 
00004                         tinystr.h -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@gmail.com
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 /*
00040 www.sourceforge.net/projects/tinyxml
00041 Original file by Yves Berquin.
00042 
00043 This software is provided 'as-is', without any express or implied
00044 warranty. In no event will the authors be held liable for any
00045 damages arising from the use of this software.
00046 
00047 Permission is granted to anyone to use this software for any
00048 purpose, including commercial applications, and to alter it and
00049 redistribute it freely, subject to the following restrictions:
00050 
00051 1. The origin of this software must not be misrepresented; you must
00052 not claim that you wrote the original software. If you use this
00053 software in a product, an acknowledgment in the product documentation
00054 would be appreciated but is not required.
00055 
00056 2. Altered source versions must be plainly marked as such, and
00057 must not be misrepresented as being the original software.
00058 
00059 3. This notice may not be removed or altered from any source
00060 distribution.
00061 */
00062 
00063 /*
00064  * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
00065  *
00066  * - completely rewritten. compact, clean, and fast implementation.
00067  * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
00068  * - fixed reserve() to work as per specification.
00069  * - fixed buggy compares operator==(), operator<(), and operator>()
00070  * - fixed operator+=() to take a const ref argument, following spec.
00071  * - added "copy" constructor with length, and most compare operators.
00072  * - added swap(), clear(), size(), capacity(), operator+().
00073  */
00074 
00075 #ifndef TIXML_USE_STL
00076 
00077 #ifndef TIXML_STRING_INCLUDED
00078 #define TIXML_STRING_INCLUDED
00079 
00080 #include <assert.h>
00081 #include <string.h>
00082 
00083 /*      The support for explicit isn't that universal, and it isn't really
00084         required - it is used to check that the TiXmlString class isn't incorrectly
00085         used. Be nice to old compilers and macro it here:
00086 */
00087 #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
00088         // Microsoft visual studio, version 6 and higher.
00089         #define TIXML_EXPLICIT explicit
00090 #elif defined(__GNUC__) && (__GNUC__ >= 3 )
00091         // GCC version 3 and higher.s
00092         #define TIXML_EXPLICIT explicit
00093 #else
00094         #define TIXML_EXPLICIT
00095 #endif
00096 
00097 namespace RTT { namespace marsh {
00098 
00099 /*
00100    TiXmlString is an emulation of a subset of the std::string template.
00101    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
00102    Only the member functions relevant to the TinyXML project have been implemented.
00103    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
00104    a string and there's no more room, we allocate a buffer twice as big as we need.
00105 */
00106 class TiXmlString
00107 {
00108   public :
00109         // The size type used
00110         typedef size_t size_type;
00111 
00112         // Error value for find primitive
00113         static const size_type npos; // = -1;
00114 
00115 
00116         // TiXmlString empty constructor
00117         TiXmlString () : rep_(&nullrep_)
00118         {
00119         }
00120 
00121         // TiXmlString copy constructor
00122         TiXmlString ( const TiXmlString & copy)
00123         {
00124                 init(copy.length());
00125                 memcpy(start(), copy.data(), length());
00126         }
00127 
00128         // TiXmlString constructor, based on a string
00129         TIXML_EXPLICIT TiXmlString ( const char * copy)
00130         {
00131                 init( static_cast<size_type>( strlen(copy) ));
00132                 memcpy(start(), copy, length());
00133         }
00134 
00135         // TiXmlString constructor, based on a string
00136         TIXML_EXPLICIT TiXmlString ( const char * str, size_type len)
00137         {
00138                 init(len);
00139                 memcpy(start(), str, len);
00140         }
00141 
00142         // TiXmlString destructor
00143         ~TiXmlString ()
00144         {
00145                 quit();
00146         }
00147 
00148         // = operator
00149         TiXmlString& operator = (const char * copy)
00150         {
00151                 return assign( copy, (size_type)strlen(copy));
00152         }
00153 
00154         // = operator
00155         TiXmlString& operator = (const TiXmlString & copy)
00156         {
00157                 return assign(copy.start(), copy.length());
00158         }
00159 
00160 
00161         // += operator. Maps to append
00162         TiXmlString& operator += (const char * suffix)
00163         {
00164                 return append(suffix, static_cast<size_type>( strlen(suffix) ));
00165         }
00166 
00167         // += operator. Maps to append
00168         TiXmlString& operator += (char single)
00169         {
00170                 return append(&single, 1);
00171         }
00172 
00173         // += operator. Maps to append
00174         TiXmlString& operator += (const TiXmlString & suffix)
00175         {
00176                 return append(suffix.data(), suffix.length());
00177         }
00178 
00179 
00180         // Convert a TiXmlString into a null-terminated char *
00181         const char * c_str () const { return rep_->str; }
00182 
00183         // Convert a TiXmlString into a char * (need not be null terminated).
00184         const char * data () const { return rep_->str; }
00185 
00186         // Return the length of a TiXmlString
00187         size_type length () const { return rep_->size; }
00188 
00189         // Alias for length()
00190         size_type size () const { return rep_->size; }
00191 
00192         // Checks if a TiXmlString is empty
00193         bool empty () const { return rep_->size == 0; }
00194 
00195         // Return capacity of string
00196         size_type capacity () const { return rep_->capacity; }
00197 
00198 
00199         // single char extraction
00200         const char& at (size_type index) const
00201         {
00202                 assert( index < length() );
00203                 return rep_->str[ index ];
00204         }
00205 
00206         // [] operator
00207         char& operator [] (size_type index) const
00208         {
00209                 assert( index < length() );
00210                 return rep_->str[ index ];
00211         }
00212 
00213         // find a char in a string. Return TiXmlString::npos if not found
00214         size_type find (char lookup) const
00215         {
00216                 return find(lookup, 0);
00217         }
00218 
00219         // find a char in a string from an offset. Return TiXmlString::npos if not found
00220         size_type find (char tofind, size_type offset) const
00221         {
00222                 if (offset >= length()) return npos;
00223 
00224                 for (const char* p = c_str() + offset; *p != '\0'; ++p)
00225                 {
00226                    if (*p == tofind) return static_cast< size_type >( p - c_str() );
00227                 }
00228                 return npos;
00229         }
00230 
00231         void clear ()
00232         {
00233                 //Lee:
00234                 //The original was just too strange, though correct:
00235                 //      TiXmlString().swap(*this);
00236                 //Instead use the quit & re-init:
00237                 quit();
00238                 init(0,0);
00239         }
00240 
00241         /*      Function to reserve a big amount of data when we know we'll need it. Be aware that this
00242                 function DOES NOT clear the content of the TiXmlString if any exists.
00243         */
00244         void reserve (size_type cap);
00245 
00246         TiXmlString& assign (const char* str, size_type len);
00247 
00248         TiXmlString& append (const char* str, size_type len);
00249 
00250         void swap (TiXmlString& other)
00251         {
00252                 Rep* r = rep_;
00253                 rep_ = other.rep_;
00254                 other.rep_ = r;
00255         }
00256 
00257   private:
00258 
00259         void init(size_type sz) { init(sz, sz); }
00260         void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
00261         char* start() const { return rep_->str; }
00262         char* finish() const { return rep_->str + rep_->size; }
00263 
00264         struct Rep
00265         {
00266                 size_type size, capacity;
00267                 char str[1];
00268         };
00269 
00270         void init(size_type sz, size_type cap)
00271         {
00272                 if (cap)
00273                 {
00274                         // Lee: the original form:
00275                         //      rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
00276                         // doesn't work in some cases of new being overloaded. Switching
00277                         // to the normal allocation, although use an 'int' for systems
00278                         // that are overly picky about structure alignment.
00279                         const size_type bytesNeeded = sizeof(Rep) + cap;
00280                         const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
00281                         rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
00282 
00283                         rep_->str[ rep_->size = sz ] = '\0';
00284                         rep_->capacity = cap;
00285                 }
00286                 else
00287                 {
00288                         rep_ = &nullrep_;
00289                 }
00290         }
00291 
00292         void quit()
00293         {
00294                 if (rep_ != &nullrep_)
00295                 {
00296                         // The rep_ is really an array of ints. (see the allocator, above).
00297                         // Cast it back before delete, so the compiler won't incorrectly call destructors.
00298                         delete [] ( reinterpret_cast<int*>( rep_ ) );
00299                 }
00300         }
00301 
00302         Rep * rep_;
00303         static Rep nullrep_;
00304 
00305 } ;
00306 
00307 
00308 inline bool operator == (const TiXmlString & a, const TiXmlString & b)
00309 {
00310         return    ( a.length() == b.length() )                          // optimization on some platforms
00311                && ( strcmp(a.c_str(), b.c_str()) == 0 );        // actual compare
00312 }
00313 inline bool operator < (const TiXmlString & a, const TiXmlString & b)
00314 {
00315         return strcmp(a.c_str(), b.c_str()) < 0;
00316 }
00317 
00318 inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
00319 inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
00320 inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
00321 inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
00322 
00323 inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
00324 inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
00325 inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
00326 inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
00327 
00328 TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
00329 TiXmlString operator + (const TiXmlString & a, const char* b);
00330 TiXmlString operator + (const char* a, const TiXmlString & b);
00331 
00332 
00333 /*
00334    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
00335    Only the operators that we need for TinyXML have been developped.
00336 */
00337 class TiXmlOutStream : public TiXmlString
00338 {
00339 public :
00340 
00341         // TiXmlOutStream << operator.
00342         TiXmlOutStream & operator << (const TiXmlString & in)
00343         {
00344                 *this += in;
00345                 return *this;
00346         }
00347 
00348         // TiXmlOutStream << operator.
00349         TiXmlOutStream & operator << (const char * in)
00350         {
00351                 *this += in;
00352                 return *this;
00353         }
00354 
00355 } ;
00356 
00357 }}
00358 
00359 #endif  // TIXML_STRING_INCLUDED
00360 #endif  // TIXML_USE_STL


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:33