Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / helper / textinputstream.cxx
blobd5e7894e49c5304029dbbe4561fe84afcba808fe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "oox/helper/textinputstream.hxx"
22 #include <com/sun/star/io/XActiveDataSink.hpp>
23 #include <com/sun/star/io/TextInputStream.hpp>
24 #include <cppuhelper/implbase1.hxx>
25 #include <osl/diagnose.h>
26 #include <rtl/tencinfo.h>
27 #include "oox/helper/binaryinputstream.hxx"
29 namespace oox {
31 using namespace ::com::sun::star::io;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::uno;
35 namespace {
37 typedef ::cppu::WeakImplHelper1< XInputStream > UnoBinaryInputStream_BASE;
39 /** Implementation of a UNO input stream wrapping a binary input stream.
41 class UnoBinaryInputStream : public UnoBinaryInputStream_BASE
43 public:
44 explicit UnoBinaryInputStream( BinaryInputStream& rInStrm );
46 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
47 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
48 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
49 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
50 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
51 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
52 virtual sal_Int32 SAL_CALL available()
53 throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
54 virtual void SAL_CALL closeInput()
55 throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
57 private:
58 void ensureConnected() const throw (NotConnectedException);
60 private:
61 BinaryInputStream* mpInStrm;
64 UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
65 mpInStrm( &rInStrm )
69 sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
70 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
72 ensureConnected();
73 return mpInStrm->readData( rData, nBytesToRead, 1 );
76 sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
77 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
79 ensureConnected();
80 return mpInStrm->readData( rData, nMaxBytesToRead, 1 );
83 void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
84 throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
86 ensureConnected();
87 mpInStrm->skip( nBytesToSkip, 1 );
90 sal_Int32 SAL_CALL UnoBinaryInputStream::available() throw (NotConnectedException, IOException, RuntimeException, std::exception)
92 ensureConnected();
93 throw RuntimeException( "Functionality not supported", Reference< XInputStream >() );
96 void SAL_CALL UnoBinaryInputStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
98 ensureConnected();
99 mpInStrm->close();
100 mpInStrm = 0;
103 void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
105 if( !mpInStrm )
106 throw NotConnectedException( "Stream closed" );
109 } // namespace
111 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
113 init( rxContext, rxInStrm, eTextEnc );
116 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
118 init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
121 TextInputStream::~TextInputStream()
125 bool TextInputStream::isEof() const
127 if( mxTextStrm.is() ) try
129 return mxTextStrm->isEOF();
131 catch (const Exception&)
134 return true;
137 OUString TextInputStream::readLine()
139 if( mxTextStrm.is() ) try
141 /* The function createFinalString() adds a character that may have
142 been buffered in the previous call of readToChar() (see below). */
143 return createFinalString( mxTextStrm->readLine() );
145 catch (const Exception&)
147 mxTextStrm.clear();
149 return OUString();
152 OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
154 if( mxTextStrm.is() ) try
156 Sequence< sal_Unicode > aDelimiters( 1 );
157 aDelimiters[ 0 ] = cChar;
158 /* Always get the delimiter character from the UNO text input stream.
159 In difference to this implementation, it will not return it in the
160 next call but silently skip it. If caller specifies to exclude the
161 character in this call, it will be returned in the next call of one
162 of the own member functions. The function createFinalString() adds
163 a character that has been buffered in the previous call. */
164 OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, sal_False ) );
165 // remove last character from string and remember it for next call
166 if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
168 mcPendingChar = cChar;
169 aString = aString.copy( 0, aString.getLength() - 1 );
171 return aString;
173 catch (const Exception&)
175 mxTextStrm.clear();
177 return OUString();
180 Reference< XTextInputStream2 > TextInputStream::createXTextInputStream(
181 const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
183 Reference< XTextInputStream2 > xTextStrm;
184 const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
185 OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
186 if( rxContext.is() && rxInStrm.is() && pcCharset ) try
188 xTextStrm = com::sun::star::io::TextInputStream::create( rxContext );
189 xTextStrm->setInputStream( rxInStrm );
190 xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
192 catch (const Exception&)
195 return xTextStrm;
198 // private --------------------------------------------------------------------
200 OUString TextInputStream::createFinalString( const OUString& rString )
202 if( mcPendingChar == 0 )
203 return rString;
205 OUString aString = OUString( mcPendingChar ) + rString;
206 mcPendingChar = 0;
207 return aString;
210 void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
212 mcPendingChar = 0;
213 mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
216 } // namespace oox
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */