libzypp  17.23.5
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 
94 
96  {
97  return make_map_key_begin( _dict );
98  }
99 
101  {
102  return make_map_key_end( _dict );
103  }
104 
105  void IniDict::insertEntry( const std::string &section,
106  const std::string &key,
107  const std::string &value )
108  {
109  consume( section, key, value );
110  }
111 
112  void IniDict::deleteSection( const std::string &section )
113  {
114  _dict.erase(section);
115  }
116 
117  bool IniDict::hasSection( const std::string &section ) const
118  {
119  SectionSet::const_iterator secit = _dict.find(section);
120  if ( secit == _dict.end() )
121  return false;
122  return true;
123  }
124 
125  bool IniDict::hasEntry( const std::string &section,
126  const std::string &entry ) const
127  {
128  SectionSet::const_iterator secit = _dict.find(section);
129  if ( secit == _dict.end() )
130  return false;
131 
132  EntrySet::const_iterator entryit = (secit->second).find(entry);
133  if ( entryit == (secit->second).end() )
134  return false;
135 
136  return true;
137  }
138 
139  /******************************************************************
140  **
141  ** FUNCTION NAME : operator<<
142  ** FUNCTION TYPE : std::ostream &
143  */
144  std::ostream & operator<<( std::ostream & str, const IniDict & obj )
145  {
147  si != obj.sectionsEnd();
148  ++si )
149  {
150  str << "[" << *si << "]" << endl;
151  for ( IniDict::entry_const_iterator ei = obj.entriesBegin(*si);
152  ei != obj.entriesEnd(*si);
153  ++ei )
154  {
155  str << ei->first << " = " << ei->second << endl;
156  }
157  str << endl;
158  }
159  return str;
160  }
161 
163  } // namespace parser
166 } // namespace zypp
zypp::parser::IniDict::sectionsBegin
section_const_iterator sectionsBegin() const
Definition: IniDict.cc:95
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:156
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:40
zypp::parser::IniDict::insertEntry
void insertEntry(const std::string &section, const std::string &key, const std::string &value)
add an entry
Definition: IniDict.cc:105
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:47
Logger.h
zypp::parser::IniDict::deleteSection
void deleteSection(const std::string &section)
add an entry
Definition: IniDict.cc:112
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:125
zypp::InputStream
Helper to create and pass std::istream.
Definition: InputStream.h:56
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
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:100
zypp::parser::operator<<
std::ostream & operator<<(std::ostream &str, const IniDict &obj)
Definition: IniDict.cc:143
zypp::parser::IniDict::_dict
SectionSet _dict
Definition: IniDict.h:150
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:46
zypp::parser::IniDict::hasSection
bool hasSection(const std::string &section) const
True if there is a section with that name.
Definition: IniDict.cc:117
str
String related utilities and Regular expression matching.
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