1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <rtl/tencinfo.h>
26 #include "oox/helper/binaryinputstream.hxx"
30 // ============================================================================
32 using namespace ::com::sun::star::io
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::uno
;
36 // ============================================================================
40 typedef ::cppu::WeakImplHelper1
< XInputStream
> UnoBinaryInputStream_BASE
;
42 /** Implementation of a UNO input stream wrapping a binary input stream.
44 class UnoBinaryInputStream
: public UnoBinaryInputStream_BASE
47 explicit UnoBinaryInputStream( BinaryInputStream
& rInStrm
);
49 virtual sal_Int32 SAL_CALL
readBytes( Sequence
< sal_Int8
>& rData
, sal_Int32 nBytesToRead
)
50 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
51 virtual sal_Int32 SAL_CALL
readSomeBytes( Sequence
< sal_Int8
>& rData
, sal_Int32 nMaxBytesToRead
)
52 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
53 virtual void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
)
54 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
55 virtual sal_Int32 SAL_CALL
available()
56 throw (NotConnectedException
, IOException
, RuntimeException
);
57 virtual void SAL_CALL
closeInput()
58 throw (NotConnectedException
, IOException
, RuntimeException
);
61 void ensureConnected() const throw (NotConnectedException
);
64 BinaryInputStream
* mpInStrm
;
67 // ----------------------------------------------------------------------------
69 UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream
& rInStrm
) :
74 sal_Int32 SAL_CALL
UnoBinaryInputStream::readBytes( Sequence
< sal_Int8
>& rData
, sal_Int32 nBytesToRead
)
75 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
78 return mpInStrm
->readData( rData
, nBytesToRead
, 1 );
81 sal_Int32 SAL_CALL
UnoBinaryInputStream::readSomeBytes( Sequence
< sal_Int8
>& rData
, sal_Int32 nMaxBytesToRead
)
82 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
85 return mpInStrm
->readData( rData
, nMaxBytesToRead
, 1 );
88 void SAL_CALL
UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip
)
89 throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
92 mpInStrm
->skip( nBytesToSkip
, 1 );
95 sal_Int32 SAL_CALL
UnoBinaryInputStream::available() throw (NotConnectedException
, IOException
, RuntimeException
)
98 throw RuntimeException( "Functionality not supported", Reference
< XInputStream
>() );
101 void SAL_CALL
UnoBinaryInputStream::closeInput() throw (NotConnectedException
, IOException
, RuntimeException
)
108 void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException
)
111 throw NotConnectedException( "Stream closed", Reference
< XInterface
>() );
116 // ============================================================================
118 TextInputStream::TextInputStream( const Reference
< XComponentContext
>& rxContext
, const Reference
< XInputStream
>& rxInStrm
, rtl_TextEncoding eTextEnc
)
120 init( rxContext
, rxInStrm
, eTextEnc
);
123 TextInputStream::TextInputStream( const Reference
< XComponentContext
>& rxContext
, BinaryInputStream
& rInStrm
, rtl_TextEncoding eTextEnc
)
125 init( rxContext
, new UnoBinaryInputStream( rInStrm
), eTextEnc
);
128 TextInputStream::~TextInputStream()
132 bool TextInputStream::isEof() const
134 if( mxTextStrm
.is() ) try
136 return mxTextStrm
->isEOF();
138 catch (const Exception
&)
144 OUString
TextInputStream::readLine()
146 if( mxTextStrm
.is() ) try
148 /* The function createFinalString() adds a character that may have
149 been buffered in the previous call of readToChar() (see below). */
150 return createFinalString( mxTextStrm
->readLine() );
152 catch (const Exception
&)
159 OUString
TextInputStream::readToChar( sal_Unicode cChar
, bool bIncludeChar
)
161 if( mxTextStrm
.is() ) try
163 Sequence
< sal_Unicode
> aDelimiters( 1 );
164 aDelimiters
[ 0 ] = cChar
;
165 /* Always get the delimiter character from the UNO text input stream.
166 In difference to this implementation, it will not return it in the
167 next call but silently skip it. If caller specifies to exclude the
168 character in this call, it will be returned in the next call of one
169 of the own member functions. The function createFinalString() adds
170 a character that has been buffered in the previous call. */
171 OUString aString
= createFinalString( mxTextStrm
->readString( aDelimiters
, sal_False
) );
172 // remove last character from string and remember it for next call
173 if( !bIncludeChar
&& !aString
.isEmpty() && (aString
[ aString
.getLength() - 1 ] == cChar
) )
175 mcPendingChar
= cChar
;
176 aString
= aString
.copy( 0, aString
.getLength() - 1 );
180 catch (const Exception
&)
187 Reference
< XTextInputStream2
> TextInputStream::createXTextInputStream(
188 const Reference
< XComponentContext
>& rxContext
, const Reference
< XInputStream
>& rxInStrm
, rtl_TextEncoding eTextEnc
)
190 Reference
< XTextInputStream2
> xTextStrm
;
191 const char* pcCharset
= rtl_getBestMimeCharsetFromTextEncoding( eTextEnc
);
192 OSL_ENSURE( pcCharset
, "TextInputStream::createXTextInputStream - unsupported text encoding" );
193 if( rxContext
.is() && rxInStrm
.is() && pcCharset
) try
195 xTextStrm
= com::sun::star::io::TextInputStream::create( rxContext
);
196 xTextStrm
->setInputStream( rxInStrm
);
197 xTextStrm
->setEncoding( OUString::createFromAscii( pcCharset
) );
199 catch (const Exception
&)
205 // private --------------------------------------------------------------------
207 OUString
TextInputStream::createFinalString( const OUString
& rString
)
209 if( mcPendingChar
== 0 )
212 OUString aString
= OUString( mcPendingChar
) + rString
;
217 void TextInputStream::init( const Reference
< XComponentContext
>& rxContext
, const Reference
< XInputStream
>& rxInStrm
, rtl_TextEncoding eTextEnc
)
220 mxTextStrm
= createXTextInputStream( rxContext
, rxInStrm
, eTextEnc
);
223 // ============================================================================
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */