00001 #include "NCSJPCVilIOStream.h" 00002 00003 #include <vil/vil_stream.h> 00004 #include <vcl_limits.h> 00005 00006 #undef max 00007 #undef min 00008 //vil_streams can only hand 32 bit offsets 00009 static const vil_streampos maxVilStreamPos = vcl_numeric_limits< vil_streampos >::max(); 00010 static const vil_streampos minVilStreamPos = vcl_numeric_limits< vil_streampos >::min(); 00011 00012 CNCSJPCVilIOStream::CNCSJPCVilIOStream() 00013 : mVilStream( 0 ), 00014 mHomePos( -1 ) 00015 { } 00016 00017 CNCSError CNCSJPCVilIOStream::Open( vil_stream* stream ) 00018 { 00019 mVilStream = stream; 00020 mVilStream->ref(); 00021 mHomePos = stream->tell(); 00022 00023 *(CNCSError*)this = CNCSJPCIOStream::Open("VIL", false); 00024 00025 return *(CNCSError*)this; 00026 } 00027 00028 CNCSError CNCSJPCVilIOStream::Close() 00029 { 00030 if ( mVilStream ){ 00031 mVilStream->unref(); 00032 mVilStream = 0; 00033 mHomePos = -1; 00034 } 00035 00036 *(CNCSError*)this = NCS_SUCCESS; 00037 00038 return *(CNCSError*)this; 00039 } 00040 00041 bool CNCSJPCVilIOStream::Seek() 00042 { 00043 return true; //TODO: is this correct? 00044 } 00045 00046 bool CNCSJPCVilIOStream::Seek(INT64 offset, Origin origin ) 00047 { 00048 #undef max 00049 #undef min 00050 //static const INT64 maxInt64 = vcl_numeric_limits< INT64 >::max(); 00051 //NOT USED static const vil_streampos maxVilStreamPos = vcl_numeric_limits< vil_streampos >::max(); 00052 //NOT USED static const vil_streampos minVilStreamPos = vcl_numeric_limits< vil_streampos >::min(); 00053 00054 INT64 absoluteOffset = mHomePos; 00055 switch ( origin ) 00056 { 00057 case START: 00058 absoluteOffset += offset; 00059 break; 00060 case END: 00061 absoluteOffset += Size() - 1 - offset; 00062 break; 00063 case CURRENT: 00064 absoluteOffset += Tell() + offset; 00065 break; 00066 default: 00067 *(CNCSError*)this = NCS_FILE_IO_ERROR; 00068 return false; 00069 } 00070 00071 //make sure the offset specifies a valid location in the stream 00072 if ( ! ( absoluteOffset >= 0 && absoluteOffset <= mVilStream->file_size() ) ) { 00073 *(CNCSError*)this = NCS_FILE_SEEK_ERROR; 00074 } else { 00075 //this cast should be safe because we tested to make sure that 00076 //absoluteOffset is < mVilStream->file_size()... if that is true 00077 //then absoluteOffset is < 2^31 (max int size) 00078 mVilStream->seek( absoluteOffset ); 00079 *(CNCSError*)this = NCS_SUCCESS; 00080 } 00081 return *(CNCSError*)this == NCS_SUCCESS; 00082 } 00083 00084 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Tell() 00085 { 00086 return (INT64) (mVilStream->tell() - mHomePos); 00087 } 00088 00089 INT64 NCS_FASTCALL CNCSJPCVilIOStream::Size() 00090 { 00091 return (INT64) (mVilStream->file_size() - mHomePos); 00092 } 00093 00094 bool NCS_FASTCALL CNCSJPCVilIOStream::Read(void* buffer, UINT32 count) 00095 { 00096 vil_streampos bytesRead = mVilStream->read( buffer, count ); 00097 if ( bytesRead != count ){ 00098 *(CNCSError*)this = NCS_INVALID_PARAMETER; 00099 return false; 00100 } 00101 return true; 00102 } 00103 00104 bool NCS_FASTCALL CNCSJPCVilIOStream::Write(void* buffer, UINT32 count) 00105 { 00106 vil_streampos bytesWritten = mVilStream->write( buffer, count ); 00107 if ( bytesWritten != count ){ 00108 *(CNCSError*)this = NCS_INVALID_PARAMETER; 00109 return false; 00110 } 00111 return true; 00112 }
1.5.1