Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "oox/helper/binarystreambase.hxx"
21 : :
22 : : #include <com/sun/star/io/XSeekable.hpp>
23 : : #include <osl/diagnose.h>
24 : :
25 : : namespace oox {
26 : :
27 : : // ============================================================================
28 : :
29 : : using namespace ::com::sun::star::io;
30 : : using namespace ::com::sun::star::uno;
31 : :
32 : : // ============================================================================
33 : :
34 : 2893 : BinaryStreamBase::~BinaryStreamBase()
35 : : {
36 [ - + ]: 2893 : }
37 : :
38 : 54 : sal_Int64 BinaryStreamBase::getRemaining() const
39 : : {
40 : : // do not use isSeekable(), implementations may provide stream position and size even if not seekable
41 : 54 : sal_Int64 nPos = tell();
42 : 54 : sal_Int64 nLen = size();
43 [ + - ][ + - ]: 54 : return ((nPos >= 0) && (nLen >= 0)) ? ::std::max< sal_Int64 >( nLen - nPos, 0 ) : -1;
[ + - ][ + - ]
[ # # # # ]
[ + - ]
44 : : }
45 : :
46 : 0 : void BinaryStreamBase::alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos )
47 : : {
48 : 0 : sal_Int64 nStrmPos = tell();
49 : : // nothing to do, if stream is at anchor position
50 [ # # ][ # # ]: 0 : if( mbSeekable && (0 <= nAnchorPos) && (nAnchorPos != nStrmPos) && (nBlockSize > 1) )
[ # # ][ # # ]
51 : : {
52 : : // prevent modulo with negative arguments...
53 : : sal_Int64 nSkipSize = (nAnchorPos < nStrmPos) ?
54 : : (nBlockSize - ((nStrmPos - nAnchorPos - 1) % nBlockSize) - 1) :
55 [ # # ]: 0 : ((nAnchorPos - nStrmPos) % nBlockSize);
56 : 0 : seek( nStrmPos + nSkipSize );
57 : : }
58 : 0 : }
59 : :
60 : : // ============================================================================
61 : :
62 : 1328 : BinaryXSeekableStream::BinaryXSeekableStream( const Reference< XSeekable >& rxSeekable ) :
63 : 0 : BinaryStreamBase( mxSeekable.is() ),
64 : 1328 : mxSeekable( rxSeekable )
65 : : {
66 : 1328 : }
67 : :
68 : 1328 : BinaryXSeekableStream::~BinaryXSeekableStream()
69 : : {
70 [ # # ][ # # ]: 1328 : }
[ - + ][ - + ]
71 : :
72 : 59 : sal_Int64 BinaryXSeekableStream::size() const
73 : : {
74 [ + - ]: 59 : if( mxSeekable.is() ) try
75 : : {
76 [ + - ][ + - ]: 59 : return mxSeekable->getLength();
77 : : }
78 : 0 : catch( Exception& )
79 : : {
80 : : OSL_FAIL( "BinaryXSeekableStream::size - exception caught" );
81 : : }
82 [ # # ]: 59 : return -1;
83 : : }
84 : :
85 : 512 : sal_Int64 BinaryXSeekableStream::tell() const
86 : : {
87 [ + - ]: 512 : if( mxSeekable.is() ) try
88 : : {
89 [ + - ][ + - ]: 512 : return mxSeekable->getPosition();
90 : : }
91 : 0 : catch( Exception& )
92 : : {
93 : : OSL_FAIL( "BinaryXSeekableStream::tell - exception caught" );
94 : : }
95 [ # # ]: 512 : return -1;
96 : : }
97 : :
98 : 255 : void BinaryXSeekableStream::seek( sal_Int64 nPos )
99 : : {
100 [ + - ]: 255 : if( mxSeekable.is() ) try
101 : : {
102 : 255 : mbEof = false;
103 [ + - ][ + - ]: 255 : mxSeekable->seek( nPos );
104 : : }
105 : 0 : catch( Exception& )
106 : : {
107 : 0 : mbEof = true;
108 : : }
109 [ # # ]: 255 : }
110 : :
111 : 1328 : void BinaryXSeekableStream::close()
112 : : {
113 : 1328 : mxSeekable.clear();
114 : 1328 : mbEof = true;
115 : 1328 : }
116 : :
117 : : // ============================================================================
118 : :
119 : 1397 : SequenceSeekableStream::SequenceSeekableStream( const StreamDataSequence& rData ) :
120 : : BinaryStreamBase( true ),
121 : : mpData( &rData ),
122 : 0 : mnPos( 0 )
123 : : {
124 : 1397 : }
125 : :
126 : 0 : sal_Int64 SequenceSeekableStream::size() const
127 : : {
128 [ # # ]: 0 : return mpData ? mpData->getLength() : -1;
129 : : }
130 : :
131 : 0 : sal_Int64 SequenceSeekableStream::tell() const
132 : : {
133 [ # # ]: 0 : return mpData ? mnPos : -1;
134 : : }
135 : :
136 : 0 : void SequenceSeekableStream::seek( sal_Int64 nPos )
137 : : {
138 [ # # ]: 0 : if( mpData )
139 : : {
140 : 0 : mnPos = getLimitedValue< sal_Int32, sal_Int64 >( nPos, 0, mpData->getLength() );
141 : 0 : mbEof = mnPos != nPos;
142 : : }
143 : 0 : }
144 : :
145 : 0 : void SequenceSeekableStream::close()
146 : : {
147 : 0 : mpData = 0;
148 : 0 : mbEof = true;
149 : 0 : }
150 : :
151 : : // ============================================================================
152 : :
153 : : } // namespace oox
154 : :
155 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|