E-MailRelay
goptionmap.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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#include "gstringfield.h"
24#include <algorithm>
25
26void G::OptionMap::insert( const Map::value_type & value )
27{
28 m_map.insert( value ) ;
29}
30
31G::OptionMap::Range G::OptionMap::findRange( string_view key ) const
32{
33 return m_map.equal_range( sv_to_string(key) ) ; // or c++14 'generic associative lookup' of string_view
34}
35
36G::OptionMap::Map::iterator G::OptionMap::findFirst( string_view key )
37{
38 return m_map.find( sv_to_string(key) ) ; // or c++14 'generic associative lookup' of string_view
39}
40
41G::OptionMap::const_iterator G::OptionMap::find( string_view key ) const
42{
43 auto pair = findRange( key ) ;
44 return pair.first == pair.second ? m_map.end() : pair.first ;
45}
46
47void G::OptionMap::replace( string_view key , const std::string & value )
48{
49 auto pair = findRange( key ) ;
50 if( pair.first != pair.second )
51 m_map.erase( pair.first , pair.second ) ;
52 m_map.insert( Map::value_type(sv_to_string(key),OptionValue(value)) ) ;
53}
54
56{
57 auto p = findFirst( key ) ;
58 if( p != m_map.end() )
59 (*p).second.increment() ;
60}
61
62G::OptionMap::const_iterator G::OptionMap::begin() const
63{
64 return m_map.begin() ;
65}
66
67#ifndef G_LIB_SMALL
68G::OptionMap::const_iterator G::OptionMap::cbegin() const
69{
70 return begin() ;
71}
72#endif
73
74G::OptionMap::const_iterator G::OptionMap::end() const
75{
76 return m_map.end() ;
77}
78
79#ifndef G_LIB_SMALL
80G::OptionMap::const_iterator G::OptionMap::cend() const
81{
82 return end() ;
83}
84#endif
85
87{
88 m_map.clear() ;
89}
90
92{
93 auto range = findRange( key ) ;
94 for( auto p = range.first ; p != range.second ; ++p )
95 {
96 if( (*p).second.isOff() )
97 continue ;
98 return true ;
99 }
100 return false ;
101}
102
103bool G::OptionMap::contains( const char * key ) const
104{
105 return contains( string_view(key) ) ;
106}
107
108bool G::OptionMap::contains( const std::string & key ) const
109{
110 return contains( string_view(key) ) ;
111}
112
113std::size_t G::OptionMap::count( string_view key ) const
114{
115 std::size_t n = 0U ;
116 auto pair = findRange( key ) ;
117 for( auto p = pair.first ; p != pair.second ; ++p )
118 n += (*p).second.count() ;
119 return n ;
120}
121
122std::string G::OptionMap::value( string_view key , string_view default_ ) const
123{
124 auto range = findRange( key ) ;
125 if( range.first == range.second )
126 return sv_to_string(default_) ;
127 else
128 return join( range.first , range.second , default_ ) ;
129}
130
131std::string G::OptionMap::join( Map::const_iterator p , Map::const_iterator end , string_view off_value ) const
132{
133 std::string result ;
134 const char * sep = "" ;
135 for( ; p != end ; ++p )
136 {
137 result.append( sep ) ; sep = "," ;
138 result.append( (*p).second.value() ) ;
139 if( (*p).second.isOn() )
140 return (*p).second.value() ;
141 if( (*p).second.isOff() )
142 return sv_to_string(off_value) ;
143 }
144 return result ;
145}
146
147unsigned int G::OptionMap::number( string_view key , unsigned int default_ ) const
148{
149 G_ASSERT( !G::Str::isUInt("") ) ;
150 return G::Str::isUInt(value(key)) ? G::Str::toUInt(value(key)) : default_ ;
151}
152
const_iterator cend() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:80
void replace(string_view key, const std::string &value)
Replaces all matching values with a single value.
Definition: goptionmap.cpp:47
void clear()
Clears the map.
Definition: goptionmap.cpp:86
std::size_t count(string_view key) const
Returns the total repeat count for all matching entries.
Definition: goptionmap.cpp:113
bool contains(string_view) const
Returns true if the map contains the given key, but ignoring 'off' option-values.
Definition: goptionmap.cpp:91
unsigned int number(string_view key, unsigned int default_) const
Returns the matching value as a number.
Definition: goptionmap.cpp:147
const_iterator cbegin() const
Returns the begin iterator.
Definition: goptionmap.cpp:68
const_iterator end() const
Returns the off-the-end iterator.
Definition: goptionmap.cpp:74
void insert(const Map::value_type &)
Inserts the key/value pair into the map.
Definition: goptionmap.cpp:26
void increment(string_view key)
Increments the repeat count for the given entry.
Definition: goptionmap.cpp:55
const_iterator find(string_view) const
Finds the map entry with the given key.
Definition: goptionmap.cpp:41
std::string value(string_view key, string_view default_={}) const
Returns the matching value, with concatentation into a comma-separated list if multivalued (with no e...
Definition: goptionmap.cpp:122
const_iterator begin() const
Returns the begin iterator.
Definition: goptionmap.cpp:62
A simple structure encapsulating the value of a command-line option.
Definition: goptionvalue.h:39
static unsigned int toUInt(string_view s)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:651
static bool isUInt(string_view s) noexcept
Returns true if the string can be converted into an unsigned integer without throwing an exception.
Definition: gstr.cpp:449
A class like c++17's std::string_view.
Definition: gstringview.h:51