Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / queryparam.cxx
blobe7f73ca2a83fe165c0c8450d99ac49dcf06d9b0e
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 "queryparam.hxx"
21 #include "queryentry.hxx"
22 #include "scmatrix.hxx"
24 #include "svl/sharedstringpool.hxx"
26 namespace {
28 const size_t MAXQUERY = 8;
30 class FindByField : public std::unary_function<ScQueryEntry, bool>
32 SCCOLROW mnField;
33 public:
34 FindByField(SCCOLROW nField) : mnField(nField) {}
35 bool operator() (const ScQueryEntry& rEntry) const
37 return rEntry.bDoQuery && rEntry.nField == mnField;
41 struct FindUnused : public std::unary_function<ScQueryEntry, bool>
43 bool operator() (const ScQueryEntry& rEntry) const
45 return !rEntry.bDoQuery;
51 ScQueryParamBase::const_iterator ScQueryParamBase::begin() const
53 return maEntries.begin();
56 ScQueryParamBase::const_iterator ScQueryParamBase::end() const
58 return maEntries.end();
61 ScQueryParamBase::ScQueryParamBase() :
62 bHasHeader(true),
63 bByRow(true),
64 bInplace(true),
65 bCaseSens(false),
66 bRegExp(false),
67 bDuplicate(false),
68 mbRangeLookup(false)
70 for (size_t i = 0; i < MAXQUERY; ++i)
71 maEntries.push_back(new ScQueryEntry);
74 ScQueryParamBase::ScQueryParamBase(const ScQueryParamBase& r) :
75 bHasHeader(r.bHasHeader), bByRow(r.bByRow), bInplace(r.bInplace), bCaseSens(r.bCaseSens),
76 bRegExp(r.bRegExp), bDuplicate(r.bDuplicate), mbRangeLookup(r.mbRangeLookup),
77 maEntries(r.maEntries)
81 ScQueryParamBase::~ScQueryParamBase()
85 bool ScQueryParamBase::IsValidFieldIndex() const
87 return true;
90 SCSIZE ScQueryParamBase::GetEntryCount() const
92 return maEntries.size();
95 const ScQueryEntry& ScQueryParamBase::GetEntry(SCSIZE n) const
97 return maEntries[n];
100 ScQueryEntry& ScQueryParamBase::GetEntry(SCSIZE n)
102 return maEntries[n];
105 ScQueryEntry& ScQueryParamBase::AppendEntry()
107 // Find the first unused entry.
108 EntriesType::iterator itr = std::find_if(
109 maEntries.begin(), maEntries.end(), FindUnused());
111 if (itr != maEntries.end())
112 // Found!
113 return *itr;
115 // Add a new entry to the end.
116 maEntries.push_back(new ScQueryEntry);
117 return maEntries.back();
120 ScQueryEntry* ScQueryParamBase::FindEntryByField(SCCOLROW nField, bool bNew)
122 EntriesType::iterator itr = std::find_if(
123 maEntries.begin(), maEntries.end(), FindByField(nField));
125 if (itr != maEntries.end())
127 // existing entry found!
128 return &(*itr);
131 if (!bNew)
132 // no existing entry found, and we are not creating a new one.
133 return NULL;
135 return &AppendEntry();
138 void ScQueryParamBase::RemoveEntryByField(SCCOLROW nField)
140 EntriesType::iterator itr = std::find_if(
141 maEntries.begin(), maEntries.end(), FindByField(nField));
143 if (itr != maEntries.end())
145 maEntries.erase(itr);
146 if (maEntries.size() < MAXQUERY)
147 // Make sure that we have at least MAXQUERY number of entries at
148 // all times.
149 maEntries.push_back(new ScQueryEntry);
153 void ScQueryParamBase::Resize(size_t nNew)
155 if (nNew < MAXQUERY)
156 nNew = MAXQUERY; // never less than MAXQUERY
158 if (nNew < maEntries.size())
160 size_t n = maEntries.size() - nNew;
161 for (size_t i = 0; i < n; ++i)
162 maEntries.pop_back();
164 else if (nNew > maEntries.size())
166 size_t n = nNew - maEntries.size();
167 for (size_t i = 0; i < n; ++i)
168 maEntries.push_back(new ScQueryEntry);
172 void ScQueryParamBase::FillInExcelSyntax(
173 svl::SharedStringPool& rPool, const OUString& rStr, SCSIZE nIndex)
175 const OUString aCellStr = rStr;
176 if (!aCellStr.isEmpty())
178 if ( nIndex >= maEntries.size() )
179 Resize( nIndex+1 );
181 ScQueryEntry& rEntry = GetEntry(nIndex);
182 ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
184 rEntry.bDoQuery = sal_True;
185 // Operatoren herausfiltern
186 if (aCellStr[0] == '<')
188 if (aCellStr[1] == '>')
190 rItem.maString = rPool.intern(aCellStr.copy(2));
191 rEntry.eOp = SC_NOT_EQUAL;
193 else if (aCellStr[1] == '=')
195 rItem.maString = rPool.intern(aCellStr.copy(2));
196 rEntry.eOp = SC_LESS_EQUAL;
198 else
200 rItem.maString = rPool.intern(aCellStr.copy(1));
201 rEntry.eOp = SC_LESS;
204 else if (aCellStr[0]== '>')
206 if (aCellStr[1] == '=')
208 rItem.maString = rPool.intern(aCellStr.copy(2));
209 rEntry.eOp = SC_GREATER_EQUAL;
211 else
213 rItem.maString = rPool.intern(aCellStr.copy(1));
214 rEntry.eOp = SC_GREATER;
217 else
219 if (aCellStr[0] == '=')
220 rItem.maString = rPool.intern(aCellStr.copy(1));
221 else
222 rItem.maString = rPool.intern(aCellStr);
223 rEntry.eOp = SC_EQUAL;
228 ScQueryParamTable::ScQueryParamTable()
232 ScQueryParamTable::ScQueryParamTable(const ScQueryParamTable& r) :
233 nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nTab(r.nTab)
237 ScQueryParamTable::~ScQueryParamTable()
241 ScQueryParam::ScQueryParam() :
242 ScQueryParamBase(),
243 ScQueryParamTable(),
244 bDestPers(true),
245 nDestTab(0),
246 nDestCol(0),
247 nDestRow(0)
249 Clear();
252 ScQueryParam::ScQueryParam( const ScQueryParam& r ) :
253 ScQueryParamBase(r),
254 ScQueryParamTable(r),
255 bDestPers(r.bDestPers), nDestTab(r.nDestTab), nDestCol(r.nDestCol), nDestRow(r.nDestRow)
259 ScQueryParam::ScQueryParam( const ScDBQueryParamInternal& r ) :
260 ScQueryParamBase(r),
261 ScQueryParamTable(r),
262 bDestPers(true),
263 nDestTab(0),
264 nDestCol(0),
265 nDestRow(0)
269 ScQueryParam::~ScQueryParam()
273 void ScQueryParam::Clear()
275 nCol1=nCol2 = 0;
276 nRow1=nRow2 = 0;
277 nTab = SCTAB_MAX;
278 bHasHeader = bCaseSens = bRegExp = false;
279 bInplace = bByRow = bDuplicate = sal_True;
281 boost::ptr_vector<ScQueryEntry>::iterator itr = maEntries.begin(), itrEnd = maEntries.end();
282 for (; itr != itrEnd; ++itr)
283 itr->Clear();
285 ClearDestParams();
288 void ScQueryParam::ClearDestParams()
290 bDestPers = true;
291 nDestTab = 0;
292 nDestCol = 0;
293 nDestRow = 0;
296 ScQueryParam& ScQueryParam::operator=( const ScQueryParam& r )
298 nCol1 = r.nCol1;
299 nRow1 = r.nRow1;
300 nCol2 = r.nCol2;
301 nRow2 = r.nRow2;
302 nTab = r.nTab;
303 nDestTab = r.nDestTab;
304 nDestCol = r.nDestCol;
305 nDestRow = r.nDestRow;
306 bHasHeader = r.bHasHeader;
307 bInplace = r.bInplace;
308 bCaseSens = r.bCaseSens;
309 bRegExp = r.bRegExp;
310 bDuplicate = r.bDuplicate;
311 bByRow = r.bByRow;
312 bDestPers = r.bDestPers;
314 maEntries = r.maEntries.clone();
316 return *this;
319 bool ScQueryParam::operator==( const ScQueryParam& rOther ) const
321 bool bEqual = false;
323 // Anzahl der Queries gleich?
324 SCSIZE nUsed = 0;
325 SCSIZE nOtherUsed = 0;
326 SCSIZE nEntryCount = GetEntryCount();
327 SCSIZE nOtherEntryCount = rOther.GetEntryCount();
329 while ( nUsed<nEntryCount && maEntries[nUsed].bDoQuery ) ++nUsed;
330 while ( nOtherUsed<nOtherEntryCount && rOther.maEntries[nOtherUsed].bDoQuery )
331 ++nOtherUsed;
333 if ( (nUsed == nOtherUsed)
334 && (nCol1 == rOther.nCol1)
335 && (nRow1 == rOther.nRow1)
336 && (nCol2 == rOther.nCol2)
337 && (nRow2 == rOther.nRow2)
338 && (nTab == rOther.nTab)
339 && (bHasHeader == rOther.bHasHeader)
340 && (bByRow == rOther.bByRow)
341 && (bInplace == rOther.bInplace)
342 && (bCaseSens == rOther.bCaseSens)
343 && (bRegExp == rOther.bRegExp)
344 && (bDuplicate == rOther.bDuplicate)
345 && (bDestPers == rOther.bDestPers)
346 && (nDestTab == rOther.nDestTab)
347 && (nDestCol == rOther.nDestCol)
348 && (nDestRow == rOther.nDestRow) )
350 bEqual = true;
351 for ( SCSIZE i=0; i<nUsed && bEqual; i++ )
352 bEqual = maEntries[i] == rOther.maEntries[i];
354 return bEqual;
357 void ScQueryParam::MoveToDest()
359 if (!bInplace)
361 SCsCOL nDifX = ((SCsCOL) nDestCol) - ((SCsCOL) nCol1);
362 SCsROW nDifY = ((SCsROW) nDestRow) - ((SCsROW) nRow1);
363 SCsTAB nDifZ = ((SCsTAB) nDestTab) - ((SCsTAB) nTab);
365 nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
366 nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
367 nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
368 nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
369 nTab = sal::static_int_cast<SCTAB>( nTab + nDifZ );
370 size_t n = maEntries.size();
371 for (size_t i=0; i<n; i++)
372 maEntries[i].nField += nDifX;
374 bInplace = sal_True;
376 else
378 OSL_FAIL("MoveToDest, bInplace == TRUE");
382 ScDBQueryParamBase::ScDBQueryParamBase(DataType eType) :
383 ScQueryParamBase(),
384 mnField(-1),
385 mbSkipString(true),
386 meType(eType)
390 ScDBQueryParamBase::~ScDBQueryParamBase()
394 ScDBQueryParamBase::DataType ScDBQueryParamBase::GetType() const
396 return meType;
399 ScDBQueryParamInternal::ScDBQueryParamInternal() :
400 ScDBQueryParamBase(ScDBQueryParamBase::INTERNAL),
401 ScQueryParamTable()
405 ScDBQueryParamInternal::~ScDBQueryParamInternal()
409 bool ScDBQueryParamInternal::IsValidFieldIndex() const
411 return nCol1 <= mnField && mnField <= nCol2;
414 ScDBQueryParamMatrix::ScDBQueryParamMatrix() :
415 ScDBQueryParamBase(ScDBQueryParamBase::MATRIX)
419 bool ScDBQueryParamMatrix::IsValidFieldIndex() const
421 SCSIZE nC, nR;
422 mpMatrix->GetDimensions(nC, nR);
423 return 0 <= mnField && mnField <= static_cast<SCCOL>(nC);
426 ScDBQueryParamMatrix::~ScDBQueryParamMatrix()
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */