vil_stream_section.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_stream_section.cxx
2 //:
3 // \file
4 // \author fsm
5 
6 #include <iostream>
7 #include "vil_stream_section.h"
8 #include <cassert>
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 
13 // underlying_: pointer to underlying stream.
14 // begin_ : start of section in the underlying stream.
15 // end_ : end of section in the underlying stream. -1 if there is no (explicit) end.
16 // current_ : current position (in the underlying stream) of the adapted stream.
17 
19  : underlying_(underlying)
20  , begin_(begin)
21  , end_((vil_streampos)(-1L))
22  , current_(begin)
23 {
24  assert(underlying != nullptr);
25  assert(begin >= 0);
26  underlying_->ref();
27 }
28 
29 vil_stream_section::vil_stream_section(vil_stream *underlying, int begin, int end)
30  : underlying_(underlying)
31  , begin_(begin)
32  , end_(end)
33  , current_(begin)
34 {
35  assert(underlying != nullptr);
36  assert(begin >= 0);
37  assert(begin <= end);
38  underlying->ref();
39 }
40 
42 {
43  // unreffing the underlying stream might cause deletion of *this, so
44  // zero out the pointer first.
46  underlying_ = nullptr;
47  u->unref();
48 }
49 
51 {
52  assert(n >= 0); // wouldn't you want to be told?
53 
54  // huh? this should never happen, even if someone else is
55  // manipulating the underlying stream too.
56  assert(begin_<=current_);
57  if (end_ != -1L)
58  assert(current_<=end_);
59 
60  // shrink given buffer so it fits into our section.
61  if (end_ != -1L && current_ + n > end_)
62  n = end_ - current_;
63 
64  // seek to where we have been telling the clients we are.
66 
67  // this could be a bug in the caller's code or merely a
68  // failure to seek on underlying stream.
69  assert(underlying_->tell() == current_);
70 
71  vil_streampos nb = underlying_->write(buf, n);
72  if (nb != -1L)
73  current_ += nb;
74  return nb;
75 }
76 
78 {
79  assert(n >= 0); // wouldn't you want to be told?
80 
81  // huh? this should never happen, even if someone else is
82  // manipulating the underlying stream too.
83  assert(begin_<=current_);
84  if (end_ != -1L)
85  assert(current_<=end_);
86 
87  // shrink given buffer so it fits into our section.
88  if (end_ != -1L && current_ + n > end_)
89  n = end_ - current_;
90 
91  // seek to where we have been telling the clients we are.
93 
94  // this could be a bug in the caller's code or merely a
95  // failure to seek on underlying stream.
96  assert(underlying_->tell() == current_);
97 
98  vil_streampos nb = underlying_->read(buf, n);
99  if (nb != -1L)
100  current_ += nb;
101  return nb;
102 }
103 
105 {
106  assert(position >= 0); // I would want to be told about this.
107 
108  if (end_ != -1L && begin_ + position > end_) {
109  std::cerr << __FILE__ << ": attempt to seek past given section (failed).\n";
110  return;
111  }
112  else
113  current_ = begin_ + position;
114 }
115 
117 {
118  return end_ >= begin_ ? end_ - begin_ : underlying_->file_size() - begin_;
119 }
vil_streampos current_
vil_streampos read(void *buf, vil_streampos n) override
Read n bytes into buf. Returns number of bytes read.
virtual vil_streampos tell() const =0
Return file pointer.
vil_streampos file_size() const override
Amount of data in the stream.
virtual vil_streampos write(void const *buf, vil_streampos n)=0
Write n bytes from buf. Returns number of bytes written.
virtual void seek(vil_streampos position)=0
Goto file pointer.
vil_streampos write(void const *buf, vil_streampos n) override
Write n bytes from buf. Returns number of bytes written.
virtual vil_streampos read(void *buf, vil_streampos n)=0
Read n bytes into buf. Returns number of bytes read.
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
vil_stream * underlying_
vil_stream_section(vil_stream *underlying, int begin)
skip to position 'begin' in underlying stream and translate seeks, reads and writes relative to that ...
void ref()
up/down the reference count.
Definition: vil_stream.h:45
void unref()
Definition: vil_stream.cxx:31
vxl_int_32 vil_streampos
Definition: vil_stream.h:16
virtual vil_streampos file_size() const =0
Amount of data in the stream.
make a section of a vil_stream behave like a vil_stream
void seek(vil_streampos position) override
Goto file pointer.