libzypp  13.10.6
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 
21 #include "zypp/parser/IniParser.h"
22 #include "zypp/ProgressData.h"
23 
24 using std::endl;
25 
27 namespace zypp
28 {
29 namespace parser
31 {
32 
34 //
35 // METHOD NAME : IniParser::IniParser
36 // METHOD TYPE : Ctor
37 //
39  : _line_nr(0)
40 {}
41 
43 //
44 // METHOD NAME : IniParser::~IniParser
45 // METHOD TYPE : Dtor
46 //
48 {}
49 
51 {}
52 
53 void IniParser::consume( const std::string &section, const std::string &key, const std::string &value )
54 {}
55 
56 void IniParser::consume( const std::string &section )
57 {}
58 
60 {}
61 
63 //
64 // METHOD NAME : IniParser::parse
65 // METHOD TYPE : void
66 //
67 void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
68 {
69  MIL << "Start parsing " << input_r << endl;
70  _inputname = input_r.name();
71  beginParse();
72 
73  ProgressData ticks( makeProgressData( input_r ) );
74  ticks.sendTo(progress);
75  ticks.toMin();
76 
77  iostr::EachLine line( input_r );
78  for ( ; line; line.next() )
79  {
80  std::string trimmed = str::trim(*line);
81 
82  if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
83  continue ; /* Comment lines */
84 
85  if (trimmed[0] == '[')
86  {
87  std::string section = trimmed.substr(1, trimmed.find(']')-1);
88  consume(section);
89  section.swap(_current_section);
90  continue;
91  }
92 
93  std::string::size_type pos = trimmed.find('=');
94 
95  if (pos != std::string::npos)
96  {
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());
101  }
102  std::string value = str::ltrim(trimmed.substr(pos+1));
103  consume( _current_section, key, value);
104  }
105  else
106  {
107  std::string msg = str::form("%s: Line %d is missing '=' sign", _inputname.c_str(), line.lineNo());
109  }
110 
111  // set progress and allow cancel
112  if ( ! ticks.set( input_r.stream().tellg() ) )
113  ZYPP_THROW(AbortRequestException());
114  }
115  ticks.toMax();
116 
117  endParse();
118  _inputname.clear();
119  MIL << "Done parsing " << input_r << endl;
120 }
121 
123 } // namespace parser
126 } // namespace zypp
void parse(const InputStream &imput_r, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Parse the stream.
Definition: IniParser.cc:67
IniParser()
Default ctor.
Definition: IniParser.cc:38
#define MIL
Definition: Logger.h:47
ProgressData makeProgressData(const InputStream &input_r)
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:231
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
bool next()
Advance to next line.
Definition: IOStream.cc:72
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:270
unsigned lineNo() const
Return the current line number.
Definition: IOStream.h:126
Helper to create and pass std::istream.
Definition: InputStream.h:56
Simple lineparser: Traverse each line in a file.
Definition: IOStream.h:111
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:144
std::string _inputname
Definition: IniParser.h:69
std::string _current_section
Definition: IniParser.h:70
std::string ltrim(const std::string &s)
Definition: String.h:792
virtual ~IniParser()
Dtor.
Definition: IniParser.cc:47
virtual void beginParse()
Called when start parsing.
Definition: IniParser.cc:50
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:266
std::string trim(const std::string &s, const Trim trim_r)
Definition: String.cc:204
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:135
SolvableIdType size_type
Definition: PoolMember.h:99
std::string rtrim(const std::string &s)
Definition: String.h:795
std::istream & stream() const
The std::istream.
Definition: InputStream.h:93
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
const std::string & name() const
Name of the std::istream.
Definition: InputStream.h:107
virtual void endParse()
Called when the parse is done.
Definition: IniParser.cc:59
bool set(value_type val_r)
Set new counter value.
Definition: ProgressData.h:251
virtual void consume(const std::string &section)
Called when a section is found.
Definition: IniParser.cc:56