LCOV - code coverage report
Current view: top level - libreoffice/sc/source/core/tool - queryparam.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 133 193 68.9 %
Date: 2012-12-17 Functions: 31 44 70.5 %
Legend: Lines: hit not hit

          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 "queryparam.hxx"
      21             : #include "queryentry.hxx"
      22             : 
      23             : namespace {
      24             : 
      25             : const size_t MAXQUERY = 8;
      26             : 
      27             : class FindByField : public std::unary_function<ScQueryEntry, bool>
      28             : {
      29             :     SCCOLROW mnField;
      30             : public:
      31           0 :     FindByField(SCCOLROW nField) : mnField(nField) {}
      32           0 :     bool operator() (const ScQueryEntry& rEntry) const
      33             :     {
      34           0 :         return rEntry.bDoQuery && rEntry.nField == mnField;
      35             :     }
      36             : };
      37             : 
      38             : struct FindUnused : public std::unary_function<ScQueryEntry, bool>
      39             : {
      40          20 :     bool operator() (const ScQueryEntry& rEntry) const
      41             :     {
      42          20 :         return !rEntry.bDoQuery;
      43             :     }
      44             : };
      45             : 
      46             : }
      47             : 
      48         870 : ScQueryParamBase::const_iterator ScQueryParamBase::begin() const
      49             : {
      50         870 :     return maEntries.begin();
      51             : }
      52             : 
      53         870 : ScQueryParamBase::const_iterator ScQueryParamBase::end() const
      54             : {
      55         870 :     return maEntries.end();
      56             : }
      57             : 
      58         384 : ScQueryParamBase::ScQueryParamBase() :
      59             :     bHasHeader(true),
      60             :     bByRow(true),
      61             :     bInplace(true),
      62             :     bCaseSens(false),
      63             :     bRegExp(false),
      64         384 :     bDuplicate(false)
      65             : {
      66        3456 :     for (size_t i = 0; i < MAXQUERY; ++i)
      67        3072 :         maEntries.push_back(new ScQueryEntry);
      68         384 : }
      69             : 
      70         606 : ScQueryParamBase::ScQueryParamBase(const ScQueryParamBase& r) :
      71             :     bHasHeader(r.bHasHeader), bByRow(r.bByRow), bInplace(r.bInplace), bCaseSens(r.bCaseSens),
      72             :     bRegExp(r.bRegExp), bDuplicate(r.bDuplicate),
      73         606 :     maEntries(r.maEntries)
      74             : {
      75         606 : }
      76             : 
      77         954 : ScQueryParamBase::~ScQueryParamBase()
      78             : {
      79         954 : }
      80             : 
      81           0 : bool ScQueryParamBase::IsValidFieldIndex() const
      82             : {
      83           0 :     return true;
      84             : }
      85             : 
      86        1830 : SCSIZE ScQueryParamBase::GetEntryCount() const
      87             : {
      88        1830 :     return maEntries.size();
      89             : }
      90             : 
      91        2244 : const ScQueryEntry& ScQueryParamBase::GetEntry(SCSIZE n) const
      92             : {
      93        2244 :     return maEntries[n];
      94             : }
      95             : 
      96        2098 : ScQueryEntry& ScQueryParamBase::GetEntry(SCSIZE n)
      97             : {
      98        2098 :     return maEntries[n];
      99             : }
     100             : 
     101          18 : ScQueryEntry& ScQueryParamBase::AppendEntry()
     102             : {
     103             :     // Find the first unused entry.
     104             :     EntriesType::iterator itr = std::find_if(
     105          18 :         maEntries.begin(), maEntries.end(), FindUnused());
     106             : 
     107          18 :     if (itr != maEntries.end())
     108             :         // Found!
     109          18 :         return *itr;
     110             : 
     111             :     // Add a new entry to the end.
     112           0 :     maEntries.push_back(new ScQueryEntry);
     113           0 :     return maEntries.back();
     114             : }
     115             : 
     116           0 : ScQueryEntry* ScQueryParamBase::FindEntryByField(SCCOLROW nField, bool bNew)
     117             : {
     118             :     EntriesType::iterator itr = std::find_if(
     119           0 :         maEntries.begin(), maEntries.end(), FindByField(nField));
     120             : 
     121           0 :     if (itr != maEntries.end())
     122             :     {
     123             :         // existing entry found!
     124           0 :         return &(*itr);
     125             :     }
     126             : 
     127           0 :     if (!bNew)
     128             :         // no existing entry found, and we are not creating a new one.
     129           0 :         return NULL;
     130             : 
     131           0 :     return &AppendEntry();
     132             : }
     133             : 
     134           0 : void ScQueryParamBase::RemoveEntryByField(SCCOLROW nField)
     135             : {
     136             :     EntriesType::iterator itr = std::find_if(
     137           0 :         maEntries.begin(), maEntries.end(), FindByField(nField));
     138             : 
     139           0 :     if (itr != maEntries.end())
     140             :     {
     141           0 :         maEntries.erase(itr);
     142           0 :         if (maEntries.size() < MAXQUERY)
     143             :             // Make sure that we have at least MAXQUERY number of entries at
     144             :             // all times.
     145           0 :             maEntries.push_back(new ScQueryEntry);
     146             :     }
     147           0 : }
     148             : 
     149          22 : void ScQueryParamBase::Resize(size_t nNew)
     150             : {
     151          22 :     if (nNew < MAXQUERY)
     152          22 :         nNew = MAXQUERY;                // never less than MAXQUERY
     153             : 
     154          22 :     if (nNew < maEntries.size())
     155             :     {
     156           0 :         size_t n = maEntries.size() - nNew;
     157           0 :         for (size_t i = 0; i < n; ++i)
     158           0 :             maEntries.pop_back();
     159             :     }
     160          22 :     else if (nNew > maEntries.size())
     161             :     {
     162           0 :         size_t n = nNew - maEntries.size();
     163           0 :         for (size_t i = 0; i < n; ++i)
     164           0 :             maEntries.push_back(new ScQueryEntry);
     165             :     }
     166          22 : }
     167             : 
     168          54 : void ScQueryParamBase::FillInExcelSyntax(const rtl::OUString& rStr, SCSIZE nIndex)
     169             : {
     170          54 :     const String aCellStr = rStr;
     171          54 :     if (aCellStr.Len() > 0)
     172             :     {
     173          54 :         if ( nIndex >= maEntries.size() )
     174           0 :             Resize( nIndex+1 );
     175             : 
     176          54 :         ScQueryEntry& rEntry = GetEntry(nIndex);
     177          54 :         ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
     178             : 
     179          54 :         rEntry.bDoQuery = sal_True;
     180             :         // Operatoren herausfiltern
     181          54 :         if (aCellStr.GetChar(0) == '<')
     182             :         {
     183           4 :             if (aCellStr.GetChar(1) == '>')
     184             :             {
     185           0 :                 rItem.maString = aCellStr.Copy(2);
     186           0 :                 rEntry.eOp   = SC_NOT_EQUAL;
     187             :             }
     188           4 :             else if (aCellStr.GetChar(1) == '=')
     189             :             {
     190           0 :                 rItem.maString = aCellStr.Copy(2);
     191           0 :                 rEntry.eOp   = SC_LESS_EQUAL;
     192             :             }
     193             :             else
     194             :             {
     195           4 :                 rItem.maString = aCellStr.Copy(1);
     196           4 :                 rEntry.eOp   = SC_LESS;
     197             :             }
     198             :         }
     199          50 :         else if (aCellStr.GetChar(0) == '>')
     200             :         {
     201          32 :             if (aCellStr.GetChar(1) == '=')
     202             :             {
     203          16 :                 rItem.maString = aCellStr.Copy(2);
     204          16 :                 rEntry.eOp   = SC_GREATER_EQUAL;
     205             :             }
     206             :             else
     207             :             {
     208          16 :                 rItem.maString = aCellStr.Copy(1);
     209          16 :                 rEntry.eOp   = SC_GREATER;
     210             :             }
     211             :         }
     212             :         else
     213             :         {
     214          18 :             if (aCellStr.GetChar(0) == '=')
     215           0 :                 rItem.maString = aCellStr.Copy(1);
     216             :             else
     217          18 :                 rItem.maString = aCellStr;
     218          18 :             rEntry.eOp = SC_EQUAL;
     219             :         }
     220          54 :     }
     221          54 : }
     222             : 
     223             : // ============================================================================
     224             : 
     225         384 : ScQueryParamTable::ScQueryParamTable()
     226             : {
     227         384 : }
     228             : 
     229         606 : ScQueryParamTable::ScQueryParamTable(const ScQueryParamTable& r) :
     230         606 :     nCol1(r.nCol1),nRow1(r.nRow1),nCol2(r.nCol2),nRow2(r.nRow2),nTab(r.nTab)
     231             : {
     232         606 : }
     233             : 
     234         954 : ScQueryParamTable::~ScQueryParamTable()
     235             : {
     236         954 : }
     237             : 
     238             : // ============================================================================
     239             : 
     240         368 : ScQueryParam::ScQueryParam() :
     241             :     ScQueryParamBase(),
     242             :     ScQueryParamTable(),
     243             :     bDestPers(true),
     244             :     nDestTab(0),
     245             :     nDestCol(0),
     246         368 :     nDestRow(0)
     247             : {
     248         368 :     Clear();
     249         368 : }
     250             : 
     251             : //------------------------------------------------------------------------
     252             : 
     253         550 : ScQueryParam::ScQueryParam( const ScQueryParam& r ) :
     254             :     ScQueryParamBase(r),
     255             :     ScQueryParamTable(r),
     256         550 :     bDestPers(r.bDestPers), nDestTab(r.nDestTab), nDestCol(r.nDestCol), nDestRow(r.nDestRow)
     257             : {
     258         550 : }
     259             : 
     260          56 : ScQueryParam::ScQueryParam( const ScDBQueryParamInternal& r ) :
     261             :     ScQueryParamBase(r),
     262             :     ScQueryParamTable(r),
     263             :     bDestPers(true),
     264             :     nDestTab(0),
     265             :     nDestCol(0),
     266          56 :     nDestRow(0)
     267             : {
     268          56 : }
     269             : 
     270             : 
     271             : //------------------------------------------------------------------------
     272             : 
     273        1294 : ScQueryParam::~ScQueryParam()
     274             : {
     275        1294 : }
     276             : 
     277             : //------------------------------------------------------------------------
     278             : 
     279         368 : void ScQueryParam::Clear()
     280             : {
     281         368 :     nCol1=nCol2 = 0;
     282         368 :     nRow1=nRow2 = 0;
     283         368 :     nTab = SCTAB_MAX;
     284         368 :     bHasHeader = bCaseSens = bRegExp = false;
     285         368 :     bInplace = bByRow = bDuplicate = sal_True;
     286             : 
     287         368 :     boost::ptr_vector<ScQueryEntry>::iterator itr = maEntries.begin(), itrEnd = maEntries.end();
     288        3312 :     for (; itr != itrEnd; ++itr)
     289        2944 :         itr->Clear();
     290             : 
     291         368 :     ClearDestParams();
     292         368 : }
     293             : 
     294         368 : void ScQueryParam::ClearDestParams()
     295             : {
     296         368 :     bDestPers = true;
     297         368 :     nDestTab = 0;
     298         368 :     nDestCol = 0;
     299         368 :     nDestRow = 0;
     300         368 : }
     301             : 
     302             : //------------------------------------------------------------------------
     303             : 
     304          82 : ScQueryParam& ScQueryParam::operator=( const ScQueryParam& r )
     305             : {
     306          82 :     nCol1       = r.nCol1;
     307          82 :     nRow1       = r.nRow1;
     308          82 :     nCol2       = r.nCol2;
     309          82 :     nRow2       = r.nRow2;
     310          82 :     nTab        = r.nTab;
     311          82 :     nDestTab    = r.nDestTab;
     312          82 :     nDestCol    = r.nDestCol;
     313          82 :     nDestRow    = r.nDestRow;
     314          82 :     bHasHeader  = r.bHasHeader;
     315          82 :     bInplace    = r.bInplace;
     316          82 :     bCaseSens   = r.bCaseSens;
     317          82 :     bRegExp     = r.bRegExp;
     318          82 :     bDuplicate  = r.bDuplicate;
     319          82 :     bByRow      = r.bByRow;
     320          82 :     bDestPers   = r.bDestPers;
     321             : 
     322          82 :     maEntries = r.maEntries.clone();
     323             : 
     324          82 :     return *this;
     325             : }
     326             : 
     327             : //------------------------------------------------------------------------
     328             : 
     329           2 : bool ScQueryParam::operator==( const ScQueryParam& rOther ) const
     330             : {
     331           2 :     bool bEqual = false;
     332             : 
     333             :     // Anzahl der Queries gleich?
     334           2 :     SCSIZE nUsed      = 0;
     335           2 :     SCSIZE nOtherUsed = 0;
     336           2 :     SCSIZE nEntryCount = GetEntryCount();
     337           2 :     SCSIZE nOtherEntryCount = rOther.GetEntryCount();
     338             : 
     339           2 :     while ( nUsed<nEntryCount && maEntries[nUsed].bDoQuery ) ++nUsed;
     340           4 :     while ( nOtherUsed<nOtherEntryCount && rOther.maEntries[nOtherUsed].bDoQuery )
     341           0 :         ++nOtherUsed;
     342             : 
     343           2 :     if (   (nUsed       == nOtherUsed)
     344             :         && (nCol1       == rOther.nCol1)
     345             :         && (nRow1       == rOther.nRow1)
     346             :         && (nCol2       == rOther.nCol2)
     347             :         && (nRow2       == rOther.nRow2)
     348             :         && (nTab        == rOther.nTab)
     349             :         && (bHasHeader  == rOther.bHasHeader)
     350             :         && (bByRow      == rOther.bByRow)
     351             :         && (bInplace    == rOther.bInplace)
     352             :         && (bCaseSens   == rOther.bCaseSens)
     353             :         && (bRegExp     == rOther.bRegExp)
     354             :         && (bDuplicate  == rOther.bDuplicate)
     355             :         && (bDestPers   == rOther.bDestPers)
     356             :         && (nDestTab    == rOther.nDestTab)
     357             :         && (nDestCol    == rOther.nDestCol)
     358             :         && (nDestRow    == rOther.nDestRow) )
     359             :     {
     360           0 :         bEqual = true;
     361           0 :         for ( SCSIZE i=0; i<nUsed && bEqual; i++ )
     362           0 :             bEqual = maEntries[i] == rOther.maEntries[i];
     363             :     }
     364           2 :     return bEqual;
     365             : }
     366             : 
     367             : //------------------------------------------------------------------------
     368             : 
     369           0 : void ScQueryParam::MoveToDest()
     370             : {
     371           0 :     if (!bInplace)
     372             :     {
     373           0 :         SCsCOL nDifX = ((SCsCOL) nDestCol) - ((SCsCOL) nCol1);
     374           0 :         SCsROW nDifY = ((SCsROW) nDestRow) - ((SCsROW) nRow1);
     375           0 :         SCsTAB nDifZ = ((SCsTAB) nDestTab) - ((SCsTAB) nTab);
     376             : 
     377           0 :         nCol1 = sal::static_int_cast<SCCOL>( nCol1 + nDifX );
     378           0 :         nRow1 = sal::static_int_cast<SCROW>( nRow1 + nDifY );
     379           0 :         nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nDifX );
     380           0 :         nRow2 = sal::static_int_cast<SCROW>( nRow2 + nDifY );
     381           0 :         nTab  = sal::static_int_cast<SCTAB>( nTab  + nDifZ );
     382           0 :         size_t n = maEntries.size();
     383           0 :         for (size_t i=0; i<n; i++)
     384           0 :             maEntries[i].nField += nDifX;
     385             : 
     386           0 :         bInplace = sal_True;
     387             :     }
     388             :     else
     389             :     {
     390             :         OSL_FAIL("MoveToDest, bInplace == TRUE");
     391             :     }
     392           0 : }
     393             : 
     394             : // ============================================================================
     395             : 
     396          16 : ScDBQueryParamBase::ScDBQueryParamBase(DataType eType) :
     397             :     ScQueryParamBase(),
     398             :     mnField(-1),
     399             :     mbSkipString(true),
     400          16 :     meType(eType)
     401             : {
     402          16 : }
     403             : 
     404          16 : ScDBQueryParamBase::~ScDBQueryParamBase()
     405             : {
     406          16 : }
     407             : 
     408          16 : ScDBQueryParamBase::DataType ScDBQueryParamBase::GetType() const
     409             : {
     410          16 :     return meType;
     411             : }
     412             : 
     413             : // ============================================================================
     414             : 
     415          16 : ScDBQueryParamInternal::ScDBQueryParamInternal() :
     416             :     ScDBQueryParamBase(ScDBQueryParamBase::INTERNAL),
     417          16 :     ScQueryParamTable()
     418             : {
     419          16 : }
     420             : 
     421          32 : ScDBQueryParamInternal::~ScDBQueryParamInternal()
     422             : {
     423          32 : }
     424             : 
     425           8 : bool ScDBQueryParamInternal::IsValidFieldIndex() const
     426             : {
     427           8 :     return nCol1 <= mnField && mnField <= nCol2;
     428             : }
     429             : 
     430             : // ============================================================================
     431             : 
     432           0 : ScDBQueryParamMatrix::ScDBQueryParamMatrix() :
     433           0 :     ScDBQueryParamBase(ScDBQueryParamBase::MATRIX)
     434             : {
     435           0 : }
     436             : 
     437           0 : bool ScDBQueryParamMatrix::IsValidFieldIndex() const
     438             : {
     439             :     SCSIZE nC, nR;
     440           0 :     mpMatrix->GetDimensions(nC, nR);
     441           0 :     return 0 <= mnField && mnField <= static_cast<SCCOL>(nC);
     442             : }
     443             : 
     444           0 : ScDBQueryParamMatrix::~ScDBQueryParamMatrix()
     445             : {
     446           0 : }
     447             : 
     448             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10