libzypp  17.30.2
inidict.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include "inidict.h"
14 
15 #include <iostream>
16 #include <map>
17 #include <string>
18 
19 #include <zypp-core/base/Logger.h>
20 
21 using std::endl;
22 
24 namespace zypp
25 {
27  namespace parser
28  {
30  //
31  // CLASS NAME : IniDict
32  //
34 
36  //
37  // METHOD NAME : IniDict::IniDict
38  // METHOD TYPE : Ctor
39  //
41  const ProgressData::ReceiverFnc & progress )
42  {
43  read(is, progress );
44  }
45 
47  {
48  }
49 
50  void IniDict::read( const InputStream &is,
51  const ProgressData::ReceiverFnc & progress )
52  {
53  parse(is, progress );
54  }
55 
57  //
58  // METHOD NAME : IniDict::~IniDict
59  // METHOD TYPE : Dtor
60  //
62  {}
63 
64  void IniDict::consume( const std::string &section )
65  {
66  _dict[section]; // remember even empty sections
67  }
68 
69  void IniDict::consume( const std::string &section, const std::string &key, const std::string &value )
70  {
71  _dict[section][key] = value;
72  }
73 
74 
75  IniDict::entry_const_iterator IniDict::entriesBegin(const std::string &section) const
76  {
77  SectionSet::const_iterator secit = _dict.find(section);
78  if ( secit == _dict.end() )
79  {
80  return _empty_map.begin();
81  }
82 
83  return (secit->second).begin();
84  }
85 
86  IniDict::entry_const_iterator IniDict::entriesEnd(const std::string &section) const
87  {
88  SectionSet::const_iterator secit = _dict.find(section);
89  if ( secit == _dict.end() )
90  {
91  return _empty_map.end();
92  }
93 
94  return (secit->second).end();
95  }
96 
98  {
99  SectionSet::const_iterator secit = _dict.find(section);
100  if ( secit == _dict.end() )
101  {
102  return makeIterable( _empty_map.begin(), _empty_map.end() );
103  }
104 
105  return makeIterable( (secit->second).begin(), (secit->second).end() );
106  }
107 
109  {
110  return make_map_key_begin( _dict );
111  }
112 
114  {
115  return make_map_key_end( _dict );
116  }
117 
119  {
120  return makeIterable( sectionsBegin(), sectionsEnd() );
121  }
122 
123  void IniDict::insertEntry( const std::string &section,
124  const std::string &key,
125  const std::string &value )
126  {
127  consume( section, key, value );
128  }
129 
130 
131  void IniDict::deleteSection( const std::string &section )
132  {
133  _dict.erase(section);
134  }
135 
136  bool IniDict::hasSection( const std::string &section ) const
137  {
138  SectionSet::const_iterator secit = _dict.find(section);
139  if ( secit == _dict.end() )
140  return false;
141  return true;
142  }
143 
144  bool IniDict::hasEntry( const std::string &section,
145  const std::string &entry ) const
146  {
147  SectionSet::const_iterator secit = _dict.find(section);
148  if ( secit == _dict.end() )
149  return false;
150 
151  EntrySet::const_iterator entryit = (secit->second).find(entry);
152  if ( entryit == (secit->second).end() )
153  return false;
154 
155  return true;
156  }
157 
158  /******************************************************************
159  **
160  ** FUNCTION NAME : operator<<
161  ** FUNCTION TYPE : std::ostream &
162  */
163  std::ostream & operator<<( std::ostream & str, const IniDict & obj )
164  {
166  si != obj.sectionsEnd();
167  ++si )
168  {
169  str << "[" << *si << "]" << endl;
170  for ( IniDict::entry_const_iterator ei = obj.entriesBegin(*si);
171  ei != obj.entriesEnd(*si);
172  ++ei )
173  {
174  str << ei->first << " = " << ei->second << endl;
175  }
176  str << endl;
177  }
178  return str;
179  }
180 
182  } // namespace parser
185 } // namespace zypp
Helper to create and pass std::istream.
Definition: inputstream.h:57
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:139
Parses a INI file and offers its structure as a dictionary.
Definition: inidict.h:42
section_const_iterator sectionsEnd() const
Definition: inidict.cc:113
void read(const InputStream &is, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Fill a dictionary from a InputStream containing a ini structured file.
Definition: inidict.cc:50
bool hasSection(const std::string &section) const
True if there is a section with that name.
Definition: inidict.cc:136
void deleteSection(const std::string &section)
add an entry
Definition: inidict.cc:131
EntrySet::const_iterator entry_const_iterator
Definition: inidict.h:48
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition: inidict.h:47
Iterable< section_const_iterator > sections() const
Definition: inidict.cc:118
entry_const_iterator entriesBegin(const std::string &section) const
Definition: inidict.cc:75
Iterable< entry_const_iterator > entries(const std::string &section) const
Definition: inidict.cc:97
void insertEntry(const std::string &section, const std::string &key, const std::string &value)
add an entry
Definition: inidict.cc:123
section_const_iterator sectionsBegin() const
Definition: inidict.cc:108
bool hasEntry(const std::string &section, const std::string &entry) const
True if an entry exists in the section.
Definition: inidict.cc:144
IniDict()
Creates a mepty dictionary.
Definition: inidict.cc:46
virtual void consume(const std::string &section)
Called when a section is found.
Definition: inidict.cc:64
SectionSet _dict
Definition: inidict.h:153
EntrySet _empty_map
empty map used to simulate iteration in non-existent sections
Definition: inidict.h:159
entry_const_iterator entriesEnd(const std::string &section) const
Definition: inidict.cc:86
void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Parse the stream.
Definition: iniparser.cc:84
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const ProductFileData &obj)
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_end(const TMap &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:233
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_begin(const TMap &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:228