libzypp  13.10.6
KVMap.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
8 | |
9 | core system |
10 | (C) SuSE Linux AG |
11 \----------------------------------------------------------------------/
12 
13  File: KVMap.h
14 
15  Author: Michael Andres <ma@suse.de>
16  Maintainer: Michael Andres <ma@suse.de>
17 
18  Purpose: Convenience stuff for handling (key,value) pairs
19 
20 /-*/
21 #ifndef KVMap_h
22 #define KVMap_h
23 
24 #include <iosfwd>
25 #include <vector>
26 #include <map>
27 
28 #include "zypp/base/String.h"
29 
30 namespace zypp {
31  namespace kvmap {
32 
34  //
35  // CLASS NAME : KVMapBase::KVMapPolicy
51  struct KVMapPolicy {
52  std::string _kvsplit;
53  std::string _fsplit;
54  std::string _kvjoin;
55  std::string _fjoin;
56  KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r )
57  : _kvsplit( kvsplit_r )
58  , _fsplit ( fsplit_r )
59  , _kvjoin ( _kvsplit )
60  , _fjoin ( _fsplit )
61  {}
62  KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r,
63  const std::string & kvjoin_r )
64  : _kvsplit( kvsplit_r )
65  , _fsplit ( fsplit_r )
66  , _kvjoin ( kvjoin_r )
67  , _fjoin ( _fsplit )
68  {}
69  KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r,
70  const std::string & kvjoin_r, const std::string & fjoin_r )
71  : _kvsplit( kvsplit_r )
72  , _fsplit ( fsplit_r )
73  , _kvjoin ( kvjoin_r )
74  , _fjoin ( fjoin_r )
75  {}
76  };
77 
79  //
80  // CLASS NAME : KVMapBase
84  struct KVMapBase : public std::map<std::string,std::string> {
85 
89  typedef std::map<std::string,std::string> map_type;
90 
92  {}
93  KVMapBase( const map_type & kvmap_r )
94  : std::map<std::string,std::string>( kvmap_r )
95  {}
96 
100  bool has( const std::string & key_r ) const {
101  return( find( key_r ) != end() );
102  }
103 
107  template<char kv, char f>
108  struct CharSep : public KVMapPolicy { CharSep() : KVMapPolicy( std::string(1,kv), std::string(1,f) ) {} };
109 
111 
116  static map_type split( const std::string & str_r,
117  const KVMapPolicy & opts_r ) {
118  map_type ret;
119  std::vector<std::string> fields;
120  str::split( str_r, std::back_inserter(fields), opts_r._fsplit );
121 
122  for ( unsigned i = 0; i < fields.size(); ++i ) {
123  std::string::size_type pos = fields[i].find( opts_r._kvsplit );
124  if ( pos == std::string::npos ) {
125  ret[fields[i]] = "";
126  } else {
127  ret[fields[i].substr( 0, pos )] = fields[i].substr( pos+1 );
128  }
129  }
130 
131  return ret;
132  }
133 
138  static std::string join( const map_type & kvmap_r,
139  const KVMapPolicy & opts_r ) {
140  std::string ret;
141 
142  for ( map_type::const_iterator it = kvmap_r.begin(); it != kvmap_r.end(); ++it ) {
143  if ( ! ret.empty() ) {
144  ret += opts_r._fjoin;
145  }
146  ret += it->first;
147  if ( !it->second.empty() ) {
148  ret += opts_r._kvjoin + it->second;
149  }
150  }
151 
152  return ret;
153  }
154 
155  };
156 
157 
158 
159  } // namespace kvmap
160 
162 
164  //
165  // CLASS NAME : KVMap<KVMapOpts>
175  template<typename KVMapOpts>
176  struct KVMap : public kvmap::KVMapBase {
177 
179  {}
180  KVMap( const char * str_r )
181  : kvmap::KVMapBase( split( (str_r?str_r:""), KVMapOpts() ) )
182  {}
183  KVMap( const std::string & str_r )
184  : kvmap::KVMapBase( split( str_r, KVMapOpts() ) )
185  {}
186  KVMap( const map_type & map_r )
187  : kvmap::KVMapBase( map_r )
188  {}
189 
190  ~KVMap() {}
191 
192  std::string asString() const {
193  return join( *this, KVMapOpts() );
194  }
195 
196  };
197 
199 
200  template<typename KVMapOpts>
201  std::ostream & operator<<( std::ostream & str, const KVMap<KVMapOpts> & obj )
202  { return str << obj.asString(); }
203 
205 } // namespace zypp
206 
207 #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:138
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:394
KVMap(const std::string &str_r)
Definition: KVMap.h:183
~KVMap()
Definition: KVMap.h:190
std::string _fsplit
Definition: KVMap.h:53
Base class for KVMaps, (key,value) pairs.
Definition: KVMap.h:84
std::string _fjoin
Definition: KVMap.h:55
std::string _kvjoin
Definition: KVMap.h:54
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r, const std::string &kvjoin_r, const std::string &fjoin_r)
Definition: KVMap.h:69
KVMap(const char *str_r)
Definition: KVMap.h:180
std::map< std::string, std::string > map_type
(key,value) map type
Definition: KVMap.h:89
KVMapPolicy for KVMaps using a single char as separator (e.g.
Definition: KVMap.h:108
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:116
SolvableIdType size_type
Definition: PoolMember.h:99
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r, const std::string &kvjoin_r)
Definition: KVMap.h:62
KVMapBase(const map_type &kvmap_r)
Definition: KVMap.h:93
KVMap(const map_type &map_r)
Definition: KVMap.h:186
std::string _kvsplit
Definition: KVMap.h:52
KVMapPolicy(const std::string &kvsplit_r, const std::string &fsplit_r)
Definition: KVMap.h:56
A map of (key,value) strings.
Definition: KVMap.h:176
KVMapPolicy for conversion of KVMaps to/from string.
Definition: KVMap.h:51
std::string asString() const
Definition: KVMap.h:192
bool has(const std::string &key_r) const
Test whether key is set.
Definition: KVMap.h:100