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