kwsys_ios_sstream.h.in 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_ios_sstream
  11. #define @KWSYS_NAMESPACE@_ios_sstream
  12. #include <@KWSYS_NAMESPACE@/Configure.hxx>
  13. /* Define this macro temporarily to keep the code readable. */
  14. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  15. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  16. #endif
  17. #if @KWSYS_NAMESPACE@_IOS_USE_SSTREAM
  18. # ifdef _MSC_VER
  19. # pragma warning (push, 1)
  20. # pragma warning (disable: 4702)
  21. # endif
  22. # include <sstream>
  23. # ifdef _MSC_VER
  24. # pragma warning(pop)
  25. # endif
  26. #else
  27. # ifdef _MSC_VER
  28. # pragma warning (push, 1)
  29. # pragma warning (disable: 4702)
  30. # pragma warning (disable: 4995) /* Old streams are deprecated. */
  31. # endif
  32. # if @KWSYS_NAMESPACE@_IOS_USE_ANSI
  33. # include <strstream>
  34. # elif @KWSYS_NAMESPACE@_IOS_USE_STRSTREAM_H
  35. # include <strstream.h>
  36. # elif @KWSYS_NAMESPACE@_IOS_USE_STRSTREA_H
  37. # include <strstrea.h>
  38. # endif
  39. # if @KWSYS_NAMESPACE@_IOS_USE_ANSI
  40. # include <new> // Need placement operator new.
  41. # else
  42. # include <new.h> // Need placement operator new.
  43. # endif
  44. # ifdef _MSC_VER
  45. # pragma warning(pop)
  46. # endif
  47. // Only have old std strstream classes. Wrap them to look like new
  48. // ostringstream and istringstream classes.
  49. # include <@KWSYS_NAMESPACE@/stl/string>
  50. namespace @KWSYS_NAMESPACE@_ios
  51. {
  52. using @KWSYS_NAMESPACE@_ios_namespace::streambuf;
  53. using @KWSYS_NAMESPACE@_ios_namespace::ostream;
  54. using @KWSYS_NAMESPACE@_ios_namespace::istream;
  55. using @KWSYS_NAMESPACE@_ios_namespace::strstream;
  56. using @KWSYS_NAMESPACE@_ios_namespace::istrstream;
  57. using @KWSYS_NAMESPACE@_ios_namespace::ostrstream;
  58. using @KWSYS_NAMESPACE@_ios_namespace::ios;
  59. using @KWSYS_NAMESPACE@_ios_namespace::endl;
  60. using @KWSYS_NAMESPACE@_ios_namespace::ends;
  61. using @KWSYS_NAMESPACE@_ios_namespace::flush;
  62. class stringstream_cleanup
  63. {
  64. public:
  65. stringstream_cleanup(strstream& str): m_StrStream(str) {}
  66. ~stringstream_cleanup() { m_StrStream.rdbuf()->freeze(0); }
  67. static void IgnoreUnusedVariable(const stringstream_cleanup&) {}
  68. protected:
  69. strstream& m_StrStream;
  70. private:
  71. void operator=(stringstream_cleanup const&);
  72. };
  73. class stringstream: public strstream
  74. {
  75. public:
  76. typedef strstream Superclass;
  77. stringstream() {}
  78. stringstream(const kwsys_stl::string& s) { *this << s.c_str(); }
  79. kwsys_stl::string str()
  80. {
  81. stringstream_cleanup cleanup(*this);
  82. stringstream_cleanup::IgnoreUnusedVariable(cleanup);
  83. // Visual Studio 6 has a strstream::pcount, but this is not rdbuf()->pcount()
  84. #if (@KWSYS_NAMESPACE@_IOS_USE_STRSTREA_H) && defined(_MSC_VER) && (_MSC_VER == 1200)
  85. int count = this->pcount();
  86. #elif defined(__WATCOMC__)
  87. int count = this->rdbuf()->out_waiting();
  88. #else
  89. int count = this->rdbuf()->pcount();
  90. #endif
  91. const char* ptr = this->Superclass::str();
  92. return kwsys_stl::string(ptr?ptr:"", count);
  93. }
  94. void str(const kwsys_stl::string& s)
  95. {
  96. this->~stringstream();
  97. new (this) stringstream(s);
  98. }
  99. private:
  100. stringstream(const stringstream&);
  101. void operator=(const stringstream&);
  102. };
  103. class ostringstream_cleanup
  104. {
  105. public:
  106. ostringstream_cleanup(ostrstream& ostr): m_OStrStream(ostr) {}
  107. ~ostringstream_cleanup() { m_OStrStream.rdbuf()->freeze(0); }
  108. static void IgnoreUnusedVariable(const ostringstream_cleanup&) {}
  109. protected:
  110. ostrstream& m_OStrStream;
  111. private:
  112. void operator=(ostringstream_cleanup const&);
  113. };
  114. class ostringstream: public ostrstream
  115. {
  116. public:
  117. typedef ostrstream Superclass;
  118. ostringstream() {}
  119. ostringstream(const kwsys_stl::string& s) { *this << s.c_str(); }
  120. kwsys_stl::string str()
  121. {
  122. ostringstream_cleanup cleanup(*this);
  123. ostringstream_cleanup::IgnoreUnusedVariable(cleanup);
  124. int count = this->pcount();
  125. const char* ptr = this->Superclass::str();
  126. return kwsys_stl::string(ptr?ptr:"", count);
  127. }
  128. void str(const kwsys_stl::string& s)
  129. {
  130. this->~ostringstream();
  131. new (this) ostringstream(s);
  132. }
  133. private:
  134. ostringstream(const ostringstream&);
  135. void operator=(const ostringstream&);
  136. };
  137. #if defined(_MSC_VER)
  138. # pragma warning (push)
  139. # pragma warning (disable: 4097) /* typedef-name used as synonym for class */
  140. #endif
  141. #if defined(__WATCOMC__)
  142. // W728: class modifiers for 'A' conflict with class modifiers for 'B'
  143. # pragma warning 728 10
  144. #endif
  145. class istringstream: private kwsys_stl::string, public istrstream
  146. {
  147. public:
  148. typedef kwsys_stl::string StdString;
  149. typedef istrstream IStrStream;
  150. istringstream(): StdString(),
  151. IStrStream(const_cast<char*>(StdString::c_str())) {}
  152. istringstream(const kwsys_stl::string& s):
  153. StdString(s), IStrStream(const_cast<char*>(StdString::c_str())) {}
  154. kwsys_stl::string str() const { return *this; }
  155. void str(const kwsys_stl::string& s)
  156. {
  157. this->~istringstream();
  158. new (this) istringstream(s);
  159. }
  160. void clear(int flags)
  161. {
  162. this->IStrStream::clear(flags);
  163. }
  164. private:
  165. istringstream(const istringstream&);
  166. void operator=(const istringstream&);
  167. };
  168. #if defined(__WATCOMC__)
  169. # pragma warning 728 9
  170. #endif
  171. #if defined(_MSC_VER)
  172. # pragma warning (pop)
  173. #endif
  174. } // namespace @KWSYS_NAMESPACE@_ios
  175. #endif
  176. /* Undefine temporary macro. */
  177. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  178. # undef kwsys_stl
  179. #endif
  180. #endif