Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 29, 2005, 10:52:08 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/we: new tinyXML-version (version 2.4.2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/world_entities/src/lib/tinyxml/tinystr.h

    r4491 r5817  
    33Original file by Yves Berquin.
    44
    5 This software is provided 'as-is', without any express or implied 
    6 warranty. In no event will the authors be held liable for any 
     5This software is provided 'as-is', without any express or implied
     6warranty. In no event will the authors be held liable for any
    77damages arising from the use of this software.
    88
    9 Permission is granted to anyone to use this software for any 
    10 purpose, including commercial applications, and to alter it and 
     9Permission is granted to anyone to use this software for any
     10purpose, including commercial applications, and to alter it and
    1111redistribute it freely, subject to the following restrictions:
    1212
    13 1. The origin of this software must not be misrepresented; you must 
    14 not claim that you wrote the original software. If you use this 
    15 software in a product, an acknowledgment in the product documentation 
     131. The origin of this software must not be misrepresented; you must
     14not claim that you wrote the original software. If you use this
     15software in a product, an acknowledgment in the product documentation
    1616would be appreciated but is not required.
    1717
     
    1919must not be misrepresented as being the original software.
    2020
    21 3. This notice may not be removed or altered from any source 
     213. This notice may not be removed or altered from any source
    2222distribution.
    2323*/
    2424
    25 #include "tinyxml.h"
    26 
     25/*
     26 * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
     27 *
     28 * - completely rewritten. compact, clean, and fast implementation.
     29 * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
     30 * - fixed reserve() to work as per specification.
     31 * - fixed buggy compares operator==(), operator<(), and operator>()
     32 * - fixed operator+=() to take a const ref argument, following spec.
     33 * - added "copy" constructor with length, and most compare operators.
     34 * - added swap(), clear(), size(), capacity(), operator+().
     35 */
    2736
    2837#ifndef TIXML_USE_STL
     
    3140#define TIXML_STRING_INCLUDED
    3241
    33 #ifdef _MSC_VER
    34 #pragma warning( disable : 4530 )
    35 #pragma warning( disable : 4786 )
    36 #endif
    37 
    3842#include <assert.h>
     43#include <string.h>
    3944
    4045/*
    41    TiXmlString is an emulation of the std::string template.
     46   TiXmlString is an emulation of a subset of the std::string template.
    4247   Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
    4348   Only the member functions relevant to the TinyXML project have been implemented.
     
    4853{
    4954  public :
    50     // TiXmlString constructor, based on a string, mark explicit to force
    51         // us to find unnecessary casting.
    52     explicit TiXmlString (const char * instring);
    53 
    54     // TiXmlString empty constructor
    55     TiXmlString ()
    56     {
    57         allocated = 0;
    58         cstring = NULL;
    59         current_length = 0;
    60     }
    61 
    62     // TiXmlString copy constructor
    63     explicit TiXmlString (const TiXmlString& copy);
    64 
    65     // TiXmlString destructor
    66     ~ TiXmlString ()
    67     {
    68         empty_it ();
    69     }
    70 
    71     // Convert a TiXmlString into a classical char *
    72     const char * c_str () const
    73     {
    74         if (allocated)
    75             return cstring;
    76         return "";
    77     }
    78 
    79     // Return the length of a TiXmlString
    80     size_t length () const
    81         {
    82                 return ( allocated ) ? current_length : 0;
    83         }
    84 
    85     // TiXmlString = operator
    86     void operator = (const char * content);
    87 
    88     // = operator
    89     void operator = (const TiXmlString & copy);
    90 
    91     // += operator. Maps to append
    92     TiXmlString& operator += (const char * suffix)
    93     {
    94         append (suffix);
    95                 return *this;
    96     }
    97 
    98     // += operator. Maps to append
    99     TiXmlString& operator += (char single)
    100     {
    101         append (single);
    102                 return *this;
    103     }
    104 
    105     // += operator. Maps to append
    106     TiXmlString& operator += (TiXmlString & suffix)
    107     {
    108         append (suffix);
    109                 return *this;
    110     }
    111     bool operator == (const TiXmlString & compare) const;
    112     bool operator == (const char* compare) const;
    113     bool operator < (const TiXmlString & compare) const;
    114     bool operator > (const TiXmlString & compare) const;
    115 
    116     // Checks if a TiXmlString is empty
    117     bool empty () const
    118     {
    119         return length () ? false : true;
    120     }
    121 
    122     // single char extraction
    123     const char& at (unsigned index) const
    124     {
    125         assert( index < length ());
    126         return cstring [index];
    127     }
    128 
    129     // find a char in a string. Return TiXmlString::notfound if not found
    130     unsigned find (char lookup) const
    131     {
    132         return find (lookup, 0);
    133     }
    134 
    135     // find a char in a string from an offset. Return TiXmlString::notfound if not found
    136     unsigned find (char tofind, unsigned offset) const;
    137 
    138     /*  Function to reserve a big amount of data when we know we'll need it. Be aware that this
    139                 function clears the content of the TiXmlString if any exists.
    140     */
    141     void reserve (unsigned size)
    142     {
    143         empty_it ();
    144         if (size)
    145         {
    146             allocated = size;
    147             cstring = new char [size];
    148             cstring [0] = 0;
    149             current_length = 0;
    150         }
    151     }
    152 
    153     // [] operator
    154     char& operator [] (unsigned index) const
    155     {
    156         assert( index < length ());
    157         return cstring [index];
    158     }
    159 
    160     // Error value for find primitive
    161     enum {      notfound = 0xffffffff,
    162             npos = notfound };
    163 
    164     void append (const char *str, size_t len );
    165 
    166   protected :
    167 
    168     // The base string
    169     char * cstring;
    170     // Number of chars allocated
    171     size_t allocated;
    172     // Current string size
    173     size_t current_length;
    174 
    175     // New size computation. It is simplistic right now : it returns twice the amount
    176     // we need
    177     size_t assign_new_size (size_t minimum_to_allocate)
    178     {
    179         return minimum_to_allocate * 2;
    180     }
    181 
    182     // Internal function that clears the content of a TiXmlString
    183     void empty_it ()
    184     {
    185         if (cstring)
    186             delete [] cstring;
    187         cstring = NULL;
    188         allocated = 0;
    189         current_length = 0;
    190     }
    191 
    192     void append (const char *suffix );
    193 
    194     // append function for another TiXmlString
    195     void append (const TiXmlString & suffix)
    196     {
    197         append (suffix . c_str ());
    198     }
    199 
    200     // append for a single char.
    201     void append (char single)
    202     {
    203         if ( cstring && current_length < (allocated-1) )
     55        // The size type used
     56        typedef unsigned int size_type;
     57
     58        // Error value for find primitive
     59        static const size_type npos; // = -1;
     60
     61
     62        // TiXmlString empty constructor
     63        TiXmlString () : rep_(&nullrep_)
     64        {
     65        }
     66
     67        // TiXmlString copy constructor
     68        TiXmlString (const TiXmlString & copy)
     69        {
     70                init(copy.length());
     71                memcpy(start(), copy.data(), length());
     72        }
     73
     74        // TiXmlString constructor, based on a string
     75        TiXmlString (const char * copy)
     76        {
     77                init( static_cast<size_type>( strlen(copy) ));
     78                memcpy(start(), copy, length());
     79        }
     80
     81        // TiXmlString constructor, based on a string
     82        TiXmlString (const char * str, size_type len)
     83        {
     84                init(len);
     85                memcpy(start(), str, len);
     86        }
     87
     88        // TiXmlString destructor
     89        ~TiXmlString ()
     90        {
     91                quit();
     92        }
     93
     94        // = operator
     95        TiXmlString& operator = (const char * copy)
     96        {
     97                return assign( copy, (size_type)strlen(copy));
     98        }
     99
     100        // = operator
     101        TiXmlString& operator = (const TiXmlString & copy)
     102        {
     103                return assign(copy.start(), copy.length());
     104        }
     105
     106
     107        // += operator. Maps to append
     108        TiXmlString& operator += (const char * suffix)
     109        {
     110                return append(suffix, static_cast<size_type>( strlen(suffix) ));
     111        }
     112
     113        // += operator. Maps to append
     114        TiXmlString& operator += (char single)
     115        {
     116                return append(&single, 1);
     117        }
     118
     119        // += operator. Maps to append
     120        TiXmlString& operator += (const TiXmlString & suffix)
     121        {
     122                return append(suffix.data(), suffix.length());
     123        }
     124
     125
     126        // Convert a TiXmlString into a null-terminated char *
     127        const char * c_str () const { return rep_->str; }
     128
     129        // Convert a TiXmlString into a char * (need not be null terminated).
     130        const char * data () const { return rep_->str; }
     131
     132        // Return the length of a TiXmlString
     133        size_type length () const { return rep_->size; }
     134
     135        // Alias for length()
     136        size_type size () const { return rep_->size; }
     137
     138        // Checks if a TiXmlString is empty
     139        bool empty () const { return rep_->size == 0; }
     140
     141        // Return capacity of string
     142        size_type capacity () const { return rep_->capacity; }
     143
     144
     145        // single char extraction
     146        const char& at (size_type index) const
     147        {
     148                assert( index < length() );
     149                return rep_->str[ index ];
     150        }
     151
     152        // [] operator
     153        char& operator [] (size_type index) const
     154        {
     155                assert( index < length() );
     156                return rep_->str[ index ];
     157        }
     158
     159        // find a char in a string. Return TiXmlString::npos if not found
     160        size_type find (char lookup) const
     161        {
     162                return find(lookup, 0);
     163        }
     164
     165        // find a char in a string from an offset. Return TiXmlString::npos if not found
     166        size_type find (char tofind, size_type offset) const
     167        {
     168                if (offset >= length()) return npos;
     169
     170                for (const char* p = c_str() + offset; *p != '\0'; ++p)
    204171                {
    205                         cstring[ current_length ] = single;
    206                         ++current_length;
    207                         cstring[ current_length ] = 0;
     172                   if (*p == tofind) return static_cast< size_type >( p - c_str() );
     173                }
     174                return npos;
     175        }
     176
     177        void clear ()
     178        {
     179                //Lee:
     180                //The original was just too strange, though correct:
     181                //      TiXmlString().swap(*this);
     182                //Instead use the quit & re-init:
     183                quit();
     184                init(0,0);
     185        }
     186
     187        /*      Function to reserve a big amount of data when we know we'll need it. Be aware that this
     188                function DOES NOT clear the content of the TiXmlString if any exists.
     189        */
     190        void reserve (size_type cap);
     191
     192        TiXmlString& assign (const char* str, size_type len);
     193
     194        TiXmlString& append (const char* str, size_type len);
     195
     196        void swap (TiXmlString& other)
     197        {
     198                Rep* r = rep_;
     199                rep_ = other.rep_;
     200                other.rep_ = r;
     201        }
     202
     203  private:
     204
     205        void init(size_type sz) { init(sz, sz); }
     206        void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
     207        char* start() const { return rep_->str; }
     208        char* finish() const { return rep_->str + rep_->size; }
     209
     210        struct Rep
     211        {
     212                size_type size, capacity;
     213                char str[1];
     214        };
     215
     216        void init(size_type sz, size_type cap)
     217        {
     218                if (cap)
     219                {
     220                        // Lee: the original form:
     221                        //      rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
     222                        // doesn't work in some cases of new being overloaded. Switching
     223                        // to the normal allocation, although use an 'int' for systems
     224                        // that are overly picky about structure alignment.
     225                        const size_type bytesNeeded = sizeof(Rep) + cap;
     226                        const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
     227                        rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
     228
     229                        rep_->str[ rep_->size = sz ] = '\0';
     230                        rep_->capacity = cap;
    208231                }
    209232                else
    210233                {
    211                         char smallstr [2];
    212                         smallstr [0] = single;
    213                         smallstr [1] = 0;
    214                         append (smallstr);
     234                        rep_ = &nullrep_;
    215235                }
    216     }
     236        }
     237
     238        void quit()
     239        {
     240                if (rep_ != &nullrep_)
     241                {
     242                        // The rep_ is really an array of ints. (see the allocator, above).
     243                        // Cast it back before delete, so the compiler won't incorrectly call destructors.
     244                        delete [] ( reinterpret_cast<int*>( rep_ ) );
     245                }
     246        }
     247
     248        Rep * rep_;
     249        static Rep nullrep_;
    217250
    218251} ;
    219252
    220 /*
     253
     254inline bool operator == (const TiXmlString & a, const TiXmlString & b)
     255{
     256        return    ( a.length() == b.length() )                          // optimization on some platforms
     257               && ( strcmp(a.c_str(), b.c_str()) == 0 );        // actual compare
     258}
     259inline bool operator < (const TiXmlString & a, const TiXmlString & b)
     260{
     261        return strcmp(a.c_str(), b.c_str()) < 0;
     262}
     263
     264inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
     265inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
     266inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
     267inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
     268
     269inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
     270inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
     271inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
     272inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
     273
     274TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
     275TiXmlString operator + (const TiXmlString & a, const char* b);
     276TiXmlString operator + (const char* a, const TiXmlString & b);
     277
     278
     279/*
    221280   TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
    222281   Only the operators that we need for TinyXML have been developped.
     
    225284{
    226285public :
    227     TiXmlOutStream () : TiXmlString () {}
    228 
    229     // TiXmlOutStream << operator. Maps to TiXmlString::append
    230     TiXmlOutStream & operator << (const char * in)
    231     {
    232         append (in);
    233         return (* this);
    234     }
    235 
    236     // TiXmlOutStream << operator. Maps to TiXmlString::append
    237     TiXmlOutStream & operator << (const TiXmlString & in)
    238     {
    239         append (in . c_str ());
    240         return (* this);
    241     }
     286
     287        // TiXmlOutStream << operator.
     288        TiXmlOutStream & operator << (const TiXmlString & in)
     289        {
     290                *this += in;
     291                return *this;
     292        }
     293
     294        // TiXmlOutStream << operator.
     295        TiXmlOutStream & operator << (const char * in)
     296        {
     297                *this += in;
     298                return *this;
     299        }
     300
    242301} ;
    243 
    244 #ifdef _MSC_VER
    245 #pragma warning( default : 4530 )
    246 #pragma warning( default : 4786 )
    247 #endif
    248302
    249303#endif  // TIXML_STRING_INCLUDED
Note: See TracChangeset for help on using the changeset viewer.