E-MailRelay
goptionmap.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // goptionmap.cpp
19 //
20 
21 #include "gdef.h"
22 #include "goptionmap.h"
23 
25 {
26 }
27 
28 void G::OptionMap::insert( const Map::value_type & value )
29 {
30  m_map.insert( value ) ;
31 }
32 
33 G::OptionMap::const_iterator G::OptionMap::begin() const
34 {
35  return m_map.begin() ;
36 }
37 
38 G::OptionMap::const_iterator G::OptionMap::end() const
39 {
40  return m_map.end() ;
41 }
42 
43 G::OptionMap::const_iterator G::OptionMap::find( const std::string & key ) const
44 {
45  return m_map.find( key ) ;
46 }
47 
49 {
50  m_map.clear() ;
51 }
52 
53 bool G::OptionMap::contains( const char * key ) const
54 {
55  return contains( std::string(key) ) ;
56 }
57 
58 bool G::OptionMap::contains( const std::string & key ) const
59 {
60  const Map::const_iterator end = m_map.end() ;
61  for( Map::const_iterator p = m_map.find(key) ; p != end && (*p).first == key ; ++p )
62  {
63  if( (*p).second.valued() || !(*p).second.is_off() )
64  return true ;
65  }
66  return false ;
67 }
68 
69 size_t G::OptionMap::count( const std::string & key ) const
70 {
71  size_t n = 0U ;
72  const Map::const_iterator end = m_map.end() ;
73  for( Map::const_iterator p = m_map.find(key) ; p != end && (*p).first == key ; ++p )
74  n++ ;
75  return n ;
76 }
77 
78 std::string G::OptionMap::value( const char * key , const char * default_ ) const
79 {
80  return value( std::string(key) , default_ ? std::string(default_) : std::string() ) ;
81 }
82 
83 std::string G::OptionMap::value( const std::string & key , const std::string & default_ ) const
84 {
85  Map::const_iterator p = m_map.find( key ) ;
86  if( p == m_map.end() || !(*p).second.valued() )
87  return default_ ;
88  else if( count(key) == 1U )
89  return (*p).second.value() ;
90  else
91  return join( p , std::upper_bound(p,m_map.end(),*p,m_map.value_comp()) ) ;
92 }
93 
94 std::string G::OptionMap::join( Map::const_iterator p , Map::const_iterator end ) const
95 {
96  std::string result ;
97  for( const char * sep = "" ; p != end ; ++p )
98  {
99  if( (*p).second.valued() )
100  {
101  result.append( sep ) ; sep = "," ;
102  result.append( (*p).second.value() ) ;
103  }
104  }
105  return result ;
106 }
107 /// \file goptionmap.cpp
size_t count(const std::string &key) const
Returns the number of times the key appears in the multimap.
Definition: goptionmap.cpp:69
bool contains(const std::string &) const
Returns true if the map contains the given key, but ignoring un-valued() 'off' options.
Definition: goptionmap.cpp:58
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.cpp:33
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:38
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.cpp:28
OptionMap()
Default constructor for an empty map.
Definition: goptionmap.cpp:24
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns the value of the valued() option identified by the given key.
Definition: goptionmap.cpp:83
void clear()
Clears the map.
Definition: goptionmap.cpp:48
const_iterator find(const std::string &) const
Finds the map entry with the given key.
Definition: goptionmap.cpp:43