libzypp  13.10.6
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 namespace std;
20 namespace zypp
21 {
22  namespace parser
24  {
25  //
27  // CLASS NAME : IniDict
28  //
30 
32  //
33  // METHOD NAME : IniDict::IniDict
34  // METHOD TYPE : Ctor
35  //
36  IniDict::IniDict( const InputStream &is,
37  const ProgressData::ReceiverFnc & progress )
38  {
39  read(is, progress );
40  }
41 
42  IniDict::IniDict()
43  {
44  }
45 
46  void IniDict::read( const InputStream &is,
47  const ProgressData::ReceiverFnc & progress )
48  {
49  parse(is, progress );
50  }
51 
53  //
54  // METHOD NAME : IniDict::~IniDict
55  // METHOD TYPE : Dtor
56  //
57  IniDict::~IniDict()
58  {}
59 
60  void IniDict::consume( const std::string &section )
61  {
62  _dict[section]; // remember even empty sections
63  }
64 
65  void IniDict::consume( const std::string &section, const std::string &key, const std::string &value )
66  {
67  _dict[section][key] = value;
68  }
69 
70 
71  IniDict::entry_const_iterator IniDict::entriesBegin(const std::string &section) const
72  {
73  SectionSet::const_iterator secit = _dict.find(section);
74  if ( secit == _dict.end() )
75  {
76  return _empty_map.begin();
77  }
78 
79  return (secit->second).begin();
80  }
81 
82  IniDict::entry_const_iterator IniDict::entriesEnd(const std::string &section) const
83  {
84  SectionSet::const_iterator secit = _dict.find(section);
85  if ( secit == _dict.end() )
86  {
87  return _empty_map.end();
88  }
89 
90  return (secit->second).end();
91  }
92 
93 
94  IniDict::section_const_iterator IniDict::sectionsBegin() const
95  {
96  return make_map_key_begin( _dict );
97  }
98 
99  IniDict::section_const_iterator IniDict::sectionsEnd() const
100  {
101  return make_map_key_end( _dict );
102  }
103 
104  void IniDict::insertEntry( const std::string &section,
105  const std::string &key,
106  const std::string &value )
107  {
108  consume( section, key, value );
109  }
110 
111  void IniDict::deleteSection( const std::string &section )
112  {
113  _dict.erase(section);
114  }
115 
116  bool IniDict::hasSection( const std::string &section ) const
117  {
118  SectionSet::const_iterator secit = _dict.find(section);
119  if ( secit == _dict.end() )
120  return false;
121  return true;
122  }
123 
124  bool IniDict::hasEntry( const std::string &section,
125  const std::string &entry ) const
126  {
127  SectionSet::const_iterator secit = _dict.find(section);
128  if ( secit == _dict.end() )
129  return false;
130 
131  EntrySet::const_iterator entryit = (secit->second).find(entry);
132  if ( entryit == (secit->second).end() )
133  return false;
134 
135  return true;
136  }
137 
138  /******************************************************************
139  **
140  ** FUNCTION NAME : operator<<
141  ** FUNCTION TYPE : std::ostream &
142  */
143  std::ostream & operator<<( std::ostream & str, const IniDict & obj )
144  {
146  si != obj.sectionsEnd();
147  ++si )
148  {
149  str << "[" << *si << "]" << endl;
150  for ( IniDict::entry_const_iterator ei = obj.entriesBegin(*si);
151  ei != obj.entriesEnd(*si);
152  ++ei )
153  {
154  str << ei->first << " = " << ei->second << endl;
155  }
156  str << endl;
157  }
158  return str;
159  }
160 
162  } // namespace parser
165 } // namespace zypp
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition: IniDict.h:46
Helper to create and pass std::istream.
Definition: InputStream.h:56
map< string, string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: Sysconfig.cc:34
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:144
std::ostream & operator<<(std::ostream &str, const ::_Dataiterator *obj)
Definition: LookupAttr.cc:799
section_const_iterator sectionsBegin() const
Definition: IniDict.cc:94
entry_const_iterator entriesEnd(const std::string &section) const
Definition: IniDict.cc:82
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_begin(const _Map &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:226
Parses a INI file and offers its structure as a dictionary.
Definition: IniDict.h:40
entry_const_iterator entriesBegin(const std::string &section) const
Definition: IniDict.cc:71
EntrySet::const_iterator entry_const_iterator
Definition: IniDict.h:47
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_end(const _Map &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:231
section_const_iterator sectionsEnd() const
Definition: IniDict.cc:99