Imported Upstream version 17.25.4
[platform/upstream/libzypp.git] / zypp / parser / IniDict.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/parser/IniDict.h
10  *
11 */
12 #ifndef ZYPP_PARSER_INIDICT_H
13 #define ZYPP_PARSER_INIDICT_H
14
15 #include <iosfwd>
16 #include <map>
17 #include <string>
18
19 #include <zypp/base/PtrTypes.h>
20 #include <zypp/base/InputStream.h>
21 #include <zypp/base/Iterator.h>
22 #include <zypp/base/Iterable.h>
23 #include <zypp/parser/IniParser.h>
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29   namespace parser
30   { /////////////////////////////////////////////////////////////////
31
32     ///////////////////////////////////////////////////////////////////
33     //
34     //  CLASS NAME : IniDict
35     //
36     /**
37      * Parses a INI file and offers its structure as a
38      * dictionary.
39      * 
40      */
41     class IniDict : public IniParser
42     {
43       friend std::ostream & operator<<( std::ostream & str, const IniDict & obj );
44     public:
45       typedef std::map<std::string, std::string> EntrySet;
46       typedef std::map<std::string, EntrySet> SectionSet;
47       typedef MapKVIteratorTraits<SectionSet>::Key_const_iterator section_const_iterator;
48       typedef EntrySet::const_iterator entry_const_iterator;
49       
50       /**
51        * \name Section Iterators
52        * Iterate trough ini file sections
53        * \code
54        * for ( IniDict::section_const_iterator it = dict.sectionsBegin(); 
55        *       it != dict.sectionsEnd();
56        *       ++it )
57        * {
58        *   MIL << (*it) << endl;
59        * }
60        * \endcode
61        */
62        //@{
63       section_const_iterator sectionsBegin() const;
64       section_const_iterator sectionsEnd() const;
65       Iterable<section_const_iterator> sections() const;
66       //@}
67       
68       /**
69        * \name Entries Iterators
70        * Iterate trough ini file entries in a section
71        * \code
72        * for ( IniDict::entry_const_iterator it = dict.entriesBegin("updates"); 
73        *       it != dict.entriesEnd("updates");
74        *       ++it )
75        * {
76        *   MIL << (*it).first << endl;
77        * }
78        * \endcode
79        */
80        
81       //@{
82       entry_const_iterator entriesBegin(const std::string &section) const;
83       entry_const_iterator entriesEnd(const std::string &section) const;
84       Iterable<entry_const_iterator> entries(const std::string &section) const;
85       //@{
86       
87       /**
88        * Creates a dictionary from a InputStream
89        * containing a ini structured file
90        */
91       IniDict( const InputStream &is,
92                const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
93
94       /**
95        * Creates a mepty dictionary
96        */
97       IniDict();
98       
99       /** Dtor */
100       ~IniDict();
101
102       /**
103        * Fill a dictionary from a InputStream
104        * containing a ini structured file
105        */
106       void read( const InputStream &is,
107                  const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
108
109       /**
110        * \short add an entry
111        * \param section
112        * \param key
113        * \param value
114        */
115       void insertEntry( const std::string &section,
116                         const std::string &key,
117                         const std::string &value );
118       
119       /**
120        * \short add an entry
121        * \param section
122        * \param key
123        * \param value
124        */
125       void deleteSection( const std::string &section );
126
127       /**
128        * \short True if there is a section with that name
129        * \param section Section Name
130        */
131       bool hasSection( const std::string &section ) const;
132
133       /**
134        * \short True if an entry exists in the section
135        * \param section Section name
136        * \param entry entry name
137        *
138        * \note If the given section does not exist, this will
139        * of course return false.
140        */
141       bool hasEntry( const std::string &section,
142                      const std::string &entry ) const;
143     public:
144
145       /** Called when a section is found. */
146       virtual void consume( const std::string &section );
147       /** Called when a key value is found. */
148       virtual void consume( const std::string &section,
149                             const std::string &key,
150                             const std::string &value );
151
152     private:
153       SectionSet _dict;
154       /**
155        * empty map used to simulate
156        * iteration in non existant
157        * sections
158        */
159       EntrySet _empty_map;
160     };
161     ///////////////////////////////////////////////////////////////////
162
163     /** \relates IniDict Stream output */
164     std::ostream & operator<<( std::ostream & str, const IniDict & obj );
165
166     /////////////////////////////////////////////////////////////////
167   } // namespace parser
168   ///////////////////////////////////////////////////////////////////
169   /////////////////////////////////////////////////////////////////
170 } // namespace zypp
171 ///////////////////////////////////////////////////////////////////
172 #endif // ZYPP_PARSER_INIDICT_H