core/vil/vil_stream_section.h
Go to the documentation of this file.
00001 // This is core/vil/vil_stream_section.h
00002 #ifndef vil_stream_section_h_
00003 #define vil_stream_section_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief make a section of a vil_stream behave like a vil_stream
00010 // \author  fsm
00011 
00012 #include <vil/vil_stream.h>
00013 
00014 //: make a section of a vil_stream behave like a vil_stream.
00015 //
00016 // It is possible to have multiple vil_stream_sections using the same
00017 // underlying stream simultaneously. This is accomplished by keeping
00018 // a note of the current position and seeking a lot.
00019 //
00020 // Note however that this is \e not threadsafe.
00021 struct vil_stream_section : public vil_stream
00022 {
00023   //:
00024   // skip to position 'begin' in underlying stream and translate seeks,
00025   // reads and writes relative to that position into seeks, reads and
00026   // writes in the underlying stream.
00027   vil_stream_section(vil_stream *underlying, int begin);
00028 
00029   //:
00030   // as above, but will not allow seeks, reads or writes past 'end'.
00031   vil_stream_section(vil_stream *underlying, int begin, int end);
00032 
00033   // implement virtual vil_stream interface:
00034   bool ok() const { return underlying_->ok(); }
00035   vil_streampos write(void const* buf, vil_streampos n);
00036   vil_streampos read(void* buf, vil_streampos n);
00037   vil_streampos tell() const { return current_; } // regardless of what the underlying stream is doing.
00038   void seek(vil_streampos position);
00039 
00040   vil_streampos file_size() const;
00041 
00042  protected:
00043   ~vil_stream_section();
00044 
00045  private:
00046   vil_stream *underlying_;
00047   vil_streampos begin_;
00048   vil_streampos end_;
00049   vil_streampos current_;
00050 };
00051 
00052 #endif // vil_stream_section_h_