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
00009
00010
00011
00012
00013 unsigned short CNCSJPCVilIOStream::mId = 0;
00014
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;
00062 }
00063
00064 bool CNCSJPCVilIOStream::Seek(INT64 offset, Origin origin )
00065 {
00066 #undef max
00067 #undef min
00068
00069
00070
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
00090 if ( ! ( absoluteOffset >= 0 && absoluteOffset <= mVilStream->file_size() ) ) {
00091 *(CNCSError*)this = NCS_FILE_SEEK_ERROR;
00092 } else {
00093
00094
00095
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 }