Imported Upstream version 17.25.4
[platform/upstream/libzypp.git] / zypp / parser / IniDict.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/parser/IniDict.cc
10  *
11 */
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
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23   ///////////////////////////////////////////////////////////////////
24   namespace parser
25   { /////////////////////////////////////////////////////////////////
26     ///////////////////////////////////////////////////////////////////
27     //
28     //  CLASS NAME : IniDict
29     //
30     ///////////////////////////////////////////////////////////////////
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //  METHOD NAME : IniDict::IniDict
35     //  METHOD TYPE : Ctor
36     //
37     IniDict::IniDict( const InputStream &is,
38                       const ProgressData::ReceiverFnc & progress )
39     {
40       read(is, progress );
41     }
42
43     IniDict::IniDict()
44     {
45     }
46
47     void IniDict::read( const InputStream &is,
48                         const ProgressData::ReceiverFnc & progress )
49     {
50       parse(is, progress );
51     }
52
53     ///////////////////////////////////////////////////////////////////
54     //
55     //  METHOD NAME : IniDict::~IniDict
56     //  METHOD TYPE : Dtor
57     //
58     IniDict::~IniDict()
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     Iterable<IniDict::entry_const_iterator> IniDict::entries(const std::string &section) const
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
105     IniDict::section_const_iterator IniDict::sectionsBegin() const
106     {
107       return make_map_key_begin( _dict );
108     }
109
110     IniDict::section_const_iterator IniDict::sectionsEnd() const
111     {
112       return make_map_key_end( _dict );
113     }
114
115     Iterable<IniDict::section_const_iterator> IniDict::sections() const
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     {
162       for ( IniDict::section_const_iterator si = obj.sectionsBegin();
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
178     /////////////////////////////////////////////////////////////////
179   } // namespace parser
180   ///////////////////////////////////////////////////////////////////
181   /////////////////////////////////////////////////////////////////
182 } // namespace zypp
183 ///////////////////////////////////////////////////////////////////