LCOV - code coverage report
Current view: top level - libreoffice/ucb/source/inc - regexpmap.tpt (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 95 153 62.1 %
Date: 2012-12-17 Functions: 28 39 71.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * This file is part of the LibreOffice project.
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * This file incorporates work covered by the following license notice:
       9             :  *
      10             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      11             :  *   contributor license agreements. See the NOTICE file distributed
      12             :  *   with this work for additional information regarding copyright
      13             :  *   ownership. The ASF licenses this file to you under the Apache
      14             :  *   License, Version 2.0 (the "License"); you may not use this file
      15             :  *   except in compliance with the License. You may obtain a copy of
      16             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      17             :  */
      18             : 
      19             : #ifndef _UCB_REGEXPMAP_TPT_
      20             : #define _UCB_REGEXPMAP_TPT_
      21             : 
      22             : #ifndef _UCB_REGEXPMAP_HXX_
      23             : #include <regexpmap.hxx>
      24             : #endif
      25             : 
      26             : #include <list>
      27             : 
      28             : #ifndef _RTL_USTRING_HXX_
      29             : #include <rtl/ustring.hxx>
      30             : #endif
      31             : 
      32             : #ifndef _UCB_REGEXP_HXX_
      33             : #include "regexp.hxx"
      34             : #endif
      35             : 
      36             : using namespace ucb_impl;
      37             : 
      38             : namespace ucb_impl {
      39             : 
      40             : //============================================================================
      41             : 
      42             : template< typename Val >
      43         810 : struct Entry
      44             : {
      45             :         Regexp m_aRegexp;
      46             :         Val m_aValue;
      47             : 
      48         270 :         inline Entry(Regexp const & rTheRegexp, Val const & rTheValue):
      49         270 :                 m_aRegexp(rTheRegexp), m_aValue(rTheValue) {}
      50             : };
      51             : 
      52             : //============================================================================
      53         504 : template< typename Val > class List: public std::list< Entry< Val > > {};
      54             : 
      55             : //============================================================================
      56             : //
      57             : //  RegexpMapIterImpl
      58             : //
      59             : //============================================================================
      60             : 
      61             : template< typename Val >
      62        3018 : class RegexpMapIterImpl
      63             : {
      64             : public:
      65             :         typedef RegexpMapImpl< Val > MapImpl;
      66             :         typedef typename List< Val >::iterator ListIterator;
      67             : 
      68             :         // Solaris needs these for the ctor...
      69             : 
      70             :         inline RegexpMapIterImpl();
      71             : 
      72             :         inline RegexpMapIterImpl(MapImpl * pTheMap, int nTheList,
      73             :                                                          ListIterator aTheIndex);
      74             : 
      75             :         RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap, bool bBegin);
      76             : 
      77             :         RegexpMapIterImpl(RegexpMapIterImpl const & rOther);
      78             : 
      79             :         RegexpMapIterImpl & operator =(RegexpMapIterImpl const & rOther);
      80             : 
      81             :         bool operator ==(RegexpMapIterImpl const & rOther) const;
      82             : 
      83           0 :         RegexpMapImpl< Val > const * getMap() const { return m_pMap; }
      84             : 
      85           0 :         int getList() const { return m_nList; }
      86             : 
      87           0 :         typename List< Val >::iterator const & getIndex() const { return m_aIndex; }
      88             : 
      89             :         void next();
      90             : 
      91             :         RegexpMapEntry< Val > & get();
      92             : 
      93             : private:
      94             :         mutable RegexpMapEntry< Val > m_aEntry;
      95             :         typename List< Val >::iterator m_aIndex;
      96             :         RegexpMapImpl< Val > * m_pMap;
      97             :         int m_nList;
      98             :         mutable bool m_bEntrySet;
      99             : 
     100             :         void setEntry() const;
     101             : };
     102             : 
     103             : }
     104             : 
     105             : template< typename Val >
     106        1006 : inline RegexpMapIterImpl< Val >::RegexpMapIterImpl():
     107             :         m_aEntry(rtl::OUString(), 0),
     108             :         m_pMap(0),
     109             :         m_nList(-1),
     110        1006 :         m_bEntrySet(false)
     111        1006 : {}
     112             : 
     113             : template< typename Val >
     114         736 : inline RegexpMapIterImpl< Val >::RegexpMapIterImpl(MapImpl * pTheMap,
     115             :                                                                                                    int nTheList,
     116             :                                                                                                    ListIterator aTheIndex):
     117             :         m_aEntry(rtl::OUString(), 0),
     118             :         m_aIndex(aTheIndex),
     119             :         m_pMap(pTheMap),
     120             :         m_nList(nTheList),
     121         736 :         m_bEntrySet(false)
     122         736 : {}
     123             : 
     124             : //============================================================================
     125             : template< typename Val >
     126         736 : void RegexpMapIterImpl< Val >::setEntry() const
     127             : {
     128         736 :         if (!m_bEntrySet)
     129             :         {
     130             :                 Entry< Val > const & rTheEntry
     131         736 :                         = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
     132         736 :                 m_aEntry
     133             :                         = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(false),
     134             :                                                                         const_cast< Val * >(&rTheEntry.m_aValue));
     135         736 :                 m_bEntrySet = true;
     136             :         }
     137         736 : }
     138             : 
     139             : //============================================================================
     140             : template< typename Val >
     141        1276 : RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap,
     142             :                                                                                         bool bBegin):
     143             :         m_aEntry(rtl::OUString(), 0),
     144             :         m_pMap(pTheMap),
     145        1276 :         m_bEntrySet(false)
     146             : {
     147        1276 :         if (bBegin)
     148             :         {
     149           0 :                 m_nList = -1;
     150           0 :                 if (!m_pMap->m_pDefault)
     151           0 :                         next();
     152             :         }
     153             :         else
     154             :         {
     155        1276 :                 m_nList = Regexp::KIND_DOMAIN;
     156        1276 :                 m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
     157             :         }
     158        1276 : }
     159             : 
     160             : //============================================================================
     161             : template< typename Val >
     162           0 : RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapIterImpl const & rOther):
     163             :     m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
     164           0 :     m_bEntrySet(rOther.m_bEntrySet)
     165             : {
     166           0 :     if (m_nList != -1)
     167           0 :         m_aIndex = rOther.m_aIndex;
     168           0 : }
     169             : 
     170             : //============================================================================
     171             : template< typename Val >
     172        1006 : RegexpMapIterImpl< Val > & RegexpMapIterImpl< Val >::operator =(
     173             :     RegexpMapIterImpl const & rOther)
     174             : {
     175        1006 :     if (this != &rOther)
     176             :     {
     177        1006 :         m_aEntry = rOther.m_aEntry;
     178        1006 :         m_pMap = rOther.m_pMap;
     179        1006 :         m_nList = rOther.m_nList;
     180        1006 :         m_bEntrySet = rOther.m_bEntrySet;
     181        1006 :         if (m_nList == -1)
     182           0 :             m_aIndex = typename List< Val >::iterator();
     183             :         else
     184        1006 :             m_aIndex = rOther.m_aIndex;
     185             :     }
     186        1006 :     return *this;
     187             : }
     188             : 
     189             : //============================================================================
     190             : template< typename Val >
     191        1006 : bool RegexpMapIterImpl< Val >::operator ==(RegexpMapIterImpl const & rOther)
     192             :         const
     193             : {
     194             :         return m_pMap == rOther.m_pMap
     195             :                    && m_nList == rOther.m_nList
     196        1006 :                    && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
     197             : }
     198             : 
     199             : //============================================================================
     200             : template< typename Val >
     201           0 : void RegexpMapIterImpl< Val >::next()
     202             : {
     203           0 :         switch (m_nList)
     204             :         {
     205             :                 case Regexp::KIND_DOMAIN:
     206           0 :                         if (m_aIndex == m_pMap->m_aList[m_nList].end())
     207           0 :                                 return;
     208             :                 default:
     209           0 :                         ++m_aIndex;
     210           0 :                         if (m_nList == Regexp::KIND_DOMAIN
     211             :                                 || m_aIndex != m_pMap->m_aList[m_nList].end())
     212           0 :                                 break;
     213             :                 case -1:
     214           0 :                         do
     215             :                         {
     216           0 :                                 ++m_nList;
     217           0 :                                 m_aIndex = m_pMap->m_aList[m_nList].begin();
     218             :                         }
     219             :                         while (m_nList < Regexp::KIND_DOMAIN
     220             :                                    && m_aIndex == m_pMap->m_aList[m_nList].end());
     221           0 :                         break;
     222             :         }
     223           0 :         m_bEntrySet = false;
     224             : }
     225             : 
     226             : //============================================================================
     227             : template< typename Val >
     228         736 : RegexpMapEntry< Val > & RegexpMapIterImpl< Val >::get()
     229             : {
     230         736 :         setEntry();
     231         736 :         return m_aEntry;
     232             : }
     233             : 
     234             : //============================================================================
     235             : //
     236             : //  RegexpMapConstIter
     237             : //
     238             : //============================================================================
     239             : 
     240             : template< typename Val >
     241        2012 : RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapIterImpl< Val > *
     242             :                                                                                               pTheImpl):
     243        2012 :         m_pImpl(pTheImpl)
     244        2012 : {}
     245             : 
     246             : //============================================================================
     247             : template< typename Val >
     248        1006 : RegexpMapConstIter< Val >::RegexpMapConstIter():
     249        1006 :         m_pImpl(new RegexpMapIterImpl< Val >)
     250        1006 : {}
     251             : 
     252             : //============================================================================
     253             : template< typename Val >
     254           0 : RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
     255             :                                                                                               rOther):
     256           0 :         m_pImpl(new RegexpMapIterImpl< Val >(*rOther.m_pImpl))
     257           0 : {}
     258             : 
     259             : //============================================================================
     260             : template< typename Val >
     261        3018 : RegexpMapConstIter< Val >::~RegexpMapConstIter()
     262             : {
     263        3018 :         delete m_pImpl;
     264        3018 : }
     265             : 
     266             : //============================================================================
     267             : template< typename Val >
     268             : RegexpMapConstIter< Val > &
     269        1006 : RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
     270             : {
     271        1006 :         *m_pImpl = *rOther.m_pImpl;
     272        1006 :         return *this;
     273             : }
     274             : 
     275             : //============================================================================
     276             : template< typename Val >
     277           0 : RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
     278             : {
     279           0 :         m_pImpl->next();
     280           0 :         return *this;
     281             : }
     282             : 
     283             : //============================================================================
     284             : template< typename Val >
     285             : RegexpMapConstIter< Val > RegexpMapConstIter< Val >::operator ++(int)
     286             : {
     287             :         RegexpMapConstIter aTemp(*this);
     288             :         m_pImpl->next();
     289             :         return aTemp;
     290             : }
     291             : 
     292             : //============================================================================
     293             : template< typename Val >
     294             : RegexpMapEntry< Val > const & RegexpMapConstIter< Val >::operator *() const
     295             : {
     296             :         return m_pImpl->get();
     297             : }
     298             : 
     299             : //============================================================================
     300             : template< typename Val >
     301           0 : RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
     302             : {
     303           0 :         return &m_pImpl->get();
     304             : }
     305             : 
     306             : //============================================================================
     307             : template< typename Val >
     308        1006 : bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
     309             :         const
     310             : {
     311        1006 :         return *m_pImpl == *rOther.m_pImpl;
     312             : }
     313             : 
     314             : //============================================================================
     315             : //
     316             : //  RegexpMapIter
     317             : //
     318             : //============================================================================
     319             : 
     320             : template< typename Val >
     321        2012 : RegexpMapIter< Val >::RegexpMapIter(RegexpMapIterImpl< Val > * pTheImpl):
     322        2012 :         RegexpMapConstIter< Val >(pTheImpl)
     323        2012 : {}
     324             : 
     325             : //============================================================================
     326             : template< typename Val >
     327             : RegexpMapIter< Val > & RegexpMapIter< Val >::operator ++()
     328             : {
     329             :         this->m_pImpl->next();
     330             :         return *this;
     331             : }
     332             : 
     333             : //============================================================================
     334             : template< typename Val >
     335             : RegexpMapIter< Val > RegexpMapIter< Val >::operator ++(int)
     336             : {
     337             :         RegexpMapIter aTemp(*this);
     338             :         this->m_pImpl->next();
     339             :         return aTemp;
     340             : }
     341             : 
     342             : //============================================================================
     343             : template< typename Val >
     344             : RegexpMapEntry< Val > & RegexpMapIter< Val >::operator *()
     345             : {
     346             :         return this->m_pImpl->get();
     347             : }
     348             : 
     349             : //============================================================================
     350             : template< typename Val >
     351             : RegexpMapEntry< Val > const & RegexpMapIter< Val >::operator *() const
     352             : {
     353             :         return this->m_pImpl->get();
     354             : }
     355             : 
     356             : //============================================================================
     357             : template< typename Val >
     358         736 : RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
     359             : {
     360         736 :         return &this->m_pImpl->get();
     361             : }
     362             : 
     363             : //============================================================================
     364             : template< typename Val >
     365             : RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
     366             : {
     367             :         return &this->m_pImpl->get();
     368             : }
     369             : 
     370             : //============================================================================
     371             : //
     372             : //  RegexpMap
     373             : //
     374             : //============================================================================
     375             : 
     376             : namespace ucb_impl {
     377             : 
     378             : template< typename Val >
     379             : struct RegexpMapImpl
     380             : {
     381             :         List< Val > m_aList[Regexp::KIND_DOMAIN + 1];
     382             :         Entry< Val > * m_pDefault;
     383             : 
     384          84 :         RegexpMapImpl(): m_pDefault(0) {}
     385             : 
     386          84 :         ~RegexpMapImpl() { delete m_pDefault; }
     387             : };
     388             : 
     389             : }
     390             : 
     391             : //============================================================================
     392             : template< typename Val >
     393          84 : RegexpMap< Val >::RegexpMap():
     394          84 :         m_pImpl(new RegexpMapImpl< Val >)
     395          84 : {}
     396             : 
     397             : //============================================================================
     398             : template< typename Val >
     399             : RegexpMap< Val >::RegexpMap(RegexpMap const & rOther):
     400             :         m_pImpl(new RegexpMapImpl< Val >(*rOther.m_pImpl))
     401             : {}
     402             : 
     403             : //============================================================================
     404             : template< typename Val >
     405          84 : RegexpMap< Val >::~RegexpMap()
     406             : {
     407          84 :         delete m_pImpl;
     408          84 : }
     409             : 
     410             : //============================================================================
     411             : template< typename Val >
     412             : RegexpMap< Val > & RegexpMap< Val >::operator =(RegexpMap const & rOther)
     413             : {
     414             :         *m_pImpl = *rOther.m_pImpl;
     415             :         return *this;
     416             : }
     417             : 
     418             : //============================================================================
     419             : template< typename Val >
     420         270 : bool RegexpMap< Val >::add(rtl::OUString const & rKey, Val const & rValue,
     421             :                                                    bool bOverwrite, rtl::OUString * pReverse)
     422             : {
     423         270 :         Regexp aRegexp(Regexp::parse(rKey));
     424             : 
     425         270 :         if (aRegexp.isDefault())
     426             :         {
     427           0 :                 if (m_pImpl->m_pDefault)
     428             :                 {
     429           0 :                         if (!bOverwrite)
     430           0 :                                 return false;
     431           0 :                         delete m_pImpl->m_pDefault;
     432             :                 }
     433           0 :                 m_pImpl->m_pDefault = new Entry< Val >(aRegexp, rValue);
     434             :         }
     435             :         else
     436             :         {
     437         270 :                 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
     438             : 
     439         270 :                 typename List< Val >::iterator aEnd(rTheList.end());
     440        1328 :                 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
     441             :                 {
     442        1058 :                         if (aIt->m_aRegexp == aRegexp)
     443             :                         {
     444           0 :                                 if (bOverwrite)
     445             :                                 {
     446           0 :                                         rTheList.erase(aIt);
     447           0 :                                         break;
     448             :                                 }
     449             :                                 else
     450           0 :                                         return false;
     451             :                         }
     452             :                 }
     453             : 
     454         270 :                 rTheList.push_back(Entry< Val >(aRegexp, rValue));
     455             :         }
     456             : 
     457         270 :         if (pReverse)
     458           0 :                 *pReverse = aRegexp.getRegexp(true);
     459             : 
     460         270 :         return true;
     461             : }
     462             : 
     463             : //============================================================================
     464             : template< typename Val >
     465        1006 : typename RegexpMap< Val >::iterator RegexpMap< Val >::find(rtl::OUString const & rKey,
     466             :                                                                                                   rtl::OUString * pReverse)
     467             : {
     468        1006 :         Regexp aRegexp(Regexp::parse(rKey));
     469             : 
     470        1006 :         if (pReverse)
     471           0 :                 *pReverse = aRegexp.getRegexp(true);
     472             : 
     473        1006 :         if (aRegexp.isDefault())
     474             :         {
     475           0 :                 if (m_pImpl->m_pDefault)
     476             :                         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
     477           0 :                                                                                                                                          true));
     478             :         }
     479             :         else
     480             :         {
     481        1006 :                 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
     482             : 
     483        1006 :                 typename List< Val > ::iterator aEnd(rTheList.end());
     484        2350 :                 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
     485        2080 :                         if (aIt->m_aRegexp == aRegexp)
     486             :                                 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(
     487             :                                                                         m_pImpl,
     488         736 :                                                                                                         aRegexp.getKind(), aIt));
     489             :         }
     490             : 
     491         270 :         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
     492             : }
     493             : 
     494             : //============================================================================
     495             : template< typename Val >
     496           0 : void RegexpMap< Val >::erase(iterator const & rPos)
     497             : {
     498           0 :         if (rPos.m_pImpl->getMap() == m_pImpl)
     499             :         {
     500           0 :                 if (rPos.m_pImpl->getList() == -1)
     501             :                 {
     502           0 :                         if (m_pImpl->m_pDefault)
     503             :                         {
     504           0 :                                 delete m_pImpl->m_pDefault;
     505           0 :                                 m_pImpl->m_pDefault = 0;
     506             :                         }
     507             :                 }
     508             :                 else
     509           0 :                         m_pImpl->m_aList[rPos.m_pImpl->getList()].
     510             :                                          erase(rPos.m_pImpl->getIndex());
     511             :         }
     512           0 : }
     513             : 
     514             : //============================================================================
     515             : template< typename Val >
     516           0 : typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
     517             : {
     518           0 :         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, true));
     519             : }
     520             : 
     521             : //============================================================================
     522             : template< typename Val >
     523             : typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
     524             : {
     525             :         return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
     526             :                                                                                                                                   true));
     527             : }
     528             : 
     529             : //============================================================================
     530             : template< typename Val >
     531        1006 : typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
     532             : {
     533        1006 :         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
     534             : }
     535             : 
     536             : //============================================================================
     537             : template< typename Val >
     538             : typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
     539             : {
     540             :         return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
     541             :                                                                                                                                   false));
     542             : }
     543             : 
     544             : //============================================================================
     545             : template< typename Val >
     546             : bool RegexpMap< Val >::empty() const
     547             : {
     548             :         return !m_pImpl->m_pDefault
     549             :                    && m_pImpl->m_aList[Regexp::KIND_PREFIX].empty()
     550             :                    && m_pImpl->m_aList[Regexp::KIND_AUTHORITY].empty()
     551             :                    && m_pImpl->m_aList[Regexp::KIND_DOMAIN].empty();
     552             : }
     553             : 
     554             : //============================================================================
     555             : template< typename Val >
     556           0 : typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
     557             : {
     558             :         return (m_pImpl->m_pDefault ? 1 : 0)
     559             :                        + m_pImpl->m_aList[Regexp::KIND_PREFIX].size()
     560             :                        + m_pImpl->m_aList[Regexp::KIND_AUTHORITY].size()
     561           0 :                        + m_pImpl->m_aList[Regexp::KIND_DOMAIN].size();
     562             : }
     563             : 
     564             : //============================================================================
     565             : template< typename Val >
     566      102889 : Val const * RegexpMap< Val >::map(rtl::OUString const & rString,
     567             :                                                                   rtl::OUString * pTranslation,
     568             :                                                                   bool * pTranslated) const
     569             : {
     570      317817 :         for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
     571             :         {
     572      308667 :                 List< Val > const & rTheList = m_pImpl->m_aList[n];
     573             : 
     574      308667 :                 typename List< Val >::const_iterator aEnd(rTheList.end());
     575      334849 :                 for (typename List< Val >::const_iterator aIt(rTheList.begin()); aIt != aEnd;
     576             :                          ++aIt)
     577      119921 :                         if (aIt->m_aRegexp.matches(rString, pTranslation, pTranslated))
     578       93739 :                                 return &aIt->m_aValue;
     579             :         }
     580        9150 :         if (m_pImpl->m_pDefault
     581             :                 && m_pImpl->m_pDefault->m_aRegexp.matches(rString, pTranslation,
     582             :                                                                                                   pTranslated))
     583           0 :                 return &m_pImpl->m_pDefault->m_aValue;
     584        9150 :         return 0;
     585             : }
     586             : 
     587             : #endif // _UCB_REGEXPMAP_TPT_

Generated by: LCOV version 1.10