Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / oox / source / helper / textinputstream.cxx
blob2884bd14d99d7bae223cbcc1ddee5f9fe2f95996
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 /** Implementation of a UNO input stream wrapping a binary input stream.
40 class UnoBinaryInputStream : public ::cppu::WeakImplHelper< XInputStream >
42 public:
43 explicit UnoBinaryInputStream( BinaryInputStream& rInStrm );
45 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead ) override;
46 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead ) override;
47 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
48 virtual sal_Int32 SAL_CALL available() override;
49 virtual void SAL_CALL closeInput() override;
51 private:
52 /// @throws NotConnectedException
53 void ensureConnected() const;
55 private:
56 BinaryInputStream* mpInStrm;
59 UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
60 mpInStrm( &rInStrm )
64 sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
66 ensureConnected();
67 return mpInStrm->readData( rData, nBytesToRead );
70 sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
72 ensureConnected();
73 return mpInStrm->readData( rData, nMaxBytesToRead );
76 void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
78 ensureConnected();
79 mpInStrm->skip( nBytesToSkip );
82 sal_Int32 SAL_CALL UnoBinaryInputStream::available()
84 ensureConnected();
85 throw RuntimeException( "Functionality not supported", Reference< XInputStream >() );
88 void SAL_CALL UnoBinaryInputStream::closeInput()
90 ensureConnected();
91 mpInStrm->close();
92 mpInStrm = nullptr;
95 void UnoBinaryInputStream::ensureConnected() const
97 if( !mpInStrm )
98 throw NotConnectedException( "Stream closed" );
101 } // namespace
103 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
105 init( rxContext, rxInStrm, eTextEnc );
108 TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
110 init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
113 TextInputStream::~TextInputStream()
117 bool TextInputStream::isEof() const
119 if( mxTextStrm.is() ) try
121 return mxTextStrm->isEOF();
123 catch (const Exception&)
126 return true;
129 OUString TextInputStream::readLine()
131 if( mxTextStrm.is() ) try
133 /* The function createFinalString() adds a character that may have
134 been buffered in the previous call of readToChar() (see below). */
135 return createFinalString( mxTextStrm->readLine() );
137 catch (const Exception&)
139 mxTextStrm.clear();
141 return OUString();
144 OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
146 if( mxTextStrm.is() ) try
148 Sequence< sal_Unicode > aDelimiters( 1 );
149 aDelimiters[ 0 ] = cChar;
150 /* Always get the delimiter character from the UNO text input stream.
151 In difference to this implementation, it will not return it in the
152 next call but silently skip it. If caller specifies to exclude the
153 character in this call, it will be returned in the next call of one
154 of the own member functions. The function createFinalString() adds
155 a character that has been buffered in the previous call. */
156 OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, false ) );
157 // remove last character from string and remember it for next call
158 if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
160 mcPendingChar = cChar;
161 aString = aString.copy( 0, aString.getLength() - 1 );
163 return aString;
165 catch (const Exception&)
167 mxTextStrm.clear();
169 return OUString();
172 Reference< XTextInputStream2 > TextInputStream::createXTextInputStream(
173 const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
175 Reference< XTextInputStream2 > xTextStrm;
176 const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
177 OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
178 if( rxContext.is() && rxInStrm.is() && pcCharset ) try
180 xTextStrm = css::io::TextInputStream::create( rxContext );
181 xTextStrm->setInputStream( rxInStrm );
182 xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
184 catch (const Exception&)
187 return xTextStrm;
190 // private --------------------------------------------------------------------
192 OUString TextInputStream::createFinalString( const OUString& rString )
194 if( mcPendingChar == 0 )
195 return rString;
197 OUString aString = OUStringLiteral1( mcPendingChar ) + rString;
198 mcPendingChar = 0;
199 return aString;
202 void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
204 mcPendingChar = 0;
205 mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
208 } // namespace oox
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */