use insert function instead of for loop
[LibreOffice.git] / sc / source / filter / excel / xladdress.cxx
blob20ef4fa7fc961112e94bb4ea3fde5f778b51e9c1
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 <xladdress.hxx>
21 #include <xestream.hxx>
22 #include <xltracer.hxx>
23 #include <xistream.hxx>
25 #include <o3tl/safeint.hxx>
26 #include <osl/diagnose.h>
28 void XclAddress::Read( XclImpStream& rStrm )
30 mnRow = rStrm.ReaduInt16();
31 mnCol = rStrm.ReaduInt16();
34 void XclAddress::Write( XclExpStream& rStrm ) const
36 rStrm << static_cast<sal_uInt16> (mnRow);
37 rStrm << mnCol;
40 bool XclRange::Contains( const XclAddress& rPos ) const
42 return (maFirst.mnCol <= rPos.mnCol) && (rPos.mnCol <= maLast.mnCol) &&
43 (maFirst.mnRow <= rPos.mnRow) && (rPos.mnRow <= maLast.mnRow);
46 void XclRange::Read( XclImpStream& rStrm, bool bCol16Bit )
48 maFirst.mnRow = rStrm.ReaduInt16();
49 maLast.mnRow = rStrm.ReaduInt16();
51 if( bCol16Bit )
53 maFirst.mnCol = rStrm.ReaduInt16();
54 maLast.mnCol = rStrm.ReaduInt16();
56 else
58 maFirst.mnCol = rStrm.ReaduInt8();
59 maLast.mnCol = rStrm.ReaduInt8();
63 void XclRange::Write( XclExpStream& rStrm, bool bCol16Bit ) const
65 rStrm << static_cast<sal_uInt16>(maFirst.mnRow) << static_cast<sal_uInt16>(maLast.mnRow);
66 if( bCol16Bit )
67 rStrm << maFirst.mnCol << maLast.mnCol;
68 else
69 rStrm << static_cast< sal_uInt8 >( maFirst.mnCol ) << static_cast< sal_uInt8 >( maLast.mnCol );
72 XclRange XclRangeList::GetEnclosingRange() const
74 XclRange aXclRange;
75 if( !mRanges.empty() )
77 XclRangeVector::const_iterator aIt = mRanges.begin(), aEnd = mRanges.end();
78 aXclRange = *aIt;
79 for( ++aIt; aIt != aEnd; ++aIt )
81 aXclRange.maFirst.mnCol = ::std::min( aXclRange.maFirst.mnCol, aIt->maFirst.mnCol );
82 aXclRange.maFirst.mnRow = ::std::min( aXclRange.maFirst.mnRow, aIt->maFirst.mnRow );
83 aXclRange.maLast.mnCol = ::std::max( aXclRange.maLast.mnCol, aIt->maLast.mnCol );
84 aXclRange.maLast.mnRow = ::std::max( aXclRange.maLast.mnRow, aIt->maLast.mnRow );
87 return aXclRange;
90 void XclRangeList::Read( XclImpStream& rStrm, bool bCol16Bit, sal_uInt16 nCountInStream )
92 sal_uInt16 nCount;
93 if (nCountInStream)
94 nCount = nCountInStream;
95 else
96 nCount = rStrm.ReaduInt16();
98 if (!nCount)
99 return;
101 XclRange aRange;
102 while (true)
104 aRange.Read(rStrm, bCol16Bit);
105 if (!rStrm.IsValid())
106 break;
107 mRanges.emplace_back(aRange);
108 --nCount;
109 if (!nCount)
110 break;
114 void XclRangeList::Write( XclExpStream& rStrm, bool bCol16Bit, sal_uInt16 nCountInStream ) const
116 WriteSubList( rStrm, 0, mRanges.size(), bCol16Bit, nCountInStream );
119 void XclRangeList::WriteSubList( XclExpStream& rStrm, size_t nBegin, size_t nCount, bool bCol16Bit,
120 sal_uInt16 nCountInStream ) const
122 OSL_ENSURE( nBegin <= mRanges.size(), "XclRangeList::WriteSubList - invalid start position" );
123 size_t nEnd = ::std::min< size_t >( nBegin + nCount, mRanges.size() );
124 if (!nCountInStream)
126 sal_uInt16 nXclCount = ulimit_cast< sal_uInt16 >( nEnd - nBegin );
127 rStrm << nXclCount;
129 rStrm.SetSliceSize( bCol16Bit ? 8 : 6 );
130 std::for_each(mRanges.begin() + nBegin, mRanges.begin() + nEnd,
131 [&rStrm, &bCol16Bit](const XclRange& rRange) { rRange.Write(rStrm, bCol16Bit); });
134 XclAddressConverterBase::XclAddressConverterBase( XclTracer& rTracer, const ScAddress& rMaxPos ) :
135 mrTracer( rTracer ),
136 maMaxPos( rMaxPos ),
137 mnMaxCol( static_cast< sal_uInt16 >( rMaxPos.Col() ) ),
138 mnMaxRow( static_cast< sal_uInt16 >( rMaxPos.Row() ) ),
139 mbColTrunc( false ),
140 mbRowTrunc( false ),
141 mbTabTrunc( false )
143 OSL_ENSURE( o3tl::make_unsigned( rMaxPos.Col() ) <= SAL_MAX_UINT16, "XclAddressConverterBase::XclAddressConverterBase - invalid max column" );
144 OSL_ENSURE( o3tl::make_unsigned( rMaxPos.Row() ) <= SAL_MAX_UINT32, "XclAddressConverterBase::XclAddressConverterBase - invalid max row" );
147 XclAddressConverterBase::~XclAddressConverterBase()
151 void XclAddressConverterBase::CheckScTab( SCTAB nScTab )
153 bool bValid = (0 <= nScTab) && (nScTab <= maMaxPos.Tab());
154 if( !bValid )
156 mbTabTrunc |= (nScTab > maMaxPos.Tab()); // do not warn for deleted refs
157 mrTracer.TraceInvalidTab( nScTab, maMaxPos.Tab() );
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */