1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
21 #define INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
23 #include "sal/config.h"
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
34 template< typename Val
> class RegexpMap
;
35 template< typename Val
> class RegexpMapIter
;
38 template< typename Val
>
42 inline RegexpMapEntry(OUString
const & rTheRegexp
,
44 m_aRegexp(rTheRegexp
), m_pValue(pTheValue
) {}
46 OUString
getRegexp() const { return m_aRegexp
; }
48 Val
const & getValue() const { return *m_pValue
; }
50 Val
& getValue() { return *m_pValue
; }
59 template< typename Val
>
65 inline Entry(Regexp
const & rTheRegexp
, Val
const & rTheValue
):
66 m_aRegexp(rTheRegexp
), m_aValue(rTheValue
) {}
70 template< typename Val
> class List
: public std::list
< Entry
< Val
> > {};
73 template< typename Val
>
76 List
< Val
> m_aList
[Regexp::KIND_DOMAIN
+ 1];
77 Entry
< Val
> * m_pDefault
;
79 RegexpMapImpl(): m_pDefault(0) {}
81 ~RegexpMapImpl() { delete m_pDefault
; }
85 template< typename Val
>
86 class RegexpMapIterImpl
89 typedef RegexpMapImpl
< Val
> MapImpl
;
90 typedef typename List
< Val
>::iterator ListIterator
;
92 // Solaris needs these for the ctor...
94 inline RegexpMapIterImpl();
96 inline RegexpMapIterImpl(MapImpl
* pTheMap
, int nTheList
,
97 ListIterator aTheIndex
);
99 RegexpMapIterImpl(RegexpMapImpl
< Val
> * pTheMap
, bool bBegin
);
101 RegexpMapIterImpl(RegexpMapIterImpl
const & rOther
);
103 RegexpMapIterImpl
& operator =(RegexpMapIterImpl
const & rOther
);
105 bool operator ==(RegexpMapIterImpl
const & rOther
) const;
107 RegexpMapImpl
< Val
> const * getMap() const { return m_pMap
; }
109 int getList() const { return m_nList
; }
111 typename List
< Val
>::iterator
const & getIndex() const { return m_aIndex
; }
115 RegexpMapEntry
< Val
> & get();
118 mutable RegexpMapEntry
< Val
> m_aEntry
;
119 typename List
< Val
>::iterator m_aIndex
;
120 RegexpMapImpl
< Val
> * m_pMap
;
122 mutable bool m_bEntrySet
;
124 void setEntry() const;
127 template< typename Val
>
128 inline RegexpMapIterImpl
< Val
>::RegexpMapIterImpl():
129 m_aEntry(rtl::OUString(), 0),
135 template< typename Val
>
136 inline RegexpMapIterImpl
< Val
>::RegexpMapIterImpl(MapImpl
* pTheMap
,
138 ListIterator aTheIndex
):
139 m_aEntry(rtl::OUString(), 0),
146 template< typename Val
>
147 void RegexpMapIterImpl
< Val
>::setEntry() const
151 Entry
< Val
> const & rTheEntry
152 = m_nList
== -1 ? *m_pMap
->m_pDefault
: *m_aIndex
;
154 = RegexpMapEntry
< Val
>(rTheEntry
.m_aRegexp
.getRegexp(false),
155 const_cast< Val
* >(&rTheEntry
.m_aValue
));
160 template< typename Val
>
161 RegexpMapIterImpl
< Val
>::RegexpMapIterImpl(RegexpMapImpl
< Val
> * pTheMap
,
163 m_aEntry(rtl::OUString(), 0),
170 if (!m_pMap
->m_pDefault
)
175 m_nList
= Regexp::KIND_DOMAIN
;
176 m_aIndex
= m_pMap
->m_aList
[Regexp::KIND_DOMAIN
].end();
180 template< typename Val
>
181 RegexpMapIterImpl
< Val
>::RegexpMapIterImpl(RegexpMapIterImpl
const & rOther
):
182 m_aEntry(rOther
.m_aEntry
), m_pMap(rOther
.m_pMap
), m_nList(rOther
.m_nList
),
183 m_bEntrySet(rOther
.m_bEntrySet
)
186 m_aIndex
= rOther
.m_aIndex
;
189 template< typename Val
>
190 RegexpMapIterImpl
< Val
> & RegexpMapIterImpl
< Val
>::operator =(
191 RegexpMapIterImpl
const & rOther
)
195 m_aEntry
= rOther
.m_aEntry
;
196 m_pMap
= rOther
.m_pMap
;
197 m_nList
= rOther
.m_nList
;
198 m_bEntrySet
= rOther
.m_bEntrySet
;
200 m_aIndex
= typename List
< Val
>::iterator();
202 m_aIndex
= rOther
.m_aIndex
;
207 template< typename Val
>
208 bool RegexpMapIterImpl
< Val
>::operator ==(RegexpMapIterImpl
const & rOther
)
211 return m_pMap
== rOther
.m_pMap
212 && m_nList
== rOther
.m_nList
213 && (m_nList
== -1 || m_aIndex
== rOther
.m_aIndex
);
216 template< typename Val
>
217 void RegexpMapIterImpl
< Val
>::next()
221 case Regexp::KIND_DOMAIN
:
222 if (m_aIndex
== m_pMap
->m_aList
[m_nList
].end())
227 if (m_nList
== Regexp::KIND_DOMAIN
228 || m_aIndex
!= m_pMap
->m_aList
[m_nList
].end())
235 m_aIndex
= m_pMap
->m_aList
[m_nList
].begin();
237 while (m_nList
< Regexp::KIND_DOMAIN
238 && m_aIndex
== m_pMap
->m_aList
[m_nList
].end());
244 template< typename Val
>
245 RegexpMapEntry
< Val
> & RegexpMapIterImpl
< Val
>::get()
252 template< typename Val
>
253 class RegexpMapConstIter
255 friend class RegexpMap
< Val
>; // to access m_pImpl, ctor
256 friend class RegexpMapIter
< Val
>; // to access m_pImpl, ctor
259 RegexpMapConstIter();
261 RegexpMapConstIter(RegexpMapConstIter
const & rOther
);
263 ~RegexpMapConstIter();
265 RegexpMapConstIter
& operator =(RegexpMapConstIter
const & rOther
);
267 RegexpMapConstIter
& operator ++();
269 RegexpMapConstIter
operator ++(int);
271 RegexpMapEntry
< Val
> const & operator *() const;
273 RegexpMapEntry
< Val
> const * operator ->() const;
275 bool equals(RegexpMapConstIter
const & rOther
) const;
276 // for free operator ==(), operator !=()
279 RegexpMapIterImpl
< Val
> * m_pImpl
;
281 RegexpMapConstIter(RegexpMapIterImpl
< Val
> * pTheImpl
);
284 template< typename Val
>
285 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMapIterImpl
< Val
> *
290 template< typename Val
>
291 RegexpMapConstIter
< Val
>::RegexpMapConstIter():
292 m_pImpl(new RegexpMapIterImpl
< Val
>)
295 template< typename Val
>
296 RegexpMapConstIter
< Val
>::RegexpMapConstIter(RegexpMapConstIter
const &
298 m_pImpl(new RegexpMapIterImpl
< Val
>(*rOther
.m_pImpl
))
301 template< typename Val
>
302 RegexpMapConstIter
< Val
>::~RegexpMapConstIter()
307 template< typename Val
>
308 RegexpMapConstIter
< Val
> &
309 RegexpMapConstIter
< Val
>::operator =(RegexpMapConstIter
const & rOther
)
311 *m_pImpl
= *rOther
.m_pImpl
;
315 template< typename Val
>
316 RegexpMapConstIter
< Val
> & RegexpMapConstIter
< Val
>::operator ++()
322 template< typename Val
>
323 RegexpMapConstIter
< Val
> RegexpMapConstIter
< Val
>::operator ++(int)
325 RegexpMapConstIter
aTemp(*this);
330 template< typename Val
>
331 RegexpMapEntry
< Val
> const & RegexpMapConstIter
< Val
>::operator *() const
333 return m_pImpl
->get();
336 template< typename Val
>
337 RegexpMapEntry
< Val
> const * RegexpMapConstIter
< Val
>::operator ->() const
339 return &m_pImpl
->get();
342 template< typename Val
>
343 bool RegexpMapConstIter
< Val
>::equals(RegexpMapConstIter
const & rOther
)
346 return *m_pImpl
== *rOther
.m_pImpl
;
350 template< typename Val
>
351 class RegexpMapIter
: public RegexpMapConstIter
< Val
>
353 friend class RegexpMap
< Val
>; // to access ctor
358 RegexpMapIter
& operator ++();
360 RegexpMapIter
operator ++(int);
362 RegexpMapEntry
< Val
> & operator *();
364 RegexpMapEntry
< Val
> const & operator *() const;
366 RegexpMapEntry
< Val
> * operator ->();
368 RegexpMapEntry
< Val
> const * operator ->() const;
371 RegexpMapIter(RegexpMapIterImpl
< Val
> * pTheImpl
);
374 template< typename Val
>
375 RegexpMapIter
< Val
>::RegexpMapIter(RegexpMapIterImpl
< Val
> * pTheImpl
):
376 RegexpMapConstIter
< Val
>(pTheImpl
)
379 template< typename Val
>
380 RegexpMapIter
< Val
> & RegexpMapIter
< Val
>::operator ++()
382 this->m_pImpl
->next();
386 template< typename Val
>
387 RegexpMapIter
< Val
> RegexpMapIter
< Val
>::operator ++(int)
389 RegexpMapIter
aTemp(*this);
390 this->m_pImpl
->next();
394 template< typename Val
>
395 RegexpMapEntry
< Val
> & RegexpMapIter
< Val
>::operator *()
397 return this->m_pImpl
->get();
400 template< typename Val
>
401 RegexpMapEntry
< Val
> const & RegexpMapIter
< Val
>::operator *() const
403 return this->m_pImpl
->get();
406 template< typename Val
>
407 RegexpMapEntry
< Val
> * RegexpMapIter
< Val
>::operator ->()
409 return &this->m_pImpl
->get();
412 template< typename Val
>
413 RegexpMapEntry
< Val
> const * RegexpMapIter
< Val
>::operator ->() const
415 return &this->m_pImpl
->get();
419 template< typename Val
>
423 typedef sal_uInt32 size_type
;
424 typedef RegexpMapIter
< Val
> iterator
;
425 typedef RegexpMapConstIter
< Val
> const_iterator
;
429 RegexpMap(RegexpMap
const & rOther
);
433 RegexpMap
& operator =(RegexpMap
const & rOther
);
435 bool add(OUString
const & rKey
, Val
const & rValue
, bool bOverwrite
,
436 OUString
* pReverse
= 0);
437 // throws com::sun::star::lang::IllegalArgumentException
439 iterator
find(OUString
const & rKey
, OUString
* pReverse
= 0);
440 // throws com::sun::star::lang::IllegalArgumentException
442 void erase(iterator
const & rPos
);
446 const_iterator
begin() const;
450 const_iterator
end() const;
454 size_type
size() const;
456 Val
const * map(OUString
const & rString
,
457 OUString
* pTranslation
= 0, bool * pTranslated
= 0)
461 RegexpMapImpl
< Val
> * m_pImpl
;
464 template< typename Val
>
465 RegexpMap
< Val
>::RegexpMap():
466 m_pImpl(new RegexpMapImpl
< Val
>)
469 template< typename Val
>
470 RegexpMap
< Val
>::RegexpMap(RegexpMap
const & rOther
):
471 m_pImpl(new RegexpMapImpl
< Val
>(*rOther
.m_pImpl
))
474 template< typename Val
>
475 RegexpMap
< Val
>::~RegexpMap()
480 template< typename Val
>
481 RegexpMap
< Val
> & RegexpMap
< Val
>::operator =(RegexpMap
const & rOther
)
483 *m_pImpl
= *rOther
.m_pImpl
;
487 template< typename Val
>
488 bool RegexpMap
< Val
>::add(rtl::OUString
const & rKey
, Val
const & rValue
,
489 bool bOverwrite
, rtl::OUString
* pReverse
)
491 Regexp
aRegexp(Regexp::parse(rKey
));
493 if (aRegexp
.isDefault())
495 if (m_pImpl
->m_pDefault
)
499 delete m_pImpl
->m_pDefault
;
501 m_pImpl
->m_pDefault
= new Entry
< Val
>(aRegexp
, rValue
);
505 List
< Val
> & rTheList
= m_pImpl
->m_aList
[aRegexp
.getKind()];
507 typename List
< Val
>::iterator
aEnd(rTheList
.end());
508 for (typename List
< Val
>::iterator
aIt(rTheList
.begin()); aIt
!= aEnd
; ++aIt
)
510 if (aIt
->m_aRegexp
== aRegexp
)
522 rTheList
.push_back(Entry
< Val
>(aRegexp
, rValue
));
526 *pReverse
= aRegexp
.getRegexp(true);
531 template< typename Val
>
532 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::find(rtl::OUString
const & rKey
,
533 rtl::OUString
* pReverse
)
535 Regexp
aRegexp(Regexp::parse(rKey
));
538 *pReverse
= aRegexp
.getRegexp(true);
540 if (aRegexp
.isDefault())
542 if (m_pImpl
->m_pDefault
)
543 return RegexpMapIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
,
548 List
< Val
> & rTheList
= m_pImpl
->m_aList
[aRegexp
.getKind()];
550 typename List
< Val
> ::iterator
aEnd(rTheList
.end());
551 for (typename List
< Val
>::iterator
aIt(rTheList
.begin()); aIt
!= aEnd
; ++aIt
)
552 if (aIt
->m_aRegexp
== aRegexp
)
553 return RegexpMapIter
< Val
>(new RegexpMapIterImpl
< Val
>(
555 aRegexp
.getKind(), aIt
));
558 return RegexpMapIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
, false));
561 template< typename Val
>
562 void RegexpMap
< Val
>::erase(iterator
const & rPos
)
564 if (rPos
.m_pImpl
->getMap() == m_pImpl
)
566 if (rPos
.m_pImpl
->getList() == -1)
568 if (m_pImpl
->m_pDefault
)
570 delete m_pImpl
->m_pDefault
;
571 m_pImpl
->m_pDefault
= 0;
575 m_pImpl
->m_aList
[rPos
.m_pImpl
->getList()].
576 erase(rPos
.m_pImpl
->getIndex());
580 template< typename Val
>
581 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::begin()
583 return RegexpMapIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
, true));
586 template< typename Val
>
587 typename RegexpMap
< Val
>::const_iterator RegexpMap
< Val
>::begin() const
589 return RegexpMapConstIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
,
593 template< typename Val
>
594 typename RegexpMap
< Val
>::iterator RegexpMap
< Val
>::end()
596 return RegexpMapIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
, false));
599 template< typename Val
>
600 typename RegexpMap
< Val
>::const_iterator RegexpMap
< Val
>::end() const
602 return RegexpMapConstIter
< Val
>(new RegexpMapIterImpl
< Val
>(m_pImpl
,
606 template< typename Val
>
607 bool RegexpMap
< Val
>::empty() const
609 return !m_pImpl
->m_pDefault
610 && m_pImpl
->m_aList
[Regexp::KIND_PREFIX
].empty()
611 && m_pImpl
->m_aList
[Regexp::KIND_AUTHORITY
].empty()
612 && m_pImpl
->m_aList
[Regexp::KIND_DOMAIN
].empty();
615 template< typename Val
>
616 typename RegexpMap
< Val
>::size_type RegexpMap
< Val
>::size() const
618 return (m_pImpl
->m_pDefault
? 1 : 0)
619 + m_pImpl
->m_aList
[Regexp::KIND_PREFIX
].size()
620 + m_pImpl
->m_aList
[Regexp::KIND_AUTHORITY
].size()
621 + m_pImpl
->m_aList
[Regexp::KIND_DOMAIN
].size();
624 template< typename Val
>
625 Val
const * RegexpMap
< Val
>::map(rtl::OUString
const & rString
,
626 rtl::OUString
* pTranslation
,
627 bool * pTranslated
) const
629 for (int n
= Regexp::KIND_DOMAIN
; n
>= Regexp::KIND_PREFIX
; --n
)
631 List
< Val
> const & rTheList
= m_pImpl
->m_aList
[n
];
633 typename List
< Val
>::const_iterator
aEnd(rTheList
.end());
634 for (typename List
< Val
>::const_iterator
aIt(rTheList
.begin()); aIt
!= aEnd
;
636 if (aIt
->m_aRegexp
.matches(rString
, pTranslation
, pTranslated
))
637 return &aIt
->m_aValue
;
639 if (m_pImpl
->m_pDefault
640 && m_pImpl
->m_pDefault
->m_aRegexp
.matches(rString
, pTranslation
,
642 return &m_pImpl
->m_pDefault
->m_aValue
;
649 template< typename Val
>
650 inline bool operator ==(ucb_impl::RegexpMapConstIter
< Val
> const & rIter1
,
651 ucb_impl::RegexpMapConstIter
< Val
> const & rIter2
)
653 return rIter1
.equals(rIter2
);
656 template< typename Val
>
657 inline bool operator !=(ucb_impl::RegexpMapConstIter
< Val
> const & rIter1
,
658 ucb_impl::RegexpMapConstIter
< Val
> const & rIter2
)
660 return !rIter1
.equals(rIter2
);
663 #endif // INCLUDED_UCB_SOURCE_INC_REGEXPMAP_HXX
665 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */