NCSJPCVilIOStream.cxx
Go to the documentation of this file.
1 #include <sstream>
2 #include <string>
3 #include <limits>
4 #include "NCSJPCVilIOStream.h"
5 #include <vil/vil_stream.h>
6 #ifdef _MSC_VER
7 # include <vcl_msvc_warnings.h>
8 #endif
9 #undef max
10 #undef min
11 // Do not remove the following notice
12 // Modifications approved for public release, distribution unlimited
13 // DISTAR Case 14074
14 //
15 
16 unsigned short CNCSJPCVilIOStream::mId = 0;//initialize static id variable
17  //vil_streams can only hand 32 bit offsets (unless large file support is on)
18  static const vil_streampos maxVilStreamPos = std::numeric_limits< vil_streampos >::max();
19  static const vil_streampos minVilStreamPos = std::numeric_limits< vil_streampos >::min();
20 
22  : mVilStream( 0 ),
23  mHomePos( -1 )
24 {
25 }
26 
28 {
29 }
30 CNCSError CNCSJPCVilIOStream::Open( vil_stream* stream, bool bWrite)
31 {
32  mVilStream = stream;
33  mVilStream->ref();
34  mHomePos = stream->tell();
35  std::stringstream str;
36  str << "name " << mId++;
37  std::string nm = str.str();
38  unsigned n = nm.size();
39  char* name = new char[n+1];
40  unsigned i = 0;
41  for(std::string::iterator sit = nm.begin(); sit !=nm.end(); ++sit, ++i)
42  name[i]=*sit;
43  name[n]='\0';
44  *(CNCSError*)this = CNCSJPCIOStream::Open(name, bWrite);
45  delete [] name;
46  return *(CNCSError*)this;
47 }
48 
50 {
51  if ( mVilStream ){
52  mVilStream->unref();
53  mVilStream = 0;
54  mHomePos = -1;
55  }
56 
57  *(CNCSError*)this = NCS_SUCCESS;
58 
59  return *(CNCSError*)this;
60 }
61 
63 {
64  return true; //TODO: is this correct?
65 }
66 
67 bool CNCSJPCVilIOStream::Seek(INT64 offset, Origin origin )
68 {
69 #undef max
70 #undef min
71  //static const INT64 maxInt64 = std::numeric_limits< INT64 >::max();
72 //NOT USED static const vil_streampos maxVilStreamPos = std::numeric_limits< vil_streampos >::max();
73 //NOT USED static const vil_streampos minVilStreamPos = std::numeric_limits< vil_streampos >::min();
74 
75  INT64 absoluteOffset = mHomePos;
76  switch ( origin )
77  {
78  case START:
79  absoluteOffset += offset;
80  break;
81  case END:
82  absoluteOffset += Size() - 1 - offset;
83  break;
84  case CURRENT:
85  absoluteOffset += Tell() + offset;
86  break;
87  default:
88  *(CNCSError*)this = NCS_FILE_IO_ERROR;
89  return false;
90  }
91 
92  //make sure the offset specifies a valid location in the stream
93  if ( ! ( absoluteOffset >= 0 && absoluteOffset <= mVilStream->file_size() ) ) {
94  *(CNCSError*)this = NCS_FILE_SEEK_ERROR;
95  } else {
96  //this cast should be safe because we tested to make sure that
97  //absoluteOffset is < mVilStream->file_size()... if that is true
98  //then absoluteOffset is < 2^31 (max int size)
99  mVilStream->seek( absoluteOffset );
100  *(CNCSError*)this = NCS_SUCCESS;
101  }
102  return *(CNCSError*)this == NCS_SUCCESS;
103 }
104 
105 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Tell()
106 {
107  return (INT64) (mVilStream->tell() - mHomePos);
108 }
109 
110 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Size()
111 {
112  return (INT64) (mVilStream->file_size() - mHomePos);
113 }
114 
115 bool NCS_FASTCALL CNCSJPCVilIOStream::Read(void* buffer, UINT32 count)
116 {
117  vil_streampos bytesRead = mVilStream->read( buffer, count );
118  if ( bytesRead != count ){
119  *(CNCSError*)this = NCS_INVALID_PARAMETER;
120  return false;
121  }
122  return true;
123 }
124 
125 bool NCS_FASTCALL CNCSJPCVilIOStream::Write(void* buffer, UINT32 count)
126 {
127  vil_streampos bytesWritten = mVilStream->write( buffer, count );
128  if ( bytesWritten != count ){
129  *(CNCSError*)this = NCS_INVALID_PARAMETER;
130  return false;
131  }
132  return true;
133 }
Stream interface for VIL image loaders.
virtual vil_streampos tell() const =0
Return file pointer.
virtual CNCSError Close()
virtual vil_streampos write(void const *buf, vil_streampos n)=0
Write n bytes from buf. Returns number of bytes written.
virtual bool NCS_FASTCALL Seek()
virtual void seek(vil_streampos position)=0
Goto file pointer.
virtual vil_streampos read(void *buf, vil_streampos n)=0
Read n bytes into buf. Returns number of bytes read.
vil_stream * mVilStream
The stream I get all my data from (and write too)
virtual bool NCS_FASTCALL Write(void *buffer, UINT32 count)
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
static unsigned short mId
virtual bool NCS_FASTCALL Read(void *buffer, UINT32 count)
void ref()
up/down the reference count.
Definition: vil_stream.h:45
virtual CNCSError Open(vil_stream *stream, bool bWrite=false)
Pass me the stream you want me to wrap.
void unref()
Definition: vil_stream.cxx:31
vil_streampos mHomePos
This position is my home position (ie.
virtual INT64 NCS_FASTCALL Size()
vxl_int_32 vil_streampos
Definition: vil_stream.h:16
virtual vil_streampos file_size() const =0
Amount of data in the stream.
virtual INT64 NCS_FASTCALL Tell()