Imported Upstream version 14.30.0
[platform/upstream/libzypp.git] / zypp / parser / IniParser.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/parser/IniParser.h
10  *
11 */
12 #ifndef ZYPP_PARSER_INIPARSER_H
13 #define ZYPP_PARSER_INIPARSER_H
14
15 #include <iosfwd>
16 #include <string>
17 #include <list>
18
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/base/NonCopyable.h"
21 #include "zypp/base/InputStream.h"
22 #include "zypp/ProgressData.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
28 namespace parser
29 { /////////////////////////////////////////////////////////////////
30
31 ///////////////////////////////////////////////////////////////////
32 /// \class IniParser
33 /// \brief Simple INI-file parser
34 ///
35 /// Lines staring with \c ; or \c # are treated as comment. Section
36 /// names are enclosed by <tt>[]</tt>. Key and value are separated by \c =.
37 ///
38 /// Lines without \c = or with a key containing any of "<tt>,|\\/</tt>"
39 /// or section lines without closing \c ] are considered garbage.
40 ///
41 class IniParser : private base::NonCopyable
42 {
43 public:
44   /** Default ctor */
45   IniParser();
46   /** Dtor */
47   virtual ~IniParser();
48   /** Parse the stream.
49    * \throw ParseException on errors. Invoke \ref consume
50    * for each tag. \ref consume might throw other exceptions
51    * as well.
52   */
53   void parse( const InputStream & imput_r, const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
54
55 public:
56   /** Called when start parsing. */
57   virtual void beginParse();
58   /** Called when a section is found. */
59   virtual void consume( const std::string &section );
60   /** Called when a key value is found. */
61   virtual void consume( const std::string &section, const std::string &key, const std::string &value );
62   /** Called when the parse is done. */
63   virtual void endParse();
64
65   /** Called whenever a garbage line is found.
66    *
67    * \throw ParseException if not overloaded.
68    *
69    * Derived parsers may overload this to examine the line
70    * and call this method to actually throw the exception.
71    *
72    * Used by some parsers to accept multi-line entires.
73    */
74   virtual void garbageLine( const std::string &section, const std::string &line );
75
76 public:
77   /** Name of the current InputStream. */
78   const std::string & inputname() const
79   {
80     return _inputname;
81   }
82
83 private:
84   std::string _inputname;
85   std::string _current_section;
86   int _line_nr;
87   //ProgressData _ticks;
88 };
89
90 /////////////////////////////////////////////////////////////////
91 } // namespace parser
92 ///////////////////////////////////////////////////////////////////
93 /////////////////////////////////////////////////////////////////
94 } // namespace zypp
95 ///////////////////////////////////////////////////////////////////
96 #endif // ZYPP_PARSER_INIPARSER_H