libzypp 9.41.1

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 
00033   namespace {
00034     inline const std::string & keyGarbage()
00035     {
00036       static const std::string & _val( ":/?|,\\" );
00037       return _val;
00038     }
00039   } //namespace
00040 
00042 //
00043 //      METHOD NAME : IniParser::IniParser
00044 //      METHOD TYPE : Ctor
00045 //
00046 IniParser::IniParser()
00047   : _line_nr(0)
00048 {}
00049 
00051 //
00052 //      METHOD NAME : IniParser::~IniParser
00053 //      METHOD TYPE : Dtor
00054 //
00055 IniParser::~IniParser()
00056 {}
00057 
00058 void IniParser::beginParse()
00059 {}
00060 
00061 void IniParser::consume( const std::string &section, const std::string &key, const std::string &value )
00062 {}
00063 
00064 void IniParser::consume( const std::string &section )
00065 {}
00066 
00067 void IniParser::endParse()
00068 {}
00069 
00071 //
00072 //      METHOD NAME : IniParser::parse
00073 //      METHOD TYPE : void
00074 //
00075 void IniParser::parse( const InputStream & input_r, const ProgressData::ReceiverFnc & progress )
00076 {
00077   MIL << "Start parsing " << input_r << endl;
00078   _inputname = input_r.name();
00079   beginParse();
00080 
00081   ProgressData ticks( makeProgressData( input_r ) );
00082   ticks.sendTo(progress);
00083   ticks.toMin();
00084 
00085   iostr::EachLine line( input_r );
00086   for ( ; line; line.next() )
00087   {
00088     std::string trimmed = str::trim(*line);
00089 
00090     if (trimmed.empty() || trimmed[0] == ';' || trimmed[0] == '#')
00091       continue ; /* Comment lines */
00092 
00093     if (trimmed[0] == '[')
00094     {
00095       std::string::size_type pos = trimmed.rfind(']');
00096       if ( pos != std::string::npos )
00097       {
00098         std::string section = trimmed.substr(1, pos-1);
00099         consume(section);
00100         section.swap(_current_section);
00101       }
00102       else
00103       {
00104         _line_nr = line.lineNo();
00105         std::string msg = str::form("%s: Section [%s]: Line %d contains garbage (no '=' or '%s' in key)",
00106                                     _inputname.c_str(), _current_section.c_str(), _line_nr, keyGarbage().c_str());
00107         ZYPP_THROW(ParseException(msg));
00108       }
00109       continue;
00110     }
00111 
00112     std::string::size_type pos = trimmed.find('=');
00113     if ( pos == std::string::npos || trimmed.find_first_of( keyGarbage() ) < pos )
00114     {
00115       _line_nr = line.lineNo();
00116       consume( _current_section, std::string(), trimmed);
00117     }
00118     else
00119     {
00120       std::string key = str::rtrim(trimmed.substr(0, pos));
00121       std::string value = str::ltrim(trimmed.substr(pos+1));
00122       consume( _current_section, key, value);
00123     }
00124 
00125     // set progress and allow cancel
00126     if ( ! ticks.set( input_r.stream().tellg() ) )
00127       ZYPP_THROW(AbortRequestException());
00128   }
00129   ticks.toMax();
00130 
00131   endParse();
00132   _inputname.clear();
00133   MIL << "Done parsing " << input_r << endl;
00134 }
00135 
00137 } // namespace parser
00140 } // namespace zypp