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