1
0

tinystr.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original file by Yves Berquin.
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. /*
  20. * THIS FILE WAS ALTERED BY Tyge Lovset, 7. April 2005.
  21. *
  22. * - completely rewritten. compact, clean, and fast implementation.
  23. * - sizeof(TiXmlString) = pointer size (4 bytes on 32-bit systems)
  24. * - fixed reserve() to work as per specification.
  25. * - fixed buggy compares operator==(), operator<(), and operator>()
  26. * - fixed operator+=() to take a const ref argument, following spec.
  27. * - added "copy" constructor with length, and most compare operators.
  28. * - added swap(), clear(), size(), capacity(), operator+().
  29. */
  30. #ifndef TIXML_USE_STL
  31. #ifndef TIXML_STRING_INCLUDED
  32. #define TIXML_STRING_INCLUDED
  33. #include <assert.h>
  34. #include <string.h>
  35. /* The support for explicit isn't that universal, and it isn't really
  36. required - it is used to check that the TiXmlString class isn't incorrectly
  37. used. Be nice to old compilers and macro it here:
  38. */
  39. #if defined(_MSC_VER) && (_MSC_VER >= 1200 )
  40. // Microsoft visual studio, version 6 and higher.
  41. #define TIXML_EXPLICIT explicit
  42. #elif defined(__GNUC__) && (__GNUC__ >= 3 )
  43. // GCC version 3 and higher.s
  44. #define TIXML_EXPLICIT explicit
  45. #else
  46. #define TIXML_EXPLICIT
  47. #endif
  48. /*
  49. TiXmlString is an emulation of a subset of the std::string template.
  50. Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
  51. Only the member functions relevant to the TinyXML project have been implemented.
  52. The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
  53. a string and there's no more room, we allocate a buffer twice as big as we need.
  54. */
  55. class TiXmlString
  56. {
  57. public :
  58. // The size type used
  59. typedef size_t size_type;
  60. // Error value for find primitive
  61. static const size_type npos; // = -1;
  62. // TiXmlString empty constructor
  63. TiXmlString () : rep_(&nullrep_)
  64. {
  65. }
  66. // TiXmlString copy constructor
  67. TiXmlString ( const TiXmlString & copy) : rep_(0)
  68. {
  69. init(copy.length());
  70. memcpy(start(), copy.data(), length());
  71. }
  72. // TiXmlString constructor, based on a string
  73. TIXML_EXPLICIT TiXmlString ( const char * copy) : rep_(0)
  74. {
  75. init( static_cast<size_type>( strlen(copy) ));
  76. memcpy(start(), copy, length());
  77. }
  78. // TiXmlString constructor, based on a string
  79. TIXML_EXPLICIT TiXmlString ( const char * str, size_type len) : rep_(0)
  80. {
  81. init(len);
  82. memcpy(start(), str, len);
  83. }
  84. // TiXmlString destructor
  85. ~TiXmlString ()
  86. {
  87. quit();
  88. }
  89. // = operator
  90. TiXmlString& operator = (const char * copy)
  91. {
  92. return assign( copy, (size_type)strlen(copy));
  93. }
  94. // = operator
  95. TiXmlString& operator = (const TiXmlString & copy)
  96. {
  97. return assign(copy.start(), copy.length());
  98. }
  99. // += operator. Maps to append
  100. TiXmlString& operator += (const char * suffix)
  101. {
  102. return append(suffix, static_cast<size_type>( strlen(suffix) ));
  103. }
  104. // += operator. Maps to append
  105. TiXmlString& operator += (char single)
  106. {
  107. return append(&single, 1);
  108. }
  109. // += operator. Maps to append
  110. TiXmlString& operator += (const TiXmlString & suffix)
  111. {
  112. return append(suffix.data(), suffix.length());
  113. }
  114. // Convert a TiXmlString into a null-terminated char *
  115. const char * c_str () const { return rep_->str; }
  116. // Convert a TiXmlString into a char * (need not be null terminated).
  117. const char * data () const { return rep_->str; }
  118. // Return the length of a TiXmlString
  119. size_type length () const { return rep_->size; }
  120. // Alias for length()
  121. size_type size () const { return rep_->size; }
  122. // Checks if a TiXmlString is empty
  123. bool empty () const { return rep_->size == 0; }
  124. // Return capacity of string
  125. size_type capacity () const { return rep_->capacity; }
  126. // single char extraction
  127. const char& at (size_type index) const
  128. {
  129. assert( index < length() );
  130. return rep_->str[ index ];
  131. }
  132. // [] operator
  133. char& operator [] (size_type index) const
  134. {
  135. assert( index < length() );
  136. return rep_->str[ index ];
  137. }
  138. // find a char in a string. Return TiXmlString::npos if not found
  139. size_type find (char lookup) const
  140. {
  141. return find(lookup, 0);
  142. }
  143. // find a char in a string from an offset. Return TiXmlString::npos if not found
  144. size_type find (char tofind, size_type offset) const
  145. {
  146. if (offset >= length()) return npos;
  147. for (const char* p = c_str() + offset; *p != '\0'; ++p)
  148. {
  149. if (*p == tofind) return static_cast< size_type >( p - c_str() );
  150. }
  151. return npos;
  152. }
  153. void clear ()
  154. {
  155. //Lee:
  156. //The original was just too strange, though correct:
  157. // TiXmlString().swap(*this);
  158. //Instead use the quit & re-init:
  159. quit();
  160. init(0,0);
  161. }
  162. /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
  163. function DOES NOT clear the content of the TiXmlString if any exists.
  164. */
  165. void reserve (size_type cap);
  166. TiXmlString& assign (const char* str, size_type len);
  167. TiXmlString& append (const char* str, size_type len);
  168. void swap (TiXmlString& other)
  169. {
  170. Rep* r = rep_;
  171. rep_ = other.rep_;
  172. other.rep_ = r;
  173. }
  174. private:
  175. void init(size_type sz) { init(sz, sz); }
  176. void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; }
  177. char* start() const { return rep_->str; }
  178. char* finish() const { return rep_->str + rep_->size; }
  179. struct Rep
  180. {
  181. size_type size, capacity;
  182. char str[1];
  183. };
  184. void init(size_type sz, size_type cap)
  185. {
  186. if (cap)
  187. {
  188. // Lee: the original form:
  189. // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
  190. // doesn't work in some cases of new being overloaded. Switching
  191. // to the normal allocation, although use an 'int' for systems
  192. // that are overly picky about structure alignment.
  193. const size_type bytesNeeded = sizeof(Rep) + cap;
  194. const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int );
  195. rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
  196. rep_->str[ rep_->size = sz ] = '\0';
  197. rep_->capacity = cap;
  198. }
  199. else
  200. {
  201. rep_ = &nullrep_;
  202. }
  203. }
  204. void quit()
  205. {
  206. if (rep_ != &nullrep_)
  207. {
  208. // The rep_ is really an array of ints. (see the allocator, above).
  209. // Cast it back before delete, so the compiler won't incorrectly call destructors.
  210. delete [] ( reinterpret_cast<int*>( rep_ ) );
  211. }
  212. }
  213. Rep * rep_;
  214. static Rep nullrep_;
  215. } ;
  216. inline bool operator == (const TiXmlString & a, const TiXmlString & b)
  217. {
  218. return ( a.length() == b.length() ) // optimization on some platforms
  219. && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
  220. }
  221. inline bool operator < (const TiXmlString & a, const TiXmlString & b)
  222. {
  223. return strcmp(a.c_str(), b.c_str()) < 0;
  224. }
  225. inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
  226. inline bool operator > (const TiXmlString & a, const TiXmlString & b) { return b < a; }
  227. inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
  228. inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
  229. inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
  230. inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
  231. inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
  232. inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
  233. TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
  234. TiXmlString operator + (const TiXmlString & a, const char* b);
  235. TiXmlString operator + (const char* a, const TiXmlString & b);
  236. /*
  237. TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
  238. Only the operators that we need for TinyXML have been developped.
  239. */
  240. class TiXmlOutStream : public TiXmlString
  241. {
  242. public :
  243. // TiXmlOutStream << operator.
  244. TiXmlOutStream & operator << (const TiXmlString & in)
  245. {
  246. *this += in;
  247. return *this;
  248. }
  249. // TiXmlOutStream << operator.
  250. TiXmlOutStream & operator << (const char * in)
  251. {
  252. *this += in;
  253. return *this;
  254. }
  255. } ;
  256. #endif // TIXML_STRING_INCLUDED
  257. #endif // TIXML_USE_STL