Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / source / core / data / subtotalparam.cxx
blobe8f32954297c3b48f781a78056724125feff06b0
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/.
8 */
10 #include <subtotalparam.hxx>
12 #include <osl/diagnose.h>
14 ScSubTotalParam::ScSubTotalParam()
16 for ( sal_uInt16 i=0; i<MAXSUBTOTAL; i++ )
18 nSubTotals[i] = 0;
19 pSubTotals[i] = nullptr;
20 pFunctions[i] = nullptr;
23 Clear();
26 ScSubTotalParam::ScSubTotalParam( const ScSubTotalParam& r ) :
27 nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nUserIndex(r.nUserIndex),
28 bRemoveOnly(r.bRemoveOnly),bReplace(r.bReplace),bPagebreak(r.bPagebreak),bCaseSens(r.bCaseSens),
29 bDoSort(r.bDoSort),bAscending(r.bAscending),bUserDef(r.bUserDef),
30 bIncludePattern(r.bIncludePattern)
32 for (sal_uInt16 i=0; i<MAXSUBTOTAL; i++)
34 bGroupActive[i] = r.bGroupActive[i];
35 nField[i] = r.nField[i];
37 if ( (r.nSubTotals[i] > 0) && r.pSubTotals[i] && r.pFunctions[i] )
39 nSubTotals[i] = r.nSubTotals[i];
40 pSubTotals[i].reset(new SCCOL [r.nSubTotals[i]]);
41 pFunctions[i].reset(new ScSubTotalFunc [r.nSubTotals[i]]);
43 for (SCCOL j=0; j<r.nSubTotals[i]; j++)
45 pSubTotals[i][j] = r.pSubTotals[i][j];
46 pFunctions[i][j] = r.pFunctions[i][j];
49 else
51 nSubTotals[i] = 0;
56 void ScSubTotalParam::Clear()
58 nCol1=nCol2= 0;
59 nRow1=nRow2 = 0;
60 nUserIndex = 0;
61 bPagebreak=bCaseSens=bUserDef=bIncludePattern=bRemoveOnly = false;
62 bAscending=bReplace=bDoSort = true;
64 for (sal_uInt16 i=0; i<MAXSUBTOTAL; i++)
66 bGroupActive[i] = false;
67 nField[i] = 0;
69 if ( (nSubTotals[i] > 0) && pSubTotals[i] && pFunctions[i] )
71 for ( SCCOL j=0; j<nSubTotals[i]; j++ ) {
72 pSubTotals[i][j] = 0;
73 pFunctions[i][j] = SUBTOTAL_FUNC_NONE;
79 ScSubTotalParam& ScSubTotalParam::operator=( const ScSubTotalParam& r )
81 if(this == &r)
82 return *this;
84 nCol1 = r.nCol1;
85 nRow1 = r.nRow1;
86 nCol2 = r.nCol2;
87 nRow2 = r.nRow2;
88 bRemoveOnly = r.bRemoveOnly;
89 bReplace = r.bReplace;
90 bPagebreak = r.bPagebreak;
91 bCaseSens = r.bCaseSens;
92 bDoSort = r.bDoSort;
93 bAscending = r.bAscending;
94 bUserDef = r.bUserDef;
95 nUserIndex = r.nUserIndex;
96 bIncludePattern = r.bIncludePattern;
98 for (sal_uInt16 i=0; i<MAXSUBTOTAL; i++)
100 bGroupActive[i] = r.bGroupActive[i];
101 nField[i] = r.nField[i];
102 nSubTotals[i] = r.nSubTotals[i];
104 pSubTotals[i].reset();
105 pFunctions[i].reset();
107 if ( r.nSubTotals[i] > 0 )
109 pSubTotals[i].reset(new SCCOL [r.nSubTotals[i]]);
110 pFunctions[i].reset(new ScSubTotalFunc [r.nSubTotals[i]]);
112 for (SCCOL j=0; j<r.nSubTotals[i]; j++)
114 pSubTotals[i][j] = r.pSubTotals[i][j];
115 pFunctions[i][j] = r.pFunctions[i][j];
118 else
120 nSubTotals[i] = 0;
124 return *this;
127 bool ScSubTotalParam::operator==( const ScSubTotalParam& rOther ) const
129 bool bEqual = (nCol1 == rOther.nCol1)
130 && (nRow1 == rOther.nRow1)
131 && (nCol2 == rOther.nCol2)
132 && (nRow2 == rOther.nRow2)
133 && (nUserIndex == rOther.nUserIndex)
134 && (bRemoveOnly == rOther.bRemoveOnly)
135 && (bReplace == rOther.bReplace)
136 && (bPagebreak == rOther.bPagebreak)
137 && (bDoSort == rOther.bDoSort)
138 && (bCaseSens == rOther.bCaseSens)
139 && (bAscending == rOther.bAscending)
140 && (bUserDef == rOther.bUserDef)
141 && (bIncludePattern== rOther.bIncludePattern);
143 if ( bEqual )
145 bEqual = true;
146 for ( sal_uInt16 i=0; i<MAXSUBTOTAL && bEqual; i++ )
148 bEqual = (bGroupActive[i] == rOther.bGroupActive[i])
149 && (nField[i] == rOther.nField[i])
150 && (nSubTotals[i] == rOther.nSubTotals[i]);
152 if ( bEqual && (nSubTotals[i] > 0) )
154 for (SCCOL j=0; (j<nSubTotals[i]) && bEqual; j++)
156 bEqual = bEqual
157 && (pSubTotals[i][j] == rOther.pSubTotals[i][j])
158 && (pFunctions[i][j] == rOther.pFunctions[i][j]);
164 return bEqual;
167 void ScSubTotalParam::SetSubTotals( sal_uInt16 nGroup,
168 const SCCOL* ptrSubTotals,
169 const ScSubTotalFunc* ptrFunctions,
170 sal_uInt16 nCount )
172 OSL_ENSURE( (nGroup <= MAXSUBTOTAL),
173 "ScSubTotalParam::SetSubTotals(): nGroup > MAXSUBTOTAL!" );
174 OSL_ENSURE( ptrSubTotals,
175 "ScSubTotalParam::SetSubTotals(): ptrSubTotals == NULL!" );
176 OSL_ENSURE( ptrFunctions,
177 "ScSubTotalParam::SetSubTotals(): ptrFunctions == NULL!" );
178 OSL_ENSURE( (nCount > 0),
179 "ScSubTotalParam::SetSubTotals(): nCount <= 0!" );
181 if ( !(ptrSubTotals && ptrFunctions && (nCount > 0) && (nGroup <= MAXSUBTOTAL)) )
182 return;
184 // 0 is interpreted as 1, otherwise decrementing the array index
185 if (nGroup != 0)
186 nGroup--;
188 pSubTotals[nGroup].reset(new SCCOL[nCount]);
189 pFunctions[nGroup].reset(new ScSubTotalFunc[nCount]);
190 nSubTotals[nGroup] = static_cast<SCCOL>(nCount);
192 for ( sal_uInt16 i=0; i<nCount; i++ )
194 pSubTotals[nGroup][i] = ptrSubTotals[i];
195 pFunctions[nGroup][i] = ptrFunctions[i];
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */