libzypp: zypp-core/parser/inidict.cc Source File

libzypp 17.38.14

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
21using std::endl;
22
24namespace 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 std::ostream & IniDict::dumpAsIniOn( std::ostream & str ) const
159 {
160 // NOTE: The output format is used e.g in EconfDict_test.
161 // If you change it you need to adapt the testcases.
162 for ( const auto & section : sections() ) {
163 str << "[" << section << "]" << endl;
164 for ( const auto & [k,v] : entries( section ) ) {
165 str << k << " = " << v << endl;
166 }
167 }
168 return str;
169 }
170
171 std::ostream & operator<<( std::ostream & str, const IniDict & obj )
172 {
173 str << "<";
174 const auto & sections { obj.sections() };
175 if ( sections.empty() ) {
176 str << "empty ini-file";
177 } else {
178 for ( const auto & section : sections ) {
179 str << "[" << section << "](" << obj.entries( section ).size() << ")";
180 }
181 }
182 return str << ">";
183 }
184
186 } // namespace parser
189} // namespace zypp
Logger.h
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::Iterable
Definition Iterable.h:37
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::operator<<
friend std::ostream & operator<<(std::ostream &str, const IniDict &obj)
relates: IniDict Stream output
Definition inidict.cc:171
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 an empty dictionary.
Definition inidict.cc:46
zypp::parser::IniDict::_dict
SectionSet _dict
Definition inidict.h:166
zypp::parser::IniDict::dumpAsIniOn
std::ostream & dumpAsIniOn(std::ostream &str) const
Dump the collected iniMap as ini-file.
Definition inidict.cc:158
zypp::parser::IniDict::_empty_map
EntrySet _empty_map
empty map used to simulate iteration in non-existent sections
Definition inidict.h:172
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:90
inidict.h
str
String related utilities and Regular expression matching.
zypp::parser
Definition HistoryLogReader.cc:30
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
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::makeIterable
Iterable< TIterator > makeIterable(TIterator &&begin_r, TIterator &&end_r)
relates: Iterable convenient construction.
Definition Iterable.h:88
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