2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef _UCB_REGEXPMAP_TPT_
20 #define _UCB_REGEXPMAP_TPT_
22 #include <regexpmap.hxx>
24 #include <rtl/ustring.hxx>
28 using namespace ucb_impl;
32 //============================================================================
34 template< typename Val >
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 //============================================================================
51 //============================================================================
53 template< typename Val >
54 class RegexpMapIterImpl
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; }
83 RegexpMapEntry< Val > & get();
86 mutable RegexpMapEntry< Val > m_aEntry;
87 typename List< Val >::iterator m_aIndex;
88 RegexpMapImpl< Val > * m_pMap;
90 mutable bool m_bEntrySet;
92 void setEntry() const;
97 template< typename Val >
98 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl():
99 m_aEntry(rtl::OUString(), 0),
105 template< typename Val >
106 inline RegexpMapIterImpl< Val >::RegexpMapIterImpl(MapImpl * pTheMap,
108 ListIterator aTheIndex):
109 m_aEntry(rtl::OUString(), 0),
116 //============================================================================
117 template< typename Val >
118 void RegexpMapIterImpl< Val >::setEntry() const
122 Entry< Val > const & rTheEntry
123 = m_nList == -1 ? *m_pMap->m_pDefault : *m_aIndex;
125 = RegexpMapEntry< Val >(rTheEntry.m_aRegexp.getRegexp(false),
126 const_cast< Val * >(&rTheEntry.m_aValue));
131 //============================================================================
132 template< typename Val >
133 RegexpMapIterImpl< Val >::RegexpMapIterImpl(RegexpMapImpl< Val > * pTheMap,
135 m_aEntry(rtl::OUString(), 0),
142 if (!m_pMap->m_pDefault)
147 m_nList = Regexp::KIND_DOMAIN;
148 m_aIndex = m_pMap->m_aList[Regexp::KIND_DOMAIN].end();
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)
159 m_aIndex = rOther.m_aIndex;
162 //============================================================================
163 template< typename Val >
164 RegexpMapIterImpl< Val > & RegexpMapIterImpl< Val >::operator =(
165 RegexpMapIterImpl const & rOther)
169 m_aEntry = rOther.m_aEntry;
170 m_pMap = rOther.m_pMap;
171 m_nList = rOther.m_nList;
172 m_bEntrySet = rOther.m_bEntrySet;
174 m_aIndex = typename List< Val >::iterator();
176 m_aIndex = rOther.m_aIndex;
181 //============================================================================
182 template< typename Val >
183 bool RegexpMapIterImpl< Val >::operator ==(RegexpMapIterImpl const & rOther)
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()
197 case Regexp::KIND_DOMAIN:
198 if (m_aIndex == m_pMap->m_aList[m_nList].end())
202 if (m_nList == Regexp::KIND_DOMAIN
203 || m_aIndex != m_pMap->m_aList[m_nList].end())
209 m_aIndex = m_pMap->m_aList[m_nList].begin();
211 while (m_nList < Regexp::KIND_DOMAIN
212 && m_aIndex == m_pMap->m_aList[m_nList].end());
218 //============================================================================
219 template< typename Val >
220 RegexpMapEntry< Val > & RegexpMapIterImpl< Val >::get()
226 //============================================================================
228 // RegexpMapConstIter
230 //============================================================================
232 template< typename Val >
233 RegexpMapConstIter< Val >::RegexpMapConstIter(RegexpMapIterImpl< Val > *
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 &
248 m_pImpl(new RegexpMapIterImpl< Val >(*rOther.m_pImpl))
251 //============================================================================
252 template< typename Val >
253 RegexpMapConstIter< Val >::~RegexpMapConstIter()
258 //============================================================================
259 template< typename Val >
260 RegexpMapConstIter< Val > &
261 RegexpMapConstIter< Val >::operator =(RegexpMapConstIter const & rOther)
263 *m_pImpl = *rOther.m_pImpl;
267 //============================================================================
268 template< typename Val >
269 RegexpMapConstIter< Val > & RegexpMapConstIter< Val >::operator ++()
275 //============================================================================
276 template< typename Val >
277 RegexpMapConstIter< Val > RegexpMapConstIter< Val >::operator ++(int)
279 RegexpMapConstIter aTemp(*this);
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)
303 return *m_pImpl == *rOther.m_pImpl;
306 //============================================================================
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();
325 //============================================================================
326 template< typename Val >
327 RegexpMapIter< Val > RegexpMapIter< Val >::operator ++(int)
329 RegexpMapIter aTemp(*this);
330 this->m_pImpl->next();
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 //============================================================================
366 //============================================================================
370 template< typename Val >
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()
402 //============================================================================
403 template< typename Val >
404 RegexpMap< Val > & RegexpMap< Val >::operator =(RegexpMap const & rOther)
406 *m_pImpl = *rOther.m_pImpl;
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())
419 if (m_pImpl->m_pDefault)
423 delete m_pImpl->m_pDefault;
425 m_pImpl->m_pDefault = new Entry< Val >(aRegexp, rValue);
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)
434 if (aIt->m_aRegexp == aRegexp)
446 rTheList.push_back(Entry< Val >(aRegexp, rValue));
450 *pReverse = aRegexp.getRegexp(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));
463 *pReverse = aRegexp.getRegexp(true);
465 if (aRegexp.isDefault())
467 if (m_pImpl->m_pDefault)
468 return RegexpMapIter< Val >(new RegexpMapIterImpl< Val >(m_pImpl,
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 >(
480 aRegexp.getKind(), aIt));
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)
492 if (rPos.m_pImpl->getList() == -1)
494 if (m_pImpl->m_pDefault)
496 delete m_pImpl->m_pDefault;
497 m_pImpl->m_pDefault = 0;
501 m_pImpl->m_aList[rPos.m_pImpl->getList()].
502 erase(rPos.m_pImpl->getIndex());
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,
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,
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)
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;
569 if (aIt->m_aRegexp.matches(rString, pTranslation, pTranslated))
570 return &aIt->m_aValue;
572 if (m_pImpl->m_pDefault
573 && m_pImpl->m_pDefault->m_aRegexp.matches(rString, pTranslation,
575 return &m_pImpl->m_pDefault->m_aValue;
579 #endif // _UCB_REGEXPMAP_TPT_