Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / helper / textinputstream.cxx
blobe592ce0b5283ae49bf530ef4f402294e57b94247
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/NotConnectedException.hpp>
23 #include <com/sun/star/io/XActiveDataSink.hpp>
24 #include <com/sun/star/io/TextInputStream.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <osl/diagnose.h>
27 #include <rtl/tencinfo.h>
28 #include "oox/helper/binaryinputstream.hxx"
30 namespace oox {
32 using namespace ::com::sun::star::io;
33 using namespace ::com::sun::star::lang;
34 using namespace ::com::sun::star::uno;
36 namespace {
38 typedef ::cppu::WeakImplHelper< XInputStream > UnoBinaryInputStream_BASE;
40 /** Implementation of a UNO input stream wrapping a binary input stream.
42 class UnoBinaryInputStream : public UnoBinaryInputStream_BASE
44 public:
45 explicit UnoBinaryInputStream( BinaryInputStream& rInStrm );
47 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead ) override;
48 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead ) override;
49 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
50 virtual sal_Int32 SAL_CALL available() override;
51 virtual void SAL_CALL closeInput() override;
53 private:
54 /// @throws NotConnectedException
55 void ensureConnected() const;
57 private:
58 BinaryInputStream* mpInStrm;
61 UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
62 mpInStrm( &rInStrm )
66 sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
68 ensureConnected();
69 return mpInStrm->readData( rData, nBytesToRead );
72 sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
74 ensureConnected();
75 return mpInStrm->readData( rData, nMaxBytesToRead );
78 void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
80 ensureConnected();
81 mpInStrm->skip( nBytesToSkip );
84 sal_Int32 SAL_CALL UnoBinaryInputStream::available()
86 ensureConnected();
87 throw RuntimeException( "Functionality not supported", Reference< XInputStream >() );
90 void SAL_CALL UnoBinaryInputStream::closeInput()
92 ensureConnected();
93 mpInStrm->close();
94 mpInStrm = nullptr;
97 void UnoBinaryInputStream::ensureConnected() const
99 if( !mpInStrm )
100 throw NotConnectedException( "Stream closed" );
103 } // namespace
105 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
107 init( rxContext, rxInStrm, eTextEnc );
110 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
112 init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
115 TextInputStream::~TextInputStream()
119 bool TextInputStream::isEof() const
121 if( mxTextStrm.is() ) try
123 return mxTextStrm->isEOF();
125 catch (const Exception&)
128 return true;
131 OUString TextInputStream::readLine()
133 if( mxTextStrm.is() ) try
135 /* The function createFinalString() adds a character that may have
136 been buffered in the previous call of readToChar() (see below). */
137 return createFinalString( mxTextStrm->readLine() );
139 catch (const Exception&)
141 mxTextStrm.clear();
143 return OUString();
146 OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
148 if( mxTextStrm.is() ) try
150 Sequence< sal_Unicode > aDelimiters( 1 );
151 aDelimiters[ 0 ] = cChar;
152 /* Always get the delimiter character from the UNO text input stream.
153 In difference to this implementation, it will not return it in the
154 next call but silently skip it. If caller specifies to exclude the
155 character in this call, it will be returned in the next call of one
156 of the own member functions. The function createFinalString() adds
157 a character that has been buffered in the previous call. */
158 OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, false ) );
159 // remove last character from string and remember it for next call
160 if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
162 mcPendingChar = cChar;
163 aString = aString.copy( 0, aString.getLength() - 1 );
165 return aString;
167 catch (const Exception&)
169 mxTextStrm.clear();
171 return OUString();
174 Reference< XTextInputStream2 > TextInputStream::createXTextInputStream(
175 const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
177 Reference< XTextInputStream2 > xTextStrm;
178 const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
179 OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
180 if( rxContext.is() && rxInStrm.is() && pcCharset ) try
182 xTextStrm = css::io::TextInputStream::create( rxContext );
183 xTextStrm->setInputStream( rxInStrm );
184 xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
186 catch (const Exception&)
189 return xTextStrm;
192 // private --------------------------------------------------------------------
194 OUString TextInputStream::createFinalString( const OUString& rString )
196 if( mcPendingChar == 0 )
197 return rString;
199 OUString aString = OUStringLiteral1( mcPendingChar ) + rString;
200 mcPendingChar = 0;
201 return aString;
204 void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
206 mcPendingChar = 0;
207 mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
210 } // namespace oox
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */