LibreOffice Module sc (master)  1
xladdress.cxx
Go to the documentation of this file.
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 <xladdress.hxx>
21 #include <xestream.hxx>
22 #include <xltracer.hxx>
23 #include <xistream.hxx>
24 
25 #include <o3tl/safeint.hxx>
26 #include <osl/diagnose.h>
27 
29 {
30  mnRow = rStrm.ReaduInt16();
31  mnCol = rStrm.ReaduInt16();
32 }
33 
34 void XclAddress::Write( XclExpStream& rStrm ) const
35 {
36  rStrm << static_cast<sal_uInt16> (mnRow);
37  rStrm << mnCol;
38 }
39 
40 bool XclRange::Contains( const XclAddress& rPos ) const
41 {
42  return (maFirst.mnCol <= rPos.mnCol) && (rPos.mnCol <= maLast.mnCol) &&
43  (maFirst.mnRow <= rPos.mnRow) && (rPos.mnRow <= maLast.mnRow);
44 }
45 
46 void XclRange::Read( XclImpStream& rStrm, bool bCol16Bit )
47 {
48  maFirst.mnRow = rStrm.ReaduInt16();
49  maLast.mnRow = rStrm.ReaduInt16();
50 
51  if( bCol16Bit )
52  {
53  maFirst.mnCol = rStrm.ReaduInt16();
54  maLast.mnCol = rStrm.ReaduInt16();
55  }
56  else
57  {
58  maFirst.mnCol = rStrm.ReaduInt8();
59  maLast.mnCol = rStrm.ReaduInt8();
60  }
61 }
62 
63 void XclRange::Write( XclExpStream& rStrm, bool bCol16Bit ) const
64 {
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 );
70 }
71 
73 {
74  XclRange aXclRange;
75  if( !mRanges.empty() )
76  {
77  XclRangeVector::const_iterator aIt = mRanges.begin(), aEnd = mRanges.end();
78  aXclRange = *aIt;
79  for( ++aIt; aIt != aEnd; ++aIt )
80  {
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 );
85  }
86  }
87  return aXclRange;
88 }
89 
90 void XclRangeList::Read( XclImpStream& rStrm, bool bCol16Bit, sal_uInt16 nCountInStream )
91 {
92  sal_uInt16 nCount;
93  if (nCountInStream)
94  nCount = nCountInStream;
95  else
96  nCount = rStrm.ReaduInt16();
97  size_t nOldSize = mRanges.size();
98  mRanges.resize( nOldSize + nCount );
99  for( XclRangeVector::iterator aIt = mRanges.begin() + nOldSize; rStrm.IsValid() && (nCount > 0); --nCount, ++aIt )
100  aIt->Read( rStrm, bCol16Bit );
101 }
102 
103 void XclRangeList::Write( XclExpStream& rStrm, bool bCol16Bit, sal_uInt16 nCountInStream ) const
104 {
105  WriteSubList( rStrm, 0, mRanges.size(), bCol16Bit, nCountInStream );
106 }
107 
108 void XclRangeList::WriteSubList( XclExpStream& rStrm, size_t nBegin, size_t nCount, bool bCol16Bit,
109  sal_uInt16 nCountInStream ) const
110 {
111  OSL_ENSURE( nBegin <= mRanges.size(), "XclRangeList::WriteSubList - invalid start position" );
112  size_t nEnd = ::std::min< size_t >( nBegin + nCount, mRanges.size() );
113  if (!nCountInStream)
114  {
115  sal_uInt16 nXclCount = ulimit_cast< sal_uInt16 >( nEnd - nBegin );
116  rStrm << nXclCount;
117  }
118  rStrm.SetSliceSize( bCol16Bit ? 8 : 6 );
119  std::for_each(mRanges.begin() + nBegin, mRanges.begin() + nEnd,
120  [&rStrm, &bCol16Bit](const XclRange& rRange) { rRange.Write(rStrm, bCol16Bit); });
121 }
122 
124  mrTracer( rTracer ),
125  maMaxPos( rMaxPos ),
126  mnMaxCol( static_cast< sal_uInt16 >( rMaxPos.Col() ) ),
127  mnMaxRow( static_cast< sal_uInt16 >( rMaxPos.Row() ) ),
128  mbColTrunc( false ),
129  mbRowTrunc( false ),
130  mbTabTrunc( false )
131 {
132  OSL_ENSURE( o3tl::make_unsigned( rMaxPos.Col() ) <= SAL_MAX_UINT16, "XclAddressConverterBase::XclAddressConverterBase - invalid max column" );
133  OSL_ENSURE( o3tl::make_unsigned( rMaxPos.Row() ) <= SAL_MAX_UINT32, "XclAddressConverterBase::XclAddressConverterBase - invalid max row" );
134 }
135 
137 {
138 }
139 
141 {
142  bool bValid = (0 <= nScTab) && (nScTab <= maMaxPos.Tab());
143  if( !bValid )
144  {
145  mbTabTrunc |= (nScTab > maMaxPos.Tab()); // do not warn for deleted refs
146  mrTracer.TraceInvalidTab( nScTab, maMaxPos.Tab() );
147  }
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScAddress maMaxPos
Tracer for invalid addresses.
Definition: xladdress.hxx:161
This class wraps an MSFilterTracer to create trace logs for import/export filters.
Definition: xltracer.hxx:51
This class is used to export Excel record streams.
Definition: xestream.hxx:71
SCROW Row() const
Definition: address.hxx:262
void Write(XclExpStream &rStrm, bool bCol16Bit=true) const
Definition: xladdress.cxx:63
bool mbTabTrunc
Flag for "rows truncated" warning box.
Definition: xladdress.hxx:166
bool IsValid() const
Returns record reading state: false = record overread.
Definition: xistream.hxx:352
void Write(XclExpStream &rStrm) const
Definition: xladdress.cxx:34
XclAddress maLast
Definition: xladdress.hxx:61
sal_uInt32 mnRow
Definition: xladdress.hxx:33
XclAddress maFirst
Definition: xladdress.hxx:60
void CheckScTab(SCTAB nScTab)
Checks if the passed sheet index is valid.
Definition: xladdress.cxx:140
void Read(XclImpStream &rStrm)
Definition: xladdress.cxx:28
void Read(XclImpStream &rStrm, bool bCol16Bit=true, sal_uInt16 nCountInStream=0)
Definition: xladdress.cxx:90
#define SAL_MAX_UINT32
#define SAL_MAX_UINT16
ReturnType ulimit_cast(Type nValue, ReturnType nMax)
Returns the value, if it is not greater than nMax, otherwise nMax.
Definition: ftools.hxx:51
int nCount
SCTAB Tab() const
Definition: address.hxx:271
void TraceInvalidTab(SCTAB nTab, SCTAB nMaxTab)
Definition: xltracer.cxx:61
void SetSliceSize(sal_uInt16 nSize)
Sets data slice length.
Definition: xestream.cxx:129
bool Contains(const XclAddress &rPos) const
Definition: xladdress.cxx:40
void Read(XclImpStream &rStrm, bool bCol16Bit=true)
Definition: xladdress.cxx:46
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
sal_uInt16 mnCol
Definition: xladdress.hxx:32
A 2D cell address struct with Excel column and row indexes.
Definition: xladdress.hxx:30
sal_uInt16 ReaduInt16()
Definition: xistream.cxx:648
SCCOL Col() const
Definition: address.hxx:267
virtual ~XclAddressConverterBase()
Definition: xladdress.cxx:136
sal_uInt8 ReaduInt8()
Definition: xistream.cxx:616
XclAddressConverterBase(XclTracer &rTracer, const ScAddress &rMaxPos)
Definition: xladdress.cxx:123
XclRange GetEnclosingRange() const
Definition: xladdress.cxx:72
This class is used to import record oriented streams.
Definition: xistream.hxx:278
void WriteSubList(XclExpStream &rStrm, size_t nBegin, size_t nCount, bool bCol16Bit=true, sal_uInt16 nCountInStream=0) const
Definition: xladdress.cxx:108
void Write(XclExpStream &rStrm, bool bCol16Bit=true, sal_uInt16 nCountInStream=0) const
Definition: xladdress.cxx:103
A 2D cell range address struct with Excel column and row indexes.
Definition: xladdress.hxx:58
sal_Int16 SCTAB
Definition: types.hxx:23
XclRangeVector mRanges
Definition: xladdress.hxx:104