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.cc

    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"
     25/*
     26 * THIS FILE WAS ALTERED BY Tyge Løvset, 7. April 2005.
     27 */
     28
    2629
    2730#ifndef TIXML_USE_STL
    2831
    29 
    30 #include <stdlib.h>
    31 #include <string.h>
    32 #include <ctype.h>
    33 
    3432#include "tinystr.h"
    3533
    36 // TiXmlString constructor, based on a C string
    37 TiXmlString::TiXmlString (const char* instring)
     34// Error value for find primitive
     35const TiXmlString::size_type TiXmlString::npos = static_cast< size_type >(-1);
     36
     37// Null rep.
     38TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, '\0' };
     39
     40
     41void TiXmlString::reserve (size_type cap)
    3842{
    39     size_t newlen;
    40     char * newstring;
    41 
    42     if (!instring)
    43     {
    44         allocated = 0;
    45         cstring = NULL;
    46         current_length = 0;
    47         return;
    48     }
    49     newlen = strlen (instring) + 1;
    50     newstring = new char [newlen];
    51     memcpy (newstring, instring, newlen);
    52     // strcpy (newstring, instring);
    53     allocated = newlen;
    54     cstring = newstring;
    55     current_length = newlen - 1;
    56 }
    57 
    58 // TiXmlString copy constructor
    59 TiXmlString::TiXmlString (const TiXmlString& copy)
    60 {
    61     size_t newlen;
    62     char * newstring;
    63 
    64         // Prevent copy to self!
    65         if ( &copy == this )
    66                 return;
    67 
    68     if (! copy . allocated)
    69     {
    70         allocated = 0;
    71         cstring = NULL;
    72         current_length = 0;
    73         return;
    74     }
    75     newlen = copy . length () + 1;
    76     newstring = new char [newlen];
    77     // strcpy (newstring, copy . cstring);
    78     memcpy (newstring, copy . cstring, newlen);
    79     allocated = newlen;
    80     cstring = newstring;
    81     current_length = newlen - 1;
    82 }
    83 
    84 // TiXmlString = operator. Safe when assign own content
    85 void TiXmlString ::operator = (const char * content)
    86 {
    87     size_t newlen;
    88     char * newstring;
    89 
    90     if (! content)
    91     {
    92         empty_it ();
    93         return;
    94     }
    95     newlen = strlen (content) + 1;
    96     newstring = new char [newlen];
    97     // strcpy (newstring, content);
    98     memcpy (newstring, content, newlen);
    99     empty_it ();
    100     allocated = newlen;
    101     cstring = newstring;
    102     current_length = newlen - 1;
    103 }
    104 
    105 // = operator. Safe when assign own content
    106 void TiXmlString ::operator = (const TiXmlString & copy)
    107 {
    108     size_t newlen;
    109     char * newstring;
    110 
    111     if (! copy . length ())
    112     {
    113         empty_it ();
    114         return;
    115     }
    116     newlen = copy . length () + 1;
    117     newstring = new char [newlen];
    118     // strcpy (newstring, copy . c_str ());
    119     memcpy (newstring, copy . c_str (), newlen);
    120     empty_it ();
    121     allocated = newlen;
    122     cstring = newstring;
    123     current_length = newlen - 1;
     43        if (cap > capacity())
     44        {
     45                TiXmlString tmp;
     46                tmp.init(length(), cap);
     47                memcpy(tmp.start(), data(), length());
     48                swap(tmp);
     49        }
    12450}
    12551
    12652
    127 // append a const char * to an existing TiXmlString
    128 void TiXmlString::append( const char* str, size_t len )
     53TiXmlString& TiXmlString::assign(const char* str, size_type len)
    12954{
    130     char * new_string;
    131     size_t new_alloc, new_size, size_suffix;
    132        
    133         // don't use strlen - it can overrun the len passed in!
    134         const char* p = str;
    135         size_suffix = 0;
    136 
    137         while ( *p && size_suffix < (unsigned)len )
     55        size_type cap = capacity();
     56        if (len > cap || cap > 3*(len + 8))
    13857        {
    139                 ++p;
    140                 ++size_suffix;
     58                TiXmlString tmp;
     59                tmp.init(len);
     60                memcpy(tmp.start(), str, len);
     61                swap(tmp);
    14162        }
    142     if ( !size_suffix)
    143         return;
    144 
    145     new_size = length () + size_suffix + 1;
    146     // check if we need to expand
    147     if (new_size > allocated)
    148     {
    149         // compute new size
    150         new_alloc = assign_new_size (new_size);
    151 
    152         // allocate new buffer
    153         new_string = new char [new_alloc];       
    154         new_string [0] = 0;
    155 
    156         // copy the previous allocated buffer into this one
    157         if (allocated && cstring)
    158             // strcpy (new_string, cstring);
    159             memcpy (new_string, cstring, length ());
    160 
    161         // append the suffix. It does exist, otherwize we wouldn't be expanding
    162         // strncat (new_string, str, len);
    163         memcpy (new_string + length (),
    164                 str,
    165                 size_suffix);
    166 
    167         // return previsously allocated buffer if any
    168         if (allocated && cstring)
    169             delete [] cstring;
    170 
    171         // update member variables
    172         cstring = new_string;
    173         allocated = new_alloc;
    174     }
    175     else
    176     {
    177         // we know we can safely append the new string
    178         // strncat (cstring, str, len);
    179         memcpy (cstring + length (),
    180                 str,
    181                 size_suffix);
    182     }
    183     current_length = new_size - 1;
    184     cstring [current_length] = 0;
     63        else
     64        {
     65                memmove(start(), str, len);
     66                set_size(len);
     67        }
     68        return *this;
    18569}
    18670
    18771
    188 // append a const char * to an existing TiXmlString
    189 void TiXmlString::append( const char * suffix )
     72TiXmlString& TiXmlString::append(const char* str, size_type len)
    19073{
    191     char * new_string;
    192     size_t new_alloc, new_size;
    193 
    194     new_size = length () + strlen (suffix) + 1;
    195     // check if we need to expand
    196     if (new_size > allocated)
    197     {
    198         // compute new size
    199         new_alloc = assign_new_size (new_size);
    200 
    201         // allocate new buffer
    202         new_string = new char [new_alloc];       
    203         new_string [0] = 0;
    204 
    205         // copy the previous allocated buffer into this one
    206         if (allocated && cstring)
    207             memcpy (new_string, cstring, 1 + length ());
    208             // strcpy (new_string, cstring);
    209 
    210         // append the suffix. It does exist, otherwize we wouldn't be expanding
    211         // strcat (new_string, suffix);
    212         memcpy (new_string + length (),
    213                 suffix,
    214                 strlen (suffix) + 1);
    215 
    216         // return previsously allocated buffer if any
    217         if (allocated && cstring)
    218             delete [] cstring;
    219 
    220         // update member variables
    221         cstring = new_string;
    222         allocated = new_alloc;
    223     }
    224     else
    225     {
    226         // we know we can safely append the new string
    227         // strcat (cstring, suffix);
    228         memcpy (cstring + length (),
    229                 suffix,
    230                 strlen (suffix) + 1);
    231     }
    232     current_length = new_size - 1;
    233 }
    234 
    235 // Check for TiXmlString equuivalence
    236 //bool TiXmlString::operator == (const TiXmlString & compare) const
    237 //{
    238 //    return (! strcmp (c_str (), compare . c_str ()));
    239 //}
    240 
    241 //unsigned TiXmlString::length () const
    242 //{
    243 //    if (allocated)
    244 //        // return strlen (cstring);
    245 //        return current_length;
    246 //    return 0;
    247 //}
    248 
    249 
    250 unsigned TiXmlString::find (char tofind, unsigned offset) const
    251 {
    252     char * lookup;
    253 
    254     if (offset >= length ())
    255         return (unsigned) notfound;
    256     for (lookup = cstring + offset; * lookup; lookup++)
    257         if (* lookup == tofind)
    258             return (unsigned)(lookup - cstring);
    259     return (unsigned) notfound;
     74        size_type newsize = length() + len;
     75        if (newsize > capacity())
     76        {
     77                reserve (newsize + capacity());
     78        }
     79        memmove(finish(), str, len);
     80        set_size(newsize);
     81        return *this;
    26082}
    26183
    26284
    263 bool TiXmlString::operator == (const TiXmlString & compare) const
     85TiXmlString operator + (const TiXmlString & a, const TiXmlString & b)
    26486{
    265         if ( allocated && compare.allocated )
    266         {
    267                 assert( cstring );
    268                 assert( compare.cstring );
    269                 return ( strcmp( cstring, compare.cstring ) == 0 );
    270         }
    271         else if ( length() == 0 && compare.length() == 0 )
    272         {
    273                 return true;
    274         }
    275         return false;
     87        TiXmlString tmp;
     88        tmp.reserve(a.length() + b.length());
     89        tmp += a;
     90        tmp += b;
     91        return tmp;
    27692}
    27793
    278 
    279 bool TiXmlString::operator == (const char* compare) const
     94TiXmlString operator + (const TiXmlString & a, const char* b)
    28095{
    281         if ( allocated && compare && *compare )
    282         {
    283                 assert( cstring );
    284                 return ( strcmp( cstring, compare ) == 0 );
    285         }
    286         else if ( length() == 0 && (!compare || !*compare ) )   // this is a little dubious, but try to duplicate behavior in other operator==
    287         {
    288                 return true;
    289         }
    290         return false;   
     96        TiXmlString tmp;
     97        TiXmlString::size_type b_len = static_cast<TiXmlString::size_type>( strlen(b) );
     98        tmp.reserve(a.length() + b_len);
     99        tmp += a;
     100        tmp.append(b, b_len);
     101        return tmp;
    291102}
    292103
    293 
    294 bool TiXmlString::operator < (const TiXmlString & compare) const
     104TiXmlString operator + (const char* a, const TiXmlString & b)
    295105{
    296         if ( allocated && compare.allocated )
    297         {
    298                 assert( cstring );
    299                 assert( compare.cstring );
    300                 return ( strcmp( cstring, compare.cstring ) > 0 );
    301         }
    302         return false;
    303 }
    304 
    305 
    306 bool TiXmlString::operator > (const TiXmlString & compare) const
    307 {
    308         if ( allocated && compare.allocated )
    309         {
    310                 assert( cstring );
    311                 assert( compare.cstring );
    312                 return ( strcmp( cstring, compare.cstring ) < 0 );
    313         }
    314         return false;
     106        TiXmlString tmp;
     107        TiXmlString::size_type a_len = static_cast<TiXmlString::size_type>( strlen(a) );
     108        tmp.reserve(a_len + b.length());
     109        tmp.append(a, a_len);
     110        tmp += b;
     111        return tmp;
    315112}
    316113
Note: See TracChangeset for help on using the changeset viewer.