CppUnit project page FAQ CppUnit home page

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages
include / cppunit / portability

Stream.h

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

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers

AltStyle によって変換されたページ (->オリジナル) /