tinystr.h 8.0 KB

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