update credits
[LibreOffice.git] / ucb / source / inc / regexpmap.tpt
bloba5836e6dc7d7a17f0eddf7622f811c8629d552fa
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  */
19 #ifndef _UCB_REGEXPMAP_TPT_
20 #define _UCB_REGEXPMAP_TPT_
22 #include <regexpmap.hxx>
23 #include <list>
24 #include <rtl/ustring.hxx>
26 #include "regexp.hxx"
28 using namespace ucb_impl;
30 namespace ucb_impl {
32 //============================================================================
34 template< typename Val >
35 struct Entry
37         Regexp m_aRegexp;
38         Val m_aValue;
40         inline Entry(Regexp const & rTheRegexp, Val const & rTheValue):
41                 m_aRegexp(rTheRegexp), m_aValue(rTheValue) {}
44 //============================================================================
45 template< typename Val > class List: public std::list< Entry< Val > > {};
47 //============================================================================
49 //  RegexpMapIterImpl
51 //============================================================================
53 template< typename Val >
54 class RegexpMapIterImpl
56 public:
57         typedef RegexpMapImpl< Val > MapImpl;
58         typedef typename List< Val >::iterator ListIterator;
60         // Solaris needs these for the ctor...
62         inline RegexpMapIterImpl();
64         inline RegexpMapIterImpl(MapImpl * pTheMap, int nTheList,
65                                                          ListIterator aTheIndex);
67         RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap, bool bBegin);
69         RegexpMapIterImpl(RegexpMapIterImpl const & rOther);
71         RegexpMapIterImpl & operator =(RegexpMapIterImpl const & rOther);
73         bool operator ==(RegexpMapIterImpl const & rOther) const;
75         RegexpMapImpl< Val > const * getMap() const { return m_pMap; }
77         int getList() const { return m_nList; }
79         typename List< Val >::iterator const & getIndex() const { return m_aIndex; }
81         void next();
83         RegexpMapEntry< Val > & get();
85 private:
86         mutable RegexpMapEntry< Val > m_aEntry;
87         typename List< Val >::iterator m_aIndex;
88         RegexpMapImpl< Val > * m_pMap;
89         int m_nList;
90         mutable bool m_bEntrySet;
92         void setEntry() const;
97 template< typename Val >
98 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl():
99         m_aEntry(rtl::OUString(), 0),
100         m_pMap(0),
101         m_nList(-1),
102         m_bEntrySet(false)
105 template< typename Val >
106 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl(MapImpl * pTheMap,
107                                                                                                    int nTheList,
108                                                                                                    ListIterator aTheIndex):
109         m_aEntry(rtl::OUString(), 0),
110         m_aIndex(aTheIndex),
111         m_pMap(pTheMap),
112         m_nList(nTheList),
113         m_bEntrySet(false)
116 //============================================================================
117 template< typename Val >
118 void RegexpMapIterImpl< Val >::setEntry() const
120         if (!m_bEntrySet)
121         {
122                 Entry< Val > const & rTheEntry
123                         = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
124                 m_aEntry
125                         = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(false),
126                                                                         const_cast< Val * >(&rTheEntry.m_aValue));
127                 m_bEntrySet = true;
128         }
131 //============================================================================
132 template< typename Val >
133 RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap,
134                                                                                         bool bBegin):
135         m_aEntry(rtl::OUString(), 0),
136         m_pMap(pTheMap),
137         m_bEntrySet(false)
139         if (bBegin)
140         {
141                 m_nList = -1;
142                 if (!m_pMap->m_pDefault)
143                         next();
144         }
145         else
146         {
147                 m_nList = Regexp::KIND_DOMAIN;
148                 m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
149         }
152 //============================================================================
153 template< typename Val >
154 RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapIterImpl const & rOther):
155     m_aEntry(rOther.m_aEntry), m_pMap(rOther.m_pMap), m_nList(rOther.m_nList),
156     m_bEntrySet(rOther.m_bEntrySet)
158     if (m_nList != -1)
159         m_aIndex = rOther.m_aIndex;
162 //============================================================================
163 template< typename Val >
164 RegexpMapIterImpl< Val > & RegexpMapIterImpl< Val >::operator =(
165     RegexpMapIterImpl const & rOther)
167     if (this != &rOther)
168     {
169         m_aEntry = rOther.m_aEntry;
170         m_pMap = rOther.m_pMap;
171         m_nList = rOther.m_nList;
172         m_bEntrySet = rOther.m_bEntrySet;
173         if (m_nList == -1)
174             m_aIndex = typename List< Val >::iterator();
175         else
176             m_aIndex = rOther.m_aIndex;
177     }
178     return *this;
181 //============================================================================
182 template< typename Val >
183 bool RegexpMapIterImpl< Val >::operator ==(RegexpMapIterImpl const & rOther)
184         const
186         return m_pMap == rOther.m_pMap
187                    && m_nList == rOther.m_nList
188                    && (m_nList == -1 || m_aIndex == rOther.m_aIndex);
191 //============================================================================
192 template< typename Val >
193 void RegexpMapIterImpl< Val >::next()
195         switch (m_nList)
196         {
197                 case Regexp::KIND_DOMAIN:
198                         if (m_aIndex == m_pMap->m_aList[m_nList].end())
199                                 return;
200                 default:
201                         ++m_aIndex;
202                         if (m_nList == Regexp::KIND_DOMAIN
203                                 || m_aIndex != m_pMap->m_aList[m_nList].end())
204                                 break;
205                 case -1:
206                         do
207                         {
208                                 ++m_nList;
209                                 m_aIndex = m_pMap->m_aList[m_nList].begin();
210                         }
211                         while (m_nList < Regexp::KIND_DOMAIN
212                                    && m_aIndex == m_pMap->m_aList[m_nList].end());
213                         break;
214         }
215         m_bEntrySet = false;
218 //============================================================================
219 template< typename Val >
220 RegexpMapEntry< Val > & RegexpMapIterImpl< Val >::get()
222         setEntry();
223         return m_aEntry;
226 //============================================================================
228 //  RegexpMapConstIter
230 //============================================================================
232 template< typename Val >
233 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapIterImpl< Val > *
234                                                                                               pTheImpl):
235         m_pImpl(pTheImpl)
238 //============================================================================
239 template< typename Val >
240 RegexpMapConstIter< Val >::RegexpMapConstIter():
241         m_pImpl(new RegexpMapIterImpl< Val >)
244 //============================================================================
245 template< typename Val >
246 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapConstIter const &
247                                                                                               rOther):
248         m_pImpl(new RegexpMapIterImpl< Val >(*rOther.m_pImpl))
251 //============================================================================
252 template< typename Val >
253 RegexpMapConstIter< Val >::~RegexpMapConstIter()
255         delete m_pImpl;
258 //============================================================================
259 template< typename Val >
260 RegexpMapConstIter< Val > &
261 RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
263         *m_pImpl = *rOther.m_pImpl;
264         return *this;
267 //============================================================================
268 template< typename Val >
269 RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
271         m_pImpl->next();
272         return *this;
275 //============================================================================
276 template< typename Val >
277 RegexpMapConstIter< Val > RegexpMapConstIter< Val >::operator ++(int)
279         RegexpMapConstIter aTemp(*this);
280         m_pImpl->next();
281         return aTemp;
284 //============================================================================
285 template< typename Val >
286 RegexpMapEntry< Val > const & RegexpMapConstIter< Val >::operator *() const
288         return m_pImpl->get();
291 //============================================================================
292 template< typename Val >
293 RegexpMapEntry< Val > const * RegexpMapConstIter< Val >::operator ->() const
295         return &m_pImpl->get();
298 //============================================================================
299 template< typename Val >
300 bool RegexpMapConstIter< Val >::equals(RegexpMapConstIter const & rOther)
301         const
303         return *m_pImpl == *rOther.m_pImpl;
306 //============================================================================
308 //  RegexpMapIter
310 //============================================================================
312 template< typename Val >
313 RegexpMapIter< Val >::RegexpMapIter(RegexpMapIterImpl< Val > * pTheImpl):
314         RegexpMapConstIter< Val >(pTheImpl)
317 //============================================================================
318 template< typename Val >
319 RegexpMapIter< Val > & RegexpMapIter< Val >::operator ++()
321         this->m_pImpl->next();
322         return *this;
325 //============================================================================
326 template< typename Val >
327 RegexpMapIter< Val > RegexpMapIter< Val >::operator ++(int)
329         RegexpMapIter aTemp(*this);
330         this->m_pImpl->next();
331         return aTemp;
334 //============================================================================
335 template< typename Val >
336 RegexpMapEntry< Val > & RegexpMapIter< Val >::operator *()
338         return this->m_pImpl->get();
341 //============================================================================
342 template< typename Val >
343 RegexpMapEntry< Val > const & RegexpMapIter< Val >::operator *() const
345         return this->m_pImpl->get();
348 //============================================================================
349 template< typename Val >
350 RegexpMapEntry< Val > * RegexpMapIter< Val >::operator ->()
352         return &this->m_pImpl->get();
355 //============================================================================
356 template< typename Val >
357 RegexpMapEntry< Val > const * RegexpMapIter< Val >::operator ->() const
359         return &this->m_pImpl->get();
362 //============================================================================
364 //  RegexpMap
366 //============================================================================
368 namespace ucb_impl {
370 template< typename Val >
371 struct RegexpMapImpl
373         List< Val > m_aList[Regexp::KIND_DOMAIN + 1];
374         Entry< Val > * m_pDefault;
376         RegexpMapImpl(): m_pDefault(0) {}
378         ~RegexpMapImpl() { delete m_pDefault; }
383 //============================================================================
384 template< typename Val >
385 RegexpMap< Val >::RegexpMap():
386         m_pImpl(new RegexpMapImpl< Val >)
389 //============================================================================
390 template< typename Val >
391 RegexpMap< Val >::RegexpMap(RegexpMap const & rOther):
392         m_pImpl(new RegexpMapImpl< Val >(*rOther.m_pImpl))
395 //============================================================================
396 template< typename Val >
397 RegexpMap< Val >::~RegexpMap()
399         delete m_pImpl;
402 //============================================================================
403 template< typename Val >
404 RegexpMap< Val > & RegexpMap< Val >::operator =(RegexpMap const & rOther)
406         *m_pImpl = *rOther.m_pImpl;
407         return *this;
410 //============================================================================
411 template< typename Val >
412 bool RegexpMap< Val >::add(rtl::OUString const & rKey, Val const & rValue,
413                                                    bool bOverwrite, rtl::OUString * pReverse)
415         Regexp aRegexp(Regexp::parse(rKey));
417         if (aRegexp.isDefault())
418         {
419                 if (m_pImpl->m_pDefault)
420                 {
421                         if (!bOverwrite)
422                                 return false;
423                         delete m_pImpl->m_pDefault;
424                 }
425                 m_pImpl->m_pDefault = new Entry< Val >(aRegexp, rValue);
426         }
427         else
428         {
429                 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
431                 typename List< Val >::iterator aEnd(rTheList.end());
432                 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
433                 {
434                         if (aIt->m_aRegexp == aRegexp)
435                         {
436                                 if (bOverwrite)
437                                 {
438                                         rTheList.erase(aIt);
439                                         break;
440                                 }
441                                 else
442                                         return false;
443                         }
444                 }
446                 rTheList.push_back(Entry< Val >(aRegexp, rValue));
447         }
449         if (pReverse)
450                 *pReverse = aRegexp.getRegexp(true);
452         return true;
455 //============================================================================
456 template< typename Val >
457 typename RegexpMap< Val >::iterator RegexpMap< Val >::find(rtl::OUString const & rKey,
458                                                                                                   rtl::OUString * pReverse)
460         Regexp aRegexp(Regexp::parse(rKey));
462         if (pReverse)
463                 *pReverse = aRegexp.getRegexp(true);
465         if (aRegexp.isDefault())
466         {
467                 if (m_pImpl->m_pDefault)
468                         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
469                                                                                                                                          true));
470         }
471         else
472         {
473                 List< Val > & rTheList = m_pImpl->m_aList[aRegexp.getKind()];
475                 typename List< Val > ::iterator aEnd(rTheList.end());
476                 for (typename List< Val >::iterator aIt(rTheList.begin()); aIt != aEnd; ++aIt)
477                         if (aIt->m_aRegexp == aRegexp)
478                                 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(
479                                                                         m_pImpl,
480                                                                                                         aRegexp.getKind(), aIt));
481         }
483         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
486 //============================================================================
487 template< typename Val >
488 void RegexpMap< Val >::erase(iterator const & rPos)
490         if (rPos.m_pImpl->getMap() == m_pImpl)
491         {
492                 if (rPos.m_pImpl->getList() == -1)
493                 {
494                         if (m_pImpl->m_pDefault)
495                         {
496                                 delete m_pImpl->m_pDefault;
497                                 m_pImpl->m_pDefault = 0;
498                         }
499                 }
500                 else
501                         m_pImpl->m_aList[rPos.m_pImpl->getList()].
502                                          erase(rPos.m_pImpl->getIndex());
503         }
506 //============================================================================
507 template< typename Val >
508 typename RegexpMap< Val >::iterator RegexpMap< Val >::begin()
510         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, true));
513 //============================================================================
514 template< typename Val >
515 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::begin() const
517         return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
518                                                                                                                                   true));
521 //============================================================================
522 template< typename Val >
523 typename RegexpMap< Val >::iterator RegexpMap< Val >::end()
525         return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl, false));
528 //============================================================================
529 template< typename Val >
530 typename RegexpMap< Val >::const_iterator RegexpMap< Val >::end() const
532         return RegexpMapConstIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
533                                                                                                                                   false));
536 //============================================================================
537 template< typename Val >
538 bool RegexpMap< Val >::empty() const
540         return !m_pImpl->m_pDefault
541                    && m_pImpl->m_aList[Regexp::KIND_PREFIX].empty()
542                    && m_pImpl->m_aList[Regexp::KIND_AUTHORITY].empty()
543                    && m_pImpl->m_aList[Regexp::KIND_DOMAIN].empty();
546 //============================================================================
547 template< typename Val >
548 typename RegexpMap< Val >::size_type RegexpMap< Val >::size() const
550         return (m_pImpl->m_pDefault ? 1 : 0)
551                        + m_pImpl->m_aList[Regexp::KIND_PREFIX].size()
552                        + m_pImpl->m_aList[Regexp::KIND_AUTHORITY].size()
553                        + m_pImpl->m_aList[Regexp::KIND_DOMAIN].size();
556 //============================================================================
557 template< typename Val >
558 Val const * RegexpMap< Val >::map(rtl::OUString const & rString,
559                                                                   rtl::OUString * pTranslation,
560                                                                   bool * pTranslated) const
562         for (int n = Regexp::KIND_DOMAIN; n >= Regexp::KIND_PREFIX; --n)
563         {
564                 List< Val > const & rTheList = m_pImpl->m_aList[n];
566                 typename List< Val >::const_iterator aEnd(rTheList.end());
567                 for (typename List< Val >::const_iterator aIt(rTheList.begin()); aIt != aEnd;
568                          ++aIt)
569                         if (aIt->m_aRegexp.matches(rString, pTranslation, pTranslated))
570                                 return &aIt->m_aValue;
571         }
572         if (m_pImpl->m_pDefault
573                 && m_pImpl->m_pDefault->m_aRegexp.matches(rString, pTranslation,
574                                                                                                   pTranslated))
575                 return &m_pImpl->m_pDefault->m_aValue;
576         return 0;
579 #endif // _UCB_REGEXPMAP_TPT_