1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Michael Andres <ma@suse.de>
16 Maintainer: Michael Andres <ma@suse.de>
18 Purpose: Convenience stuff for handling (key,value) pairs
28 #include "zypp/base/String.h"
33 ///////////////////////////////////////////////////////////////////
35 // CLASS NAME : KVMapBase::KVMapPolicy
37 * @short KVMapPolicy for conversion of KVMaps to/from string.
39 * <b>_kvsplit</b>: The string separating key from value
41 * <b>_fsplit</b>: (key,value) pairs are separated by any nonempty
42 * sequence of characers occurring in _fsplit
44 * <b>_kvjoin</b>: The string used to join key and value.
46 * <b>_fjoin</b>: The string used to separate (key,value) pairs.
48 * TODO: Maybe options for exact _fsplit handling and timming of values.
56 KVMapPolicy( const std::string & kvsplit_r, const std::string & fsplit_r )
57 : _kvsplit( kvsplit_r )
58 , _fsplit ( fsplit_r )
59 , _kvjoin ( _kvsplit )
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 )
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 )
78 ///////////////////////////////////////////////////////////////////
80 // CLASS NAME : KVMapBase
82 * @short Base class for KVMaps, (key,value) pairs
84 struct KVMapBase : public std::map<std::string,std::string> {
87 * (key,value) map type
89 typedef std::map<std::string,std::string> map_type;
93 KVMapBase( const map_type & kvmap_r )
94 : std::map<std::string,std::string>( kvmap_r )
98 * Test whether key is set.
100 bool has( const std::string & key_r ) const {
101 return( find( key_r ) != end() );
105 * @short KVMapPolicy for KVMaps using a single char as separator (e.g. mount options).
107 template<char kv, char f>
108 struct CharSep : public KVMapPolicy { CharSep() : KVMapPolicy( std::string(1,kv), std::string(1,f) ) {} };
110 ///////////////////////////////////////////////////////////////////
113 * Split str_r into (key,value) map, using the separators defined
116 static map_type split( const std::string & str_r,
117 const KVMapPolicy & opts_r ) {
119 std::vector<std::string> fields;
120 str::split( str_r, std::back_inserter(fields), opts_r._fsplit );
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 ) {
127 ret[fields[i].substr( 0, pos )] = fields[i].substr( pos+1 );
135 * Join (key,value) map into string, using the separators defined
138 static std::string join( const map_type & kvmap_r,
139 const KVMapPolicy & opts_r ) {
142 for ( map_type::const_iterator it = kvmap_r.begin(); it != kvmap_r.end(); ++it ) {
143 if ( ! ret.empty() ) {
144 ret += opts_r._fjoin;
147 if ( !it->second.empty() ) {
148 ret += opts_r._kvjoin + it->second;
161 ///////////////////////////////////////////////////////////////////
163 ///////////////////////////////////////////////////////////////////
165 // CLASS NAME : KVMap<KVMapOpts>
167 * @short A map of (key,value) strings.
169 * The template argument defines the @ref kvmap::Options for
172 * E.g. mount options (a comma separated list of key[=value] pairs)
173 * could be handled by KVMap<kvmap::KVMapBase::Comma>.
175 template<typename KVMapOpts>
176 struct KVMap : public kvmap::KVMapBase {
180 KVMap( const char * str_r )
181 : kvmap::KVMapBase( split( (str_r?str_r:""), KVMapOpts() ) )
183 KVMap( const std::string & str_r )
184 : kvmap::KVMapBase( split( str_r, KVMapOpts() ) )
186 KVMap( const map_type & map_r )
187 : kvmap::KVMapBase( map_r )
192 std::string asString() const {
193 return join( *this, KVMapOpts() );
198 ///////////////////////////////////////////////////////////////////
200 template<typename KVMapOpts>
201 std::ostream & operator<<( std::ostream & str, const KVMap<KVMapOpts> & obj )
202 { return str << obj.asString(); }
204 ///////////////////////////////////////////////////////////////////