libzypp 17.37.10

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
18#include <zypp-core/base/Logger.h>
19#include <zypp-core/base/String.h>
20#include <zypp-core/base/IOStream.h>
21#include <zypp-core/base/UserRequestException>
22
23#include <zypp-core/parser/ParseException>
24#include <zypp-core/ui/ProgressData>
25
26 using std::endl;
27
29 namespace zypp
30{
32 namespace 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//
48 IniParser::IniParser()
49 : _line_nr(0)
50{}
51
53//
54// METHOD NAME : IniParser::~IniParser
55// METHOD TYPE : Dtor
56//
57 IniParser::~IniParser()
58{}
59
60 void IniParser::beginParse()
61{}
62
63 void IniParser::consume( const std::string &section, const std::string &key, const std::string &value )
64{}
65
66 void IniParser::consume( const std::string &section )
67{}
68
69 void IniParser::endParse()
70{}
71
72 void IniParser::garbageLine( const std::string &section, const std::string &line )
73{
74 std::string msg = str::form("%s: Section [%s]: Line %d contains garbage (no '=' or '%s' in key)",
75 _inputname.c_str(), section.c_str(), _line_nr, keyGarbage().c_str());
76 ZYPP_THROW(ParseException(msg));
77}
78
80//
81// METHOD NAME : IniParser::parse
82// METHOD TYPE : void
83//
84 void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
85{
86 MIL << "Start parsing " << input_r << endl;
87 _inputname = input_r.name();
88 beginParse();
89
90 ProgressData ticks( makeProgressData( input_r ) );
91 ticks.sendTo(progress);
92 ticks.toMin();
93
94 iostr::EachLine line( input_r );
95 for ( ; line; line.next() )
96 {
97 std::string trimmed = str::trim(*line);
98
99 if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
100 continue ; /* Comment lines */
101
102 if (trimmed[0] == '[')
103 {
104 std::string::size_type pos = trimmed.rfind(']');
105 if ( pos != std::string::npos )
106 {
107 std::string section = trimmed.substr(1, pos-1);
108 consume(section);
109 section.swap(_current_section);
110 }
111 else
112 {
113 _line_nr = line.lineNo();
114 garbageLine( _current_section, trimmed );
115 }
116 continue;
117 }
118
119 std::string::size_type pos = trimmed.find('=');
120 if ( pos == std::string::npos || trimmed.find_first_of( keyGarbage() ) < pos )
121 {
122 _line_nr = line.lineNo();
123 garbageLine( _current_section, trimmed ); // may or may not throw
124 }
125 else
126 {
127 std::string key = str::rtrim(trimmed.substr(0, pos));
128 std::string value = str::ltrim(trimmed.substr(pos+1));
129 consume( _current_section, key, value);
130 }
131
132 // set progress and allow cancel
133 if ( ! ticks.set( input_r.stream().tellg() ) )
134 ZYPP_THROW(AbortRequestException());
135 }
136 ticks.toMax();
137
138 endParse();
139 _inputname.clear();
140 MIL << "Done parsing " << input_r << endl;
141}
142
144} // namespace parser
147} // namespace zypp
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::InputStream::name
const std::string & name() const
Name of the std::istream.
Definition inputstream.h:107
zypp::InputStream::stream
std::istream & stream() const
The std::istream.
Definition inputstream.h:93
zypp::ProgressData
Maintain [min,max] and counter (value) for progress counting.
Definition progressdata.h:132
zypp::ProgressData::sendTo
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition progressdata.h:229
zypp::ProgressData::toMax
bool toMax()
Set counter value to current max value (unless no range).
Definition progressdata.h:276
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition progressdata.h:140
zypp::ProgressData::toMin
bool toMin()
Set counter value to current min value.
Definition progressdata.h:272
zypp::ProgressData::set
bool set(value_type val_r)
Set new counter value.
Definition progressdata.h:249
zypp::iostr::EachLine
Simple lineparser: Traverse each line in a file.
Definition IOStream.h:113
zypp::iostr::EachLine::lineNo
unsigned lineNo() const
Return the current line number.
Definition IOStream.h:127
zypp::iostr::EachLine::next
bool next()
Advance to next line.
Definition IOStream.cc:72
zypp::parser::IniParser::_inputname
std::string _inputname
Definition iniparser.h:84
zypp::parser::IniParser::_line_nr
int _line_nr
Definition iniparser.h:86
zypp::parser::IniParser::garbageLine
virtual void garbageLine(const std::string &section, const std::string &line)
Called whenever a garbage line is found.
Definition iniparser.cc:72
zypp::parser::IniParser::~IniParser
virtual ~IniParser()
Dtor.
Definition iniparser.cc:57
zypp::parser::IniParser::beginParse
virtual void beginParse()
Called when start parsing.
Definition iniparser.cc:60
zypp::parser::IniParser::IniParser
IniParser()
Default ctor.
Definition iniparser.cc:48
zypp::parser::IniParser::_current_section
std::string _current_section
Definition iniparser.h:85
zypp::parser::IniParser::consume
virtual void consume(const std::string &section)
Called when a section is found.
Definition iniparser.cc:66
zypp::parser::IniParser::endParse
virtual void endParse()
Called when the parse is done.
Definition iniparser.cc:69
zypp::parser::IniParser::parse
void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Parse the stream.
Definition iniparser.cc:84
zypp::parser::ParseException
Definition parseexception.h:34
iniparser.h
zypp::parser
Definition HistoryLogReader.cc:30
zypp::str::rtrim
std::string rtrim(const std::string &s)
Definition String.h:582
zypp::str::ltrim
std::string ltrim(const std::string &s)
Definition String.h:577
zypp::str::form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
zypp::str::trim
std::string trim(const std::string &s, const Trim trim_r)
Definition String.cc:226
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::makeProgressData
ProgressData makeProgressData(const InputStream &input_r)
Definition progressdata.cc:124
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
IOStream.h
Logger.h
MIL
#define MIL
Definition Logger.h:100
String.h