1 #ifndef CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
2 #define CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
13 #if defined( CPPUNIT_NO_STREAM )
24 virtual ~StreamBuffer() {}
26 virtual void write(
const char *text,
unsigned int length ) = 0;
28 virtual void flush() {}
32 class StringStreamBuffer :
public StreamBuffer
35 std::string str()
const
41 void write(
const char *text,
unsigned int length )
43 str_.append( text, length );
51 class FileStreamBuffer :
public StreamBuffer
54 FileStreamBuffer( FILE *file )
65 void write(
const char *text,
unsigned int length )
68 fwrite( text,
sizeof(
char), length, file_ );
107 void setBuffer( StreamBuffer *buffer )
112 OStream &write(
const char *text,
unsigned int length )
115 buffer_->write( text, length );
119 OStream &write(
const char *text )
121 return write( text, strlen(text) );
126 const char *out = v ?
"true" :
"false";
133 sprintf( buffer,
"%hd", v );
134 return write( buffer );
140 sprintf( buffer,
"%hu", v );
141 return write( buffer );
147 sprintf( buffer,
"%d", v );
148 return write( buffer );
154 sprintf( buffer,
"%u", v );
155 return write( buffer );
161 sprintf( buffer,
"%ld", v );
162 return write( buffer );
168 sprintf( buffer,
"%lu", v );
169 return write( buffer );
175 sprintf( buffer,
"%.16g",
double(v) );
176 return write( buffer );
182 sprintf( buffer,
"%.16g", v );
183 return write( buffer );
189 sprintf( buffer,
"%.16g",
double(v) );
190 return write( buffer );
196 sprintf( buffer,
"%p", v );
197 return write( buffer );
202 return write( v ? v :
"NULL" );
208 sprintf( buffer,
"%c", c );
209 return write( buffer );
214 return write( s.c_str(), s.length() );
218 StreamBuffer *buffer_;
222 class OStringStream :
public OStream
230 std::string str()
const
232 return buffer_.str();
236 StringStreamBuffer buffer_;
240 class OFileStream :
public OStream
243 OFileStream( FILE *file )
250 OFileStream(
const char *path )
252 , buffer_( fopen( path,
"wt" ) )
257 virtual ~OFileStream()
259 if ( ownFile_ && buffer_.file() )
260 fclose( buffer_.file() );
264 FileStreamBuffer buffer_;
270 static OFileStream stream( stdout );
276 static OFileStream stream( stderr );
282 #elif CPPUNIT_HAVE_SSTREAM // #if defined( CPPUNIT_NO_STREAM )
287 typedef std::ostringstream OStringStream;
288 typedef std::ofstream OFileStream;
292 #elif CPPUNIT_HAVE_CLASS_STRSTREAM
294 # if CPPUNIT_HAVE_STRSTREAM
295 # include <strstream>
296 # else // CPPUNIT_HAVE_STRSTREAM
297 # include <strstream.h>
298 # endif // CPPUNIT_HAVE_CLASS_STRSTREAM
302 class OStringStream :
public std::ostrstream
313 return std::string( std::ostrstream::str(), pcount() );
318 #else // CPPUNIT_HAVE_CLASS_STRSTREAM
319 # error Cannot define CppUnit::OStringStream.
320 #endif // #if defined( CPPUNIT_NO_STREAM )
324 #if !defined( CPPUNIT_NO_STREAM )
344 #endif // #if !defined( CPPUNIT_NO_STREAM )
346 #endif // CPPUNIT_PORTABILITY_STREAM_H_INCLUDED