E-MailRelay

goptionmap.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file goptionmap.cpp
19///
20
21#include "gdef.h"
22#include "goptionmap.h"
23
24 void G::OptionMap::insert( const Map::value_type & value )
25{
26 m_map.insert( value ) ;
27}
28
29 void G::OptionMap::replace( const std::string & key , const std::string & value )
30{
31 auto pair = m_map.equal_range( key ) ;
32 if( pair.first != pair.second )
33 m_map.erase( pair.first , pair.second ) ;
34 m_map.insert( Map::value_type(key,OptionValue(value)) ) ;
35}
36
37 void G::OptionMap::increment( const std::string & key )
38{
39 auto p = m_map.find( key ) ;
40 if( p != m_map.end() )
41 (*p).second.increment() ;
42}
43
44G::OptionMap::const_iterator G::OptionMap::begin() const
45{
46 return m_map.begin() ;
47}
48
49G::OptionMap::const_iterator G::OptionMap::cbegin() const
50{
51 return begin() ;
52}
53
54G::OptionMap::const_iterator G::OptionMap::end() const
55{
56 return m_map.end() ;
57}
58
59G::OptionMap::const_iterator G::OptionMap::cend() const
60{
61 return end() ;
62}
63
64G::OptionMap::const_iterator G::OptionMap::find( const std::string & key ) const
65{
66 return m_map.find( key ) ;
67}
68
69 void G::OptionMap::clear()
70{
71 m_map.clear() ;
72}
73
74 bool G::OptionMap::contains( const char * key ) const
75{
76 return contains( std::string(key) ) ;
77}
78
79 bool G::OptionMap::contains( const std::string & key ) const
80{
81 const Map::const_iterator end = m_map.end() ;
82 for( auto p = m_map.find(key) ; p != end && (*p).first == key ; ++p )
83 {
84 if( (*p).second.isOff() )
85 continue ;
86 return true ;
87 }
88 return false ;
89}
90
91std::size_t G::OptionMap::count( const std::string & key ) const
92{
93 std::size_t n = 0U ;
94 auto pair = m_map.equal_range( key ) ;
95 for( auto p = pair.first ; p != pair.second ; ++p )
96 n += (*p).second.count() ;
97 return n ;
98}
99
100std::string G::OptionMap::value( const char * key , const char * default_ ) const
101{
102 return value( std::string(key) , default_ ? std::string(default_) : std::string() ) ;
103}
104
105std::string G::OptionMap::value( const std::string & key , const std::string & default_ ) const
106{
107 auto range = m_map.equal_range( key ) ;
108 if( range.first == range.second )
109 return default_ ;
110 else
111 return join( range.first , range.second , default_ ) ;
112}
113
114std::string G::OptionMap::join( Map::const_iterator p , Map::const_iterator end , const std::string & off_value ) const
115{
116 std::string result ;
117 const char * sep = "" ;
118 for( ; p != end ; ++p )
119 {
120 result.append( sep ) ; sep = "," ;
121 result.append( (*p).second.value() ) ;
122 if( (*p).second.isOn() )
123 return (*p).second.value() ;
124 if( (*p).second.isOff() )
125 return off_value ;
126 }
127 return result ;
128}
129
130 bool G::OptionMap::compare( const Map::value_type & pair1 , const Map::value_type & pair2 )
131{
132 return pair1.first < pair2.first ;
133}
134
G::OptionMap::cend
const_iterator cend() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:59
G::OptionMap::count
std::size_t count(const std::string &key) const
Returns the total repeat count for all matching entries.
Definition: goptionmap.cpp:91
G::OptionMap::clear
void clear()
Clears the map.
Definition: goptionmap.cpp:69
G::OptionMap::find
const_iterator find(const std::string &) const
Finds the map entry with the given key.
Definition: goptionmap.cpp:64
G::OptionMap::value
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns the matching value, with concatentation into a comma-separated list if multivalued.
Definition: goptionmap.cpp:105
G::OptionMap::cbegin
const_iterator cbegin() const
Returns the begin iterator.
Definition: goptionmap.cpp:49
G::OptionMap::end
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:54
G::OptionMap::increment
void increment(const std::string &key)
Increments the repeat count for the given entry.
Definition: goptionmap.cpp:37
G::OptionMap::insert
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.cpp:24
G::OptionMap::replace
void replace(const std::string &key, const std::string &value)
Replaces all matching values with a single value.
Definition: goptionmap.cpp:29
G::OptionMap::contains
bool contains(const std::string &) const
Returns true if the map contains the given key, but ignoring 'off' option-values.
Definition: goptionmap.cpp:79
G::OptionMap::begin
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.cpp:44
G::OptionValue
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:39
goptionmap.h