core/vil/file_formats/NCSJPCVilIOStream.cxx
Go to the documentation of this file.
00001 #include "NCSJPCVilIOStream.h"
00002 #include <vcl_sstream.h>
00003 #include <vil/vil_stream.h>
00004 #include <vcl_string.h>
00005 #include <vcl_limits.h>
00006 #undef max
00007 #undef min
00008 // Do not remove the following notice
00009 // Modifications approved for public release, distribution unlimited 
00010 // DISTAR Case 14074
00011 //
00012 
00013 unsigned short CNCSJPCVilIOStream::mId = 0;//initialize static id variable
00014   //vil_streams can only hand 32 bit offsets (unless large file support is on)
00015   static const vil_streampos maxVilStreamPos = vcl_numeric_limits< vil_streampos >::max();
00016   static const vil_streampos minVilStreamPos = vcl_numeric_limits< vil_streampos >::min();
00017 
00018 CNCSJPCVilIOStream::CNCSJPCVilIOStream()
00019   : mVilStream( 0 ),
00020     mHomePos( -1 )
00021 { 
00022 }
00023 
00024 CNCSJPCVilIOStream::~CNCSJPCVilIOStream()
00025 {
00026 }
00027 CNCSError CNCSJPCVilIOStream::Open( vil_stream* stream, bool bWrite)
00028 {
00029   mVilStream = stream;
00030   mVilStream->ref();
00031   mHomePos = stream->tell();
00032   vcl_stringstream str;
00033   str << "name " << mId++;
00034   vcl_string nm = str.str();
00035   unsigned n = nm.size();
00036   char* name = new char[n+1];
00037   unsigned i = 0;
00038   for(vcl_string::iterator sit = nm.begin(); sit !=nm.end(); ++sit, ++i)
00039     name[i]=*sit;
00040   name[n]='\0';
00041   *(CNCSError*)this = CNCSJPCIOStream::Open(name, bWrite);
00042   delete [] name;
00043   return *(CNCSError*)this;
00044 }
00045 
00046 CNCSError CNCSJPCVilIOStream::Close()
00047 {
00048   if ( mVilStream ){
00049     mVilStream->unref();
00050     mVilStream = 0;
00051     mHomePos = -1;
00052   }
00053 
00054   *(CNCSError*)this = NCS_SUCCESS;
00055 
00056   return *(CNCSError*)this;
00057 }
00058 
00059 bool CNCSJPCVilIOStream::Seek()
00060 {
00061   return true; //TODO: is this correct?
00062 }
00063 
00064 bool CNCSJPCVilIOStream::Seek(INT64 offset, Origin origin )
00065 {
00066 #undef max
00067 #undef min
00068   //static const INT64 maxInt64 = vcl_numeric_limits< INT64 >::max();
00069 //NOT USED  static const vil_streampos maxVilStreamPos = vcl_numeric_limits< vil_streampos >::max();
00070 //NOT USED  static const vil_streampos minVilStreamPos = vcl_numeric_limits< vil_streampos >::min();
00071 
00072   INT64 absoluteOffset = mHomePos;
00073   switch ( origin )
00074   {
00075     case START:
00076       absoluteOffset += offset;
00077       break;
00078     case END:
00079       absoluteOffset += Size() - 1 - offset;
00080       break;
00081     case CURRENT:
00082       absoluteOffset += Tell() + offset;
00083       break;
00084     default:
00085       *(CNCSError*)this = NCS_FILE_IO_ERROR;
00086       return false;
00087   }
00088 
00089   //make sure the offset specifies a valid location in the stream
00090   if ( ! ( absoluteOffset >= 0 && absoluteOffset <= mVilStream->file_size() ) ) {
00091     *(CNCSError*)this = NCS_FILE_SEEK_ERROR;
00092   } else {
00093     //this cast should be safe because we tested to make sure that
00094     //absoluteOffset is < mVilStream->file_size()... if that is true
00095     //then absoluteOffset is < 2^31 (max int size)
00096     mVilStream->seek( absoluteOffset );
00097     *(CNCSError*)this = NCS_SUCCESS;
00098   }
00099   return *(CNCSError*)this == NCS_SUCCESS;
00100 }
00101 
00102 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Tell()
00103 {
00104   return (INT64) (mVilStream->tell() - mHomePos);
00105 }
00106 
00107 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Size()
00108 {
00109   return (INT64) (mVilStream->file_size() - mHomePos);
00110 }
00111 
00112 bool NCS_FASTCALL CNCSJPCVilIOStream::Read(void* buffer, UINT32 count)
00113 {
00114   vil_streampos bytesRead = mVilStream->read( buffer, count );
00115   if ( bytesRead != count ){
00116     *(CNCSError*)this = NCS_INVALID_PARAMETER;
00117     return false;
00118   }
00119   return true;
00120 }
00121 
00122 bool NCS_FASTCALL CNCSJPCVilIOStream::Write(void* buffer, UINT32 count)
00123 {
00124   vil_streampos bytesWritten = mVilStream->write( buffer, count );
00125   if ( bytesWritten != count ){
00126     *(CNCSError*)this = NCS_INVALID_PARAMETER;
00127     return false;
00128   }
00129   return true;
00130 }