tinystr.h
上传用户:dlhongjiu
上传日期:2013-06-14
资源大小:1516k
文件大小:8k
源码类别:

信息检索与抽取

开发平台:

MultiPlatform

  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. /*
  36.    TiXmlString is an emulation of a subset of the std::string template.
  37.    Its purpose is to allow compiling TinyXML on compilers with no or poor STL support.
  38.    Only the member functions relevant to the TinyXML project have been implemented.
  39.    The buffer allocation is made by a simplistic power of 2 like mechanism : if we increase
  40.    a string and there's no more room, we allocate a buffer twice as big as we need.
  41. */
  42. class TiXmlString
  43. {
  44.   public :
  45. // The size type used
  46.    typedef unsigned int size_type;
  47. // Error value for find primitive
  48. static const size_type npos; // = -1;
  49. // TiXmlString empty constructor
  50. TiXmlString () : rep_(&nullrep_)
  51. {
  52. }
  53. // TiXmlString copy constructor
  54. TiXmlString (const TiXmlString & copy)
  55. {
  56. init(copy.length());
  57. memcpy(start(), copy.data(), length());
  58. }
  59. // TiXmlString constructor, based on a string
  60. TiXmlString (const char * copy)
  61. {
  62. init( static_cast<size_type>( strlen(copy) ));
  63. memcpy(start(), copy, length());
  64. }
  65. // TiXmlString constructor, based on a string
  66. TiXmlString (const char * str, size_type len)
  67. {
  68. init(len);
  69. memcpy(start(), str, len);
  70. }
  71. // TiXmlString destructor
  72. ~TiXmlString ()
  73. {
  74. quit();
  75. }
  76. // = operator
  77. TiXmlString& operator = (const char * copy)
  78. {
  79. return assign( copy, (size_type)strlen(copy));
  80. }
  81. // = operator
  82. TiXmlString& operator = (const TiXmlString & copy)
  83. {
  84. return assign(copy.start(), copy.length());
  85. }
  86. // += operator. Maps to append
  87. TiXmlString& operator += (const char * suffix)
  88. {
  89. return append(suffix, static_cast<size_type>( strlen(suffix) ));
  90. }
  91. // += operator. Maps to append
  92. TiXmlString& operator += (char single)
  93. {
  94. return append(&single, 1);
  95. }
  96. // += operator. Maps to append
  97. TiXmlString& operator += (const TiXmlString & suffix)
  98. {
  99. return append(suffix.data(), suffix.length());
  100. }
  101. // Convert a TiXmlString into a null-terminated char *
  102. const char * c_str () const { return rep_->str; }
  103. // Convert a TiXmlString into a char * (need not be null terminated).
  104. const char * data () const { return rep_->str; }
  105. // Return the length of a TiXmlString
  106. size_type length () const { return rep_->size; }
  107. // Alias for length()
  108. size_type size () const { return rep_->size; }
  109. // Checks if a TiXmlString is empty
  110. bool empty () const { return rep_->size == 0; }
  111. // Return capacity of string
  112. size_type capacity () const { return rep_->capacity; }
  113. // single char extraction
  114. const char& at (size_type index) const
  115. {
  116. assert( index < length() );
  117. return rep_->str[ index ];
  118. }
  119. // [] operator
  120. char& operator [] (size_type index) const
  121. {
  122. assert( index < length() );
  123. return rep_->str[ index ];
  124. }
  125. // find a char in a string. Return TiXmlString::npos if not found
  126. size_type find (char lookup) const
  127. {
  128. return find(lookup, 0);
  129. }
  130. // find a char in a string from an offset. Return TiXmlString::npos if not found
  131. size_type find (char tofind, size_type offset) const
  132. {
  133. if (offset >= length()) return npos;
  134. for (const char* p = c_str() + offset; *p != ''; ++p)
  135. {
  136.    if (*p == tofind) return static_cast< size_type >( p - c_str() );
  137. }
  138. return npos;
  139. }
  140. void clear ()
  141. {
  142. //Lee:
  143. //The original was just too strange, though correct:
  144. // TiXmlString().swap(*this);
  145. //Instead use the quit & re-init:
  146. quit();
  147. init(0,0);
  148. }
  149. /* Function to reserve a big amount of data when we know we'll need it. Be aware that this
  150. function DOES NOT clear the content of the TiXmlString if any exists.
  151. */
  152. void reserve (size_type cap);
  153. TiXmlString& assign (const char* str, size_type len);
  154. TiXmlString& append (const char* str, size_type len);
  155. void swap (TiXmlString& other)
  156. {
  157. Rep* r = rep_;
  158. rep_ = other.rep_;
  159. other.rep_ = r;
  160. }
  161.   private:
  162. void init(size_type sz) { init(sz, sz); }
  163. void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = ''; }
  164. char* start() const { return rep_->str; }
  165. char* finish() const { return rep_->str + rep_->size; }
  166. struct Rep
  167. {
  168. size_type size, capacity;
  169. char str[1];
  170. };
  171. void init(size_type sz, size_type cap)
  172. {
  173. if (cap)
  174. {
  175. // Lee: the original form:
  176. // rep_ = static_cast<Rep*>(operator new(sizeof(Rep) + cap));
  177. // doesn't work in some cases of new being overloaded. Switching
  178. // to the normal allocation, although use an 'int' for systems
  179. // that are overly picky about structure alignment.
  180. const size_type bytesNeeded = sizeof(Rep) + cap;
  181. const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); 
  182. rep_ = reinterpret_cast<Rep*>( new int[ intsNeeded ] );
  183. rep_->str[ rep_->size = sz ] = '';
  184. rep_->capacity = cap;
  185. }
  186. else
  187. {
  188. rep_ = &nullrep_;
  189. }
  190. }
  191. void quit()
  192. {
  193. if (rep_ != &nullrep_)
  194. {
  195. // The rep_ is really an array of ints. (see the allocator, above).
  196. // Cast it back before delete, so the compiler won't incorrectly call destructors.
  197. delete [] ( reinterpret_cast<int*>( rep_ ) );
  198. }
  199. }
  200. Rep * rep_;
  201. static Rep nullrep_;
  202. } ;
  203. inline bool operator == (const TiXmlString & a, const TiXmlString & b)
  204. {
  205. return    ( a.length() == b.length() ) // optimization on some platforms
  206.        && ( strcmp(a.c_str(), b.c_str()) == 0 ); // actual compare
  207. }
  208. inline bool operator < (const TiXmlString & a, const TiXmlString & b)
  209. {
  210. return strcmp(a.c_str(), b.c_str()) < 0;
  211. }
  212. inline bool operator != (const TiXmlString & a, const TiXmlString & b) { return !(a == b); }
  213. inline bool operator >  (const TiXmlString & a, const TiXmlString & b) { return b < a; }
  214. inline bool operator <= (const TiXmlString & a, const TiXmlString & b) { return !(b < a); }
  215. inline bool operator >= (const TiXmlString & a, const TiXmlString & b) { return !(a < b); }
  216. inline bool operator == (const TiXmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; }
  217. inline bool operator == (const char* a, const TiXmlString & b) { return b == a; }
  218. inline bool operator != (const TiXmlString & a, const char* b) { return !(a == b); }
  219. inline bool operator != (const char* a, const TiXmlString & b) { return !(b == a); }
  220. TiXmlString operator + (const TiXmlString & a, const TiXmlString & b);
  221. TiXmlString operator + (const TiXmlString & a, const char* b);
  222. TiXmlString operator + (const char* a, const TiXmlString & b);
  223. /*
  224.    TiXmlOutStream is an emulation of std::ostream. It is based on TiXmlString.
  225.    Only the operators that we need for TinyXML have been developped.
  226. */
  227. class TiXmlOutStream : public TiXmlString
  228. {
  229. public :
  230. // TiXmlOutStream << operator.
  231. TiXmlOutStream & operator << (const TiXmlString & in)
  232. {
  233. *this += in;
  234. return *this;
  235. }
  236. // TiXmlOutStream << operator.
  237. TiXmlOutStream & operator << (const char * in)
  238. {
  239. *this += in;
  240. return *this;
  241. }
  242. } ;
  243. #endif // TIXML_STRING_INCLUDED
  244. #endif // TIXML_USE_STL