libzypp  17.35.11
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 <utility>
23 #include <vector>
24 #include <map>
25 
26 #include <zypp/base/String.h>
27 
28 namespace zypp {
29  namespace kvmap {
30 
32  //
33  // CLASS NAME : KVMapBase::KVMapPolicy
49  struct KVMapPolicy {
50  std::string _kvsplit;
51  std::string _fsplit;
52  std::string _kvjoin;
53  std::string _fjoin;
54  KVMapPolicy( std::string kvsplit_r, std::string fsplit_r )
55  : _kvsplit(std::move( kvsplit_r ))
56  , _fsplit (std::move( fsplit_r ))
57  , _kvjoin ( _kvsplit )
58  , _fjoin ( _fsplit )
59  {}
60  KVMapPolicy( std::string kvsplit_r, std::string fsplit_r,
61  std::string kvjoin_r )
62  : _kvsplit(std::move( kvsplit_r ))
63  , _fsplit (std::move( fsplit_r ))
64  , _kvjoin (std::move( kvjoin_r ))
65  , _fjoin ( _fsplit )
66  {}
67  KVMapPolicy( std::string kvsplit_r, std::string fsplit_r,
68  std::string kvjoin_r, std::string fjoin_r )
69  : _kvsplit(std::move( kvsplit_r ))
70  , _fsplit (std::move( fsplit_r ))
71  , _kvjoin (std::move( kvjoin_r ))
72  , _fjoin (std::move( fjoin_r ))
73  {}
74  };
75 
77  //
78  // CLASS NAME : KVMapBase
82  struct KVMapBase : public std::map<std::string,std::string> {
83 
87  using map_type = std::map<std::string, std::string>;
88 
90  {}
91  KVMapBase( const map_type & kvmap_r )
92  : std::map<std::string,std::string>( kvmap_r )
93  {}
94 
98  bool has( const std::string & key_r ) const {
99  return( find( key_r ) != end() );
100  }
101 
105  template<char kv, char f>
106  struct CharSep : public KVMapPolicy { CharSep() : KVMapPolicy( std::string(1,kv), std::string(1,f) ) {} };
107 
109 
114  static map_type split( const std::string & str_r,
115  const KVMapPolicy & opts_r ) {
116  map_type ret;
117  std::vector<std::string> fields;
118  str::split( str_r, std::back_inserter(fields), opts_r._fsplit );
119 
120  for ( unsigned i = 0; i < fields.size(); ++i ) {
121  std::string::size_type pos = fields[i].find( opts_r._kvsplit );
122  if ( pos == std::string::npos ) {
123  ret[fields[i]] = "";
124  } else {
125  ret[fields[i].substr( 0, pos )] = fields[i].substr( pos+1 );
126  }
127  }
128 
129  return ret;
130  }
131 
136  static std::string join( const map_type & kvmap_r,
137  const KVMapPolicy & opts_r ) {
138  std::string ret;
139 
140  for ( map_type::const_iterator it = kvmap_r.begin(); it != kvmap_r.end(); ++it ) {
141  if ( ! ret.empty() ) {
142  ret += opts_r._fjoin;
143  }
144  ret += it->first;
145  if ( !it->second.empty() ) {
146  ret += opts_r._kvjoin + it->second;
147  }
148  }
149 
150  return ret;
151  }
152 
153  };
154 
155 
156 
157  } // namespace kvmap
158 
160 
162  //
163  // CLASS NAME : KVMap<KVMapOpts>
173  template<typename KVMapOpts>
174  struct KVMap : public kvmap::KVMapBase {
175 
177  {}
178  KVMap( const char * str_r )
179  : kvmap::KVMapBase( split( (str_r?str_r:""), KVMapOpts() ) )
180  {}
181  KVMap( const std::string & str_r )
182  : kvmap::KVMapBase( split( str_r, KVMapOpts() ) )
183  {}
184  KVMap( const map_type & map_r )
185  : kvmap::KVMapBase( map_r )
186  {}
187 
188  ~KVMap() {}
189 
190  std::string asString() const {
191  return join( *this, KVMapOpts() );
192  }
193 
194  };
195 
197 
198  template<typename KVMapOpts>
199  std::ostream & operator<<( std::ostream & str, const KVMap<KVMapOpts> & obj )
200  { return str << obj.asString(); }
201 
203 } // namespace zypp
204 
205 #endif // KVMap_h
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:136
KVMap(const std::string &str_r)
Definition: kvmap.h:181
~KVMap()
Definition: kvmap.h:188
std::string _fsplit
Definition: kvmap.h:51
KVMapPolicy(std::string kvsplit_r, std::string fsplit_r, std::string kvjoin_r, std::string fjoin_r)
Definition: kvmap.h:67
Base class for KVMaps, (key,value) pairs.
Definition: kvmap.h:82
std::string _fjoin
Definition: kvmap.h:53
String related utilities and Regular expression matching.
std::string _kvjoin
Definition: kvmap.h:52
Definition: Arch.h:363
KVMap(const char *str_r)
Definition: kvmap.h:178
KVMapPolicy for KVMaps using a single char as separator (e.g.
Definition: kvmap.h:106
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:114
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:531
std::map< std::string, std::string > map_type
(key,value) map type
Definition: kvmap.h:87
KVMapPolicy(std::string kvsplit_r, std::string fsplit_r, std::string kvjoin_r)
Definition: kvmap.h:60
KVMapBase(const map_type &kvmap_r)
Definition: kvmap.h:91
KVMapPolicy(std::string kvsplit_r, std::string fsplit_r)
Definition: kvmap.h:54
std::string asString() const
Definition: kvmap.h:190
bool has(const std::string &key_r) const
Test whether key is set.
Definition: kvmap.h:98
KVMap(const map_type &map_r)
Definition: kvmap.h:184
std::string _kvsplit
Definition: kvmap.h:50
A map of (key,value) strings.
Definition: kvmap.h:174
KVMapPolicy for conversion of KVMaps to/from string.
Definition: kvmap.h:49
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
SolvableIdType size_type
Definition: PoolMember.h:126