libzypp 17.31.23
kvmap.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\----------------------------------------------------------------------/
9
10 File: KVMap.h
11
12 Author: Michael Andres <ma@suse.de>
13 Maintainer: Michael Andres <ma@suse.de>
14
15 Purpose: Convenience stuff for handling (key,value) pairs
16
17*/
18#ifndef KVMap_h
19#define KVMap_h
20
21#include <iosfwd>
22#include <vector>
23#include <map>
24
25#include <zypp/base/String.h>
26
27namespace zypp {
28 namespace kvmap {
29
31 //
32 // CLASS NAME : KVMapBase::KVMapPolicy
48 struct KVMapPolicy {
49 std::string _kvsplit;
50 std::string _fsplit;
51 std::string _kvjoin;
52 std::string _fjoin;
53 KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r )
54 : _kvsplit( kvsplit_r )
55 , _fsplit ( fsplit_r )
56 , _kvjoin ( _kvsplit )
57 , _fjoin ( _fsplit )
58 {}
59 KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r,
60 const std::string & kvjoin_r )
61 : _kvsplit( kvsplit_r )
62 , _fsplit ( fsplit_r )
63 , _kvjoin ( kvjoin_r )
64 , _fjoin ( _fsplit )
65 {}
66 KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r,
67 const std::string & kvjoin_r, const std::string & fjoin_r )
68 : _kvsplit( kvsplit_r )
69 , _fsplit ( fsplit_r )
70 , _kvjoin ( kvjoin_r )
71 , _fjoin ( fjoin_r )
72 {}
73 };
74
76 //
77 // CLASS NAME : KVMapBase
81 struct KVMapBase : public std::map<std::string,std::string> {
82
86 typedef std::map<std::string,std::string> map_type;
87
89 {}
90 KVMapBase( const map_type & kvmap_r )
91 : std::map<std::string,std::string>( kvmap_r )
92 {}
93
97 bool has( const std::string & key_r ) const {
98 return( find( key_r ) != end() );
99 }
100
104 template<char kv, char f>
105 struct CharSep : public KVMapPolicy { CharSep() : KVMapPolicy( std::string(1,kv), std::string(1,f) ) {} };
106
108
113 static map_type split( const std::string & str_r,
114 const KVMapPolicy & opts_r ) {
115 map_type ret;
116 std::vector<std::string> fields;
117 str::split( str_r, std::back_inserter(fields), opts_r._fsplit );
118
119 for ( unsigned i = 0; i < fields.size(); ++i ) {
120 std::string::size_type pos = fields[i].find( opts_r._kvsplit );
121 if ( pos == std::string::npos ) {
122 ret[fields[i]] = "";
123 } else {
124 ret[fields[i].substr( 0, pos )] = fields[i].substr( pos+1 );
125 }
126 }
127
128 return ret;
129 }
130
135 static std::string join( const map_type & kvmap_r,
136 const KVMapPolicy & opts_r ) {
137 std::string ret;
138
139 for ( map_type::const_iterator it = kvmap_r.begin(); it != kvmap_r.end(); ++it ) {
140 if ( ! ret.empty() ) {
141 ret += opts_r._fjoin;
142 }
143 ret += it->first;
144 if ( !it->second.empty() ) {
145 ret += opts_r._kvjoin + it->second;
146 }
147 }
148
149 return ret;
150 }
151
152 };
153
154
155
156 } // namespace kvmap
157
159
161 //
162 // CLASS NAME : KVMap<KVMapOpts>
172 template<typename KVMapOpts>
173 struct KVMap : public kvmap::KVMapBase {
174
176 {}
177 KVMap( const char * str_r )
178 : kvmap::KVMapBase( split( (str_r?str_r:""), KVMapOpts() ) )
179 {}
180 KVMap( const std::string & str_r )
181 : kvmap::KVMapBase( split( str_r, KVMapOpts() ) )
182 {}
183 KVMap( const map_type & map_r )
184 : kvmap::KVMapBase( map_r )
185 {}
186
188
189 std::string asString() const {
190 return join( *this, KVMapOpts() );
191 }
192
193 };
194
196
197 template<typename KVMapOpts>
198 std::ostream & operator<<( std::ostream & str, const KVMap<KVMapOpts> & obj )
199 { return str << obj.asString(); }
200
202} // namespace zypp
203
204#endif // KVMap_h
Definition: Arch.h:361
String related utilities and Regular expression matching.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:531
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
A map of (key,value) strings.
Definition: kvmap.h:173
std::string asString() const
Definition: kvmap.h:189
KVMap(const map_type &map_r)
Definition: kvmap.h:183
~KVMap()
Definition: kvmap.h:187
KVMap(const char *str_r)
Definition: kvmap.h:177
KVMap(const std::string &str_r)
Definition: kvmap.h:180
KVMapPolicy for KVMaps using a single char as separator (e.g.
Definition: kvmap.h:105
Base class for KVMaps, (key,value) pairs.
Definition: kvmap.h:81
bool has(const std::string &key_r) const
Test whether key is set.
Definition: kvmap.h:97
KVMapBase(const map_type &kvmap_r)
Definition: kvmap.h:90
static map_type split(const std::string &str_r, const KVMapPolicy &opts_r)
Split str_r into (key,value) map, using the separators defined by opts_r.
Definition: kvmap.h:113
std::map< std::string, std::string > map_type
(key,value) map type
Definition: kvmap.h:86
static std::string join(const map_type &kvmap_r, const KVMapPolicy &opts_r)
Join (key,value) map into string, using the separators defined by opts_r.
Definition: kvmap.h:135
KVMapPolicy for conversion of KVMaps to/from string.
Definition: kvmap.h:48
std::string _kvjoin
Definition: kvmap.h:51
std::string _fsplit
Definition: kvmap.h:50
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r, const std::string &kvjoin_r)
Definition: kvmap.h:59
std::string _fjoin
Definition: kvmap.h:52
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r, const std::string &kvjoin_r, const std::string &fjoin_r)
Definition: kvmap.h:66
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r)
Definition: kvmap.h:53
std::string _kvsplit
Definition: kvmap.h:49