00001
00002
00003
00004
00005
00006
00007
00008 #ifndef _IMPULSE_STREAM_H_
00009 #define _IMPULSE_STREAM_H_
00010
00011 #include <cstdlib>
00012
00013 namespace impulse {
00014
00015
00016
00017
00018
00022
00023 class Stream : public Frame {
00024
00025 public:
00026
00027 Stream() : _file( NULL ) { }
00028 Stream( FILE* file ) : _file( file ) { }
00029 Stream( string name ) { _file = fopen( name.c_str(), "w+" ); }
00030
00031 string inspect() { return "[Symbol]"; }
00032
00033 Stream& operator <<( double value ) { fprintf( _file, "%lf", value ); return *this; }
00034 Stream& operator <<( const char* value ) { if (_file) fprintf( _file, "%s", value ); return *this; }
00035 Stream& operator <<( string value ) { if (_file) fprintf( _file, "%s", value.c_str() ); return *this; }
00036 Stream& operator <<( int value ) { if (_file) fprintf( _file, "%d", value ); return *this; }
00037 Stream& operator <<( long value ) { if (_file) fprintf( _file, "%ld", value ); return *this; }
00038 Stream& operator <<( unsigned int value ) { if (_file) fprintf( _file, "%u", value ); return *this; }
00039 Stream& operator <<( unsigned long value ) { if (_file) fprintf( _file, "%lu", value ); return *this; }
00040 Stream& operator <<( bool value ) { if (_file) fprintf( _file, "%d", value ); return *this; }
00041 Stream& operator <<( void* value ) { if (_file) fprintf( _file, "%p", value ); return *this; }
00042
00043 Stream& operator >>( long c ) { return ::fgetc( _file ), *this; }
00044
00045 long peek() { int c = ::fgetc( _file ); ::ungetc( c, _file ); return c; }
00046 long getc() { return ::fgetc( _file ); }
00047
00048 long unget( int c ) { return ::ungetc( c, _file ); }
00049
00050 inline Stream& operator <<( Stream& (*func)(Stream&) )
00051 {
00052 return (*func)(*this);
00053 }
00054
00055 Stream& flush() { if (_file) fflush( _file ); return *this; }
00056
00057 private:
00058
00059 FILE* _file;
00060
00061 };
00062
00063
00064
00065
00066
00067 namespace io {
00068
00069 inline Stream& endl( Stream& stream )
00070 {
00071 return stream << "\n", stream.flush();
00072 }
00073
00074 extern Stream cin;
00075 extern Stream cout;
00076 extern Stream trace;
00077
00078 }
00079
00080 }
00081
00082 #endif