libzypp  10.5.0
IniParser.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include <sstream>
00014 
00015 #include "zypp/base/Logger.h"
00016 #include "zypp/base/String.h"
00017 #include "zypp/base/IOStream.h"
00018 #include "zypp/base/UserRequestException.h"
00019 
00020 #include "zypp/parser/ParseException.h"
00021 #include "zypp/parser/IniParser.h"
00022 #include "zypp/ProgressData.h"
00023 
00024 using std::endl;
00025 
00027 namespace zypp
00028 { 
00029 
00030 namespace parser
00031 { 
00032 
00034 //
00035 //      METHOD NAME : IniParser::IniParser
00036 //      METHOD TYPE : Ctor
00037 //
00038 IniParser::IniParser()
00039   : _line_nr(0)
00040 {}
00041 
00043 //
00044 //      METHOD NAME : IniParser::~IniParser
00045 //      METHOD TYPE : Dtor
00046 //
00047 IniParser::~IniParser()
00048 {}
00049 
00050 void IniParser::beginParse()
00051 {}
00052 
00053 void IniParser::consume( const std::string &section, const std::string &key, const std::string &value )
00054 {}
00055 
00056 void IniParser::consume( const std::string &section )
00057 {}
00058 
00059 void IniParser::endParse()
00060 {}
00061 
00063 //
00064 //      METHOD NAME : IniParser::parse
00065 //      METHOD TYPE : void
00066 //
00067 void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
00068 {
00069   MIL << "Start parsing " << input_r << endl;
00070   _inputname = input_r.name();
00071   beginParse();
00072 
00073   ProgressData ticks( makeProgressData( input_r ) );
00074   ticks.sendTo(progress);
00075   ticks.toMin();
00076 
00077   iostr::EachLine line( input_r );
00078   for ( ; line; line.next() )
00079   {
00080     std::string trimmed = str::trim(*line);
00081 
00082     if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
00083       continue ; /* Comment lines */
00084 
00085     if (trimmed[0] == '[')
00086     {
00087       std::string section = trimmed.substr(1, trimmed.find(']')-1);
00088       consume(section);
00089       section.swap(_current_section);
00090       continue;
00091     }
00092 
00093     std::string::size_type pos = trimmed.find('=');
00094 
00095     if (pos != std::string::npos)
00096     {
00097       std::string key = str::rtrim(trimmed.substr(0, pos));
00098       if(key.find_first_of(" \t") != std::string::npos) {
00099         std::string msg = str::form("%s: Key in line %d contains whitespace", _inputname.c_str(), line.lineNo());
00100         ZYPP_THROW(ParseException(msg));
00101       }
00102       std::string value = str::ltrim(trimmed.substr(pos+1));
00103       consume( _current_section, key, value);
00104     }
00105     else
00106     {
00107       std::string msg = str::form("%s: Line %d is missing '=' sign", _inputname.c_str(), line.lineNo());
00108       ZYPP_THROW(ParseException(msg));
00109     }
00110 
00111     // set progress and allow cancel
00112     if ( ! ticks.set( input_r.stream().tellg() ) )
00113       ZYPP_THROW(AbortRequestException());
00114   }
00115   ticks.toMax();
00116 
00117   endParse();
00118   _inputname.clear();
00119   MIL << "Done parsing " << input_r << endl;
00120 }
00121 
00123 } // namespace parser
00126 } // namespace zypp