Stream.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #ifndef CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
  2. #define CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
  3. // This module define:
  4. // Type CppUT::Stream (either std::stream or a custom type)
  5. // Type CppUT::OStringStream (eitjer std::ostringstream, older alternate or a custom type)
  6. // Functions stdCOut() & stdCErr() which returns a reference on cout & cerr stream (or our
  7. // custom stream).
  8. #include <cppunit/Portability.h>
  9. #if defined( CPPUNIT_NO_STREAM )
  10. #include <string>
  11. #include <stdio.h>
  12. #include <string.h>
  13. CPPUNIT_NS_BEGIN
  14. class StreamBuffer
  15. {
  16. public:
  17. virtual ~StreamBuffer() {}
  18. virtual void write( const char *text, unsigned int length ) = 0;
  19. virtual void flush() {}
  20. };
  21. class StringStreamBuffer : public StreamBuffer
  22. {
  23. public:
  24. std::string str() const
  25. {
  26. return str_;
  27. }
  28. public: // overridden from StreamBuffer
  29. void write( const char *text, unsigned int length )
  30. {
  31. str_.append( text, length );
  32. }
  33. private:
  34. std::string str_;
  35. };
  36. class FileStreamBuffer : public StreamBuffer
  37. {
  38. public:
  39. FileStreamBuffer( FILE *file )
  40. : file_( file )
  41. {
  42. }
  43. FILE *file() const
  44. {
  45. return file_;
  46. }
  47. public: // overridden from StreamBuffer
  48. void write( const char *text, unsigned int length )
  49. {
  50. if ( file_ )
  51. fwrite( text, sizeof(char), length, file_ );
  52. }
  53. void flush()
  54. {
  55. if ( file_ )
  56. fflush( file_ );
  57. }
  58. private:
  59. FILE *file_;
  60. };
  61. class OStream
  62. {
  63. public:
  64. OStream()
  65. : buffer_( 0 )
  66. {
  67. }
  68. OStream( StreamBuffer *buffer )
  69. : buffer_( buffer )
  70. {
  71. }
  72. virtual ~OStream()
  73. {
  74. flush();
  75. }
  76. OStream &flush()
  77. {
  78. if ( buffer_ )
  79. buffer_->flush();
  80. return *this;
  81. }
  82. void setBuffer( StreamBuffer *buffer )
  83. {
  84. buffer_ = buffer;
  85. }
  86. OStream &write( const char *text, unsigned int length )
  87. {
  88. if ( buffer_ )
  89. buffer_->write( text, length );
  90. return *this;
  91. }
  92. OStream &write( const char *text )
  93. {
  94. return write( text, strlen(text) );
  95. }
  96. OStream &operator <<( bool v )
  97. {
  98. const char *out = v ? "true" : "false";
  99. return write( out );
  100. }
  101. OStream &operator <<( short v )
  102. {
  103. char buffer[64];
  104. sprintf( buffer, "%hd", v );
  105. return write( buffer );
  106. }
  107. OStream &operator <<( unsigned short v )
  108. {
  109. char buffer[64];
  110. sprintf( buffer, "%hu", v );
  111. return write( buffer );
  112. }
  113. OStream &operator <<( int v )
  114. {
  115. char buffer[64];
  116. sprintf( buffer, "%d", v );
  117. return write( buffer );
  118. }
  119. OStream &operator <<( unsigned int v )
  120. {
  121. char buffer[64];
  122. sprintf( buffer, "%u", v );
  123. return write( buffer );
  124. }
  125. OStream &operator <<( long v )
  126. {
  127. char buffer[64];
  128. sprintf( buffer, "%ld", v );
  129. return write( buffer );
  130. }
  131. OStream &operator <<( unsigned long v )
  132. {
  133. char buffer[64];
  134. sprintf( buffer, "%lu", v );
  135. return write( buffer );
  136. }
  137. OStream &operator <<( float v )
  138. {
  139. char buffer[128];
  140. sprintf( buffer, "%.16g", double(v) );
  141. return write( buffer );
  142. }
  143. OStream &operator <<( double v )
  144. {
  145. char buffer[128];
  146. sprintf( buffer, "%.16g", v );
  147. return write( buffer );
  148. }
  149. OStream &operator <<( long double v )
  150. {
  151. char buffer[128];
  152. sprintf( buffer, "%.16g", double(v) );
  153. return write( buffer );
  154. }
  155. OStream &operator <<( const void *v )
  156. {
  157. char buffer[64];
  158. sprintf( buffer, "%p", v );
  159. return write( buffer );
  160. }
  161. OStream &operator <<( const char *v )
  162. {
  163. return write( v ? v : "NULL" );
  164. }
  165. OStream &operator <<( char c )
  166. {
  167. char buffer[16];
  168. sprintf( buffer, "%c", c );
  169. return write( buffer );
  170. }
  171. OStream &operator <<( const std::string &s )
  172. {
  173. return write( s.c_str(), s.length() );
  174. }
  175. private:
  176. StreamBuffer *buffer_;
  177. };
  178. class OStringStream : public OStream
  179. {
  180. public:
  181. OStringStream()
  182. : OStream( &buffer_ )
  183. {
  184. }
  185. std::string str() const
  186. {
  187. return buffer_.str();
  188. }
  189. private:
  190. StringStreamBuffer buffer_;
  191. };
  192. class OFileStream : public OStream
  193. {
  194. public:
  195. OFileStream( FILE *file )
  196. : OStream( &buffer_ )
  197. , buffer_( file )
  198. , ownFile_( false )
  199. {
  200. }
  201. OFileStream( const char *path )
  202. : OStream( &buffer_ )
  203. , buffer_( fopen( path, "wt" ) )
  204. , ownFile_( true )
  205. {
  206. }
  207. virtual ~OFileStream()
  208. {
  209. if ( ownFile_ && buffer_.file() )
  210. fclose( buffer_.file() );
  211. }
  212. private:
  213. FileStreamBuffer buffer_;
  214. bool ownFile_;
  215. };
  216. inline OStream &stdCOut()
  217. {
  218. static OFileStream stream( stdout );
  219. return stream;
  220. }
  221. inline OStream &stdCErr()
  222. {
  223. static OFileStream stream( stderr );
  224. return stream;
  225. }
  226. CPPUNIT_NS_END
  227. #elif CPPUNIT_HAVE_SSTREAM // #if defined( CPPUNIT_NO_STREAM )
  228. # include <sstream>
  229. # include <fstream>
  230. CPPUNIT_NS_BEGIN
  231. typedef std::ostringstream OStringStream; // The standard C++ way
  232. typedef std::ofstream OFileStream;
  233. CPPUNIT_NS_END
  234. #elif CPPUNIT_HAVE_CLASS_STRSTREAM
  235. # include <string>
  236. # if CPPUNIT_HAVE_STRSTREAM
  237. # include <strstream>
  238. # else // CPPUNIT_HAVE_STRSTREAM
  239. # include <strstream.h>
  240. # endif // CPPUNIT_HAVE_CLASS_STRSTREAM
  241. CPPUNIT_NS_BEGIN
  242. class OStringStream : public std::ostrstream
  243. {
  244. public:
  245. std::string str()
  246. {
  247. // (*this) << '\0';
  248. // std::string msg(std::ostrstream::str());
  249. // std::ostrstream::freeze(false);
  250. // return msg;
  251. // Alternative implementation that don't rely on freeze which is not
  252. // available on some platforms:
  253. return std::string( std::ostrstream::str(), pcount() );
  254. }
  255. };
  256. CPPUNIT_NS_END
  257. #else // CPPUNIT_HAVE_CLASS_STRSTREAM
  258. # error Cannot define CppUnit::OStringStream.
  259. #endif // #if defined( CPPUNIT_NO_STREAM )
  260. #if !defined( CPPUNIT_NO_STREAM )
  261. #include <iostream>
  262. CPPUNIT_NS_BEGIN
  263. typedef std::ostream OStream;
  264. inline OStream &stdCOut()
  265. {
  266. return std::cout;
  267. }
  268. inline OStream &stdCErr()
  269. {
  270. return std::cerr;
  271. }
  272. CPPUNIT_NS_END
  273. #endif // #if !defined( CPPUNIT_NO_STREAM )
  274. #endif // CPPUNIT_PORTABILITY_STREAM_H_INCLUDED