Imported Upstream version 17.23.5
[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
95     IniDict::section_const_iterator IniDict::sectionsBegin() const
96     {
97       return make_map_key_begin( _dict );
98     }
99
100     IniDict::section_const_iterator IniDict::sectionsEnd() const
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     {
146       for ( IniDict::section_const_iterator si = obj.sectionsBegin();
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
162     /////////////////////////////////////////////////////////////////
163   } // namespace parser
164   ///////////////////////////////////////////////////////////////////
165   /////////////////////////////////////////////////////////////////
166 } // namespace zypp
167 ///////////////////////////////////////////////////////////////////