libzypp 17.37.10

inidict.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
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 //
40 IniDict::IniDict( const InputStream &is,
41 const ProgressData::ReceiverFnc & progress )
42 {
43 read(is, progress );
44 }
45
46 IniDict::IniDict()
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 //
61 IniDict::~IniDict()
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
97 Iterable<IniDict::entry_const_iterator> IniDict::entries(const std::string &section) const
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
108 IniDict::section_const_iterator IniDict::sectionsBegin() const
109 {
110 return make_map_key_begin( _dict );
111 }
112
113 IniDict::section_const_iterator IniDict::sectionsEnd() const
114 {
115 return make_map_key_end( _dict );
116 }
117
118 Iterable<IniDict::section_const_iterator> IniDict::sections() const
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 {
165 for ( IniDict::section_const_iterator si = obj.sectionsBegin();
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
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::Iterable
Definition Iterable.h:37
zypp::Iterable::makeIterable
Iterable< TIterator > makeIterable(TIterator &&begin_r, TIterator &&end_r)
convenient construction.
Definition Iterable.h:88
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition progressdata.h:140
zypp::parser::IniDict::sectionsEnd
section_const_iterator sectionsEnd() const
Definition inidict.cc:113
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:50
zypp::parser::IniDict::hasSection
bool hasSection(const std::string &section) const
True if there is a section with that name.
Definition inidict.cc:136
zypp::parser::IniDict::deleteSection
void deleteSection(const std::string &section)
add an entry
Definition inidict.cc:131
zypp::parser::IniDict::IniDict
IniDict(const InputStream &is, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Creates a dictionary from a InputStream containing a ini structured file.
Definition inidict.cc:40
zypp::parser::IniDict::sections
Iterable< section_const_iterator > sections() const
Definition inidict.cc:118
zypp::parser::IniDict::entriesBegin
entry_const_iterator entriesBegin(const std::string &section) const
Definition inidict.cc:75
zypp::parser::IniDict::entries
Iterable< entry_const_iterator > entries(const std::string &section) const
Definition inidict.cc:97
zypp::parser::IniDict::insertEntry
void insertEntry(const std::string &section, const std::string &key, const std::string &value)
add an entry
Definition inidict.cc:123
zypp::parser::IniDict::consume
void consume(const std::string &section) override
Called when a section is found.
Definition inidict.cc:64
zypp::parser::IniDict::sectionsBegin
section_const_iterator sectionsBegin() const
Definition inidict.cc:108
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:144
zypp::parser::IniDict::IniDict
IniDict()
Creates a mepty dictionary.
Definition inidict.cc:46
zypp::parser::IniDict::_dict
SectionSet _dict
Definition inidict.h:153
zypp::parser::IniDict::_empty_map
EntrySet _empty_map
empty map used to simulate iteration in non-existent sections
Definition inidict.h:159
zypp::parser::IniDict::~IniDict
~IniDict() override
Dtor.
Definition inidict.cc:61
zypp::parser::IniDict::section_const_iterator
MapKVIteratorTraits< SectionSet >::Key_const_iterator section_const_iterator
Definition inidict.h:47
zypp::parser::IniDict::entry_const_iterator
EntrySet::const_iterator entry_const_iterator
Definition inidict.h:48
zypp::parser::IniDict::entriesEnd
entry_const_iterator entriesEnd(const std::string &section) const
Definition inidict.cc:86
zypp::parser::IniParser::parse
void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Parse the stream.
Definition iniparser.cc:84
inidict.h
str
String related utilities and Regular expression matching.
zypp::parser
Definition HistoryLogReader.cc:30
zypp::parser::operator<<
std::ostream & operator<<(std::ostream &str, const ProductFileData &obj)
Definition ProductFileReader.cc:107
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
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:226
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:231
Logger.h