merge the formfield patch from ooo-build
[ooovba.git] / writerperfect / source / stream / WPXSvStream.cxx
blob85f10261b13b9a5734bf8f36edebcd5b58654c80
1 #include "WPXSvStream.h"
2 #include "filter/FilterInternal.hxx"
3 #include <tools/stream.hxx>
4 #include <unotools/streamwrap.hxx>
5 #include <unotools/ucbstreamhelper.hxx>
6 #include <limits>
8 using namespace ::com::sun::star::uno;
9 using namespace ::com::sun::star::io;
11 WPXSvInputStream::WPXSvInputStream( Reference< XInputStream > xStream ) :
12 WPSInputStream(),
13 mxChildStorage(),
14 mxChildStream(),
15 mxStream(xStream),
16 mxSeekable(xStream, UNO_QUERY),
17 maData(0)
19 if (!xStream.is() || !mxStream.is())
20 mnLength = 0;
21 else
23 if (!mxSeekable.is())
24 mnLength = 0;
25 else
27 try
29 mnLength = mxSeekable->getLength();
31 catch ( ... )
33 WRITER_DEBUG_MSG(("mnLength = mxSeekable->getLength() threw exception\n"));
34 mnLength = 0;
40 WPXSvInputStream::~WPXSvInputStream()
44 const uint8_t * WPXSvInputStream::read(size_t numBytes, size_t &numBytesRead)
46 numBytesRead = 0;
48 if (numBytes == 0 || atEOS())
49 return 0;
51 numBytesRead = mxStream->readSomeBytes (maData, numBytes);
52 if (numBytesRead == 0)
53 return 0;
55 return (const uint8_t *)maData.getConstArray();
58 long WPXSvInputStream::tell()
60 if ((mnLength == 0) || !mxStream.is() || !mxSeekable.is())
61 return -1L;
62 else
64 sal_Int64 tmpPosition = mxSeekable->getPosition();
65 if ((tmpPosition < 0) || (tmpPosition > (std::numeric_limits<long>::max)()))
66 return -1L;
67 return (long)tmpPosition;
71 int WPXSvInputStream::seek(long offset, WPX_SEEK_TYPE seekType)
73 if ((mnLength == 0) || !mxStream.is() || !mxSeekable.is())
74 return -1;
76 sal_Int64 tmpPosition = mxSeekable->getPosition();
77 if ((tmpPosition < 0) || (tmpPosition > (std::numeric_limits<long>::max)()))
78 return -1;
80 sal_Int64 tmpOffset = offset;
81 if (seekType == WPX_SEEK_CUR)
82 tmpOffset += tmpPosition;
84 int retVal = 0;
85 if (tmpOffset < 0)
87 tmpOffset = 0;
88 retVal = -1;
90 if (offset > mnLength)
92 tmpOffset = mnLength;
93 retVal = -1;
96 try
98 mxSeekable->seek(tmpOffset);
99 return retVal;
101 catch (...)
103 WRITER_DEBUG_MSG(("mxSeekable->seek(offset) threw exception\n"));
104 return -1;
108 bool WPXSvInputStream::atEOS()
110 if ((mnLength == 0) || !mxStream.is() || !mxSeekable.is())
111 return true;
112 return (mxSeekable->getPosition() >= mnLength);
115 bool WPXSvInputStream::isOLEStream()
117 if ((mnLength == 0) || !mxStream.is() || !mxSeekable.is())
118 return false;
120 sal_Int64 tmpPosition = mxSeekable->getPosition();
121 mxSeekable->seek(0);
123 SvStream *pStream = utl::UcbStreamHelper::CreateStream( mxStream );
124 bool bAns = pStream && SotStorage::IsOLEStorage( pStream );
125 if (pStream)
126 delete pStream;
128 mxSeekable->seek(tmpPosition);
130 return bAns;
133 WPXInputStream * WPXSvInputStream::getDocumentOLEStream(const char * name)
135 if ((mnLength == 0) || !mxStream.is() || !mxSeekable.is())
136 return 0;
138 sal_Int64 tmpPosition = mxSeekable->getPosition();
139 mxSeekable->seek(0);
141 SvStream *pStream = utl::UcbStreamHelper::CreateStream( mxStream );
143 if (!pStream || !SotStorage::IsOLEStorage( pStream ))
145 mxSeekable->seek(tmpPosition);
146 return 0;
149 mxChildStorage = new SotStorage( pStream, TRUE );
151 mxChildStream = mxChildStorage->OpenSotStream(
152 rtl::OUString::createFromAscii( name ),
153 STREAM_STD_READ );
155 mxSeekable->seek(tmpPosition);
157 if ( !mxChildStream.Is() || mxChildStream->GetError() )
159 mxSeekable->seek(tmpPosition);
160 return 0;
163 Reference < XInputStream > xContents(new utl::OSeekableInputStreamWrapper( mxChildStream ));
164 mxSeekable->seek(tmpPosition);
165 if (xContents.is())
166 return new WPXSvInputStream( xContents );
167 else
168 return 0;
171 WPXInputStream * WPXSvInputStream::getDocumentOLEStream()
173 return getDocumentOLEStream( "PerfectOffice_MAIN" );