00001 #ifndef CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
00002 #define CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
00003
00004
00005
00006
00007
00008
00009
00010 #include <cppunit/Portability.h>
00011
00012
00013 #if defined( CPPUNIT_NO_STREAM )
00014
00015 #include <string>
00016 #include <stdio.h>
00017 #include <string.h>
00018
00019 CPPUNIT_NS_BEGIN
00020
00021 class StreamBuffer
00022 {
00023 public:
00024 virtual ~StreamBuffer() {}
00025
00026 virtual void write( const char *text, unsigned int length ) = 0;
00027
00028 virtual void flush() {}
00029 };
00030
00031
00032 class StringStreamBuffer : public StreamBuffer
00033 {
00034 public:
00035 std::string str() const
00036 {
00037 return str_;
00038 }
00039
00040 public:
00041 void write( const char *text, unsigned int length )
00042 {
00043 str_.append( text, length );
00044 }
00045
00046 private:
00047 std::string str_;
00048 };
00049
00050
00051 class FileStreamBuffer : public StreamBuffer
00052 {
00053 public:
00054 FileStreamBuffer( FILE *file )
00055 : file_( file )
00056 {
00057 }
00058
00059 FILE *file() const
00060 {
00061 return file_;
00062 }
00063
00064 public:
00065 void write( const char *text, unsigned int length )
00066 {
00067 if ( file_ )
00068 fwrite( text, sizeof(char), length, file_ );
00069 }
00070
00071 void flush()
00072 {
00073 if ( file_ )
00074 fflush( file_ );
00075 }
00076
00077 private:
00078 FILE *file_;
00079 };
00080
00081
00082 class OStream
00083 {
00084 public:
00085 OStream()
00086 : buffer_( 0 )
00087 {
00088 }
00089
00090 OStream( StreamBuffer *buffer )
00091 : buffer_( buffer )
00092 {
00093 }
00094
00095 virtual ~OStream()
00096 {
00097 flush();
00098 }
00099
00100 OStream &flush()
00101 {
00102 if ( buffer_ )
00103 buffer_->flush();
00104 return *this;
00105 }
00106
00107 void setBuffer( StreamBuffer *buffer )
00108 {
00109 buffer_ = buffer;
00110 }
00111
00112 OStream &write( const char *text, unsigned int length )
00113 {
00114 if ( buffer_ )
00115 buffer_->write( text, length );
00116 return *this;
00117 }
00118
00119 OStream &write( const char *text )
00120 {
00121 return write( text, strlen(text) );
00122 }
00123
00124 OStream &operator <<( bool v )
00125 {
00126 const char *out = v ? "true" : "false";
00127 return write( out );
00128 }
00129
00130 OStream &operator <<( short v )
00131 {
00132 char buffer[64];
00133 sprintf( buffer, "%hd", v );
00134 return write( buffer );
00135 }
00136
00137 OStream &operator <<( unsigned short v )
00138 {
00139 char buffer[64];
00140 sprintf( buffer, "%hu", v );
00141 return write( buffer );
00142 }
00143
00144 OStream &operator <<( int v )
00145 {
00146 char buffer[64];
00147 sprintf( buffer, "%d", v );
00148 return write( buffer );
00149 }
00150
00151 OStream &operator <<( unsigned int v )
00152 {
00153 char buffer[64];
00154 sprintf( buffer, "%u", v );
00155 return write( buffer );
00156 }
00157
00158 OStream &operator <<( long v )
00159 {
00160 char buffer[64];
00161 sprintf( buffer, "%ld", v );
00162 return write( buffer );
00163 }
00164
00165 OStream &operator <<( unsigned long v )
00166 {
00167 char buffer[64];
00168 sprintf( buffer, "%lu", v );
00169 return write( buffer );
00170 }
00171
00172 OStream &operator <<( float v )
00173 {
00174 char buffer[128];
00175 sprintf( buffer, "%.16g", double(v) );
00176 return write( buffer );
00177 }
00178
00179 OStream &operator <<( double v )
00180 {
00181 char buffer[128];
00182 sprintf( buffer, "%.16g", v );
00183 return write( buffer );
00184 }
00185
00186 OStream &operator <<( long double v )
00187 {
00188 char buffer[128];
00189 sprintf( buffer, "%.16g", double(v) );
00190 return write( buffer );
00191 }
00192
00193 OStream &operator <<( const void *v )
00194 {
00195 char buffer[64];
00196 sprintf( buffer, "%p", v );
00197 return write( buffer );
00198 }
00199
00200 OStream &operator <<( const char *v )
00201 {
00202 return write( v ? v : "NULL" );
00203 }
00204
00205 OStream &operator <<( char c )
00206 {
00207 char buffer[16];
00208 sprintf( buffer, "%c", c );
00209 return write( buffer );
00210 }
00211
00212 OStream &operator <<( const std::string &s )
00213 {
00214 return write( s.c_str(), s.length() );
00215 }
00216
00217 private:
00218 StreamBuffer *buffer_;
00219 };
00220
00221
00222 class OStringStream : public OStream
00223 {
00224 public:
00225 OStringStream()
00226 : OStream( &buffer_ )
00227 {
00228 }
00229
00230 std::string str() const
00231 {
00232 return buffer_.str();
00233 }
00234
00235 private:
00236 StringStreamBuffer buffer_;
00237 };
00238
00239
00240 class OFileStream : public OStream
00241 {
00242 public:
00243 OFileStream( FILE *file )
00244 : OStream( &buffer_ )
00245 , buffer_( file )
00246 , ownFile_( false )
00247 {
00248 }
00249
00250 OFileStream( const char *path )
00251 : OStream( &buffer_ )
00252 , buffer_( fopen( path, "wt" ) )
00253 , ownFile_( true )
00254 {
00255 }
00256
00257 virtual ~OFileStream()
00258 {
00259 if ( ownFile_ && buffer_.file() )
00260 fclose( buffer_.file() );
00261 }
00262
00263 private:
00264 FileStreamBuffer buffer_;
00265 bool ownFile_;
00266 };
00267
00268 inline OStream &stdCOut()
00269 {
00270 static OFileStream stream( stdout );
00271 return stream;
00272 }
00273
00274 inline OStream &stdCErr()
00275 {
00276 static OFileStream stream( stderr );
00277 return stream;
00278 }
00279
00280 CPPUNIT_NS_END
00281
00282 #elif CPPUNIT_HAVE_SSTREAM // #if defined( CPPUNIT_NO_STREAM )
00283 # include <sstream>
00284 # include <fstream>
00285
00286 CPPUNIT_NS_BEGIN
00287 typedef std::ostringstream OStringStream;
00288 typedef std::ofstream OFileStream;
00289 CPPUNIT_NS_END
00290
00291
00292 #elif CPPUNIT_HAVE_CLASS_STRSTREAM
00293 # include <string>
00294 # if CPPUNIT_HAVE_STRSTREAM
00295 # include <strstream>
00296 # else // CPPUNIT_HAVE_STRSTREAM
00297 # include <strstream.h>
00298 # endif // CPPUNIT_HAVE_CLASS_STRSTREAM
00299
00300 CPPUNIT_NS_BEGIN
00301
00302 class OStringStream : public std::ostrstream
00303 {
00304 public:
00305 std::string str()
00306 {
00307
00308
00309
00310
00311
00312
00313 return std::string( std::ostrstream::str(), pcount() );
00314 }
00315 };
00316
00317 CPPUNIT_NS_END
00318 #else // CPPUNIT_HAVE_CLASS_STRSTREAM
00319 # error Cannot define CppUnit::OStringStream.
00320 #endif // #if defined( CPPUNIT_NO_STREAM )
00321
00322
00323
00324 #if !defined( CPPUNIT_NO_STREAM )
00325
00326 #include <iostream>
00327
00328 CPPUNIT_NS_BEGIN
00329
00330 typedef std::ostream OStream;
00331
00332 inline OStream &stdCOut()
00333 {
00334 return std::cout;
00335 }
00336
00337 inline OStream &stdCErr()
00338 {
00339 return std::cerr;
00340 }
00341
00342 CPPUNIT_NS_END
00343
00344 #endif // #if !defined( CPPUNIT_NO_STREAM )
00345
00346 #endif // CPPUNIT_PORTABILITY_STREAM_H_INCLUDED
00347