libzypp 17.38.1
iniparser.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12
13#include "iniparser.h"
14
15#include <iostream>
16#include <sstream>
17
21#include <zypp-core/base/UserRequestException>
22
23#include <zypp-core/parser/ParseException>
24#include <zypp-core/ui/ProgressData>
25
26using std::endl;
27
29namespace zypp
30{
32namespace parser
33{
34
35 namespace {
36 inline const std::string & keyGarbage()
37 {
38 static const std::string & _val( ":/?|,\\" );
39 return _val;
40 }
41 } //namespace
42
44//
45// METHOD NAME : IniParser::IniParser
46// METHOD TYPE : Ctor
47//
51
53 : _inputname { std::move(rhs._inputname) }
55 , _line_nr { std::move(rhs._line_nr) }
56{}
57
59//
60// METHOD NAME : IniParser::~IniParser
61// METHOD TYPE : Dtor
62//
65
68
69void IniParser::consume( const std::string &section, const std::string &key, const std::string &value )
70{}
71
72void IniParser::consume( const std::string &section )
73{}
74
77
78void IniParser::garbageLine( const std::string &section, const std::string &line )
79{
80 std::string msg = str::form("%s: Section [%s]: Line %d contains garbage (no '=' or '%s' in key)",
81 _inputname.c_str(), section.c_str(), _line_nr, keyGarbage().c_str());
83}
84
86//
87// METHOD NAME : IniParser::parse
88// METHOD TYPE : void
89//
90void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
91{
92 MIL << "Start parsing " << input_r << endl;
93 _inputname = input_r.name();
94 beginParse();
95
96 ProgressData ticks( makeProgressData( input_r ) );
97 ticks.sendTo(progress);
98 ticks.toMin();
99
100 iostr::EachLine line( input_r );
101 for ( ; line; line.next() )
102 {
103 std::string trimmed = str::trim(*line);
104
105 if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
106 continue ; /* Comment lines */
107
108 if (trimmed[0] == '[')
109 {
110 std::string::size_type pos = trimmed.rfind(']');
111 if ( pos != std::string::npos )
112 {
113 std::string section = trimmed.substr(1, pos-1);
114 consume(section);
115 section.swap(_current_section);
116 }
117 else
118 {
119 _line_nr = line.lineNo();
120 garbageLine( _current_section, trimmed );
121 }
122 continue;
123 }
124
125 std::string::size_type pos = trimmed.find('=');
126 if ( pos == std::string::npos || trimmed.find_first_of( keyGarbage() ) < pos )
127 {
128 _line_nr = line.lineNo();
129 garbageLine( _current_section, trimmed ); // may or may not throw
130 }
131 else
132 {
133 std::string key = str::rtrim(trimmed.substr(0, pos));
134 std::string value = str::ltrim(trimmed.substr(pos+1));
135 consume( _current_section, key, value);
136 }
137
138 // set progress and allow cancel
139 if ( ! ticks.set( input_r.stream().tellg() ) )
140 ZYPP_THROW(AbortRequestException());
141 }
142 ticks.toMax();
143
144 endParse();
145 _inputname.clear();
146 MIL << "Done parsing " << input_r << endl;
147}
148
150} // namespace parser
153} // namespace zypp
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
#define MIL
Definition Logger.h:100
Helper to create and pass std::istream.
Definition inputstream.h:57
const std::string & name() const
Name of the std::istream.
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
Maintain [min,max] and counter (value) for progress counting.
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
bool toMax()
Set counter value to current max value (unless no range).
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
bool toMin()
Set counter value to current min value.
bool set(value_type val_r)
Set new counter value.
Simple lineparser: Traverse each line in a file.
Definition IOStream.h:113
unsigned lineNo() const
Return the current line number.
Definition IOStream.h:127
bool next()
Advance to next line.
Definition IOStream.cc:72
std::string _inputname
Definition iniparser.h:94
virtual void garbageLine(const std::string &section, const std::string &line)
Called whenever a garbage line is found.
Definition iniparser.cc:78
virtual ~IniParser()
Dtor.
Definition iniparser.cc:63
virtual void beginParse()
Called when start parsing.
Definition iniparser.cc:66
IniParser()
Default ctor.
Definition iniparser.cc:48
std::string _current_section
Definition iniparser.h:95
virtual void consume(const std::string &section)
Called when a section is found.
Definition iniparser.cc:72
virtual void endParse()
Called when the parse is done.
Definition iniparser.cc:75
void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Parse the stream.
Definition iniparser.cc:90
Definition ansi.h:855
std::string rtrim(const std::string &s)
Definition String.h:582
std::string ltrim(const std::string &s)
Definition String.h:577
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
ProgressData makeProgressData(const InputStream &input_r)