1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/IniParser.cc
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/IOStream.h"
18 #include "zypp/base/UserRequestException.h"
20 #include "zypp/parser/ParseException.h"
21 #include "zypp/parser/IniParser.h"
22 #include "zypp/ProgressData.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////
35 // METHOD NAME : IniParser::IniParser
38 IniParser::IniParser()
42 ///////////////////////////////////////////////////////////////////
44 // METHOD NAME : IniParser::~IniParser
47 IniParser::~IniParser()
50 void IniParser::beginParse()
53 void IniParser::consume( const std::string §ion, const std::string &key, const std::string &value )
56 void IniParser::consume( const std::string §ion )
59 void IniParser::endParse()
62 ///////////////////////////////////////////////////////////////////
64 // METHOD NAME : IniParser::parse
67 void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
69 MIL << "Start parsing " << input_r << endl;
70 _inputname = input_r.name();
73 ProgressData ticks( makeProgressData( input_r ) );
74 ticks.sendTo(progress);
77 iostr::EachLine line( input_r );
78 for ( ; line; line.next() )
80 std::string trimmed = str::trim(*line);
82 if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
83 continue ; /* Comment lines */
85 if (trimmed[0] == '[')
87 std::string section = trimmed.substr(1, trimmed.find(']')-1);
89 section.swap(_current_section);
93 std::string::size_type pos = trimmed.find('=');
95 if (pos != std::string::npos)
97 std::string key = str::rtrim(trimmed.substr(0, pos));
98 if(key.find_first_of(" \t") != std::string::npos) {
99 std::string msg = str::form("%s: Key in line %d contains whitespace", _inputname.c_str(), line.lineNo());
100 ZYPP_THROW(ParseException(msg));
102 std::string value = str::ltrim(trimmed.substr(pos+1));
103 consume( _current_section, key, value);
107 std::string msg = str::form("%s: Line %d is missing '=' sign", _inputname.c_str(), line.lineNo());
108 ZYPP_THROW(ParseException(msg));
111 // set progress and allow cancel
112 if ( ! ticks.set( input_r.stream().tellg() ) )
113 ZYPP_THROW(AbortRequestException());
119 MIL << "Done parsing " << input_r << endl;
122 /////////////////////////////////////////////////////////////////
123 } // namespace parser
124 ///////////////////////////////////////////////////////////////////
125 /////////////////////////////////////////////////////////////////
127 ///////////////////////////////////////////////////////////////////