00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00023 #ifndef flc_stream_H
00024 #define flc_stream_H
00025
00026 #include <falcon/setup.h>
00027 #include <falcon/types.h>
00028 #include <falcon/falcondata.h>
00029 #include <falcon/string.h>
00030 #include <falcon/vm_sys.h>
00031
00032 #define FALCON_READAHEAD_BUFFER_BLOCK 32
00033
00034 namespace Falcon {
00035
00036
00050 class FALCON_DYN_CLASS Stream: public FalconData
00051 {
00052 protected:
00053 uint32 *m_rhBuffer;
00054 uint32 m_rhBufferSize;
00055 uint32 m_rhBufferPos;
00056
00062 void pushBuffer( uint32 chr );
00063
00070 bool popBuffer( uint32 &chr );
00071
00073 bool bufferEmpty() const { return m_rhBufferPos == 0; }
00074
00088 public:
00089 typedef enum {
00090 t_undefined = 0,
00091 t_file = 1,
00092 t_stream = 2,
00093 t_membuf = 3,
00094 t_network = 4,
00095 t_proxy = 5
00096 } t_streamType;
00097
00098 typedef enum {
00099 t_none = 0,
00100 t_open = 0x1,
00101 t_eof = 0x2,
00102 t_error = 0x4,
00103 t_unsupported = 0x8,
00104 t_invalid = 0x10,
00105 t_interrupted = 0x20
00106
00107 } t_status ;
00108
00109
00110 protected:
00111 t_streamType m_streamType;
00112 t_status m_status;
00113 int32 m_lastMoved;
00114
00116 Stream( t_streamType streamType ):
00117 m_rhBuffer( 0 ),
00118 m_rhBufferSize( 0 ),
00119 m_rhBufferPos( 0 ),
00120 m_streamType( streamType ),
00121 m_status( t_none ),
00122 m_lastMoved( 0 )
00123 {}
00124
00125 typedef enum {
00126 ew_begin,
00127 ew_cur,
00128 ew_end
00129 } e_whence;
00130
00131 friend class Transcoder;
00132
00133 public:
00134
00135 Stream( const Stream &other );
00136
00137 t_streamType type() const { return m_streamType; }
00138 virtual t_status status() const { return m_status; }
00139 void status( t_status s ) { m_status = s; }
00140
00141 uint32 lastMoved() const { return m_lastMoved; }
00142
00143 void reset();
00144 bool good() const;
00145 bool bad() const;
00146 bool open() const;
00147 bool eof() const;
00148 bool unsupported() const;
00149 bool invalid() const;
00150 bool error() const;
00151 bool interrupted() const;
00152
00153 virtual ~Stream();
00154
00155 virtual void gcMark( uint32 mark ) {}
00156 virtual bool isStreamBuffer() const { return false; }
00157 virtual bool isTranscoder() const { return false; }
00158
00164 virtual int32 read( void *buffer, int32 size );
00165
00168 virtual int32 write( const void *buffer, int32 size );
00169
00172 virtual bool close();
00173 virtual int64 tell();
00174 virtual bool truncate( int64 pos=-1 );
00175 virtual bool errorDescription( ::Falcon::String &description ) const;
00176
00180 virtual int32 readAvailable( int32 msecs_timeout, const Sys::SystemData *sysData = 0 );
00181
00185 virtual int32 writeAvailable( int32 msecs_timeout, const Sys::SystemData *sysData = 0 );
00186
00187 int64 seekBegin( int64 pos ) {
00188 return seek( pos, ew_begin );
00189 }
00190
00191 int64 seekCurrent( int64 pos ) {
00192 return seek( pos, ew_cur );
00193 }
00194
00195 int64 seekEnd( int64 pos ) {
00196 return seek( pos, ew_end );
00197 }
00198
00199 virtual int64 seek( int64 pos, e_whence w );
00200
00201 virtual int64 lastError() const;
00202
00208 virtual bool get( uint32 &chr ) = 0;
00209
00220 virtual bool readString( String &target, uint32 size );
00221
00226 virtual bool put( uint32 chr );
00227
00235 virtual bool writeString( const String &source, uint32 begin=0, uint32 end = csh::npos );
00236
00259 void unget( uint32 chr ) { pushBuffer( chr ); }
00260
00268 void unget( const String &target );
00269
00283 bool readAhead( uint32 &chr );
00284
00298 bool readAhead( String &target, uint32 size );
00299
00315 void discardReadAhead( uint32 count = 0 );
00316
00320 virtual bool flush();
00321
00326 virtual FalconData *clone() const;
00327 };
00328
00329
00333 inline Stream::t_status operator|( const Stream::t_status &elem1, const Stream::t_status &elem2)
00334 {
00335 return static_cast<Stream::t_status>(
00336 static_cast<unsigned int>(elem1) | static_cast<unsigned int>(elem2) );
00337 }
00338
00342 inline Stream::t_status operator&( const Stream::t_status &elem1, const Stream::t_status &elem2)
00343 {
00344 return static_cast<Stream::t_status>(
00345 static_cast<unsigned int>(elem1) & static_cast<unsigned int>(elem2) );
00346 }
00347
00352 inline Stream::t_status operator^( const Stream::t_status &elem1, const Stream::t_status &elem2)
00353 {
00354 return static_cast<Stream::t_status>(
00355 static_cast<unsigned int>(elem1) ^ static_cast<unsigned int>(elem2) );
00356 }
00357
00362 inline Stream::t_status operator~( const Stream::t_status &elem1 )
00363 {
00364 return static_cast<Stream::t_status>( ~ static_cast<unsigned int>(elem1) );
00365 }
00366
00367 inline void Stream::reset()
00368 {
00369 status( status() &
00370 static_cast<t_status>(~static_cast<unsigned int>(t_error|t_unsupported|t_invalid)) );
00371 m_lastMoved = 0;
00372 }
00373
00374 inline bool Stream::good() const
00375 { return (status() &( t_error | t_unsupported | t_invalid )) == 0; }
00376 inline bool Stream::bad() const
00377 { return (status() &( t_error | t_unsupported | t_invalid )) != 0; }
00378
00379 inline bool Stream::open() const
00380 { return (status() & t_open ) != 0; }
00381 inline bool Stream::eof() const
00382 { return (status() & t_eof ) != 0; }
00383 inline bool Stream::unsupported() const
00384 { return (status() & t_unsupported ) != 0; }
00385 inline bool Stream::invalid() const
00386 { return (status() & t_invalid ) != 0; }
00387 inline bool Stream::error() const
00388 { return ( status() & t_error ) != 0; }
00389 inline bool Stream::interrupted() const
00390 { return ( status() & t_interrupted ) != 0; }
00391
00392 }
00393
00394 #endif
00395
00396